-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Minimum_number_to_form_the_sum_even.cpp
72 lines (63 loc) · 1.32 KB
/
Minimum_number_to_form_the_sum_even.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
/*
Description : Given an array and size, the task is to add the minimum number
(should be greater than 0) to the array so that the sum of the
array becomes even .
*/
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
// function is being used to find that minimum number
int minNum(long long int arr[], int n)
{
long sum = 0;
sum = accumulate(arr, arr + n, sum);
if (sum == 0)
{
return 0;
}
else if (sum % 2 == 0)
{
return 2;
}
else
{
return 1;
}
}
};
int main()
{
//n = size of array
int n;
cout << "Enter the size of the array : " << endl;
cin >> n;
// array size can be big so used long long
long long a[n];
cout << "Enter " << n << " number of elements : " << endl;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
Solution obj;
cout << "Minimum Number to be added in the array : " << endl;
cout << obj.minNum(a, n);
return 0;
}
/*
Time complexity : O(n)
Space complexity : O(n)
*/
/*
Test Cases :
Test Case 1 :
Input :
Enter the size of the array :
8
Enter 8 number of elements :
1 2 3 4 5 6 7 8
Output :
Minimum Number to be added in the array :
2
*/