0% found this document useful (0 votes)
13 views4 pages

Problem 1:: Trapping Rain Water Hard 33.14% 467K+ 8 20m Arr Examples: Input: Output: Explanation

The document presents two programming problems: 'Trapping Rain Water' and 'Maximum Sub Array'. The first problem involves calculating the amount of water that can be trapped between blocks represented by an array of heights, while the second problem requires finding the contiguous subarray with the maximum sum containing only non-negative numbers. Each problem includes example inputs, outputs, and constraints along with a sample program for the solution.

Uploaded by

wmishra44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Problem 1:: Trapping Rain Water Hard 33.14% 467K+ 8 20m Arr Examples: Input: Output: Explanation

The document presents two programming problems: 'Trapping Rain Water' and 'Maximum Sub Array'. The first problem involves calculating the amount of water that can be trapped between blocks represented by an array of heights, while the second problem requires finding the contiguous subarray with the maximum sum containing only non-negative numbers. Each problem includes example inputs, outputs, and constraints along with a sample program for the solution.

Uploaded by

wmishra44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem 1:

Trapping Rain Water


Difficulty: HardAccuracy: 33.14%Submissions: 467K+Points: 8Average Time: 20m
Given an array arr[] with non-negative integers representing the height of blocks. If the width of each
block is 1, compute how much water can be trapped between the blocks during the rainy season.
Examples:
Input: arr[] = [3, 0, 1, 0, 4, 0 2]
Output: 10
Explanation: Total water trapped = 0 + 3 + 2 + 3 + 0 + 2 + 0 = 10 units.

Input: arr[] = [3, 0, 2, 0, 4]


Output: 7
Explanation: Total water trapped = 0 + 3 + 1 + 3 + 0 = 7 units.
Input: arr[] = [1, 2, 3, 4]
Output: 0
Explanation: We cannot trap water as there is no height bound on both sides.
Input: arr[] = [2, 1, 5, 3, 1, 0, 4]
Output: 9
Explanation: Total water trapped = 0 + 1 + 0 + 1 + 3 + 4 + 0 = 9 units.
Constraints:
1 < arr.size() < 105
0 < arr[i] < 103
Program:
int n=arr.length;
int[] left=new int [n];
int[] right=new int [n];
left[0]=arr[0];
for(int i=1;i<n;i++){
left[i]=Math.max(left[i-1],arr[i]);
}
right[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--){
right[i]=Math.max(right[i+1],arr[i]);
}
int total=0;
for(int i=0;i<n;i++){
total=total+Math.min(right[i],left[i])-arr[i];
}
return total;
}
}

Output:
Problem 2:
Maximum Sub Array
Difficulty: MediumAccuracy: 15.84%Submissions: 96K+Points: 4
Given an array of integers, find the contiguous subarray with the maximum sum that contains only non-
negative numbers. If multiple subarrays have the same sum, return the one with the smallest starting
index.
A subarray is a contiguous non-empty sequence of elements within an array.
Examples:
Input: arr[] = [1, 2, 3]
Output: [1, 2, 3]
Explanation: In the given array, every element is non-negative, so the entire array [1, 2, 3] is the valid
subarray with the maximum sum.
Input: arr[] = [-1, 2]
Output: 2
Explanation: The only valid non-negative subarray is [2], so the output is [2].

Input: arr[] = [1, 2, 5, -7, 2, 6]


Output: [1, 2, 5]
Explanation: The valid non-negative subarrays are [1, 2, 5] and [2, 6]. Both have the same sum of 8, but [1,
2, 5] starts earlier, so it is the preferred subarray.
Constraints:
1 ≤ arr.size() ≤ 106
-105 ≤ arr[i] ≤ 105

Program:
class Solution {
public ArrayList<Integer> findSubarray(int arr[]) {
ArrayList<Integer> maxSubarray = new ArrayList<>(), temp = new ArrayList<>();
long maxSum = -1, tempSum = 0;

for (int num : arr) {


if (num >= 0) {
tempSum += num;
temp.add(num);
} else {
if (tempSum > maxSum) {
maxSum = tempSum;
maxSubarray = new ArrayList<>(temp);
}
tempSum = 0;
temp.clear();
}
}
return (tempSum > maxSum) ? temp : maxSubarray.isEmpty() ? new ArrayList<>(List.of(-1)) :
maxSubarray;
}
}

Output:

You might also like