Minimum operations required to make all elements in an array of first N odd numbers equal
Last Updated :
27 May, 2021
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 = 3
Output: 2
Explanation:
Initially, the array is {1, 3, 5}. Following operations are performed:
Operation 1: Decrement arr[2] by 1 and increment arr[0] by 1. The array modifies to {2, 3, 4}.
Operation 2: Decrement arr[2] by 1 and increment arr[0] by 1. The array modifies to {3, 3, 3}.
Therefore, the minimum number of operations required is 2.
Input: N = 6
Output: 9
Naive Approach: The given problem can be solved based on the following observations:
- It can be observed that making all the array elements equal to the middle element of the array requires minimum number of operations.
- Therefore, the idea is to traverse the array using a variable i and select the element at index i and (N - i - 1) in each operation and make them equal to the middle element.
Follow the steps below to solve the problem:
- Initialize a variable, say mid, that stores the middle element of the array.
- Initialize an array arr[] that stores the array element as arr[i] = 2 * i + 1.
- Find the sum of the array arr[] and store it in a variable say sum.
- If N is odd, then update the value of mid to arr[N / 2]. Otherwise, update the value of mid to sum / N.
- Initialize a variable, say ans as 0 that stores the minimum number of operations.
- Traverse the given array over the range [0, N/2] and increment the value of ans by the value (mid - arr[i]).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++14
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum number
// of operations required to make the
// array elements equal
int minOperations(int N)
{
// Stores the array elements
int arr[N];
// Stores the sum of the array
int sum = 0;
// Iterate over the range [0, N]
for(int i = 0; i < N; i++)
{
// Update the value arr[i]
arr[i] = (2 * i) + 1;
// Increment the sum by
// the value arr[i]
sum = sum + arr[i];
}
// Stores the middle element
int mid = 0;
// If N is even
if (N % 2 == 0)
{
mid = sum / N;
}
// Otherwise
else {
mid = arr[N / 2];
}
// Stores the result
int ans = 0;
// Traverse the range [0, N / 2]
for(int i = 0; i < N / 2; i++)
{
// Update the value of ans
ans += mid - arr[i];
}
// Return the result
return ans;
}
// Driver Code
int main()
{
int N = 6;
cout << minOperations(N);
return 0;
}
// This code is contributed by susmitakundugoaldanga
Java
// Java program for the above approach
class GFG {
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperations(int N)
{
// Stores the array elements
int[] arr = new int[N];
// Stores the sum of the array
int sum = 0;
// Iterate over the range [0, N]
for (int i = 0; i < N; i++) {
// Update the value arr[i]
arr[i] = (2 * i) + 1;
// Increment the sum by
// the value arr[i]
sum = sum + arr[i];
}
// Stores the middle element
int mid = 0;
// If N is even
if (N % 2 == 0) {
mid = sum / N;
}
// Otherwise
else {
mid = arr[N / 2];
}
// Stores the result
int ans = 0;
// Traverse the range [0, N / 2]
for (int i = 0; i < N / 2; i++) {
// Update the value of ans
ans += mid - arr[i];
}
// Return the result
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
System.out.println(
minOperations(N));
}
}
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of operations required to make the
# array elements equal
def minOperations(N):
# Stores the array elements
arr = [0] * N
# Stores the sum of the array
sum = 0
# Iterate over the range [0, N]
for i in range(N) :
# Update the value arr[i]
arr[i] = (2 * i) + 1
# Increment the sum by
# the value arr[i]
sum = sum + arr[i]
# Stores the middle element
mid = 0
# If N is even
if N % 2 == 0 :
mid = sum / N
# Otherwise
else:
mid = arr[int(N / 2)]
# Stores the result
ans = 0
# Traverse the range [0, N / 2]
for i in range(int(N / 2)):
# Update the value of ans
ans += mid - arr[i]
# Return the result
return int(ans)
# Driver Code
N = 6
print(minOperations(N))
# This code is contributed by Dharanendra L V.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperations(int N)
{
// Stores the array elements
int[] arr = new int[N];
// Stores the sum of the array
int sum = 0;
// Iterate over the range [0, N]
for(int i = 0; i < N; i++)
{
// Update the value arr[i]
arr[i] = (2 * i) + 1;
// Increment the sum by
// the value arr[i]
sum = sum + arr[i];
}
// Stores the middle element
int mid = 0;
// If N is even
if (N % 2 == 0)
{
mid = sum / N;
}
// Otherwise
else
{
mid = arr[N / 2];
}
// Stores the result
int ans = 0;
// Traverse the range [0, N / 2]
for(int i = 0; i < N / 2; i++)
{
// Update the value of ans
ans += mid - arr[i];
}
// Return the result
return ans;
}
// Driver Code
public static void Main(string[] args)
{
int N = 6;
Console.WriteLine(minOperations(N));
}
}
// This code is contributed by ukasp
JavaScript
<script>
// javascript program for the above approach
// Function to find the minimum number
// of operations required to make the
// array elements equal
function minOperations(N)
{
// Stores the array elements
let arr = Array.from({length: N}, (_, i) => 0);
// Stores the sum of the array
let sum = 0;
// Iterate over the range [0, N]
for(let i = 0; i < N; i++)
{
// Update the value arr[i]
arr[i] = (2 * i) + 1;
// Increment the sum by
// the value arr[i]
sum = sum + arr[i];
}
// Stores the middle element
let mid = 0;
// If N is even
if (N % 2 == 0)
{
mid = sum / N;
}
// Otherwise
else
{
mid = arr[N / 2];
}
// Stores the result
let ans = 0;
// Traverse the range [0, N / 2]
for(let i = 0; i < N / 2; i++)
{
// Update the value of ans
ans += mid - arr[i];
}
// Return the result
return ans;
}
// Driver Code
let N = 6;
document.write(minOperations(N));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the following observation:
Therefore, if the value of N is even, then print the value of (N / 2)2. Otherwise, print the value of K * (K + 1) / 2, where K = ((N - 1) / 2) as the resultant operation.
Below is the implementation of the above approach:
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum number
// of operations required to make the
// array elements equal
int minOperation(int N)
{
// If the value of N is even
if (N % 2 == 0) {
// Return the value
return (N / 2) * (N / 2);
}
// Otherwise, N is odd
int k = (N - 1) / 2;
// Return the value
return k * (k + 1);
}
// Driver Code
int main()
{
int N = 6;
cout << minOperation(N);
return 0;
}
// This code is contributed by code_hunt.
Java
// Java program for the above approach
class GFG {
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperation(int N)
{
// If the value of N is even
if (N % 2 == 0) {
// Return the value
return (N / 2) * (N / 2);
}
// Otherwise, N is odd
int k = (N - 1) / 2;
// Return the value
return k * (k + 1);
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
System.out.println(
minOperation(N));
}
}
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum number
// of operations required to make the
// array elements equal
public static int minOperation(int N)
{
// If the value of N is even
if (N % 2 == 0)
{
// Return the value
return (N / 2) * (N / 2);
}
// Otherwise, N is odd
int k = (N - 1) / 2;
// Return the value
return k * (k + 1);
}
// Driver code
static void Main()
{
int N = 6;
Console.WriteLine(minOperation(N));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
# Function to find the minimum number
# of operations required to make the
# array elements equal
def minOperation(N) :
# If the value of N is even
if (N % 2 == 0) :
# Return the value
return (N / 2) * (N / 2)
# Otherwise, N is odd
k = (N - 1) / 2
# Return the value
return (k * (k + 1))
# Driver Code
N = 6
print(int(minOperation(N)))
# This code is contributed by souravghosh0416.
JavaScript
<script>
// Javascript program implementation
// of the approach
// Function to find the minimum number
// of operations required to make the
// array elements equal
function minOperation(N)
{
// If the value of N is even
if (N % 2 == 0) {
// Return the value
return (N / 2) * (N / 2);
}
// Otherwise, N is odd
let k = (N - 1) / 2;
// Return the value
return k * (k + 1);
}
// Driver Code
let N = 6;
document.write(
minOperation(N));
// This code is contributed by splevel62.
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimum operations required to make two elements equal in Array Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read
Make all the array elements odd with minimum operations of given type Given an array arr[] consisting of even integers. At each move, you can select any even number X from the array and divide all the occurrences of X by 2. The task is to find the minimum number of moves needed so that all the elements in the array become odd. Examples: Input: arr[] = {40, 6, 40, 20}
5 min read
Make all the array elements odd with minimum operations of given type Given an array arr[] consisting of even integers. At each move, you can select any even number X from the array and divide all the occurrences of X by 2. The task is to find the minimum number of moves needed so that all the elements in the array become odd. Examples: Input: arr[] = {40, 6, 40, 20}
5 min read
Minimize adding odd and subtracting even numbers to make all array elements equal to K 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
7 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