Advertisement
Guest User

smallest_sequence

a guest
Apr 20th, 2014
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1.     public static int smallest_sequence(int[] arr, int desired) {
  2.  
  3.         if(desired == 0){
  4.             return 1;
  5.         }
  6.        
  7.         int sum = 0;
  8.         int count = 0;
  9.         int shortest = 0;
  10.         int index = 0;
  11.    
  12.         for (int i = 0; i < arr.length; i++) {
  13.             sum += arr[i];
  14.             count += 1;
  15.             while(sum >= desired) {
  16.                 if(shortest == 0 || count < shortest) {
  17.                     shortest = count;
  18.                 }
  19.                 sum -= arr[index];
  20.                 count -= 1;
  21.                 index += 1;
  22.             }
  23.         };
  24.         return shortest;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement