703304. Inverse Permutation

Easy

Here is a detailed Markdown blog post for the Inverse Permutation problem:

Inverse Permutation

Summary

Given an array of distinct integers, return the inverse permutation. The inverse permutation is an array where the value at each index i is equal to the number of elements less than or equal to i. This problem involves arrays and data structures.

Detailed Explanation

The approach to solve this problem is straightforward. We iterate through the input array from left to right, keeping track of the current count of elements less than or equal to each index i. For each element at index i, we add 1 to our count since it's a new element that's less than or equal to i. Then, we append this count to the result array.

Here's a step-by-step breakdown:

  • Initialize an empty array res to store the inverse permutation.
  • Iterate through the input array from left to right:
    • For each element at index i, increment the current count by 1.
    • Append the current count to the res array.
  • Return the res array.

Time complexity: O(n), where n is the length of the input array, since we make a single pass through the array. Space complexity: O(n), since we need to store the inverse permutation in an array of size n.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.Arrays;

public int[] findInversePermutation(int[] arr) {
    int[] res = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        int count = 0;
        for (int j = 0; j <= i; j++) {
            if (arr[j] <= i) {
                count++;
            }
        }
        res[i] = count;
    }
    return res;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.