给定大小为N的二进制数组arr [] 。任务是找到所需的最小反转数,以使两个相邻元素都不相同。一次反转后,元素可以从0更改为1或从1更改为0 。
例子:
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
方法:只有两种方法可以使数组为{1、0、1、0、1、0、1,…}或{0、1、0、1、0、1、0,…}。假设ans_a和ans_b分别是获取这些数组所需的更改计数。现在,最终答案将是min(ans_a,ans_b) 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
3