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

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →