VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-an-array-to-reduced-for-using-binary-search/

⇱ Convert an Array to reduced for using Binary Search - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert an Array to reduced for using Binary Search

Last Updated : 23 Jul, 2025

Given an array arr[] consisting of N distinct integers, the task is to convert the given array into a sequence of first N non-negative integers, i.e. [0, N - 1] such that the order of the elements is the same, i.e. 0 is placed at the index of the smallest array element, 1 at the index of the second smallest element, and so on.

Examples:

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

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

Hashing-Based Approach: Please refer to the Set 1 post of this article for the hashing-based approach. 
Time Complexity: O(N* log N)
Auxiliary Space: O(N)

Vector Of Pairs Based Approach: Please refer to the Set 2 post of this article for the approach using the vector of pairs
Time Complexity: O(N* log N)
Auxiliary Space: O(N)

Binary Search-based Approach: Follow the steps to solve the problem:

Below is the implementation of the above approach:


Output: 
0 4 3 2 1 5

 

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

Comment