Thursday, September 21, 2017

Indeed Prime Challenge 2015

Problem:
For example, consider array A such that:
A[0] = 1 A[1] = 3 A[2] = 2 A[3] = 1 A[4] = 2 A[5] = 1 A[6] = 5 A[7] = 3 A[8] = 3 A[9] = 4 A[10] = 2
The following picture illustrates the landscape after it has flooded:
The gray area is the rock floor described by the array A above and the blue area with dashed lines represents the water filling the low-lying areas with maximum possible volume. Thus, blocks 3 and 5 have a water depth of 2 while blocks 2, 4, 7 and 8 have a water depth of 1. Therefore, the maximum water depth of this area is 2.


Code:

public class Solution {

    public static void main(String[] nt){
        int[] a={1,3,2,1,2,1,5,3,3,4,2};
        System.out.print("Max Depth is : "+solution(a));
    }

    static int solution(int[] a){
        int i=Integer.MIN_VALUE;
        int[] left=new int[a.length];
        int[] right=new int[a.length];

        int max=a[0];
        for(int x=0;x<a.length;x++){
            if(a[x]>max){
                max=a[x];
            }
            left[x]=max;
        }

        max=a[a.length-1];
        for(int x=a.length-1;x>=0;x--){
            if(a[x]>max){
                max=a[x];
            }
            right[x]=max;
        }

        for(int x=0;x<a.length;x++){
            int z=min(left[x],right[x])-a[x];
            i=i<z?z:i;
        }

        return i;
    }

    static int min(int a,int b){
        if(a>b){
            return b;
        }return a;
    }
}

Output: