给定一个长度为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 ,以存储数组元素的总和。
- 遍历数组arr[] ,在索引[1, N]的范围内,并通过减去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
Javascript
B
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live