Longest Subarray With Sum K.

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
446 upvotes
Asked in companies
OLX GroupIntuitFastenal

Problem statement

Ninja and his friend are playing a game of subarrays. They have an array ‘NUMS’ of length ‘N’. Ninja’s friend gives him an arbitrary integer ‘K’ and asks him to find the length of the longest subarray in which the sum of elements is equal to ‘K’.

Ninjas asks for your help to win this game. Find the length of the longest subarray in which the sum of elements is equal to ‘K’.

If there is no subarray whose sum is ‘K’ then you should return 0.

Example:
Input: ‘N’ = 5,  ‘K’ = 4, ‘NUMS’ = [ 1, 2, 1, 0, 1 ]

Output: 4

There are two subarrays with sum = 4, [1, 2, 1] and [2, 1, 0, 1]. Hence the length of the longest subarray with sum = 4 is 4.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line will contain the integer 'T', denoting the number of test cases.

The first line of each test case contains two integers ‘N’ and ‘K’ denoting the length of the array ‘NUMS’ and an arbitrary integer.

The second line of each test case contains ‘N’ space separated integers.
Output format :
For each test case, you don’t need to print anything just return the length of the longest subarray with the sum ‘K’.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^5
-10^6 <= NUMS[ i ] <= 10^6
-10^6 <= K <= 10^6

Sum of N Over all the Test cases <= 10^5

Time Limit: 1 sec
Sample Input 1 :
2
3 5
2 3 5
3 1
-1 1 1
Sample Output 1 :
2
3
Explanation Of Sample Input 1 :
For the first case:
There are two subarrays with sum = 5, [2, 3] and [5]. Hence the length of the longest subarray is 2.

For the second case:
Longest subarray with sum = 1 is [1, -1, 1].
Sample Input 2 :
2
3 4
1 1 1
3 2
-50 0 52
Sample Output 2 :
0 
3
Hint

Can you think of a way to iterate over all the subarrays and then find the longest subarray?

Approaches (2)
Bruteforce

In this approach, We can iterate over all the subarrays and calculate the sum for each subarray then we can check if the sum of that subarray is equal to ‘K’ or not. If it is equal to ‘K’ then it can be one of the possible answers. We can maintain a variable to find the maximum length among all the subarrays with the sum equal to ‘K’.


 

The steps are as follows:-

function getLongestSubarray(vector<int>& nums, int k):

  1. Initialize a variable 'ANS' with 0.
  2. Run a loop from 'i' = 0... 'N' - 1:
    • Initialize a variable 'SUM' with 0.
    • Again Run a loop from 'j' = 'i'... 'N' -1:
      • Add the value of 'NUMS[ j ]' in variable 'SUM'.
      • If 'SUM' is equal to 'K' then update the value of 'ANS' with the maximum of 'ANS' and ('j' - 'i' +1)
  3. Return the value of 'ANS'.
Time Complexity

O( N^2 ), Where ‘N’ is the length of array ‘NUMS’.

 

Number of subarrays with size ‘N’ is ‘N’ * (‘N’ + 1) / 2. We are iterating over all the subarrays.

Hence the time complexity is O( N^2  ).

Space Complexity

O( 1 ).

 

We are not using any extra space.

Hence space complexity is O( 1 ).

Code Solution
(100% EXP penalty)
Longest Subarray With Sum K.
Full screen
Console