Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int smallest_sequence(int[] arr, int desired) {
- if(desired == 0){
- return 1;
- }
- int sum = 0;
- int count = 0;
- int shortest = 0;
- int index = 0;
- for (int i = 0; i < arr.length; i++) {
- sum += arr[i];
- count += 1;
- while(sum >= desired) {
- if(shortest == 0 || count < shortest) {
- shortest = count;
- }
- sum -= arr[index];
- count -= 1;
- index += 1;
- }
- };
- return shortest;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement