JavaScript Total subarrays with Sum K


In this problem statement, our task is to write the function for getting the total subarrays with sum K with the help of Javascript. So for doing this task we will use basic functionality of Javascript.

Understanding the problem statement

The problem statement is to create a function which will take an array of integers and a target sum K. So after processing the calculation it will return the total number of subarrays in the array with a sum of K. A subarray can be defined as a continuous series of items in the array. For example suppose we have the given array [1, 2, 3, 4, 5] and K is 3 so there are two subarrays with the sum of 3 that is [1, 2] and [3].

Logic for the above problem

To solve the given problem we can use the loop to traverse through the items of the array and also we will keep track of the sum of the elements up to the given point. So we can use a for loop to store the sum and its frequency. And we will check that the current sum is equal to the value of K. If this is true then increment the count value by. And after processing all the items of the array we will get the total count of subarrays formed with sum K.

Algorithm

Step 1: As we have to find the total number of subarrays with sum K, so for doing this task we will define a function called subarraysWithSumK. And use two parameters arr and K inside this function.

Step 2: After defining the above function, inside the body of the function we will define a variable called count and start this variable with zero value.

Step 3: So with the help of a loop we will iterate the given array to get the sum of the items in the array.

Step 4: Then we will define a variable and give it a name as sum and begin its value with zero.

Step 5: After that we will use another loop to traverse the items of the above loop. And inside this loop we will sum each item and store it in the sum variable.

Step 6: And then we will check the condition that the value of sum is equal to the value of K then increase the count value by 1.

Example

function subarraysWithSumK(arr, K) {
   let count = 0;
   for (let i = 0; i < arr.length; i++) {
      let sum = 0;
      for (let j = i; j < arr.length; j++) {
         sum += arr[j];
         if (sum === K) {
            count++;
         }
      }
   }
    return count;
}
 
const arr = [1, 2, 3, 4, 5];
const K = 6;
console.log(subarraysWithSumK(arr, K));

Output

1

Complexity

The algorithm for finding the total number of subarrays with sum K takes O(n) time, in which n is the number of items in the array. This code uses a constant amount of time to process every item in the array. And the space complexity for the program is O(1).

Conclusion

This is the way we can solve the above problem statement. We have implemented a code for getting the count of sub-arrays with a sum of K. It keeps track of the array items and checks that the sum is equal to K.

Updated on: 14-Aug-2023

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements