Given an array find the maximum integer remainder i.e ( maximum(a1 % a2) in the array where a1, a2 are the elements of the array and a1 > a2).
Examples :
Input : a = { 3, 4 ,5 }
Output : 2
Explanation :
Remainder 2 is obtained by 5 % 3 which is maximum in this array .
Input : a = { 3, 4 ,11 }
Output : 3
In this problem, we have to calculate the maximum remainder possible in the array.
Brute-Force Approach : First simple solution is to iterate over all pairs in the array and compute the maximum remainder possible. This has O(n^2) time complexity.
Efficient Approach :
Now in this approach what we have to do is to iterate over all integer x, such that x is divisible by a1 (a1 is any element of array ) in range from a1 to 2X(max of all array elements). For each such x we need to find maximum a2, such a2 < x .
Let us describe the idea more clearly . To calculate maximum remainder first we have to determine the maximum value of an array , so that we can iterate values till 2 times the maximum . Here we are iterating till 2 times of maximum because if maximum is not multiple of any array element than , we can check the remainder by calulating till its second multiple . For example if array contain 2 elements { 999 , 1000 }. Then if we iterate till max then we get remainder 0 , but it should be 1 .
Another important thing here is one array to maintain the values to check the maximum remainder . We have taken an array b which maintains the array elements at its index by assigning ( b[a[i]] = a[i] ) to all those elements present in the array and b[i] = b[i-1] to all those index i which are not present in the array . This is done because remainder obatained is always less than that value present in the array .
After calculating array b , next thing we have to do is to iterate over all the multiples of the elements present in the array till 2 times the maximum of all elements present in the array ( as it is explained previously why it has been done like this ). Then in this we have to check the maximum remainder by dividing the b[i] ( ith index value of array b , multiples of a1 ) by a1( an element present in the array ). Through this loop we get the maximum remainder present in the array .

Implementation :
#include <bits/stdc++.h>
using namespace std;
int MaximumRemainder(int a[] , int n)
{
// mx stores the maximum value of array
// Here one assumption is made that
// numbers should be less than 5*10^5
int mx= 0 , b[1000001];
memset(b,0,sizeof(b));
for (int i = 0; i<n ; i++)
{
b[ a[i] ] = a[i];
// mx stores the maximum
// value of the array
mx = max(mx,a[i]);
}
// ans stores the final answer .
int ans = 0;
// Here in this loop we are assigning
// values in the array b as b[i] = b[i-1]
// by which we assign those index of array b
// which is present in the array . By assigning
// previous value we can assure that remainder
// is basically less than that index and what
// would be the remainder if we divide those
// values with these index values , which is what
// done in the next loop .
for (int i=1 ; i<=2*mx ; i++)
{
// those index which are not
// present in array is initialized as
// 0.
if (b[i] == 0)
{
// prroviding previous value
b[i] = b[i-1];
}
}
// Now in this loop we are calculating
// the maximum remainder . In the previous
// loop we have assigned value to all the elements
// of array b i.e previous value . Due to this we
// can calculate remainder of those array elements
// which is not multiple of any particular array
// elements smaller than it .
for (int i=2 ; i<=mx ; i++)
{
// first we check whether the element
// is present in the array or not
if (b[i] == i)
{
// Through this loop we can check
// for all values remainder for all
// values multiple of i . As if that
// element(multiple of i) is not there
// then due to the array b smaller element
// than that value is there at that index j.
// That's why we have assigned previous value
// in the array b.
for (int j=2*i-1;j<=2*mx;j+=i)
{
// checking whether tha ans is maximum .
if (i <= b[j] && ans < b[j]%i)
{
ans = b[j]%i;
}
}
}
}
return ans;
}
int main()
{
int a[] = { 3 , 4 , 5 };
int n = sizeof(a)/sizeof(a[0]);
cout << MaximumRemainder(a,n) << endl;
return 0;
}
Output : 2