Question Name:Monk and Modulo Based Sorting

#include <iostream>
#include <algorithm>
 
using namespace std;
 
int main() {
	int n,k;
	cin>>n>>k;
	
	int a[n];
	
	for(int i=0;i<n;i++)
	{
	    cin>>a[i];
	}
	
	for(int i=0;i<n-1;i++)
	{
	    for(int j=0;j<n-i-1;j++)
	    {
	        if(a[j] % k > a[j+1] % k)
	        {
	            swap(a[j],a[j+1]);
	        }
	    }
	}
	
	for(int i=0;i<n;i++)
	{
	    cout<<a[i]<<" ";
	}
	
	
	
	
}
  • Problem Description
    Monk likes to experiment with algorithms. His one such experiment is using modulo in sorting.He describes an array modulo sorted as:

    Given an integer k, we need to sort the values in the array according to their modulo with k. That is, if there are two integers a and 
    b, and a%k<b%k, then a would come before b in the sorted array. If a%k=b%k , then the integer which comes first in the given array remains first in the sorted array.

    Given an initial array, you need to print modulo sorted array.

    Input:
    The first line consists of two integers N and k, N being the number of elements in the array and k is the number with which we need to take the modulo.
    The next line consists of N space separated integers , denoting the elements of the array A.

    Output:
    Print the modulo sorted array of the given array.

    Constraints:

    1<=N<=10^4
    1<=k<=10^9
    1<=A[i]<=10^9
    1<=i<=N
  • Test Case 1
    Input (stdin)5 6
    12 18 17 65 46
    Expected Output12 18 46 17 65
  • Test Case 2
    Input (stdin)4 6
    4 6 2 8
    Expected Output6 2 8 4
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO