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
Find the minimum number of operations required to make all array elements equal
Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Minimum number of increment-other operations to make all array elements equal.
We are given an array consisting of n elements. At each operation we can select any one element and increase rest of n-1 elements by 1. We have to make all elements equal performing such operation as many times you wish. Find the minimum number of operations needed for this.Examples: Input: arr[] =
5 min read
Find the number of operations required to make all array elements Equal
Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after perfo
6 min read
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
Minimum operation to make all elements equal in array
Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Make the array elements equal by performing given operations minimum number of times
Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times: Increase all array elements of any suffix array by 1.Decrease all the elements of any suffix array by 1.Replace any array element y another. Examples: Input: a
7 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
Minimum prime numbers required to be subtracted to make all array elements equal
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of primes numbers required to be subtracted from the array elements to make all array elements equal. Examples: Input: arr[]= {7, 10, 4, 5}Output: 5Explanation: Following subtraction of primes numbers make
12 min read
Minimum changes required to make all element in an array equal
Given an array of length N, the task is to find minimum operation required to make all elements in the array equal. Operation is as follows: Replace the value of one element of the array by one of its adjacent elements. Examples: Input: N = 4, arr[] = {2, 3, 3, 4} Output: 2 Explanation: Replace 2 an
5 min read