3232. Find if Digit Game Can Be Won LeetCode Solution

In this guide, you will get 3232. Find if Digit Game Can Be Won LeetCode Solution with the best time and space complexity. The solution to Find if Digit Game Can Be Won problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find if Digit Game Can Be Won solution in C++
  4. Find if Digit Game Can Be Won solution in Java
  5. Find if Digit Game Can Be Won solution in Python
  6. Additional Resources
3232. Find if Digit Game Can Be Won LeetCode Solution image

Problem Statement of Find if Digit Game Can Be Won

You are given an array of positive integers nums.
Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob’s numbers.
Return true if Alice can win this game, otherwise, return false.

Example 1:

Input: nums = [1,2,3,4,10]
Output: false
Explanation:
Alice cannot win by choosing either single-digit or double-digit numbers.

Example 2:

Input: nums = [1,2,3,4,5,14]
Output: true
Explanation:
Alice can win by choosing single-digit numbers which have a sum equal to 15.

Example 3:

Input: nums = [5,5,5,25]
Output: true
Explanation:
Alice can win by choosing double-digit numbers which have a sum equal to 25.

See also  51. N-Queens LeetCode Solution

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 99

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3232. Find if Digit Game Can Be Won LeetCode Solution in C++

class Solution {
 public:
  bool canAliceWin(vector<int>& nums) {
    return accumulate(nums.begin(), nums.end(), 0, [](int subtotal, int num) {
      return subtotal + (num < 10 ? num : -num);
    }) != 0;
  }
};
/* code provided by PROGIEZ */

3232. Find if Digit Game Can Be Won LeetCode Solution in Java

class Solution {
  public boolean canAliceWin(int[] nums) {
    return Arrays.stream(nums).map(num -> num < 10 ? num : -num).sum() != 0;
  }
}
// code provided by PROGIEZ

3232. Find if Digit Game Can Be Won LeetCode Solution in Python

class Solution:
  def canAliceWin(self, nums: list[int]) -> bool:
    return sum(num if num < 10 else -num for num in nums) != 0
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.