for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
I'm new to programming and I came across this piece of code here. Can anyone explain what it does?
for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
I'm new to programming and I came across this piece of code here. Can anyone explain what it does?
Translate to:
for (int i = 0; i< n; i++) {
int temp1 = arr[i];
int temp2 = temp1%k;
int temp3 = arr[temp2];
arr[temp2] = temp3+k;
}
Edit: thanks for the correction @R Sahu
The loop
for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
is equivalent to
for (int i = 0; i< n; i++)
{
int temp1 = arr[i];
int temp2 = temp1%k;
arr[temp2] = arr[temp2]+k;
}
arr[arr[i]%k] += k;
arr // (array)
arr[i]; // hereinafter "x" (The i-th element of the array)
x%k; // hereinafter "res" (result of the modulo)
arr[res]; //(The res-th element of the array)