Given an array, arr[] consisting of N integers, the task is to minimize the number of increments of array elements required to make all differences pairs of array elements even.
Examples:
Input: arr[] = {4, 1, 2}
Output: 1
Explanation:
Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4, 2, 2}.
All pairs: [4, 2] → difference = 2
[4, 2] → difference = 2
[2, 2] → difference = 0
Now, the pairwise differences between array elements is even. Hence, the answer is 1.Input: arr[] = {2, 4}
Output: 0
Explanation: Differences between all pairs of array elements is already even. Therefore, the answer is 0.
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: The given problem can be solved by observing the fact that, to make the difference between all pairs of array elements even, both the elements must be of the same parity. Therefore, the idea is to convert all the array elements to either even numbers or odd numbers. The minimum number of increments is equal to the minimum of the count of even and odd array elements.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include
using
namespace
std;
// Function to find the minimum increments
// required to difference between all
// pairs of array elements even
void
// C++ program for the above approach
0// C++ program for the above approach
1 // C++ program for the above approach
2// C++ program for the above approach
1 // C++ program for the above approach
4
// C++ program for the above approach
5
// C++ program for the above approach
6// C++ program for the above approach
7
// C++ program for the above approach
6// C++ program for the above approach
9
// C++ program for the above approach
6// C++ program for the above approach
1 #include
2
// C++ program for the above approach
6#include
4
// C++ program for the above approach
6#include
6 #include
7// C++ program for the above approach
1 #include
9
using
0using
1 using
2
using
3using
4
using
3using
6
using
0using
8
using
0namespace
0 // C++ program for the above approach
5
using
3using
4
using
3namespace
5
using
0using
8
// C++ program for the above approach
6using
8
// C++ program for the above approach
6std;
1
// C++ program for the above approach
6std;
3
// C++ program for the above approach
6std;
5
using
8
std;
7
// C++ program for the above approach
1 std;
9
// C++ program for the above approach
5
// C++ program for the above approach
6// C++ program for the above approach
1 // Function to find the minimum increments
3
// C++ program for the above approach
6// C++ program for the above approach
1 // Function to find the minimum increments
6// Function to find the minimum increments
7// Function to find the minimum increments
8// Function to find the minimum increments
7// required to difference between all
0
// C++ program for the above approach
6// required to difference between all
2
// C++ program for the above approach
6// required to difference between all
4 // required to difference between all
5
using
8
Java
// required to difference between all
7
// required to difference between all
8 // required to difference between all
9 // pairs of array elements even
0
// C++ program for the above approach
5
// pairs of array elements even
2// Function to find the minimum increments
// pairs of array elements even
2// required to difference between all
// pairs of array elements even
2// pairs of array elements even
// pairs of array elements even
2// pairs of array elements even
9 void
// C++ program for the above approach
0// C++ program for the above approach
1void
3// C++ program for the above approach
1 // C++ program for the above approach
4
// pairs of array elements even
2// C++ program for the above approach
5
// C++ program for the above approach
6// C++ program for the above approach
7
// C++ program for the above approach
6// C++ program for the above approach
9
// C++ program for the above approach
6// C++ program for the above approach
1 // C++ program for the above approach
04// C++ program for the above approach
05// C++ program for the above approach
06// C++ program for the above approach
05// C++ program for the above approach
08
// C++ program for the above approach
6#include
4
// C++ program for the above approach
6#include
6 #include
7// C++ program for the above approach
1 // C++ program for the above approach
15// C++ program for the above approach
05// C++ program for the above approach
17
// C++ program for the above approach
6// C++ program for the above approach
5
// C++ program for the above approach
20using
1 // C++ program for the above approach
22// C++ program for the above approach
23 // C++ program for the above approach
24// C++ program for the above approach
05// C++ program for the above approach
26
// C++ program for the above approach
20// C++ program for the above approach
5
using
0using
4
using
0using
6
// C++ program for the above approach
20using
8
// C++ program for the above approach
20namespace
0
// C++ program for the above approach
20// C++ program for the above approach
5
using
0// C++ program for the above approach
40
using
0namespace
5
// C++ program for the above approach
20using
8
// C++ program for the above approach
6using
8
// C++ program for the above approach
6std;
1
// C++ program for the above approach
6std;
3
// C++ program for the above approach
6// C++ program for the above approach
52
// pairs of array elements even
2// C++ program for the above approach
54
// pairs of array elements even
2// C++ program for the above approach
56
// pairs of array elements even
2// required to difference between all
8 // pairs of array elements even
9 void
// C++ program for the above approach
61
// pairs of array elements even
2// C++ program for the above approach
5
// C++ program for the above approach
6// C++ program for the above approach
1// C++ program for the above approach
66// C++ program for the above approach
67// C++ program for the above approach
68// C++ program for the above approach
69// C++ program for the above approach
68// C++ program for the above approach
23 // C++ program for the above approach
72
// C++ program for the above approach
6// C++ program for the above approach
1 // C++ program for the above approach
75
// C++ program for the above approach
6// required to difference between all
2
// pairs of array elements even
2using
8
using
8
// C++ program for the above approach
81
Python3
// C++ program for the above approach
82
// C++ program for the above approach
83
// C++ program for the above approach
84
// C++ program for the above approach
85
// C++ program for the above approach
86 // C++ program for the above approach
87
// C++ program for the above approach
6
// C++ program for the above approach
6// C++ program for the above approach
90
// C++ program for the above approach
6// C++ program for the above approach
92
// C++ program for the above approach
6// C++ program for the above approach
94// C++ program for the above approach
95 // C++ program for the above approach
05
// C++ program for the above approach
6// C++ program for the above approach
98// C++ program for the above approach
95 // C++ program for the above approach
05
// C++ program for the above approach
6#include
02
// C++ program for the above approach
6#include
6 #include
05#include
06 #include
07#include
08
using
0using
1 #include
11#include
12 // C++ program for the above approach
23 // C++ program for the above approach
95// C++ program for the above approach
95 // C++ program for the above approach
05#include
17
using
3#include
19
using
3// C++ program for the above approach
98#include
22// C++ program for the above approach
95 // C++ program for the above approach
69
using
0
using
0namespace
0 #include
28
using
3#include
19
using
3// C++ program for the above approach
94#include
22// C++ program for the above approach
95 // C++ program for the above approach
69
using
0
// C++ program for the above approach
6#include
38
// C++ program for the above approach
6#include
40
// C++ program for the above approach
6#include
42#include
7#include
44#include
45
#include
46
#include
47// C++ program for the above approach
95 #include
49// C++ program for the above approach
67// C++ program for the above approach
68// C++ program for the above approach
69// C++ program for the above approach
68// C++ program for the above approach
23 #include
55