Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions trappingrainwater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class trappingw {
public static void value(int hight[]){
//calculate left max boundary
int n=hight.length-1;
int leftmax[] =new int[n];
leftmax[0]=hight[0];
for(int i=0;i<n;i++){
leftmax[i]=Math.max(hight[i],leftmax[i-1]);
}


//calculate right max boundary
int rightmax[]= new int[n];
rightmax[n-1]=hight[n-1];
for(int i=n-2;i>0;i--){
rightmax[i]=Math.max(hight[i],rightmax[i+1]);
}

//loop{
int trappedw=0;
for(int i=0;i<n;i++){
//water level minval
int waterlevel=Math.min(rightmax[i], leftmax[i]);
//trapping water}
trappedw+=waterlevel-hight[i];
}
System.out.print(trappedw);



}
public static void main(String[] args){
int hight[]={4,2,0,6,3,2,5};
value(hight);
}
}