Minimize adding odd and subtracting even numbers to make all array elements equal to K
Last Updated :
15 Jul, 2025
Given an array, arr[] of size N and an integer K, the task is to find the minimum number of operations required to make all array elements equal to K by performing the following operations any number of times:
- Convert arr[i] to arr[i] + X, where X is an odd number.
- Convert arr[i] to arr[i] - Y, where Y is an even number.
Examples:
Input: arr[] = {8, 7, 2, 1, 3}, K = 5
Output: 8
Explanation: To make all elements of the given array equal to K(= 5), following operations are required:
arr[0] = arr[0] + X, X = 1
arr[0] = arr[0] - Y, Y = 4
arr[1] = arr[1] - Y, Y = 2
arr[2] = arr[2] + X, X = 3
arr[3] = arr[3] + X, X = 3
arr[3] = arr[3] + X, X = 1
arr[4] = arr[4] + X, X = 1
arr[4] = arr[4] + X, X = 1
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, K = 3
Output: 9
Approach: The problem can be solved using the Greedy technique. Following are the observations:
Even + Even = Even
Even + Odd = Odd
Odd + Odd = Even
Odd + Even = Odd
Follow the steps below to solve the problem:
- Traverse the given array and check the following conditions.
- If K > arr[i] and (K - arr[i]) % 2 == 0 then add two odd numbers(X) into arr[i]. Therefore, total 2 operations required.
- If K > arr[i] and (K - arr[i]) % 2 != 0 then add one odd numbers(X) into arr[i]. Therefore, total 1 operations required.
- If K < arr[i] and (arr[i] - arr[i]) % 2 == 0 then subtract one even numbers(Y) into arr[i]. Therefore, total 1 operations required.
- If K < arr[i] and (K - arr[i]) % 2 != 0 then add an odd numbers(X) into arr[i] and subtract an even numbers(Y) from arr[i]. Therefore, total 2 operations required.
- Finally, print the total number of operations required to make all the array elements equal to K.
Below is the implementation of the above approach
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum operations
// required to make array elements equal to K
int MinOperation(int arr[], int N, int K)
{
// Stores minimum count of operations
int cntOpe = 0;
// Traverse the given array
for (int i = 0; i < N; i++) {
// If K is greater than arr[i]
if (K > arr[i]) {
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0) {
// Update cntOpe
cntOpe += 2;
}
else {
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than arr[i]
else if (K < arr[i]) {
// If (arr[i] - K) is even
if ((K - arr[i]) % 2 == 0) {
// Update cntOpe
cntOpe += 1;
}
else {
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver Code
int main()
{
int arr[] = { 8, 7, 2, 1, 3 };
int K = 5;
int N = sizeof(arr) / sizeof(arr[0]);
cout << MinOperation(arr, N, K);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the minimum
// operations required to make
// array elements equal to K
public static int MinOperation(int arr[],
int N, int K)
{
// Stores minimum count of
// operations
int cntOpe = 0;
// Traverse the given array
for (int i = 0; i < N; i++)
{
// If K is greater than
// arr[i]
if (K > arr[i])
{
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 2;
}
else
{
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than
// arr[i]
else if (K < arr[i])
{
// If (arr[i] - K) is
// even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 1;
}
else
{
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {8, 7, 2, 1, 3};
int K = 5;
int N = arr.length;
System.out.println(
MinOperation(arr, N, K));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to implement
# the above approach
# Function to find the minimum operations
# required to make array elements equal to K
def MinOperation(arr, N, K):
# Stores minimum count of operations
cntOpe = 0
# Traverse the given array
for i in range(N):
# If K is greater than arr[i]
if (K > arr[i]):
# If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0):
# Update cntOpe
cntOpe += 2
else:
# Update cntOpe
cntOpe += 1
# If K is less than arr[i]
elif (K < arr[i]):
# If (arr[i] - K) is even
if ((K - arr[i]) % 2 == 0):
# Update cntOpe
cntOpe += 1
else:
# Update cntOpe
cntOpe += 2
return cntOpe
# Driver Code
arr = [ 8, 7, 2, 1, 3 ]
K = 5
N = len(arr)
print(MinOperation(arr, N, K))
# This code is contributed by sanjoy_62
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the minimum
// operations required to make
// array elements equal to K
public static int MinOperation(int []arr,
int N, int K)
{
// Stores minimum count of
// operations
int cntOpe = 0;
// Traverse the given array
for(int i = 0; i < N; i++)
{
// If K is greater than
// arr[i]
if (K > arr[i])
{
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 2;
}
else
{
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than
// arr[i]
else if (K < arr[i])
{
// If (arr[i] - K) is
// even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 1;
}
else
{
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {8, 7, 2, 1, 3};
int K = 5;
int N = arr.Length;
Console.WriteLine(
MinOperation(arr, N, K));
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to find the minimum
// operations required to make
// array elements equal to K
function MinOperation(arr, N, K)
{
// Stores minimum count of
// operations
let cntOpe = 0;
// Traverse the given array
for (let i = 0; i < N; i++)
{
// If K is greater than
// arr[i]
if (K > arr[i])
{
// If (K - arr[i]) is even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 2;
}
else
{
// Update cntOpe
cntOpe += 1;
}
}
// If K is less than
// arr[i]
else if (K < arr[i])
{
// If (arr[i] - K) is
// even
if ((K - arr[i]) % 2 == 0)
{
// Update cntOpe
cntOpe += 1;
}
else
{
// Update cntOpe
cntOpe += 2;
}
}
}
return cntOpe;
}
// Driver Code
let arr = [8, 7, 2, 1, 3];
let K = 5;
let N = arr.length;
document.write(
MinOperation(arr, N, K));
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other Given an array arr[] and an integer K, the task is to find the minimum number of operations to make all the elements of the array arr[] equal. In one operation, K is subtracted from an element and this K is added into other element. If it is not possible then print -1. Examples: Input: arr[] = {5, 8
7 min read
Minimum operations required to make all elements in an array of first N odd numbers equal Given an array consisting of first N odd numbers, the task is to find the minimum number of operations required to make all the array elements equal by repeatedly selecting a pair and incrementing one element and decrementing the other element in the pair by 1. Examples: Input: N = 3Output: 2Explana
10 min read
Minimum Bitwise AND operations to make any two array elements equal Given an array of integers of size 'n' and an integer 'k', We can perform the Bitwise AND operation between any array element and 'k' any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make an
10 min read
Add minimum number to an array so that the sum becomes even Given an array, write a program to add the minimum number(should be greater than 0) to the array so that the sum of array becomes even. Examples: Input : 1 2 3 4 5 6 7 8 Output : 2 Explanation : Sum of array is 36, so we add minimum number 2 to make the sum even. Input : 1 2 3 4 5 6 7 8 9 Output : 1
8 min read
Make all elements of an array equal with the given operation Given an array arr[] of n integers and an integer k. The task is to make all the elements of arr[] equal with the given operation. In a single operation, any non-negative number x ? k (can be a floating point value) can be added to any element of the array and k will be updated as k = k - x. Print Y
5 min read
Minimize increments required to make count of even and odd array elements equal Given an array arr[] of size N, the task is to find the minimum increments by 1 required to be performed on the array elements such that the count of even and odd integers in the given array becomes equal. If it is not possible, then print "-1". Examples: Input: arr[] = {1, 3, 4, 9}Output: 1Explanat
6 min read