Find the next greater element in an array

Sam Radhakrishnan

Given an array , for every element I need to find the smallest element to the right of the given element which is greater than the current element.

Mathematically, For every index i in array A, I need to find index j such that

A[j] > A[i]
j > i
A[j] - A[i] is minimum

I need to find j for every index i

The brute force solution would be O(n^2) and I am hoping to do better. I am thinking that an O(n log n) solution is possible using self-balancing BST but that seems pretty complex. Moreover I need a O(n) solution.

Is there a O(n) solution to this problem? Is there a proof that the lower bound is O(n log n)?

Abhishek Bansal

Proof of O(nlogn) lower bound: (for comparison based algorithms)

Lets say we have a comparison-based algorithm that would accomplish this task in O(n). That is for each index, we have the immediate greater element to its right (say R[i]).

Similarly we can run this algorithm on the reversed input array and then reverse the result. For each index we have the immediate greater element to its left (say L[i]).

This means that in O(n) we have for each element, the immediate greater element in the array = min (R[i], L[i]).

We can now sort the array using this information.

Find the minimum element in the array. Find its successor (immediate greater element), then its successor's successor etc. Hence you would have got the entire array in sorted order.

Sorted the array in O(n) using only comparisons (a contradiction).

O(nlogn) Algorithm:
Start building the balanced BST from the right of the array. The nodes would contain the values and the respective indices.

Then for each new element encountered, inserting it in the BST would get corresponding nearest larger index/value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the next greater element in an array

From Dev

Find next greater element in AVL tree

From Dev

Find next greater or equal element using two arrays

From Dev

Interview - find greater element for each array`s element

From Dev

PHP, How to find next greater key value from array

From Dev

Time Complexity of Next Greater Element

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

How to find number of elements greater/smaller than an element in an array?

From Dev

How to find the next element given a certain element in array

From Dev

Find the least greater element on the right

From Dev

Find the least greater element on the right

From Dev

Find next element in jquery

From Dev

Unable to find next element

From Dev

pointer to next element of an array

From Dev

Get next array element

From Dev

jQuery, find next element with id

From Dev

Find Element Next to Element With Specified Text

From Dev

How to find element next to another element?

From Dev

Reduced time complexity of inner loop: Find count of elements greater than current element in the first loop and store that in solved array

From Dev

Grabbing previous and next element in an array

From Dev

Check for next 'false' element in array

From Dev

MongoDB: find element by array element

From Dev

Mongoose find element in array

From Dev

Find the last element in an array

From Dev

Find an element from a array

From Dev

php array chunk with next element of an array

From Dev

Find Next Sibling Element In DOM With JavaScript

From Dev

Find next select element inside class

From Dev

Find first next element and then stop the search

Related Related

HotTag

Archive