Server
Introduction
This problem gives you an array of numbers, and asks you the largest n such that the first n elements sum to less than t.
Solutions
Iterate and sum
Idea
Walk through the array adding the numbers until the sum is greater than t. Print out the index.
Runtime
- n
Gotchas
Be sure to handle the case when all the numbers sum to less than t, or some n numbers sum to exactly t.
Code
Solution - Java
import java.util.*;
public class a {
Scanner in=new Scanner(System.in);
public static void main(String[] args) {
new a().go();
}
private void go() {
int n=in.nextInt(),t=in.nextInt(),ans=0;
while(t>0&&n-->0){
t-=in.nextInt();
if(t<0)break;
ans++;
}
System.out.println(ans);
}
}