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++
#include <bits/stdc++.h>
using namespace std;
int MinOperation( int arr[], int N, int K)
{
int cntOpe = 0;
for ( int i = 0; i < N; i++) {
if (K > arr[i]) {
if ((K - arr[i]) % 2 == 0) {
cntOpe += 2;
}
else {
cntOpe += 1;
}
}
else if (K < arr[i]) {
if ((K - arr[i]) % 2 == 0) {
cntOpe += 1;
}
else {
cntOpe += 2;
}
}
}
return cntOpe;
}
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
class GFG{
public static int MinOperation( int arr[],
int N, int K)
{
int cntOpe = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (K > arr[i])
{
if ((K - arr[i]) % 2 == 0 )
{
cntOpe += 2 ;
}
else
{
cntOpe += 1 ;
}
}
else if (K < arr[i])
{
if ((K - arr[i]) % 2 == 0 )
{
cntOpe += 1 ;
}
else
{
cntOpe += 2 ;
}
}
}
return cntOpe;
}
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));
}
}
|
Python3
def MinOperation(arr, N, K):
cntOpe = 0
for i in range (N):
if (K > arr[i]):
if ((K - arr[i]) % 2 = = 0 ):
cntOpe + = 2
else :
cntOpe + = 1
elif (K < arr[i]):
if ((K - arr[i]) % 2 = = 0 ):
cntOpe + = 1
else :
cntOpe + = 2
return cntOpe
arr = [ 8 , 7 , 2 , 1 , 3 ]
K = 5
N = len (arr)
print (MinOperation(arr, N, K))
|
C#
using System;
class GFG{
public static int MinOperation( int []arr,
int N, int K)
{
int cntOpe = 0;
for ( int i = 0; i < N; i++)
{
if (K > arr[i])
{
if ((K - arr[i]) % 2 == 0)
{
cntOpe += 2;
}
else
{
cntOpe += 1;
}
}
else if (K < arr[i])
{
if ((K - arr[i]) % 2 == 0)
{
cntOpe += 1;
}
else
{
cntOpe += 2;
}
}
}
return cntOpe;
}
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));
}
}
|
Javascript
<script>
function MinOperation(arr, N, K)
{
let cntOpe = 0;
for (let i = 0; i < N; i++)
{
if (K > arr[i])
{
if ((K - arr[i]) % 2 == 0)
{
cntOpe += 2;
}
else
{
cntOpe += 1;
}
}
else if (K < arr[i])
{
if ((K - arr[i]) % 2 == 0)
{
cntOpe += 1;
}
else
{
cntOpe += 2;
}
}
}
return cntOpe;
}
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
26 Apr, 2021
Like Article
Save Article