给定长度为N的数组arr [] ,任务是通过执行以下操作来找到两个玩家A和B最优玩的游戏的获胜者:
- 玩家A采取第一步。
- 玩家需要轮流在数组元素前面交替放置+和– 。
- 在所有数组元素的前面放置符号后,如果所有元素的差为偶数,则玩家A获胜。
- 否则,玩家B获胜。
例子:
Input: arr[] = {1, 2}
Output: B
Explanation:
All possible ways the game can be played out are:
(+1) – (+2) = -1
(−1) – (+2) = -3
(+1) – (-2) = 3
(-1) – (-2) = 1
Since the differences are odd in all the possibilities, B wins.
Input: arr[] = {1, 1, 2}
Output: A
Explanation:
All possible ways the game can be played out are:
(1) – (1) – (2) = -2
(1) – (1) – (-2) = 2
(1) – (-1) – (2) = 0
(1) – (-1) – (-2) = 4
(-1) – (1) – (2) = -4
(-1) – (1) – (-2) = 0
(-1) – (-1) – (2) = -2
(-1) – (-1) – (-2) = 4
Since the differences are even in all the possibilities, A wins.
天真的方法:最简单的方法是生成所有可能的2 N个组合,可以将符号放置在数组中,并检查每个组合,检查玩家A是否赢。如果发现对所有排列都是正确的,则打印A。否则,玩家B获胜。
时间复杂度: O(2 N * N)
辅助空间: O(1)
高效方法:请按照以下步骤优化上述方法:
- 初始化一个变量,例如diff ,以存储数组元素的总和。
- 在索引[1,N]的范围内遍历数组arr [] ,并通过减去arr [i]来更新diff 。
- 如果发现diff%2等于0 ,则打印‘A’ 。否则,打印‘B’ 。
下面是上述方法的实现:
C++
// C++ program for the
// above approach
#include
using namespace std;
// Function to check which
// player wins the game
void checkWinner(int arr[], int N)
{
// Stores the difference between
// +ve and -ve array elements
int diff = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update diff
diff -= arr[i];
}
// Checks if diff is even
if (diff % 2 == 0) {
cout << "A";
}
else {
cout << "B";
}
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to check
// which player wins the game
checkWinner(arr, N);
return 0;
}
Python3
# Python3 program for the
# above approach
# Function to check which
# player wins the game
def checkWinner(arr, N):
# Stores the difference between
# +ve and -ve array elements
diff = 0
# Traverse the array
for i in range(N):
# Update diff
diff -= arr[i]
# Checks if diff is even
if (diff % 2 == 0):
print("A")
else:
print("B")
# Driver Code
if __name__ == "__main__":
# Given Input
arr = [1, 2]
N = len(arr)
# Function call to check
# which player wins the game
checkWinner(arr, N)
# This code is contributed by ukasp.
Java
// Java program for the
// above approach
import java.util.*;
class GFG
{
// Function to check which
// player wins the game
static void checkWinner(int arr[], int N)
{
// Stores the difference between
// +ve and -ve array elements
int diff = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update diff
diff -= arr[i];
}
// Checks if diff is even
if (diff % 2 == 0) {
System.out.println("A");
}
else {
System.out.println("B");
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int arr[] = { 1, 2 };
int N = arr.length;
// Function call to check
// which player wins the game
checkWinner(arr, N);
}
}
// This code is contributed by Stream-Cipher
C#
// C# program for the above approach
using System;
class GFG{
// Function to check which
// player wins the game
static void checkWinner(int[] arr, int N)
{
// Stores the difference between
// +ve and -ve array elements
int diff = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update diff
diff -= arr[i];
}
// Checks if diff is even
if (diff % 2 == 0)
{
Console.Write("A");
}
else
{
Console.Write("B");
}
}
// Driver Code
public static void Main()
{
// Given Input
int[] arr = { 1, 2 };
int N = arr.Length;
// Function call to check
// which player wins the game
checkWinner(arr, N);
}
}
// This code is contributed by sanjoy_62
B
时间复杂度: O(N)
辅助空间: O(1)