Skip to content

Commit 2243e32

Browse files
Create 11SubarraywithGivenSum.cpp
1 parent e0d953e commit 2243e32

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

11SubarraywithGivenSum.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
//SUBARRAY WITH GIVEN SUM
5+
int main() {
6+
int n=5;
7+
int s=6;
8+
int flag=0;
9+
int i,j;
10+
int arr[n]={1,2,3,7,5};
11+
for( i=0;i<n;i++){
12+
int sum=arr[i];
13+
for(j=i+1;j<n;j++){
14+
sum=sum+arr[j];
15+
if(sum==s){
16+
flag=1;
17+
break;
18+
}
19+
if(sum>s){
20+
break;
21+
}
22+
}
23+
if(flag==1){
24+
cout<<i+1<<" "<<j+1;
25+
break;
26+
}
27+
}
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)