Score of two players after the alternative round of game
Last Updated :
09 Jan, 2024
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 6
Input: 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.
- Output the final sums p1 and p2.
Below is the implementation of the approach:
C++
#include <iostream>
#include <vector>
using namespace std;
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;
while (i <= j) {
if (fromLeft) {
if (!turn) {
p1 += nums[i];
}
else {
p2 += nums[i];
}
if ((nums[i] & 1) == 0) {
fromLeft = !fromLeft;
}
i++;
}
else {
if (!turn) {
p1 += nums[j];
}
else {
p2 += nums[j];
}
if ((nums[j] & 1) == 0) {
fromLeft = !fromLeft;
}
j--;
}
turn = !turn;
}
cout << p1 << " " << p2 << endl;
}
int main()
{
vector< int > n1 = { 1, 2, 4, 3 };
solve(n1);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Solution {
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 ;
while (i <= j) {
if (fromLeft) {
if (!turn) {
p1 += nums.get(i);
} else {
p2 += nums.get(i);
}
if ((nums.get(i) & 1 ) == 0 ) {
fromLeft = !fromLeft;
}
i++;
} else {
if (!turn) {
p1 += nums.get(j);
} else {
p2 += nums.get(j);
}
if ((nums.get(j) & 1 ) == 0 ) {
fromLeft = !fromLeft;
}
j--;
}
turn = !turn;
}
System.out.println(p1 + " " + p2);
}
public static void main(String[] args) {
List<Integer> n1 = new ArrayList<>();
n1.add( 1 );
n1.add( 2 );
n1.add( 4 );
n1.add( 3 );
solve(n1);
}
}
|
Python3
def solve(nums):
n = len (nums)
i = 0
j = n - 1
from_left = True
turn = False
p1 = 0
p2 = 0
while i < = j:
if from_left:
if not turn:
p1 + = nums[i]
else :
p2 + = nums[i]
if nums[i] % 2 = = 0 :
from_left = not from_left
i + = 1
else :
if not turn:
p1 + = nums[j]
else :
p2 + = nums[j]
if nums[j] % 2 = = 0 :
from_left = not from_left
j - = 1
turn = not turn
print (p1, p2)
if __name__ = = "__main__" :
n1 = [ 1 , 2 , 4 , 3 ]
solve(n1)
|
C#
using System;
using System.Collections.Generic;
public class Solution
{
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;
while (i <= j)
{
if (fromLeft)
{
if (!turn)
{
p1 += nums[i];
}
else
{
p2 += nums[i];
}
if (nums[i] % 2 == 0)
{
fromLeft = !fromLeft;
}
i++;
}
else
{
if (!turn)
{
p1 += nums[j];
}
else
{
p2 += nums[j];
}
if (nums[j] % 2 == 0)
{
fromLeft = !fromLeft;
}
j--;
}
turn = !turn;
}
Console.WriteLine(p1 + " " + p2);
}
public static void Main( string [] args)
{
List< int > n1 = new List< int > { 1, 2, 4, 3 };
Solve(n1);
}
}
|
Javascript
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;
while (i <= j) {
if (fromLeft) {
if (!turn) {
p1 += nums[i];
} else {
p2 += nums[i];
}
if (nums[i] % 2 === 0) {
fromLeft = !fromLeft;
}
i++;
} else {
if (!turn) {
p1 += nums[j];
} else {
p2 += nums[j];
}
if (nums[j] % 2 === 0) {
fromLeft = !fromLeft;
}
j--;
}
turn = !turn;
}
console.log(p1 + " " + p2);
}
let n1 = [1, 2, 4, 3];
solve(n1);
|
Time Complexity: O(N), where N is the number of elements in the input array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...