`{"id":"8ab656a1-7710-4dd5-a84b-2eb25cb8af40","name":"Count Of Subarrays Having Sum Equals To K","description":"1. You are given an array of integers(arr) and a number K.\r\n2. You have to find the count of subarrays whose sum equals k.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"A number representing the count of subarrays having sum equal to K.","constraints":"1 &lt;= N &lt;= 10^5\r\n-1000 &lt;= arr[i] &lt;= 1000\r\n-10^9 &lt;= K &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n using namespace std;\n \n // Function to display an array.\n void display(vector<int> &arr) {\n for (int i = 0; i < arr.size(); i++) {\n cout<<arr[i]<<\" \";\n }\n cout<<endl;\n }\n \n// This is a functional problem. You have to complete this function.\n// It takes as input an array and K.\n// It should return the length of maximum subarray whose sum is K. If no such subarray, return 0.\n int maxLenSubarray(vector<int> &v, int k) {\n // write your code here. \n }\n \n int main(int argc, char** argv){\n int n;\n cin>>n;\n vector<int> arr;\n for (int j = 0; j < n; j++) {\n int val;\n cin>>val;\n arr.push_back(val);\n }\n int K;\n cin>>K;\n cout<<maxLenSubarray(arr, K);\n }"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int solution(int[] arr, int target){\r\n\t\t// write your code here\r\n\t\t\r\n\t\treturn 0;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n int target = scn.nextInt();\r\n System.out.println(solution(arr,target));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1 1 1\r\n2","sampleOutput":"2\r\n","questionVideo":"https://www.youtube.com/embed/20v8zSo2v18?end=16","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"bc28865b-f445-4302-82d1-5388c3db98c5","name":"Count Of Subarrays Having Sum Equals To K","slug":"count-of-subarrays-having-sum-equals-to-k","type":1}],"next":{"id":"efde8fb1-3063-4e0e-b2df-70b5e578ea41","name":"Count Of Subarrays Having Sum Equals To K MCQ","type":0,"slug":"count-of-subarrays-having-sum-equals-to-k-mcq"},"prev":{"id":"af1716a6-9a2c-461a-a588-cd25c3d94806","name":"Word Pattern","type":3,"slug":"word-pattern"}}}`

# Count Of Subarrays Having Sum Equals To K

1. You are given an array of integers(arr) and a number K. 2. You have to find the count of subarrays whose sum equals k.

`{"id":"8ab656a1-7710-4dd5-a84b-2eb25cb8af40","name":"Count Of Subarrays Having Sum Equals To K","description":"1. You are given an array of integers(arr) and a number K.\r\n2. You have to find the count of subarrays whose sum equals k.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"A number representing the count of subarrays having sum equal to K.","constraints":"1 &lt;= N &lt;= 10^5\r\n-1000 &lt;= arr[i] &lt;= 1000\r\n-10^9 &lt;= K &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\n using namespace std;\n \n // Function to display an array.\n void display(vector<int> &arr) {\n for (int i = 0; i < arr.size(); i++) {\n cout<<arr[i]<<\" \";\n }\n cout<<endl;\n }\n \n// This is a functional problem. You have to complete this function.\n// It takes as input an array and K.\n// It should return the length of maximum subarray whose sum is K. If no such subarray, return 0.\n int maxLenSubarray(vector<int> &v, int k) {\n // write your code here. \n }\n \n int main(int argc, char** argv){\n int n;\n cin>>n;\n vector<int> arr;\n for (int j = 0; j < n; j++) {\n int val;\n cin>>val;\n arr.push_back(val);\n }\n int K;\n cin>>K;\n cout<<maxLenSubarray(arr, K);\n }"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static int solution(int[] arr, int target){\r\n\t\t// write your code here\r\n\t\t\r\n\t\treturn 0;\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for(int i = 0 ; i < n; i++){\r\n arr[i] = scn.nextInt();\r\n }\r\n int target = scn.nextInt();\r\n System.out.println(solution(arr,target));\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"3\r\n1 1 1\r\n2","sampleOutput":"2\r\n","questionVideo":"https://www.youtube.com/embed/20v8zSo2v18?end=16","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"bc28865b-f445-4302-82d1-5388c3db98c5","name":"Count Of Subarrays Having Sum Equals To K","slug":"count-of-subarrays-having-sum-equals-to-k","type":1}],"next":{"id":"efde8fb1-3063-4e0e-b2df-70b5e578ea41","name":"Count Of Subarrays Having Sum Equals To K MCQ","type":0,"slug":"count-of-subarrays-having-sum-equals-to-k-mcq"},"prev":{"id":"af1716a6-9a2c-461a-a588-cd25c3d94806","name":"Word Pattern","type":3,"slug":"word-pattern"}}}`

Editor

# Count Of Subarrays Having Sum Equals To K

medium

1. You are given an array of integers(arr) and a number K. 2. You have to find the count of subarrays whose sum equals k.

## Constraints

1 <= N <= 10^5 -1000 <= arr[i] <= 1000 -10^9 <= K <= 10^9

## Format

### Input

A number N arr1 arr2.. N numbers A number K

### Output

A number representing the count of subarrays having sum equal to K.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}3 1 1 1 2```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2 ```

Question Video

Discussions

Show Discussion

Related Resources