Minimum inversions required so that no two adjacent elements are same

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.



Improved By : Rajput-Ji