在一行中生成大小为 2 到全 0 的给定二进制数组
给定一个二进制数组arr[N] ,(其中N = 2 )大小为 2,至少有一个元素为零。任务是编写一个单行函数来将数组的两个元素都设置为零。编写函数有一个限制。不能使用三元运算符和元素的直接赋值。
As per problem constraints, only three combinations of array elements are possible:
- arr[0] = 1 and arr[1] = 0
- arr[0] = 0 and arr[1] = 1
- arr[0] = 0 and arr[1] = 0
本文讨论了以下方法:
- 仅使用赋值运算符。
- 使用赋值运算符两次。
- 否定 (!)运算符(逻辑非)。
让我们开始详细讨论这些方法。
1.仅使用赋值运算符:
赋值运算符可用于将给定二进制数组的两个元素都设置为 0,但在这种方法中,不直接使用索引。
方法:
有三种方法可以实现这一点:
1. arr[arr[1]] = arr[arr[0]]
If arr={0, 1}, then arr[0] will be assigned to arr[1].
If arr={1, 0}, then arr[1] will be assigned to arr[0].
2. arr[arr[1]] = 0
If arr[1]=0, then arr[0] will be 1, so arr[arr[1]] will make arr[0]=0.
If arr[1]=1, then arr[1] will be 1, so arr[arr[1]] will make arr[1]=0.
3. arr[1 – arr[0]] = arr[1 – arr[1]]
If arr[1]=0 and arr[0]=1, then 1-arr[1] will be 1, so arr[1] will be assigned to arr[0].
If arr[1]=1 and arr[0]=0, then 1-arr[1] will be 0, so arr[0] will be assigned to arr[1].
下面是实现该方法的 C++ 代码:
C++
// C++ program to set both elements
// to 0 in binary array[2].
#include
using namespace std;
void MakeBothZeros(int arr[])
{
arr[arr[1]] = arr[arr[0]];
// Two other approaches to solve
// the problem
// arr[arr[1]] = 0;
// arr[1 - arr[0]] = arr[1 - arr[1]];
}
// Driver code
int main()
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
cout << First_Arr[0] << " " <<
First_Arr[1] << endl;
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
cout << Second_Arr[0] << " " <<
Second_Arr[1] << endl;
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
cout << Thrd_Arr[0] << " " <<
Thrd_Arr[1] << endl;
return 0;
}
Java
// Java program to set both elements
// to 0 in binary array[2]
import java.util.*;
class GFG{
static void MakeBothZeros(int arr[])
{
arr[arr[1]] = arr[arr[0]];
// Two other approaches to solve
// the problem
// arr[arr[1]] = 0;
// arr[1 - arr[0]] = arr[1 - arr[1]];
}
// Driver code
public static void main(String[] args)
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
System.out.print(First_Arr[0]+ " " +
First_Arr[1] +"\n");
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
System.out.print(Second_Arr[0]+ " " +
Second_Arr[1] +"\n");
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
System.out.print(Thrd_Arr[0]+ " " +
Thrd_Arr[1] +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
def MakeBothZeros(arr):
arr[arr[1]] = arr[arr[0]]
# Two other approaches to solve
# the problem
# arr[arr[1]] = 0;
# arr[1 - arr[0]] = arr[1 - arr[1]];
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(f"{First_Arr[0]} {First_Arr[1]} ")
Second_Arr = [1, 0]
MakeBothZeros(Second_Arr)
print(f"{Second_Arr[0]} {Second_Arr[1]} ")
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(f"{Thrd_Arr[0]} {Thrd_Arr[1]} ")
# This code is contributed by GFGKING
C#
// C# program to set both elements
// to 0 in binary array[2]
using System;
class GFG {
static void MakeBothZeros(int[] arr)
{
arr[arr[1]] = arr[arr[0]];
// Two other approaches to solve
// the problem
// arr[arr[1]] = 0;
// arr[1 - arr[0]] = arr[1 - arr[1]];
}
// Driver code
public static void Main()
{
int[] First_Arr = { 0, 1 };
MakeBothZeros(First_Arr);
Console.WriteLine(First_Arr[0] + " "
+ First_Arr[1]);
int[] Second_Arr = { 1, 0 };
MakeBothZeros(Second_Arr);
Console.WriteLine(Second_Arr[0] + " "
+ Second_Arr[1]);
int[] Thrd_Arr = { 0, 0 };
MakeBothZeros(Thrd_Arr);
Console.WriteLine(Thrd_Arr[0] + " " + Thrd_Arr[1]);
}
}
// This code is contributed by ukasp.
Javascript
C++
// C++ program to set both elements
// to 0 in binary array[2].
#include
using namespace std;
void MakeBothZeros(int arr[])
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
int main()
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
cout << First_Arr[0] << " " <<
First_Arr[1] << endl;
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
cout << Second_Arr[0] << " " <<
Second_Arr[1] << endl;
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
cout << Thrd_Arr[0] << " " <<
Thrd_Arr[1] << endl;
return 0;
}
Java
// Java program to set both elements
// to 0 in binary array[2].
import java.util.*;
class GFG{
static void MakeBothZeros(int arr[])
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
public static void main(String[] args)
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
System.out.print(First_Arr[0]+ " " +
First_Arr[1] +"\n");
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
System.out.print(Second_Arr[0]+ " " +
Second_Arr[1] +"\n");
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
System.out.print(Thrd_Arr[0]+ " " +
Thrd_Arr[1] +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to set both elements
# to 0 in binary array[2].
def MakeBothZeros(arr):
arr[0] = arr[1] = arr[0] & arr[1]
# Two other approaches to solve
# the problem
# arr[0] = arr[1] -= arr[1];
# arr[1] = arr[0] -= arr[0];
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(First_Arr[0], end=" ")
print(First_Arr[1])
Second_Arr = [0, 1]
MakeBothZeros(Second_Arr)
print(Second_Arr[0], end=" ")
print(Second_Arr[1])
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(Thrd_Arr[0], end=" ")
print(Thrd_Arr[1])
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to set both elements
// to 0 in binary array[2].
using System;
public class GFG
{
static void MakeBothZeros(int []arr)
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
public static void Main(String[] args) {
int[] First_Arr = { 0, 1 };
MakeBothZeros(First_Arr);
Console.Write(First_Arr[0] + " " + First_Arr[1] + "\n");
int []Second_Arr = { 1, 0 };
MakeBothZeros(Second_Arr);
Console.Write(Second_Arr[0] + " " + Second_Arr[1] + "\n");
int []Thrd_Arr = { 0, 0 };
MakeBothZeros(Thrd_Arr);
Console.Write(Thrd_Arr[0] + " " + Thrd_Arr[1] + "\n");
}
}
// This code is contributed by Rajput-Ji
Javascript
C++
// C++ program to set both elements
// to 0 in binary array[2].
#include
using namespace std;
void MakeBothZeros(int arr[])
{
arr[!arr[0]] = arr[arr[0]];
// Two other approaches to solve
// the problem
// arr[arr[1]] = arr[!arr[1]]
// arr[!arr[0]] = arr[!arr[1]]
}
// Driver code
int main()
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
cout << First_Arr[0] << " " <<
First_Arr[1] << endl;
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
cout << Second_Arr[0] << " " <<
Second_Arr[1] << endl;
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
cout << Thrd_Arr[0] << " " <<
Thrd_Arr[1] << endl;
return 0;
}
Javascript
0 0
0 0
0 0
2. 使用赋值运算符两次:
如约束中所列,不允许直接分配。因此 arr[0]=0 和 arr[1]=0 不是有效的语句。赋值运算符将被使用两次以将两个元素都设置为零。
方法:
有三种方法可以实现这一点:
1. arr[0] = arr[1] = arr[0] & arr[1]
if any one of the elements is 1.
AND of 1 and 0 is always 0. So, both gets value 0.
2. arr[0] = arr[1] -= arr[1]
If arr[1]=1 then arr[1] gets 1-1=0 so, both becomes 0.
3. arr[1] = arr[0] -= arr[0]
If arr[0]=1, then arr[0] gets 1-1=0.
else arr[0]= 0-0 = 0. So, both becomes 0.
下面是实现该方法的 C++ 程序:
C++
// C++ program to set both elements
// to 0 in binary array[2].
#include
using namespace std;
void MakeBothZeros(int arr[])
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
int main()
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
cout << First_Arr[0] << " " <<
First_Arr[1] << endl;
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
cout << Second_Arr[0] << " " <<
Second_Arr[1] << endl;
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
cout << Thrd_Arr[0] << " " <<
Thrd_Arr[1] << endl;
return 0;
}
Java
// Java program to set both elements
// to 0 in binary array[2].
import java.util.*;
class GFG{
static void MakeBothZeros(int arr[])
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
public static void main(String[] args)
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
System.out.print(First_Arr[0]+ " " +
First_Arr[1] +"\n");
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
System.out.print(Second_Arr[0]+ " " +
Second_Arr[1] +"\n");
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
System.out.print(Thrd_Arr[0]+ " " +
Thrd_Arr[1] +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to set both elements
# to 0 in binary array[2].
def MakeBothZeros(arr):
arr[0] = arr[1] = arr[0] & arr[1]
# Two other approaches to solve
# the problem
# arr[0] = arr[1] -= arr[1];
# arr[1] = arr[0] -= arr[0];
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(First_Arr[0], end=" ")
print(First_Arr[1])
Second_Arr = [0, 1]
MakeBothZeros(Second_Arr)
print(Second_Arr[0], end=" ")
print(Second_Arr[1])
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(Thrd_Arr[0], end=" ")
print(Thrd_Arr[1])
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to set both elements
// to 0 in binary array[2].
using System;
public class GFG
{
static void MakeBothZeros(int []arr)
{
arr[0] = arr[1] = arr[0] & arr[1];
// Two other approaches to solve
// the problem
// arr[0] = arr[1] -= arr[1];
// arr[1] = arr[0] -= arr[0];
}
// Driver code
public static void Main(String[] args) {
int[] First_Arr = { 0, 1 };
MakeBothZeros(First_Arr);
Console.Write(First_Arr[0] + " " + First_Arr[1] + "\n");
int []Second_Arr = { 1, 0 };
MakeBothZeros(Second_Arr);
Console.Write(Second_Arr[0] + " " + Second_Arr[1] + "\n");
int []Thrd_Arr = { 0, 0 };
MakeBothZeros(Thrd_Arr);
Console.Write(Thrd_Arr[0] + " " + Thrd_Arr[1] + "\n");
}
}
// This code is contributed by Rajput-Ji
Javascript
0 0
0 0
0 0
笔记:
时间复杂度为 O(1),因为只使用了一条语句。
3. 通过使用否定 (!)运算符(逻辑非):
在这种方法中,赋值运算符与否定运算符一起使用,在一行代码中使给定数组的两个元素都为 0。
方法:
有三种方法可以做到这一点:
1. arr[!arr[0]] = arr[arr[0]]
If arr={0, 1} then index 1 is assigned the index 0 value.
If arr={1, 0} then index 0 is given the index 1 value.
2. arr[arr[1]] = arr[!arr[1]]
If arr={0, 1} then index 0 value is assigned to the index 1.
If arr={1, 0} then index 1 value is assigned to the index 0.
3. arr[!arr[0]] = arr[!arr[1]]
If arr={0, 1}, since 1 is the value at index 1, the index 0 value which is 0 again,
will be assigned to index 1, making array full of zeros.
If arr={1, 0} then index 1 value is assigned to the index 0.
下面是实现该方法的 C++ 程序:
C++
// C++ program to set both elements
// to 0 in binary array[2].
#include
using namespace std;
void MakeBothZeros(int arr[])
{
arr[!arr[0]] = arr[arr[0]];
// Two other approaches to solve
// the problem
// arr[arr[1]] = arr[!arr[1]]
// arr[!arr[0]] = arr[!arr[1]]
}
// Driver code
int main()
{
int First_Arr[] = {0, 1};
MakeBothZeros(First_Arr);
cout << First_Arr[0] << " " <<
First_Arr[1] << endl;
int Second_Arr[] = {1, 0};
MakeBothZeros(Second_Arr);
cout << Second_Arr[0] << " " <<
Second_Arr[1] << endl;
int Thrd_Arr[] = {0, 0};
MakeBothZeros(Thrd_Arr);
cout << Thrd_Arr[0] << " " <<
Thrd_Arr[1] << endl;
return 0;
}
Javascript
0 0
0 0
0 0