• + 0 comments

    here is my code which passes only few test cases. Can someone help me identify what is wrong with this?

    public static List<Integer> solve(List<Integer> a, List<List<Integer>> queries) {
            int n = a.size();
            int[][] arr = new int[n][n];
            List<Integer> ans = new ArrayList<>();
            for (int i=0; i< n ; i++) {
                 arr[i][i] = a.get(i);            
                for (int j=i+1; j<n; j++) {
                    if (a.get(j) > arr[i][j-1]) {
                        arr[i][j] = a.get(j);
                    }
                    else arr[i][j] = arr[i][j-1];
                }
            }
            for (int i=0;i<n;i++) {
                for (int j=0;j<n;j++) {
                    System.out.print(arr[i][j]+" ");
                }
                System.out.println();
            }
            int[][] sum = new int[n][n];
            for (int i=0;i<n;i++) {
                for (int j=0;j<n;j++) {
                    if (j==0) {
                        sum[i][0] = arr[i][0];
                    }
                    else {
                        sum[i][j] = sum[i][j-1] + arr[i][j];
                    }
                    System.out.print(sum[i][j]+" ");
                }
                System.out.println();
            }
            for (int i=0; i<queries.size(); i++) {
                int l = queries.get(i).get(0)-1;
                int r = queries.get(i).get(1)-1;
                int maxSum = 0;
                for (int j=l;j<=r;j++){
                    if (l==0){
                        maxSum += sum[j][r];
                    }
                    else {
                        maxSum += sum[j][r] - sum[j][l-1];
                    }
                }
                ans.add(maxSum);
            }
            return ans;
    
        }