AfterAcademy Tech
•
05 Nov 2019

Level: Medium
Asked in: Amazon, Microsoft
Problem Description: Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.
The next greater element is the first greater element on an element’s right side in the array. e.g., an element A[j] would be the next greater element of A[i] such that A[j] > A[i] , i < j and j - i is minimum.
For example :
Input: A[] = {1, 2, 3, 4, 5}
Output = {2, 3, 4, 5, -1}
Input: A[] = {12, 1, 0, 17, 10}
Output: {17, 17, 17, -1, -1}
Possible questions to ask the interviewer:-
We would be discussing two possible solutions for this problem
The naive approach to this problem would be to linearly traverse the right side from the element until you find the next element greater of the current element.
Pseudo-Code
int[] findNextGreaterElement(int A[], int n)
{
int ans[]
for( i = 0 to n-1 )
{
bool found = False
for ( j = i+1 to n-1 )
{
if ( A[j] > A[i] )
{
found = True
ans.append(A[j])
break
}
}
if ( found == False )
ans.append(-1)
}
return ans
}
Complexity Analysis
There are two nested loops where worst-case occurs when elements are sorted in decreasing order. Time Complexity = O(n²)
Space Complexity: O(n), for storing ans[] array
Critical ideas to think!
Stack is an ideal data structure for such problems. It stores the elements or its index for whom the next greater element is not yet found and pops them as soon as it meets the next greater element.
Solution Steps
Stack can be used to store either element values or their indices. Since we need to return an array with the next greater element at corresponding indices, the order is important for us and hence we shall store indices.
4. All elements that remain in the stack, in the end, have the next greater element as -1. Since the default value in ans[] was -1, the corresponding indices of these elements are already filled with -1. Return ans[] array
Pseudo-Code
int[] findNextGreaterElement(int A[], int n)
{
int ans[n]
for( i = 0 to n-1 )
ans[i] = -1
Create an integer stack S
S.push(0)
for ( i = 1 to n-1 )
{
while (S.empty()== False && A[S.top()] < A[i])
{
ans[S.top()] = A[i]
S.pop()
}
S.push(i)
}
return ans
}
Complexity Analysis
Note that each element is visited at most twice, first while pushing them onto the stack and second while popping them from the stack. (Think!)
Time Complexity: Initializing ans[] array with -1 + Traversal of the array and Stack Manipulation = O(n) + O(2*n) (Why?) = O(n)
Space Complexity: Stack of size n + ans[] array of size n = O(n) + O(n) = O(n)
Critical ideas to think!

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.

AfterAcademy Tech
You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.
