// creating a monotonic stack int[] arr = new int[]{1,2,6,3,5,9, 11,7,}; Deque<Integer> stack = new LinkedList<>(); for(int i=0; i<arr.length; i++) { while(!stack.isEmpty() && stack.peek()< arr[i]) { stack.pop(); } stack.push(arr[i]); } System.out.println("stack is " + stack);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter