Long Distance - Amazon Top Interview Questions


Problem Statement :


Given a list of integers nums, return a new list where each element in the new list is the number of smaller elements to the right of that element in the original input list.

Constraints

n ≤ 100,000 where n is the length of nums

Example 1

Input

lst = [3, 4, 9, 6, 1]

Output

[1, 1, 2, 1, 0]

Explanation

There is 1 smaller element to the right of 3

There is 1 smaller element to the right of 4

There are 2 smaller elements to the right of 9

There is 1 smaller element to the right of 6

There are no smaller elements to the right of 1

Example 2

Input

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Output

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Example 3

Input

lst = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Output

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



Solution :



title-img




                        Solution in C++ :

vector<int> ans;
void merge(vector<int> &v, int idx[], int l, int mid, int r) {
    int l_n = mid - l + 1, r_n = r - mid;
    int left[l_n], right[r_n];
    for (int i = 0; i < l_n; i++) left[i] = idx[l + i];
    for (int i = 0; i < r_n; i++) right[i] = idx[mid + 1 + i];
    for (int i = l_n - 1, j = r_n - 1, k = r; k >= l; k--) {
        if (j < 0 or (i >= 0 and v[left[i]] > v[right[j]])) {
            ans[left[i]] += j + 1;
            idx[k] = left[i--];
        } else {
            idx[k] = right[j--];
        }
    }
}

void mergesort(vector<int> &v, int idx[], int l, int r) {
    if (l < r) {
        int mid = l + (r - l) / 2;
        mergesort(v, idx, l, mid);
        mergesort(v, idx, mid + 1, r);
        merge(v, idx, l, mid, r);
    }
}

vector<int> solve(vector<int> &nums) {
    int n = nums.size(), idx[n];
    ans = vector<int>(n, 0);
    for (int i = 0; i < n; i++) idx[i] = i;
    mergesort(nums, idx, 0, n - 1);
    return ans;
}
                    


                        Solution in Java :

import java.util.*;

// This is esentially count the number of inversions problem.

class Solution {
    public int[] solve(int[] lst) {
        if (lst.length == 0)
            return new int[0];

        Point[] points = new Point[lst.length];

        // convert given numbers to Points
        for (int i = 0; i < lst.length; i++) {
            points[i] = new Point(lst[i], i);
        }

        int[] result = new int[points.length];

        mergeSort(points, 0, points.length - 1, result);

        return result;
    }

    private Point[] mergeSort(Point[] points, int start, int end, int[] result) {
        if (start >= end)
            return new Point[] {points[start]};

        int mid = start + (end - start) / 2;

        Point[] left = mergeSort(points, start, mid, result);
        Point[] right = mergeSort(points, mid + 1, end, result);

        return merge(left, right, result);
    }

    private Point[] merge(Point[] left, Point[] right, int[] result) {
        Point[] merged = new Point[left.length + right.length];

        int i = 0;
        int j = 0;

        while (i < left.length && j < right.length) {
            if (left[i].val <= right[j].val) {
                merged[i + j] = left[i];
                result[left[i].index] += j;
                i++;
            } else {
                merged[i + j] = right[j];
                j++;
            }
        }

        while (i < left.length) {
            merged[i + j] = left[i];
            result[left[i].index] += j;
            i++;
        }

        while (j < right.length) {
            merged[i + j] = right[j];
            j++;
        }

        return merged;
    }

    // Used as a utility class to easily identify the index of an element
    private class Point {
        int index;
        int val;

        public Point(int val, int i) {
            this.val = val;
            this.index = i;
        }
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, lst):
        def mergesort(indexes):
            k = len(indexes)
            if k <= 1:
                return indexes
            left = mergesort(indexes[: k // 2])
            right = mergesort(indexes[k // 2 :])
            for i in range(len(indexes) - 1, -1, -1):
                if not right or left and lst[left[-1]] > lst[right[-1]]:
                    ans[left[-1]] += len(right)
                    indexes[i] = left.pop()
                else:
                    indexes[i] = right.pop()
            return indexes

        n = len(lst)
        ans = [0] * n
        mergesort(list(range(n)))
        return ans
                    


View More Similar Problems

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →