how to find the length of the longest contiguous subarray whose sum is divisible by a given number

Shefali Chaudhary

I want to find the longest continuous sub array whose sum is divisible by a number 'k'.I have already done it using brute force with complexity o(n^2).But wants to do it for o(n).can anyone suggest me the efficient way to solve this problem in o(n) time.

for eg: {1 3 1 3 6}

the max length of subarray which is divisible by 3 is 2. i.e {3,6}

Also to note that the value of k here is very large around 10^6 so I can not use the prefix sum method where in modulo of each element is recorded as it is valid for small numbers only.

so kindly suggest me the efficient way to solve this problem in C.

vish4071

Here is a way that can be used:
Create an array of summation-modulo k, eg.
Let the array be: {3,4,10,15,1,4,7}
and k = 5. Then, summation modulo array would look like:
{3,2,2,2,3,2,4} which is created as: {3%5, (3+4)%5, (3+4+10)%5... } and so on. Now find max index difference b/w similar numbers. Since k<=10^6, you can easily do this using array of size k.
In this case, it can be: {(4-0 = 4) ->index of 3} or {(5-1 = 4) ->index of 2}, so 4.

#include<stdio.h>
int main(){
    int n,k,i,j;
    scanf("%d%d",&n,&k);                    //size of the input array 'n' and modular 'k'
    int a[n];
    for(i = 0;i < n;i++)
            scanf("%d",&a[i]);

    //actual processing starts
    //creating summation modulo array in 'a' itself
    a[0] %= k;
    for(i = 1;i < n;i++){
            a[i] = (a[i-1] + a[i]) % k;
    }

    int r[2][k];
    for(i = 0;i < k;i++)
            r[0][i] = r[1][i] = -1;                 //initializing 'r' to -1
    //now evaluating min and max position of any spec no.
    for(i = 0;i < n;i++){
            if(r[0][a[i]] == -1)
                    r[0][a[i]] = i;
            else
                    r[1][a[i]] = i;
    }
    //evaluation of min-max indices complete

    int g = 0;
    //now find max diff if both values are set
    for(i = 0;i < k;i++){
            if(r[0][i] != -1 && r[1][i] != -1 && r[1][i] - r[0][i] > g)
                    g = r[1][i] - r[0][i];
    }
    printf("%d\n",g);               //this is the required answer
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the longest sequence length whose sum is divisible by 3

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

Number of Subarray whose sum greater than given value

From Dev

How to find out the best combination of a given vector whose sum is closest to a given number

From Dev

how to find number that's divisible by a number in a given range?

From Dev

Find length of the longest contiguous subsequence of the same character

From Dev

Find permutations of a given set of numbers whose sum APPROACH a target number

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

Maximum sum in a contiguous subarray of a given 2D array

From Dev

program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

From Dev

Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

From Dev

Find widest subarray with given sum (array slicing)

From Dev

How do I find the size of the longest subarray?

From Java

Find all subarray with sum equal to number?

From Dev

Find number of continuous subarray having sum zero

From Dev

what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

From Dev

how to find the minimum number of primatics that sum to a given number

From Dev

How to find the number of subsets of a set X such as the sum of their elements is divisible by 3 in C?

From Dev

Finding subarray with given sum

From Dev

Find elements that sum to a given number

From Dev

Find the largest sum subarray from the given array using segment trees

From Dev

Using a map to find subarray with given sum (with negative numbers)

From Dev

How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

From Dev

largest sum of contiguous subarray No Larger than k

From Dev

how can you find the length of the longest subset (powerset) with sum equal to k with least time complexity?

From Dev

Find the length of longest chain formed using given words in String

From Dev

Find the length of longest chain formed using given words in String

From Dev

How to check if given number is divisible of 15 in fastest way?

From Dev

Find the minimum set of integers whose sum is greater than a given integer

Related Related

  1. 1

    Find the longest sequence length whose sum is divisible by 3

  2. 2

    Find numbers of subarray of an array whose sum is divided by given number

  3. 3

    Number of Subarray whose sum greater than given value

  4. 4

    How to find out the best combination of a given vector whose sum is closest to a given number

  5. 5

    how to find number that's divisible by a number in a given range?

  6. 6

    Find length of the longest contiguous subsequence of the same character

  7. 7

    Find permutations of a given set of numbers whose sum APPROACH a target number

  8. 8

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  9. 9

    Maximum sum in a contiguous subarray of a given 2D array

  10. 10

    program to find perfect number : error in output . perfect number is number whose sum of factors equals the given number

  11. 11

    Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

  12. 12

    Find widest subarray with given sum (array slicing)

  13. 13

    How do I find the size of the longest subarray?

  14. 14

    Find all subarray with sum equal to number?

  15. 15

    Find number of continuous subarray having sum zero

  16. 16

    what can be the best algorithm to find numbers with only single set bit and whose sum is equal to given number?

  17. 17

    how to find the minimum number of primatics that sum to a given number

  18. 18

    How to find the number of subsets of a set X such as the sum of their elements is divisible by 3 in C?

  19. 19

    Finding subarray with given sum

  20. 20

    Find elements that sum to a given number

  21. 21

    Find the largest sum subarray from the given array using segment trees

  22. 22

    Using a map to find subarray with given sum (with negative numbers)

  23. 23

    How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

  24. 24

    largest sum of contiguous subarray No Larger than k

  25. 25

    how can you find the length of the longest subset (powerset) with sum equal to k with least time complexity?

  26. 26

    Find the length of longest chain formed using given words in String

  27. 27

    Find the length of longest chain formed using given words in String

  28. 28

    How to check if given number is divisible of 15 in fastest way?

  29. 29

    Find the minimum set of integers whose sum is greater than a given integer

HotTag

Archive