Given an array with n distinct elements, convert the given array to a form where all elements are in range from 0 to n-1. The order of elements is same, i.e., 0 is placed in place of smallest element, 1 is placed for second smallest element, … n-1 is placed for largest element.

Input: arr[] = {10, 40, 20} Output: arr[] = {0, 2, 1} Input: arr[] = {5, 10, 40, 30, 20} Output: arr[] = {0, 1, 4, 3, 2}

We have discussed simple and hashing based solutions.

In this post, a new solution is discussed. The idea is to create a vector of pairs. Every element of pair contains element and index. We sort vector by array values. After sorting, we copy indexes to original array.

`// C++ program to convert an array in reduced ` `// form ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Converts arr[0..n-1] to reduced form. ` `void` `convert(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// A vector of pairs. Every element of ` ` ` `// pair contains array element and its ` ` ` `// index ` ` ` `vector <pair<` `int` `, ` `int` `> > v; ` ` ` ` ` `// Put all elements and their index in ` ` ` `// the vector ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `v.push_back(make_pair(arr[i], i)); ` ` ` ` ` `// Sort the vector by array values ` ` ` `sort(v.begin(), v.end()); ` ` ` ` ` `// Put indexes of modified vector in arr[] ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `arr[v[i].second] = i; ` `} ` ` ` `// Utility function to print an array. ` `void` `printArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Driver program to test above method ` `int` `main() ` `{ ` ` ` `int` `arr[] = {10, 20, 15, 12, 11, 50}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` ` ` `cout << ` `"Given Array is \n"` `; ` ` ` `printArr(arr, n); ` ` ` ` ` `convert(arr , n); ` ` ` ` ` `cout << ` `"\n\nConverted Array is \n"` `; ` ` ` `printArr(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

Given Array is 10 20 15 12 11 50 Converted Array is 0 4 3 2 1 5

Time Complexity : O(n Log n)

Auxiliary Space : O(n)

This article is contributed by **Arpit Gupta**. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Convert an array to reduced form | Set 1 (Simple and Hashing)
- Minimum length of the reduced Array formed using given operations
- Count the numbers that can be reduced to zero or less in a game
- Find the index which is the last to be reduced to zero after performing a given operation
- vector::front() and vector::back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Binary search in sorted vector of pairs
- Find the maximum cost of an array of pairs choosing at most K pairs
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Find and print duplicate words in std::vector<string> using STL functions
- Using std::vector::reserve whenever possible
- How to find common elements between two Vector using STL in C++?