Advertisement
Infernale

Search The Index

Dec 21st, 2018
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int i;
  4. void searchIndex(int data[], int length, int target){
  5.     int start=0, sum=0;
  6.     for(int end=0;end<length;end++){
  7.         if(end<length){
  8.             sum+=data[end];
  9.         }
  10.         while(sum>target){
  11.             sum-=data[start];
  12.             start++;
  13.         }
  14.         if(sum==target){
  15.             printf("Case #%d: %d %d\n", i, start, end);
  16.             return;
  17.         }
  18.     }
  19.     printf("Case #%d: -1\n", i);
  20. }
  21.  
  22. int main(){
  23.     int sum, n, q;
  24.     scanf("%d", &n);
  25.     int data[n];
  26.     scanf("%d", &q);
  27.     for(i=0;i<n;i++){
  28.         scanf("%d", &data[i]);
  29.     }
  30.     for(i=1;i<=q;i++){
  31.         scanf("%d", &sum);
  32.         searchIndex(data, n, sum);
  33.     }
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement