Minimum inversions required so that no two adjacent elements are same
Last Updated :
01 Mar, 2022
Given a binary array arr[] of size N. The task is to find the minimum number of inversions required so that no two adjacent elements are same. After a single inversion, an element could change from 0 to 1 or from 1 to 0.
Examples:
Input: arr[] = {1, 1, 1}
Output: 1
Change arr[1] from 1 to 0 and
the array becomes {1, 0, 1}.
Input: arr[] = {1, 0, 0, 1, 0, 0, 1, 0}
Output: 3
Approach: There are only two possibilities to make the array {1, 0, 1, 0, 1, 0, 1, ...} or {0, 1, 0, 1, 0, 1, 0, ...}. Let ans_a and ans_b be the count of changes required to get these arrays respectively. Now, the final answer will be min(ans_a, ans_b).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the minimum
// inversions required so that no
// two adjacent elements are same
int min_changes(int a[], int n)
{
// To store the inversions required
// to make the array {1, 0, 1, 0, 1, 0, 1, ...}
// and {0, 1, 0, 1, 0, 1, 0, ...} respectively
int ans_a = 0, ans_b = 0;
// Find all the changes required
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (a[i] == 0)
ans_a++;
else
ans_b++;
}
else {
if (a[i] == 0)
ans_b++;
else
ans_a++;
}
}
// Return the required answer
return min(ans_a, ans_b);
}
// Driver code
int main()
{
int a[] = { 1, 0, 0, 1, 0, 0, 1, 0 };
int n = sizeof(a) / sizeof(a[0]);
cout << min_changes(a, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum
// inversions required so that no
// two adjacent elements are same
static int min_changes(int a[], int n)
{
// To store the inversions required
// to make the array {1, 0, 1, 0, 1, 0, 1, ...}
// and {0, 1, 0, 1, 0, 1, 0, ...} respectively
int ans_a = 0, ans_b = 0;
// Find all the changes required
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (a[i] == 0)
ans_a++;
else
ans_b++;
}
else
{
if (a[i] == 0)
ans_b++;
else
ans_a++;
}
}
// Return the required answer
return Math.min(ans_a, ans_b);
}
// Driver code
public static void main(String[] args)
{
int a[] = { 1, 0, 0, 1, 0, 0, 1, 0 };
int n = a.length;
System.out.println(min_changes(a, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to return the minimum
# inversions required so that no
# two adjacent elements are same
def min_changes(a, n):
# To store the inversions required
# to make the array {1, 0, 1, 0, 1, 0, 1, ...}
# and {0, 1, 0, 1, 0, 1, 0, ...} respectively
ans_a = 0;
ans_b = 0;
# Find all the changes required
for i in range(n):
if (i % 2 == 0):
if (a[i] == 0):
ans_a += 1;
else:
ans_b += 1;
else:
if (a[i] == 0):
ans_b += 1;
else:
ans_a += 1;
# Return the required answer
return min(ans_a, ans_b);
# Driver code
if __name__ == '__main__':
a = [ 1, 0, 0, 1, 0, 0, 1, 0 ];
n = len(a);
print(min_changes(a, n));
# This code is contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum
// inversions required so that no
// two adjacent elements are same
static int min_changes(int []a, int n)
{
// To store the inversions required
// to make the array {1, 0, 1, 0, 1, 0, 1, ...}
// and {0, 1, 0, 1, 0, 1, 0, ...} respectively
int ans_a = 0, ans_b = 0;
// Find all the changes required
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
if (a[i] == 0)
ans_a++;
else
ans_b++;
}
else
{
if (a[i] == 0)
ans_b++;
else
ans_a++;
}
}
// Return the required answer
return Math.Min(ans_a, ans_b);
}
// Driver code
public static void Main(String[] args)
{
int []a = { 1, 0, 0, 1, 0, 0, 1, 0 };
int n = a.Length;
Console.WriteLine(min_changes(a, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the minimum
// inversions required so that no
// two adjacent elements are same
function min_changes(a, n) {
// To store the inversions required
// to make the array {1, 0, 1, 0, 1, 0, 1, ...}
// and {0, 1, 0, 1, 0, 1, 0, ...} respectively
let ans_a = 0, ans_b = 0;
// Find all the changes required
for (let i = 0; i < n; i++) {
if (i % 2 == 0) {
if (a[i] == 0)
ans_a++;
else
ans_b++;
}
else {
if (a[i] == 0)
ans_b++;
else
ans_a++;
}
}
// Return the required answer
return Math.min(ans_a, ans_b);
}
// Driver code
let a = [1, 0, 0, 1, 0, 0, 1, 0];
let n = a.length;
document.write(min_changes(a, n));
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Longest sequence such that no two adjacent element are of same type Given an array arr[] of size N, where each value represents the number of elements present of the ith type, the task is to find the longest sequence that can be made such that no two adjacent elements are of the same type. Examples: Input: N = 3, arr[] = {7, 3, 2}Output: 11Explanation:In the above e
8 min read
Minimize insertion of 0 or 1 such that no adjacent pair has same value Given a binary array A[] of length N, the task is to find the minimum number of operations required such that no adjacent pair has the same value where in each operation we can insert either 0 or 1 at any position in the array. Examples: Input: A[] = {0, 0, 1, 0, 0} Output: 2?Explanation: We can per
4 min read
Minimum number of elements to be removed so that pairwise consecutive elements are same Given a string str. The task is to count the minimum number of elements to be removed so that pairwise consecutive elements are same Examples: Input : str = "11344" Output: 1 Remove the digit 3 from 3rd place so that the string becomes 1144. Thus pairwise two consecutive elements are same. Hence ans
4 min read
Minimize steps required to make all array elements same by adding 1, 2 or 5 Given an array arr[] of size N, the task is to count the minimum number of steps required to make all the array elements the same by adding 1, 2, or 5 to exactly (N - 1) elements of the array at each step. Examples: Input: N = 4, arr[] = {2, 2, 3, 7}Output: 2Explanation: Step 1: {2, 2, 3, 7} -> {
11 min read
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same Given a string S consisting of N characters, the task is to find the minimum number of pairs of characters that are required to be swapped such that no two adjacent characters are the same. If it is not possible to do so, then print "-1". Examples: Input: S = "ABAACD"Output: 1Explanation: Swapping S
9 min read