Print indices in non-decreasing order of quotients of array elements on division by X
Given an array order[] consisting of N integers and an integer X, the task is to perform integer division on the array elements by X and print the indices of the array in non-decreasing order of their quotients obtained.
Examples:
Input: N = 3, X = 3, order[] = {2, 7, 4}
Output: 1 3 2
Explanation:
After dividing the array elements by 3, the array modifies to {0, 2, 1}. Therefore, required order of output is 1 3 2.
Input: N = 5, X = 6, order[] = {9, 10, 4, 7, 2}
Output: 3 5 1 2 4
Explanation:
After dividing the array elements by 6, the array elements modify to 1 1 0 1 0. Therefore, the required sequence is 3 5 1 2 4.
Approach: Follow the steps below to solve the problem:
- Traverse the array
- Initialize a vector of pairs.
- For each array element, store the value of the quotient obtained after division by X as the first element of the pair in the vector and the second element as the position of the integer in the required order.
- After the traversal, sort the vector and then finally print all the second elements of the pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printOrder( int order[], int N, int X)
{
vector<pair< int , int > > vect;
for ( int i = 0; i < N; i++) {
if (order[i] % X == 0) {
vect.push_back({ order[i] / X,
i + 1 });
}
else {
vect.push_back({ order[i] / X + 1,
i + 1 });
}
}
sort(vect.begin(), vect.end());
for ( int i = 0; i < N; i++) {
cout << vect[i].second << " " ;
}
cout << endl;
}
int main()
{
int N = 3, X = 3;
int order[] = { 2, 7, 4 };
printOrder(order, N, X);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void printOrder( int order[], int N, int X)
{
ArrayList< int []> vect = new ArrayList<>();
for ( int i = 0 ; i < N; i++) {
if (order[i] % X == 0 ) {
vect.add( new int [] { order[i] / X, i + 1 });
}
else {
vect.add(
new int [] { order[i] / X + 1 , i + 1 });
}
}
Collections.sort(vect, (a, b) -> a[ 0 ] - b[ 0 ]);
for ( int i = 0 ; i < N; i++) {
System.out.print(vect.get(i)[ 1 ] + " " );
}
System.out.println();
}
public static void main(String args[])
{
int N = 3 , X = 3 ;
int order[] = { 2 , 7 , 4 };
printOrder(order, N, X);
}
}
|
Python3
def printOrder(order, N, X):
vect = []
for i in range (N):
if (order[i] % X = = 0 ):
vect.append([order[i] / / X, i + 1 ])
else :
vect.append([order[i] / / X + 1 ,i + 1 ])
vect = sorted (vect)
for i in range (N):
print (vect[i][ 1 ], end = " " )
if __name__ = = '__main__' :
N, X = 3 , 3
order = [ 2 , 7 , 4 ]
printOrder(order, N, X)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printOrder( int [] order, int N, int X)
{
List<Tuple< int ,
int >> vect = new List<Tuple< int ,
int >>();
for ( int i = 0; i < N; i++)
{
if (order[i] % X == 0)
{
vect.Add( new Tuple< int , int >((order[i] / X), i + 1));
}
else
{
vect.Add( new Tuple< int , int >((order[i] / X + 1), i + 1));
}
}
vect.Sort();
for ( int i = 0; i < N; i++)
{
Console.Write(vect[i].Item2 + " " );
}
Console.WriteLine();
}
public static void Main()
{
int N = 3, X = 3;
int [] order = { 2, 7, 4 };
printOrder(order, N, X);
}
}
|
Javascript
<script>
function printOrder(order, N, X)
{
let vect = [];
for (let i = 0; i < N; i++)
{
if (order[i] % X == 0)
{
vect.push([ order[i] / X, i + 1 ]);
}
else
{
vect.push([ order[i] / X + 1, i + 1 ]);
}
}
vect.sort( function (a, b){ return a[0] - b[0]});
for (let i = 0; i < N; i++)
{
document.write(vect[i][1] + " " );
}
document.write();
}
let N = 3, X = 3;
let order = [ 2, 7, 4 ];
printOrder(order, N, X);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
01 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...