Rainwater trapping

public static void rainwater(int[] arr,int n ) {

    int res=0;

      for(int i = 1; i < n - 1; i++)

      {

           

          

          int left = arr[i];

          for(int j = 0; j < i; j++)

          {

              left = Math.max(left, arr[j]);

          }

   

       

          int right = arr[i];

          for(int j = i + 1; j < n; j++)

          {

              right = Math.max(right, arr[j]);

          }

   

        

          res += Math.min(left, right) - arr[i];

      }

      System.out.println(res);

     

    }

      public static void main(String args[]) {

              Scanner scn= new Scanner(System.in);

                   int N=scn.nextInt();

 

                  for(int i=0;i<N;i++){

                      int n=scn.nextInt();

                      int[] arr=new int[n];

                          for(int j=0;j<n;j++){

                              arr[i]=scn.nextInt();

                          }

                          rainwater(arr,n);

                  }

               

      }

this is my code for rainwater trapping problem.and its answer is coming out zero.
any solution??!!!