给定长度为N且只有一个零的二进制数组(将其视为循环,起始端和末尾连接在一起) 。任务是找到使阵列美观所需的最少操作。
如果数组没有连续的1或0,则称其为美丽数组。如果不可能,则打印-1。
在一项操作中,您可以执行以下操作:
- 将阵列分为两部分。
- 颠倒这两个部分之一。
- 连接这两部分的相应端点,再次创建一个完整的数组。
找到要在数组上执行的最少数量的上述操作,以使其美观,使其不包含任何连续的1或0。
例子:
Input : A[] = { 1, 1, 0, 0 }
Output : 1
Explanation:
Make first cut between A[0] and A[1]
and second cut between A[2] and A[3].
Reverse array A[1] to A[2] and tie both array together.
Thus new array is A = [1, 0, 1, 0] which is beautiful.
Input : A[] = { 1, 1, 0, 0, 0 }
Output : -1
方法:目标是使数组的形式为A1 [] = {1,0,1,…,0,1,0}或A2 [] = {0,1,0,…1,0,1 }。
- 如果我们有奇数个元素,那么就不可能使数组漂亮,因为我们总是比另一个多一个二进制数。因此,我们返回-1。
- 如果我们有偶数个元素,那么只有当我们有二进制零和一个等于计数的数组时,我们才能使数组A1或A2中的任何一个成为数组。
- 同样,所需的最小割除量是连续零或我们拥有的且将相等的数量。
- 因此,我们迭代数组并维护零和一以及连续零的计数。
- 如果零= 1,则返回连续零的计数,否则返回-1。
注意:一种特殊情况是数组中只有一个元素。如果数组中只有1个元素,则该数组已经很漂亮,因此答案为零。
下面是上述方法的实现:
C++
// CPP implementation of above approach
#include
using namespace std;
// Function to find minimum operations
// required to make array beautiful
int minOperations(int A[], int n)
{
if (n & 1)
return -1;
int zeros = 0, consZeros = 0, ones = 0;
for (int i = 0; i < n; ++i) {
A[i] == 0 ? zeros++ : ones++;
// counting consecutive zeros.
if (i + 1 < n) {
if (A[i] == 0 && A[i + 1] == 0)
consZeros++;
}
}
// check that start and end are same
if (A[0] == A[n - 1] && A[0] == 0)
consZeros++;
// check is zero and one are equal
if (zeros == ones)
return consZeros;
else
return -1;
}
// Driver program
int main()
{
int A[] = { 1, 1, 0, 0 };
int n = sizeof(A) / sizeof(A[0]);
cout << minOperations(A, n);
return 0;
}
Java
// Java implementation of above approach
class GFG{
// Function to find minimum operations
// required to make array beautiful
static int minOperations(int[] A, int n)
{
if ((n & 1)>0)
return -1;
int zeros = 0, consZeros = 0, ones = 0;
for (int i = 0; i < n; ++i) {
if(A[i] == 0) zeros++; else ones++;
// counting consecutive zeros.
if (i + 1 < n) {
if (A[i] == 0 && A[i + 1] == 0)
consZeros++;
}
}
// check that start and end are same
if (A[0] == A[n - 1] && A[0] == 0)
consZeros++;
// check is zero and one are equal
if (zeros == ones)
return consZeros;
else
return -1;
}
// Driver program
public static void main(String[] args)
{
int[] A =new int[] { 1, 1, 0, 0 };
int n = A.length;
System.out.println(minOperations(A, n));
}
}
// This code is contributed by mits
Python3
# Python 3 implementation of
# above approach
# Function to find minimum operations
# required to make array beautiful
def minOperations(A, n) :
if n & 1 :
return -1
zeros, consZeros, ones = 0, 0, 0
for i in range(n) :
if A[i] :
zeros += 1
else :
ones += 1
# counting consecutive zeros.
if( i + 1 < n) :
if A[i] == 0 and A[i + 1] == 0 :
consZeros += 1
# check that start and end are same
if A[0] == A[n - 1] and A[0] == 0 :
consZeros += 1
# check is zero and one are equal
if zeros == ones :
return consZeros
else :
return -1
# Driver code
if __name__ == "__main__" :
A = [1, 1, 0, 0]
n = len(A)
print(minOperations(A, n))
# This code is contributed by ANKITRAI1
C#
// C# implementation of above approach
class GFG{
// Function to find minimum operations
// required to make array beautiful
static int minOperations(int[] A, int n)
{
if ((n & 1)>0)
return -1;
int zeros = 0, consZeros = 0, ones = 0;
for (int i = 0; i < n; ++i) {
if(A[i] == 0) zeros++; else ones++;
// counting consecutive zeros.
if (i + 1 < n) {
if (A[i] == 0 && A[i + 1] == 0)
consZeros++;
}
}
// check that start and end are same
if (A[0] == A[n - 1] && A[0] == 0)
consZeros++;
// check is zero and one are equal
if (zeros == ones)
return consZeros;
else
return -1;
}
// Driver program
static void Main()
{
int[] A =new int[] { 1, 1, 0, 0 };
int n = A.Length;
System.Console.WriteLine(minOperations(A, n));
}
}
// This code is contributed by mits
PHP
输出:
1
时间复杂度: O(N)