C Program to Replace each element by its Rank in the given Array

Replace each element by its rank given in array in C

In this article, we will brief in on how to replace each element by its rank in the given array using C language. There is an array of n elements, replace each element of the array by its corresponding rank. The minimum value element will have the highest rank.

Replace each element by its rank given in array in C

Algorithm :

  • Create a copy of the given input array.
  • Sort the copied array.
  • Run a nested loop and find the position of the given array element in the sorted array.
  • And replace the element with the position.
  • Print the modified input array.

Time and Space Complexity:

  • Time Complexity : O(n2)
  • Space Complexity : O(n)

Code in C++

//Write a program to Replace each element by its rank given in array in C
#include<stdio.h>

int main(){
    int arr[] = { 100, 2, 70, 12 , 90};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    int temp[n];
    for(int i=0; i<n; i++)
    temp[i] = arr[i];
    
    //sort the copied array
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            int x = temp[i];
            temp[i] = temp[j];
            temp[j] = x;
        }
    }
    
    for(int i=0; i<n; i++){
    
        for(int j=0; j<n; j++){
            if(temp[j]==arr[i])
            {
                arr[i] = j+1;
                break;
            }
        }
    }
    
    for(int i=0; i<n; i++)
    printf("%d ", arr[i]);
}

Output

5 1 3 2 4