find longest subarray whose sum divisible by K.
It is possible in O(n)?
If not can it be done in better then n^2 ?
find longest subarray whose sum divisible by K. It is possible in O(n)? If
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let
s[i] = sum of first i elements modulo K.We have:
We have to find, for each
i, the smallestjsuch thats[i] == s[j]. You can find this by hashing thes[i]values. IfKis small, you can just keep an arrayp[i] = first position for which s[k] == i.The complexity is
O(n).