There is an array of integers and there is a number M.
The array needs to be sorted array with respect to the following criterion:
(arr[i] % M, arr[i] / M)
arr[i] % M
arr[i] / M
std::sort(arr, arr + n, comp) // comp is a custom comparator.
sort
std::sort()
you want the values to be sorted by arr[i]%m
, and if two values are equal then you want to sort by arr[i]/m
.
bool cmp(int a,int b)
{
if(a%m < b%m)
return true;
if(a%m > b%m)
return false;
return a/m < b/m;
}
See here for a working example.