Sort it out!

You are given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array.

Example:

A={4,5,3,7,1}

After sorting the new array becomes A={1,3,4,5,7}.

The required output should be "4 2 0 1 3"   

Example:

Input:  m = 5, a = {4, 5, 3, 7, 1}
Output: 4 2 0 1 3

Approach

C++

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;
const int INF = 1e9;

void sortItOut(int mvector<int&a)
{

    vector<pair<intint>> v;

    for (int i = 0i < mi++)
    {
        v.push_back(make_pair(a[i]i));
    }

    sort(v.begin(), v.end());

    for (int i = 0i < mi++)
    {
        cout << v[i].second << " ";
    }
    cout << "\n";
}

int main()
{
    int m = 5;

    vector<inta = {45371};

    sortItOut(ma);

    return 0;
}


No comments:

Post a Comment