forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subarray_challenges.cpp
81 lines (76 loc) · 1.59 KB
/
subarray_challenges.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
using namespace std;
void maxSubarraySum(int arr[],int n){
// o of n*2
// int sum=0,mx=INT_MIN;
// int sumArray[n];
// for(int i=0;i<n;i++){
// for(int j=i;j<n;j++){
// sum+=arr[j];
// mx=max(mx,sum);
// }
// sumArray[i]=mx;
// sum=0;
// }
// mx=sumArray[0];
// for(int i=0;i<n;i++){
// mx=max(mx,sumArray[i]);
// }
// cout<<mx<<endl;
// o of n
int mx=INT_MIN;
int currentsum=0;
for(int i=0;i<n;i++){
currentsum+=arr[i];
if(currentsum<0){
currentsum=0;
}
mx=max(mx,currentsum);
}
// end of debate;
}
void maxCircularSubarraySum(int arr[],int n){
int mx=INT_MIN;
int currentsum=0;
int i=0,loop=0;
while(loop<n+1){
loop++;
if(i>=n)i=i-n;
currentsum+=arr[i];
mx=max(mx,currentsum);
if(currentsum<0){
currentsum=0;
loop=0;
}
i++;
}
cout<<mx;
}
void pairSumProblem(int arr[],int n,int s){
int i=0;
int j=n-1;
while(i<j){
if(arr[i]+arr[j]>s)j--;
else if(arr[i]+arr[j]<s)i++;
else {
cout<<i<<" "<<j;
break;
}
}
}
int main(){
int n;
// cout<<"enter the size of array "<<endl;
cin>>n;
int arr[n];
// cout<<"enter the array"<<endl;
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
maxCircularSubarraySum(arr,n);
// int s;
// cout<<"enter the sum"<<endl;
// cin>>s;
// pairSumProblem(arr,n,s);
return 0;
}