-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_arraysum_even_in least possible changes.cpp
More file actions
73 lines (68 loc) · 1.28 KB
/
make_arraysum_even_in least possible changes.cpp
File metadata and controls
73 lines (68 loc) · 1.28 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
66
67
68
69
70
71
72
73
//QUESTION
/*You are given an array A consisting of n positive integers.
In one operation, you can choose any index i such that 1 <= i <=n and Ai is even, then you can assign Ai = Ai / 2.
Your task is to make total sum of array even using minimum number of operations (possibly 0) or return -1 if it is not possible.
Example 1:
Input:
N = 5
A[] = {1, 2, 1, 2, 1}
Output: 1
Explanation: Initially sum is 7
which is odd. Assign A2 = A2/2
then array becomes 1, 1, 1, 2, 1
having sum 6 which is even. So total one
operation is required which is minimum
possible.
Example 2:
Input:
N = 3
B[] = {1, 1, 2}
Output: 0
Explanation: Sum is even ie 4.
*/
//MY CODE:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"\n enter the array size:";
cin>>n;
int a[n];
cout<<"\n enter array elements:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i];
}
if(sum%2==0)
{
return 0;
}
int c,ans=-1;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
c=0;
while(a[i]%2==0)
{
a[i]=a[i]/2;
c++;
}
if(ans==-1)
{
ans=c;
}
else
{
ans=min(ans,c);
}
}
}
return ans;
}