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 = 1Input: arr[] = {1, 2, 3, 4, 5, 6, 7}, K = 3
Output: 9
Approach: The problem can be solved using 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 Kint 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 Codeint 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 codepublic 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 Kdef 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 Codearr = [ 8, 7, 2, 1, 3 ]K = 5N = 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 codepublic 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 |
8
Time Complexity: O(N)
Auxiliary Space: O(1)
Recommended Posts:
- Make all elements of an Array equal by adding or subtracting at most K
- Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K]
- Even numbers at even index and odd numbers at odd index
- Minimize steps required to make all array elements same by adding 1, 2 or 5
- Check if it is possible to get back to 12'0 clock only by adding or subtracting given seconds
- Check whether N can be a Perfect Cube after adding or subtracting K
- Check if given intervals can be made non-overlapping by adding/subtracting some X
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Minimize difference after changing all odd elements to even
- Minimum sum of the elements of an array after subtracting smaller elements from larger
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Generate an array of given size with equal count and sum of odd and even numbers
- Modify given array to make sum of odd and even indexed elements same
- Minimize increment/decrement of Array elements to make each modulo K equal
- Minimizing array sum by subtracting larger elements from smaller ones
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


