-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG_Array_SumExist_Count_RotatedArray.cpp
More file actions
65 lines (56 loc) · 1.01 KB
/
GFG_Array_SumExist_Count_RotatedArray.cpp
File metadata and controls
65 lines (56 loc) · 1.01 KB
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void fast(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
ll a[100];
void printarray(ll n)
{
for(ll i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int sum_exist(ll k,ll n)
{
ll l=0,r=0;
for(ll i=0;i<n-1;i++)
{
if(a[i]>a[i+1])
{
l=(i+1)%n;
r=i;
break;
}
}
ll count=0;
while(l!=r)
{
cout<<"f"<<endl;
ll sum=a[l]+a[r];
if(sum == k)
{
count++;
if(l==((n+r-1)%n))
return count;
l=(l+1)%n;
r=(n+r-1)%n;
}
if(sum<k)
{
l=(l+1)%n;
//cout<<"forca";
}
else
r=(n+r-1)%n;
}
return count;
}
int main()
{
ll n;
cin>>n;
for(ll i=0;i<n;i++)
cin>>a[i];
ll k;
cin>>k;
cout<<"Sum exist with count as \n"<<sum_exist(k,n);
}