Problem of the day:Geeksforgeeks Convert an array to reduced form

 Convert an array to reduced form

MediumAccuracy: 54.74%Submissions: 32K+Points: 4

Given an array with N distinct elements, convert the given array to a reduced 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, and N-1 is placed for the largest element.

Note: You don't have to return anything. You just have to change the given array.

Example 1:

Input:
N = 3
Arr[] = {10, 40, 20}
Output: 0 2 1
Explanation: 10 is the least element so it
is replaced by 0. 40 is the largest
element so it is replaced by 3-1 = 2. And
20 is the 2nd smallest element so it is
replaced by 1.

Example 2:

Input:
N = 5
Arr[] = {5, 10, 40, 30, 20}
Output: 0 1 4 3 2
Explanation: As 5 is smallest element,
it's replaced by 0. Then 10 is 2nd
smallest element, so it's replaced by 1.
Then 20 is 3rd smallest element, so it's
replaced by 2. And so on.

Your Task:
You don't need to read input or print anything. Your task is to complete the function convert() which takes the array of integers arr and as parameters and makes changes in the given array.

Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 106



class Solution{

public:

// Converts arr[0..n-1] to reduced form.

void convert(int arr[], int n) {

    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;

    

    for(int i=0;i<n;i++)

    {

        pq.push(make_pair(arr[i],i));

    }

    int x=0;

    while(pq.size()!=0)

    {

        pair<int,int>temp=pq.top();

        pq.pop();

        arr[temp.second]=x;

        x++;

    }

    return;

}

};

This code defines a class "Solution" that contains a single public method "convert". The method takes in an integer array "arr" and its size "n" as input.

The method uses a priority queue, implemented using the STL container 'priority_queue', to store pairs of integers (val, index). The priority queue is initialized to store pairs in increasing order of the val based on the 'greater' comparison operator.

The method then loops through the input array, adding each element and its index as a pair to the priority queue.

Next, the method pops elements from the priority queue, assigns the popped element's index to the input array and increments a variable "x". This continues until the priority queue is empty. This results in the input array being sorted in increasing order, and elements being replaced by consecutive integers starting from 0.

In summary, this code converts an array of integers into a reduced form by sorting the array in increasing order and replacing each element with its corresponding index in the sorted array.

The time complexity of this code is O(nlogn) because the method uses a priority queue, which is implemented using a heap data structure. The heapifying process takes O(n) time, and each insertion and deletion from the heap takes O(log n) time. Since the method loops through the input array once and performs an insertion and deletion for each element, the total time complexity is O(n) + O(n log n) = O(n log n).


The space complexity of this code is O(n) because the method uses a priority queue to store n pairs of integers, each pair requires O(1) space. The priority queue also stores these n pairs of integers, which also requires O(n) space. So the total space complexity is O(n).





Comments