**Problem**

Given an array, which can have positive and negative numbers, write a code to print the next greatest element for each element. For the last number, next greater element will always be Integer.MIN.

For element, for which no greater number is present, print Integer.MIN.

*Example – input array – [7,1,5,3,6,4]*

*Expected output – [MIN,6,6,6,MIN,MIN]* .{MIN is considered as Integer.MIN}

Method to implement :

public String[] solution(int[] input);

**Solution**

public String[] solution(int[] input){ if(input == null || input.length <=1 ) return null; int n = input.length; String[] result = new String[n]; Stack<Integer> s = new Stack<>(); result[n-1] = "MIN"; s.push(input[n-1]); for(int i = n-2;i>=0;i--){ while (!s.empty() && s.peek() < input[i]){ s.pop(); } if(s.isEmpty()){ result[i] = "MIN"; s.push(input[i]); }else{ result[i] = s.peek().toString(); } } return result; }

**Implementation**

- Find the day on which stock should be bought and sell, to have maximum profit, Stock can be bought and sold only once.