-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamboozeled_array.cpp
More file actions
73 lines (69 loc) · 1.71 KB
/
bamboozeled_array.cpp
File metadata and controls
73 lines (69 loc) · 1.71 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
/* C++ program to count minimum number of operations
to get the given target array */
#include <bits/stdc++.h>
using namespace std;
//we make the target array to zero array
//count the number of moves
//it is nothing but converting zero array into target array
int Minimum_Operations(int target[], int n)
{
int min_moves = 0;
//Till all elements become zero
while(1)
{
//count of zeroes
int zeroes = 0;
int i;
// i is index of first odd number
for(i=0; i<n; i++)
{
if (target[i] & 1)// odd number
{
break;
}
else if (target[i] == 1)//zeroes in target array
{
zeroes++;
}
}
//return moves when all the elements in target array are zero
if (zeroes == n)
{
return min_moves;
}
//if i = n odd number is not found in the array
// so all are even
//Divide by two and increment 1 move
if (i == n)
{
for (int j=0; j<n; j++)
{
target[j] = target[j]/2;
}
min_moves++;
}
//if odd number found decrese it by 1
// increment moves by 1
for (int j=i; j<n; j++)
{
if (target[j] & 1)
{
target[j]--;
min_moves++;
}
}
}
}
//main function
int main()
{
int n ;
cin>>n;
int target[n];
for (int i = 0; i < n; i++)
{
cin>>target[i];
}
cout << Minimum_Operations(target, n);
return 0;
}