Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

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.

**Your Task:**

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<=10^{6}

-10^{5}<=A[i]<=10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Longest subarray with sum divisible by K

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.