• + 1 comment

    hey, I tried ur logic but it giving wrong output for testcases #3,5, & 7. I m not getting what I missed. please help. here is my code. public class Solution {

    public static int canGet(Vector<Integer> r, Vector<Integer> c, int i){
        if(r.get(i)<=r.get(i-1) && r.get(i)<=r.get(i+1)){
            return 1;
        }
        if(r.get(i)<=r.get(i-1) && r.get(i)>r.get(i+1)){
            if(c.get(i+1)==0){
                int temp = canGet(r,c,i+1);
                c.set(i+1,temp);
            }
            return 1 + c.get(i+1);
        }
        if(r.get(i)>r.get(i-1) && r.get(i)<=r.get(i+1)){
            if(c.get(i-1)==0){
                int temp = canGet(r,c,i-1);
                c.set(i-1,temp);
            }
            return 1 + c.get(i-1);
        }
        if(r.get(i)>r.get(i-1) && r.get(i)>r.get(i+1)){
            if(c.get(i-1)==0){
                int temp = canGet(r,c,i-1);
                c.set(i-1,temp);
            }
            if(c.get(i+1)==0){
                int temp = canGet(r,c,i+1);
                c.set(i+1,temp);
            }
            return 1 + Math.max(c.get(i-1),c.get(i+1));
        }
        return 0;
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Vector<Integer> rank = new Vector<Integer>(n);
        for(int i=0;i<n;i++){
            int t = in.nextInt();
            rank.add(i,t);
        }
        Vector<Integer> cand = new Vector<>(n);
        for(int i=0;i<n;i++){
            cand.add(i,0);
        }
    
        if(rank.get(0)<rank.get(1)){
            cand.set(0,1);
        }
        if(rank.get(n-1)<=rank.get(n-2)){
            cand.set(n-1,1);
        }
        if(rank.get(0)>=rank.get(1)){
            int c = canGet(rank,cand,1);
            cand.set(0,c+1);
        }
        if(rank.get(n-1)>rank.get(n-2)){
            int c = canGet(rank,cand,n-2);
            cand.set(n-1,c+1);
        }
        for(int i=0;i<n;i++){
            if(cand.get(i)==0){
                int c = canGet(rank,cand,i);
                cand.set(i,c);
            }
        }
        long result = 0;
        Enumeration e = cand.elements();
        while(e.hasMoreElements()){
            int t = (Integer)e.nextElement();
            result = result + t;
        }
        System.out.println(result);
    }
    

    }