Given an array nums of size n. There are two people in the game P1 and P2, where P1 starts the game. The game is as follows:
Each person always chooses the first number in the nums adds it to their score and then removes the first element.
If the removed number is even then reverse the rest of the array. Print the final scores of p1 and p2.
Examples:
Input: nums = [1, 2, 4, 3]
Output: 4 6
Explanation: Turn1: Player1 pops 1 at index 0 and add it to their score score_p1 = 1 .Since it is odd we leave the array as it is so now the nums = [2,3,4] .Turn2: Player2 pops 2 at index 0, so score_p2 = 2,Since the popped number is even we reverse the array. So nums = [3,4] .Turn3: Player1 pops 3(odd) index so socre_p1 = 4; nums= [4]. Turn4: Player2 pops 4 at index 0 so score_p2 = 6. Popped number is even but nums =[] so we terminate and Output is: 4 6Input: nums = {4, 3, 1, 2}
Output: 7 3
Approach: Follow the below steps to solve the problem
The approach is to iterates through an array of integers, with two players taking turns to accumulate sums of elements alternatively from either end of the array, switching direction when encountering even numbers, and outputs the final sums for each player.
Steps to solve the problem:
- Initialize two pointers, i and j, at the beginning and end of the array, respectively.
- Initialize boolean variables fromLeft and turn to keep track of the direction and current person turn, respectively.
- Initialize variables p1 and p2 to track the score of players.
-
Iterate through the array while i is less than or equal to j.
-
If fromLeft is true:
- If it’s P1‘s turn (!turn), add the element at index i to p1; otherwise, add it to p2.
- iIf the current element is even, update fromLeft to its opposite value.
- Increment i.
-
If fromLeft is false:
- If it’s P1‘s turn (!turn), add the element at index j to p1; otherwise, add it to p2.
- If the current element is even, update fromLeft to its opposite value.
- Decrement j.
- Toggle the value of turn to switch to the other player’s turn.
-
If fromLeft is true:
- Output the final sums p1 and p2.
Below is the implementation of the approach:
#include <iostream> #include <vector> using namespace std;
// Function to solve the problem void solve(vector< int >& nums)
{ int n = nums.size();
int i = 0;
int j = n - 1;
bool fromLeft = true ;
bool turn = false ;
int p1 = 0, p2 = 0;
// Iterate through the array
while (i <= j) {
if (fromLeft) {
// If it's p1's turn
if (!turn) {
p1 += nums[i];
}
else {
// If it's p2's turn
p2 += nums[i];
}
// Check if the current element is even, change
// direction if true
if ((nums[i] & 1) == 0) {
fromLeft = !fromLeft;
}
i++;
}
else {
// If it's p1's turn
if (!turn) {
p1 += nums[j];
}
else {
// If it's p2's turn
p2 += nums[j];
}
// Check if the current element is even, change
// direction if true
if ((nums[j] & 1) == 0) {
fromLeft = !fromLeft;
}
j--;
}
// Switch turns
turn = !turn;
}
// Output the results
cout << p1 << " " << p2 << endl;
} // Main function int main()
{ vector< int > n1 = { 1, 2, 4, 3 };
// Solve and display results for each test case
solve(n1);
return 0;
} |
/*code by flutterfly*/ import java.util.ArrayList;
import java.util.List;
public class Solution {
// Function to solve the problem
static void solve(List<Integer> nums) {
int n = nums.size();
int i = 0 ;
int j = n - 1 ;
boolean fromLeft = true ;
boolean turn = false ;
int p1 = 0 , p2 = 0 ;
// Iterate through the list
while (i <= j) {
if (fromLeft) {
// If it's p1's turn
if (!turn) {
p1 += nums.get(i);
} else {
// If it's p2's turn
p2 += nums.get(i);
}
// Check if the current element is even, change
// direction if true
if ((nums.get(i) & 1 ) == 0 ) {
fromLeft = !fromLeft;
}
i++;
} else {
// If it's p1's turn
if (!turn) {
p1 += nums.get(j);
} else {
// If it's p2's turn
p2 += nums.get(j);
}
// Check if the current element is even, change
// direction if true
if ((nums.get(j) & 1 ) == 0 ) {
fromLeft = !fromLeft;
}
j--;
}
// Switch turns
turn = !turn;
}
// Output the results
System.out.println(p1 + " " + p2);
}
// Main function
public static void main(String[] args) {
List<Integer> n1 = new ArrayList<>();
n1.add( 1 );
n1.add( 2 );
n1.add( 4 );
n1.add( 3 );
// Solve and display results for each test case
solve(n1);
}
} |
# code by flutterfly def solve(nums):
n = len (nums)
i = 0
j = n - 1
from_left = True
turn = False
p1 = 0
p2 = 0
# Iterate through the list
while i < = j:
if from_left:
# If it's p1's turn
if not turn:
p1 + = nums[i]
else :
# If it's p2's turn
p2 + = nums[i]
# Check if the current element is even, change
# direction if true
if nums[i] % 2 = = 0 :
from_left = not from_left
i + = 1
else :
# If it's p1's turn
if not turn:
p1 + = nums[j]
else :
# If it's p2's turn
p2 + = nums[j]
# Check if the current element is even, change
# direction if true
if nums[j] % 2 = = 0 :
from_left = not from_left
j - = 1
# Switch turns
turn = not turn
# Output the results
print (p1, p2)
# Main function if __name__ = = "__main__" :
n1 = [ 1 , 2 , 4 , 3 ]
# Solve and display results for each test case
solve(n1)
|
// code by flutterfly using System;
using System.Collections.Generic;
public class Solution
{ // Function to solve the problem
static void Solve(List< int > nums)
{
int n = nums.Count;
int i = 0;
int j = n - 1;
bool fromLeft = true ;
bool turn = false ;
int p1 = 0;
int p2 = 0;
// Iterate through the list
while (i <= j)
{
if (fromLeft)
{
// If it's p1's turn
if (!turn)
{
p1 += nums[i];
}
else
{
// If it's p2's turn
p2 += nums[i];
}
// Check if the current element is even, change
// direction if true
if (nums[i] % 2 == 0)
{
fromLeft = !fromLeft;
}
i++;
}
else
{
// If it's p1's turn
if (!turn)
{
p1 += nums[j];
}
else
{
// If it's p2's turn
p2 += nums[j];
}
// Check if the current element is even, change
// direction if true
if (nums[j] % 2 == 0)
{
fromLeft = !fromLeft;
}
j--;
}
// Switch turns
turn = !turn;
}
// Output the results
Console.WriteLine(p1 + " " + p2);
}
// Main function
public static void Main( string [] args)
{
List< int > n1 = new List< int > { 1, 2, 4, 3 };
// Solve and display results for each test case
Solve(n1);
}
} |
// code by flutterfly // Function to solve the problem function solve(nums) {
let n = nums.length;
let i = 0;
let j = n - 1;
let fromLeft = true ;
let turn = false ;
let p1 = 0;
let p2 = 0;
// Iterate through the list
while (i <= j) {
if (fromLeft) {
// If it's p1's turn
if (!turn) {
p1 += nums[i];
} else {
// If it's p2's turn
p2 += nums[i];
}
// Check if the current element is even, change
// direction if true
if (nums[i] % 2 === 0) {
fromLeft = !fromLeft;
}
i++;
} else {
// If it's p1's turn
if (!turn) {
p1 += nums[j];
} else {
// If it's p2's turn
p2 += nums[j];
}
// Check if the current element is even, change
// direction if true
if (nums[j] % 2 === 0) {
fromLeft = !fromLeft;
}
j--;
}
// Switch turns
turn = !turn;
}
// Output the results
console.log(p1 + " " + p2);
} // Main function let n1 = [1, 2, 4, 3]; // Solve and display results for each test case solve(n1); |
4 6
Time Complexity: O(N), where N is the number of elements in the input array.
Auxiliary Space: O(1)