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 m, vector<int> &a){vector<pair<int, int>> v;for (int i = 0; i < m; i++){v.push_back(make_pair(a[i], i));}sort(v.begin(), v.end());for (int i = 0; i < m; i++){cout << v[i].second << " ";}cout << "\n";}int main(){int m = 5;vector<int> a = {4, 5, 3, 7, 1};sortItOut(m, a);return 0;}
No comments:
Post a Comment