-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geeks.c
58 lines (42 loc) · 1.08 KB
/
Geeks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
#define MAX 100
void findSubArray(int arr[],int len,int sum);
int main(){
int test,len,sum;
int arr[MAX];
// printf("How many tests do you wish to perform? ");
scanf("%d",&test);
while(test){
// printf("Define array size : ");
scanf("%d",&len);
// printf("Enter the value of sum you're looking for in a sub-array? ");
scanf("%d",&sum);
// printf("Enter each individual element into the array one by one - \n");
for(int i=0; i<len; i++){
scanf("%d",&arr[i]);
}
findSubArray(arr,len,sum);
test--;
}
return 0;
}
void findSubArray(int arr[],int len,int sum){
int tempSum,i,startIndex,endIndex;
for(i=0; i<len; i++){
startIndex = endIndex = i;
tempSum = 0;
while(endIndex < len){
tempSum += arr[endIndex];
if(tempSum == sum){
startIndex++;
endIndex++;
// printf("The sub-array resulting to a sum of %d ranges from position %d - %d \n",sum,startIndex,endIndex);
printf("%d %d \n",startIndex,endIndex);
return;
}
endIndex++;
}
}
// printf("There is no sub-array which sums upto %d \n",sum);
printf("1 \n");
}