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

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →