Skip to content

Commit 87dfecb

Browse files
authored
Merge pull request #22 from ArchanaPrabhu/patch-1
Solve Candy Pairing problem
2 parents fef0c5c + ffe63fd commit 87dfecb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<iostream>
2+
using namespace std;
3+
int a[100005];
4+
int main()
5+
{
6+
int n,k;
7+
long long available_pairs=0;
8+
cin>>n>>k;
9+
for(int i=1;i<=n;i++)
10+
{
11+
12+
cin>>a[i];
13+
available_pairs+=a[i]/2;
14+
a[i]-=1;
15+
16+
}
17+
18+
if(available_pairs < k)
19+
{
20+
cout<<"-1\n";
21+
}
22+
else
23+
{
24+
long long total=n;
25+
long long pairs =0;
26+
for(int i=1;i<=n;i++)
27+
{
28+
if(a[i]>=2)
29+
{
30+
total+=a[i]/2*2;
31+
pairs+=a[i]/2;
32+
a[i]=a[i]%2;
33+
}
34+
}
35+
if(pairs >=k)
36+
{
37+
cout<<k*2+n-1<<"\n";
38+
return 0;
39+
}
40+
cout<<total+k-pairs<<"\n";
41+
}
42+
return 0;
43+
}

0 commit comments

Comments
 (0)