bro, if we take the array [2,7,6,1,4,3] which you have used, the longest subarray length should be 5 not 4 because the subarray [7,6,1,4,3] can be considered which is divisible be k=3, which is of length 5. But in the video illustration, the iteration would stop at index i=4, am i right? please correct me
@ERROR-os9wm
8 ай бұрын
Can Somebody plzz Explain me why what is the benefit of adding k when the remainder is -ve Explain me by giving small example As if the remainder is -2 and k=3 then we add k to the remainder which will become rem = -2+3 => 1 ,Now why we chech 1 in the map
@yashdwivedigfg
8 ай бұрын
Suppose we have an array arr = {-5, 2, 10} and we want to find the length of the longest subarray with a sum divisible by k = 3. Calculate Prefix Sums: The prefix sum array would be: prefixSum = [-5, -3, 7]. Calculate Cumulative Sums Modulo K: The cumulative sums modulo k would be: modSum = [-2, 0, 1]. Using the Remainders: When calculating the cumulative sum modulo k for the first element (-5), we get -2. This is a negative remainder. If we don't adjust this negative remainder, we might run into issues when using it in further calculations or comparisons. Adjusting the Negative Remainder: By adding k to the negative remainder, we shift it to the positive range: -2 + 3 = 1. Now, the adjusted cumulative sums modulo k become: adjustedModSum = [1, 0, 1]. Using Adjusted Remainders: When we iterate through the array, using the adjusted remainders helps in correctly identifying subarrays with a sum divisible by k. If you still have doubt let me know
Пікірлер: 4