`{"id":"b03bd9f4-2dc2-47ad-a641-1e69511bfa31","name":"Longest Subarray With Sum Divisible By K","description":"1. You are given an array of integers(arr) and a number K.\r\n2. You have to find length of the longest subarray whose sum is divisible by K.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"A number representing length of the longest subarray whose sum is divisible by K.","constraints":"1 &lt;= N,K &lt;= 10^5\r\n-1000 &lt;= arr[i] &lt;= 1000","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint solution(vector<int> &arr, int k) {\n // write your code here\n}\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for(int i = 0; i < n; i++){\n cin >> arr[i];\n }\n int k;\n cin >> k;\n cout << solution(arr, k) << endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr, int k) {\r\n // write your code here\r\n\r\n return 0;\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner 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 k = scn.nextInt();\r\n System.out.println(solution(arr, k));\r\n }\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n2 7 6 1 4 5\r\n3","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/GrV3MTR_Uk0?end=24","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":"4f7f69c0-a46e-49b3-bcf1-f1fb96d4eb9a","name":"Longest Subarray With Sum Divisible By K","slug":"longest-subarray-with-sum-divisible-by-k","type":1}],"next":{"id":"e0f1565e-1c2f-4155-b914-b3eaf767cabf","name":"Longest Subarray With Sum Divisible By K MCQ","type":0,"slug":"longest-subarray-with-sum-divisible-by-k-mcq"},"prev":{"id":"b9493ecb-69f9-4ca2-9175-1dcb83b71ee6","name":"Count of Subarrays Having Sum Equals to K.","type":3,"slug":"count-of-subarrays-having-sum-equals-to-k"}}}`

# Longest Subarray With Sum Divisible By K

1. You are given an array of integers(arr) and a number K. 2. You have to find length of the longest subarray whose sum is divisible by K.

`{"id":"b03bd9f4-2dc2-47ad-a641-1e69511bfa31","name":"Longest Subarray With Sum Divisible By K","description":"1. You are given an array of integers(arr) and a number K.\r\n2. You have to find length of the longest subarray whose sum is divisible by K.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number K","outputFormat":"A number representing length of the longest subarray whose sum is divisible by K.","constraints":"1 &lt;= N,K &lt;= 10^5\r\n-1000 &lt;= arr[i] &lt;= 1000","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nint solution(vector<int> &arr, int k) {\n // write your code here\n}\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for(int i = 0; i < n; i++){\n cin >> arr[i];\n }\n int k;\n cin >> k;\n cout << solution(arr, k) << endl;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int[] arr, int k) {\r\n // write your code here\r\n\r\n return 0;\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner 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 k = scn.nextInt();\r\n System.out.println(solution(arr, k));\r\n }\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"6\r\n2 7 6 1 4 5\r\n3","sampleOutput":"4\r\n","questionVideo":"https://www.youtube.com/embed/GrV3MTR_Uk0?end=24","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":"4f7f69c0-a46e-49b3-bcf1-f1fb96d4eb9a","name":"Longest Subarray With Sum Divisible By K","slug":"longest-subarray-with-sum-divisible-by-k","type":1}],"next":{"id":"e0f1565e-1c2f-4155-b914-b3eaf767cabf","name":"Longest Subarray With Sum Divisible By K MCQ","type":0,"slug":"longest-subarray-with-sum-divisible-by-k-mcq"},"prev":{"id":"b9493ecb-69f9-4ca2-9175-1dcb83b71ee6","name":"Count of Subarrays Having Sum Equals to K.","type":3,"slug":"count-of-subarrays-having-sum-equals-to-k"}}}`

Editor

# Longest Subarray With Sum Divisible By K

easy

1. You are given an array of integers(arr) and a number K. 2. You have to find length of the longest subarray whose sum is divisible by K.

## Constraints

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

## Format

### Input

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

### Output

A number representing length of the longest subarray whose sum is divisible by 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;}6 2 7 6 1 4 5 3```

### 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;}4 ```

Question Video

Discussions

Show Discussion

Related Resources