Skip to content

Commit

Permalink
Merge pull request #470 from Kaustubh251002/kaustubh
Browse files Browse the repository at this point in the history
Added Trapping Rainwater problem to Java_Problems; Added name to contributors.html
  • Loading branch information
fineanmol authored Oct 3, 2021
2 parents 65e1fca + 72acacf commit ea96841
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Contributors.html
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,13 @@
<a class="box-item" href="https://github.com/a-ayush19"><span>Ayush Awasthi</span></a>
<a class="box-item" href="https://github.com/SamarthSawhney"><spann>Samarth Sawhney</span></a>
<a class="box-item" href="https://github.com/highflyer910"><span>Thea M.</span></a>

<a class="box-item" href="https://github.com/nyctonio"><span>Ritesh Kumar</span></a>
<a class="box-item" href="https://github.com/geeky01adarsh"><span>Adarsh Navneet Sinha</span></a>
<a class="box-item" href="https://github.com/RAJASETHI"><span>Raja Sethi</span></a>
<a class="box-item" href="https://github.com/PrajaktaSathe"><span>Prajakta Sathe</span></a>
<a class="box-item" href="https://github.com/Kaustubh251002"><span>Kaustubh Mishra</span></a>




Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gfg_practice;
import java.io.*;
import java.util.*;
import java.lang.*;
class Trappingwater{
static long trappingWater(int arr[], int n) {
int left[]=new int[n];
int right[]=new int[n];
left[0]=0;
right[n-1]=0;
int leftmax=0;
int rightmax=0;
for(int i=1;i<n;i++){
if(arr[i-1]>leftmax)
leftmax=arr[i-1];
left[i]=leftmax;
}
for(int i=n-2;i>=0;i--){
if(arr[i+1]>rightmax)
rightmax=arr[i+1];
right[i]=rightmax;
}
long amount=0;
for(int i=1;i<n-1;i++){
int trapped_water= Math.min(left[i],right[i])-arr[i];
if(trapped_water>0)
amount+=trapped_water;
}
return amount;
}
public static void main(String args[]) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine().trim());// for inputting the testcases
while(t-->0) {
int n=Integer.parseInt(br.readLine().trim());
int arr[]=new int[n];
String inputLine[]=br.readLine().trim().split(" ");
for(int i=0;i<n;i++) {
arr[i]=Integer.parseInt(inputLine[i]);
}
System.out.println(trappingWater(arr,n));
}
}
}

0 comments on commit ea96841

Please sign in to comment.