# Longest subarray with sum divisible by K | Solution

Given an array containing N integers and a positive integer K, find the length of the longest sub array with sum of the elements divisible by the given value K.

Example 1:

```Input:
A[] = {2, 7, 6, 1, 4, 5}
K = 3
Output: 4
Explanation:The subarray is {7, 6, 1, 4}
with sum 18, which is divisible by 3.```

Example 2:

```Input:
A[] = {-2, 2, -5, 12, -11, -1, 7}
K = 3
Output: 5
Explanation:
The subarray is {2,-5,12,-11,-1} with
sum -3, which is divisible by 3.```

The input is already taken care of by the driver code. You only need to complete the function longSubarrWthSumDivByK() that takes an array (arr), sizeOfArray (n), positive integer K, and return the length of the longest subarray which has sum divisible by K. The driver code takes care of the printing.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1<=N,K<=106
-105<=A[i]<=105

class Solution{
public:
int longSubarrWthSumDivByK(int arr[], int n, int k)
{
int ans=0;
int pre[n]={0};
pre=arr;
for(int i=1; i<n; i++){
pre[i]=pre[i-1]+arr[i];
}
int sum=0;
map<int,int>m;
m=0;
for(int i=0; i<n; i++){
sum+=arr[i];
if(sum%k==0)ans=i+1;
else{
int r=sum%k;
if(r<0)r+=k;
auto it=m.find(r);
if(it!=m.end())ans=max(ans,abs(it->second-i));
else m[r]=i;
}
}
return ans;
}
};