Poisonous Plants - Stack Span Problem - Coding Challenge

There are plants in a garden. Each of these plants has been added with some amount of pesticide. After each day, if any plant has more pesticide than the plant at its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each plant. Print the number of days after which no plant dies, i.e. the time after which there are no plants with more pesticide content than the plant to their left.

Input Format

The input consists of an integer . The next line consists of integers describing the array where denotes the amount of pesticide in plant .

Constraints

Output Format

Output a single value equal to the number of days after which no plants die.

Sample Input

7
6 5 8 4 7 10 9

Sample Output

2

package com.test;

import java.util.*;

public class StkLife {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                int N = sc.nextInt();

                int[] a = new int[N];

                for (int i = 0; i < a.length; i++) {
                        a[i] = sc.nextInt();
                }

                System.out.println(process(a));
        }

        public static int process(int[] a) {

                Stk A = new Stk(a.length);

                int max = 0;

                A.push(0);

                int[] k = new int[a.length];
                int[] d = new int[a.length];

                k[0] = -1;
                for (int i = 1; i < a.length; i++) {

                        if (a[A.peep()] < a[i]) {

                                k[i] = A.peep();

                                d[i] = 1;

                        } else if (a[A.peep()] == a[i]) {

                                k[i] = k[A.peep()];

                                if (k[i] != -1)
                                        d[i] = d[A.peep()] + 1;
                                else
                                        d[i] = d[A.peep()];

                        } else if (a[A.peep()] > a[i]) {

                                while (A.getCurrentPointer() != -1 && a[A.peep()] > a[i]) {
                                        A.pop();
                                }
                                if (A.getCurrentPointer() == -1) {
                                        k[i] = -1;
                                } else if (a[A.peep()] == a[i]) {

                                        k[i] = k[A.peep()];
                                        if (k[i] != -1) {

                                                d[i] = getMax(d, k[i] + 1, i - 1) + 1;
                                        } else
                                                d[i] = d[A.peep()] + 1;

                                } else {
                                        k[i] = A.peep();

                                        d[i] = getMax(d, k[i] + 1, i - 1) + 1;

                                }

                        }
                        A.push(i);
                        if (max < d[i])
                                max = d[i];
                }

                return max;
        }

        public static int getMax(int[] maxK, int start, int end) {

                int max = 0;
                for (int i = start; i <= end; i++) {
                        int c = maxK[i];
                        if (c > max)
                                max = c;
                }
                return max;
        }

}

class Stk {

        private int[] stkArr = null;

        private static int[] stkMaxArr = null;

        private static int maxPointer = 0;

        private int currentPointer = -1;

        private int size = 0;

        private int total = 0;

        public Stk(int size) {
                super();
                this.size = size;
                stkArr = new int[size];
        }

        public void push(int x) {
                currentPointer++;
                stkArr[currentPointer] = x;

                total = total + x;

        }

        public int getTotal() {
                return total;
        }

        public int pop() {
                int val = stkArr[currentPointer];
                stkArr[currentPointer] = 0;
                currentPointer--;

                total = total - val;

                return val;

        }

        public int peep() {
                if (currentPointer > -1)
                        return stkArr[currentPointer];
                else
                        return -99;
        }

        public int getCurrentPointer() {
                return currentPointer;
        }

        public int getSize() {
                return size;
        }

        public int getMax() {
                return stkMaxArr[maxPointer];
        }
}

Interview Questions: 

Search