📜  按除以X的数组元素的商的非降序打印索引

📅  最后修改于: 2021-04-27 18:50:57             🧑  作者: Mango

给定一个由N个整数和一个整数X组成的数组arr [] ,任务是用X对数组元素进行整数除法,并以得到的商的非降序打印该数组的索引。

例子:

方法:请按照以下步骤解决问题:

  • 遍历数组
  • 初始化向量对。
  • 对于每个数组元素,将按X除后获得的商的值存储为向量中对的第一个元素,将第二个元素存储为整数(按要求的顺序)。
  • 遍历之后,对向量进行排序,然后最终打印对中的所有第二个元素。

下面是上述方法的实现:

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 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((order[i] / X), i + 1));
        }
        else
        {
            vect.Add(new Tuple((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.


输出:
1 3 2

时间复杂度: O(N)
辅助空间: O(N)