📜  查找在阵列元素前面放置备用+和–符号的游戏中获胜的玩家

📅  最后修改于: 2021-04-17 17:27:53             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是通过执行以下操作来找到两个玩家AB最优玩的游戏的获胜者:

  • 玩家A采取第一步。
  • 玩家需要轮流在数组元素前面交替放置+
  • 在所有数组元素的前面放置符号后,如果所有元素的差为偶数,则玩家A获胜。
  • 否则,玩家B获胜。

例子:

天真的方法:最简单的方法是生成所有可能的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)