You have an array of n numbers and an integer x, find the biggest subarray so that its Product is not dividable to x and n is up to 1e5
I also have a similar question for the biggest array so that their sum is not dividable to x
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 154 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 146 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 135 |
| 9 | maroonrk | 133 |
| 10 | cry | 130 |
You have an array of n numbers and an integer x, find the biggest subarray so that its Product is not dividable to x and n is up to 1e5
I also have a similar question for the biggest array so that their sum is not dividable to x
| Name |
|---|



Assuming that you mean, that we have to maximize the length of subarray such that it's sum is not divisible by $$$x$$$. Here is an approach I thought of for your second problem.
We consider the entire array, and if the sum is divisible by $$$x$$$, then search from both start and the end to find the first number that is not a multiple of $$$x$$$, choose to remove elements from the side which minimizes the length of the array the least.
For example consider, $$$x = 10$$$ and the array to be $$$A = [10, 20, 34, 55, 49, 32, 30]$$$. We first choose the entire array, then we get the sum to be $$$230$$$, now we see that it is divisible by $$$x$$$ so we search from the start and from the end for the element that is not divisible by $$$x$$$. So, from the start the third element $$$34$$$ is such an element, and from the end the second element from last that is $$$32$$$ is one such element. Since, if we remove the prefix, we will end up decreasing the length by 3 and if we remove suffix we will reduce the length by 2. So, we'll prefer suffix (since it minimizes the length by the least).
I cannot prove that this works, but I think it should, counter examples are always appreciated.
Yes i had a similar idea but i didn't pay attention that it had to be a multiple of x instead of x it self, rookie mistake, thanks
We create prefix sum of array in such a manner:
prefixsum[i] = ( prefixsum[i-1] + a[i] ) % x
Hence, the prefix_sum array would have values in the range [0,x-1].
Now, we iterate on the prefix_sum array and if we are currently at index i, and if we want to consider maximum length subarray ending at i and starting at index j+1 then we would need to find an index (j<i) such that
prefixsum[j] != prefixsum[i]
which would guarantee that prefixsum[i]-prefixsum[j] is not a multiple of x and hence the subarray sum from index j+1 to i is not divisible by x.
So, to find j we can the following logic:
If prefixsum[i] != 0, then j+1 = 0 hence max length subarray would be from 0 to i.
If prefixsum[i] = 0, then we just need the least index such that prefixsum[j] != 0 that can be easily pre-calculated.
define
dp[i]as the length of the longest subarray ending at index i.keep a variable
divwhich stores the product of some factors ofxtaken from the current subarray. The took array tracks which factor was taken from each position.For each element, we pick a factor of
xthat divides it, update div, and store that factor intook[i].If
divbecomes divisible byx, we move the left end(j)forward and remove factors untildivis no longer divisible by x.Then,
dp[i] = i - j + 1. After checking all positions, the maximum value in dp is our answer.