Very Simple, C++ Longest Sub-Array Sum ✅✅
if it Helps You. Please UpVote Me...! \u2764\n# Approach\n Describe your approach to solving the problem. \n\nApproach is Simple But we have to change the probl
# if it Helps You. Please UpVote Me...! \u2764\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nApproach is Simple But we have to change the problem Statement to **Longest Sub-Array with Sum <=K.**\n\n**Just Follow the Below Points to change the Problem Statement:-**\n1. Calculate the absolute Cost for Each Character and store it to the new Cost Vector.\n2. Now Calculate the Longest Sub-Array With Sum <= k.\n\n**EX:- S = "abc"  and t = "bcd"\ncost[0] = abs(s[0]-t[0]) = 1\ncost[1] = abs(s[1]-t[1]) = 1\ncost[2] = abs(s[2]-t[2]) = 1**\n\n**Cost[] = {1,1,1} maxcost = 2 Now find the Longest SubSub-Array with Sum<=2.**\n\n\n\n\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int equalSubstring(string s, string t, int maxcost) {\n\n        int n = s.size(),sum = 0,ans = 0;\n        vector<int>cost(n,0);\n\n        for(int i = 0;i<n;i++)\n        cost[i] = abs(s[i]-t[i]);\n\n        queue<int> q;\n\t    for(int i=0;i<n;i++)\n\t    {\n\t      if(sum+cost[i]<=maxcost)\n\t      {\n\t        sum+=cost[i];\n\t        q.push(cost[i]);\n\t        if(ans<q.size())\n\t            ans=q.size();\n\t      }\n\t      else if(q.size()>0)\n\t      {\n\t        sum-=q.front();\n\t        q.pop();\n\t        i--;\n\t      }\n\t    }\n\n        return ans;\n        \n    }\n};\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/46b72ab1-178f-42da-94e9-0225ed60c9c7_1686649809.4518604.jpeg)\n
['Sliding Window', 'Prefix Sum', 'C++']