We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hey, Thanks for pointing it out!!
I think this serves it,
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N =in.nextInt();
int sum = 0;
int position = 0;
int residue = 0;
int a,b;
int preA=0;
int preB=0;
for(int i=0;i<N;i++){
a = in.nextInt();
b = in.nextInt();
sum =sum +(a-b);
if(sum<0){
residue+=sum;
sum=0;
position = i+1;
preA=a;
preB=b;
}
if(i==N-1){
if(sum+residue>=0){
System.out.println(position);
}
else{
if(position<N-1){
i=position+1;
position=i;
sum=0;
residue+=preA+preB;
}
else{
System.out.println(-1);
}
}
}
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Truck Tour
You are viewing a single comment's thread. Return to all comments →
Hey, Thanks for pointing it out!! I think this serves it,
public class Solution {
}