 Open in App
Not now

# Print indices in non-decreasing order of quotients of array elements on division by X

• Last Updated : 01 Dec, 2022

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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to print the order of array` `// elements generating non-decreasing` `// quotient after division by X` `void` `printOrder(``int` `order[], ``int` `N, ``int` `X)` `{`   `    ``// Stores the quotient and the order` `    ``vector > vect;`   `    ``// Traverse the array` `    ``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 the vector` `    ``sort(vect.begin(), vect.end());`   `    ``// Print the order` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``cout << vect[i].second << ``" "``;` `    ``}`   `    ``cout << endl;` `}`   `// Driver Code` `int` `main()` `{`   `    ``int` `N = 3, X = 3;` `    ``int` `order[] = { 2, 7, 4 };` `    ``printOrder(order, N, X);`   `    ``return` `0;` `}`

## Java

 `/*package whatever //do not write package name here */`   `import` `java.io.*;` `import` `java.util.*;` `class` `GFG {`   `    ``// Function to print the order of array` `    ``// elements generating non-decreasing` `    ``// quotient after division by X` `    ``static` `void` `printOrder(``int` `order[], ``int` `N, ``int` `X)` `    ``{`   `        ``// Stores the quotient and the order` `        ``ArrayList<``int``[]> vect = ``new` `ArrayList<>();`   `        ``// Traverse the array` `        ``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` `});` `            ``}` `        ``}`   `        ``// Sort the vector` `        ``Collections.sort(vect, (a, b) -> a[``0``] - b[``0``]);`   `        ``// Print the order` `        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``System.out.print(vect.get(i)[``1``] + ``" "``);` `        ``}`   `        ``System.out.println();` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String args[])` `    ``{`   `        ``int` `N = ``3``, X = ``3``;` `        ``int` `order[] = { ``2``, ``7``, ``4` `};` `        ``printOrder(order, N, X);` `    ``}` `}` `// This code is contributed by hemanth gadarla`

## Python3

 `# Python3 program for the above approach`   `# Function to print the order of array` `# elements generating non-decreasing` `# quotient after division by X` `def` `printOrder(order, N, X):`   `    ``# Stores the quotient and the order` `    ``vect ``=` `[]`   `    ``# Traverse the array` `    ``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``])`   `    ``# Sort the vector` `    ``vect ``=` `sorted``(vect)`   `    ``# Print the order` `    ``for` `i ``in` `range``(N):` `        ``print``(vect[i][``1``], end ``=` `" "``)`   `        ``# Driver Code` `if` `__name__ ``=``=` `'__main__'``:`   `    ``N, X ``=` `3``, ``3` `    ``order ``=` `[``2``, ``7``, ``4``]` `    ``printOrder(order, N, X)`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement` `// the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG ` `{`   `// Function to print the order of array` `// elements generating non-decreasing` `// quotient after division by X` `static` `void` `printOrder(``int``[] order, ``int` `N, ``int` `X)` `{`   `    ``// Stores the quotient and the order` `    ``List> vect = ``new` `List>(); `   `    ``// Traverse the array` `    ``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));` `        ``}` `    ``}`   `    ``// Sort the vector` `    ``vect.Sort();`   `    ``// Print the order` `    ``for` `(``int` `i = 0; i < N; i++)` `    ``{` `        ``Console.Write(vect[i].Item2 + ``" "``);` `    ``}`   `    ``Console.WriteLine();` `}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int` `N = 3, X = 3;` `    ``int``[] order = { 2, 7, 4 };` `    ``printOrder(order, N, X);` `}` `}`   `// This code is contributed by code_hunt.`

## Javascript

 ``

Output:

`1 3 2`

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles