Find winner when players can increment any element following given constraints
You are given an arr[] of length N, containing positive integers. Then the task is to find the winner when two players are playing the game by alternatively following the given constraints:
- At his turn a player can select any index i such that (0 ? i < N) and increment arr[i].
- The game continues, until a permutation {P1, P2, . . . ., PN} of numbers 1 to N exists such that arr[i] ? P[i] for (0 ? i < N).
Note: The first player makes the first move.
Examples:
Input: N = 4, arr[] = {3, 1, 2, 3}
Output: 1
Explanation:
- First player choose index 3 and increment 3 to 4, arr[] = {3, 1, 2, 4}
- Second player can’t make any operation after first player:
- If second player increment 3 at index 0, Then arr[] = {4, 1, 2, 4}. No permutation from 1 to 4 exists such which have 2 elements at least 4.
- If second player increment 1 at index 1, Then arr[] = {3, 2, 2, 4}. There is no element which is same or equal to 1 in a permutation of first 4 numbers
- If second player increment 2 at index 2, Then arr[] = {3, 1, 3, 4}. There are total 3 elements which have value of at least of 3 but in the permutation there can be only 2 such elements.
- If second player increment 4 at index 3, Then arr[] = {3, 1, 2, 5}. No permutation of first 4 numbers have the value 5.
Input: N = 1, arr[] = {1}
Output: 2
Explanation: As First player can’t make any move, Because no permutation will exist after incrementing 1 at index 0.
Input: N = 2, arr[] = {1, 1}
Output: 1
Approach: Implement the idea below to solve the problem
The problem can be solved by sorting arr[] and then take difference of A[i] with sorted Permutation P[i] (1, 2, . . . N). There are two things needed to understand:
- Why we are sorting both arr[i] and P[i].
- Calculating difference of P[i]-arr[i].
- Reason for sorting: As it is given in the problem in the problem statement that arr[i] ? P[i], Which is only possible when arr[i] contains element such that (P[i]-arr[i] ? 0).So we need to sort both arr[i] and P[i].
- Reason for calculating difference: In each operation a player can increment an element. We can calculate total operation of game by calculating (i+1)-arr[i] for i = 0 to (N-1) after sorting the array. If for any index i, the value ((i+1)-arr[i]) < 0, it is not possible to make any move by the current player. Otherwise, winner can be checked on the basis of calculated difference:
- If the total difference is odd, first player wins.
- If the total difference is even, second player wins.
Follow the steps mentioned below to implement the idea:
- Create two variables as flag and difference.
- Sort the array arr[].
- Run a loop from i = 0 to N-1:
- If (i+1 – arr[i]) < 0, mark the flag as false and break the loop.
- Else add (i+1 – arr[i]) into the variable difference.
- If the flag is true and difference is odd, the first player wins.
- If the flag is true and difference is even, the second player wins.
- If the flag is false, only the second player wins.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool findWinner( int N, long arr[])
{
bool flag = true ;
long difference = 0;
for ( int i = 0; i < N; i++) {
if (((i + 1) - arr[i]) < 0) {
flag = false ;
break ;
}
else {
difference += ((i + 1) - arr[i]);
}
}
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
}
int main()
{
int N = 5;
long arr[] = { 1, 2, 2, 4, 1 };
sort(arr,arr+N);
cout<<(
findWinner(N, arr) == true ? "1" : "2" )<<endl;
int N2 = 6;
long arr2[] = { 3, 2, 4, 9, 7 };
sort(arr2,arr2+N2);
cout<<(
findWinner(N2, arr2) == true ? "1" : "2" )<<endl;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 5 ;
long [] arr = { 1 , 2 , 2 , 4 , 1 };
Arrays.sort(arr);
System.out.println(
findWinner(N, arr) == true ? "1" : "2" );
int N2 = 6 ;
long [] arr2 = { 3 , 2 , 4 , 9 , 7 };
Arrays.sort(arr2);
System.out.println(
findWinner(N2, arr2) == true ? "1" : "2" );
}
static boolean findWinner( int N, long [] arr)
{
boolean flag = true ;
long difference = 0 ;
for ( int i = 0 ; i < N; i++) {
if (((i + 1 ) - arr[i]) < 0 ) {
flag = false ;
break ;
}
else {
difference += ((i + 1 ) - arr[i]);
}
}
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
}
}
|
Python3
def find_winner(N, arr):
flag = True
difference = 0
for i in range (N):
if ((i + 1 ) - arr[i]) < 0 :
flag = False
break
else :
difference + = ((i + 1 ) - arr[i])
if flag:
return difference % 2 ! = 0
else :
return False
N = 5
arr = [ 1 , 2 , 2 , 4 , 1 ]
arr.sort()
print ( "1" if find_winner(N, arr) else "2" )
N2 = 6
arr2 = [ 3 , 2 , 4 , 9 , 7 ]
arr2.sort()
print ( "1" if find_winner(N2, arr2) else "2" )
|
C#
using System;
using System.Collections;
public class GFG {
static public void Main()
{
int N = 5;
long [] arr = { 1, 2, 2, 4, 1 };
Array.Sort(arr);
Console.WriteLine(findWinner(N, arr) ? "1" : "2" );
int N2 = 6;
long [] arr2 = { 3, 2, 4, 9, 7 };
Array.Sort(arr2);
Console.WriteLine(findWinner(N2, arr2) ? "1" : "2" );
}
static bool findWinner( int N, long [] arr)
{
bool flag = true ;
long difference = 0;
for ( int i = 0; i < N; i++) {
if (((i + 1) - arr[i]) < 0) {
flag = false ;
break ;
}
else {
difference += ((i + 1) - arr[i]);
}
}
if (flag)
return difference % 2 != 0;
else
return false ;
}
}
|
Javascript
function findWinner(N, arr)
{
let flag = true ;
let difference = 0;
for (let i = 0; i < N; i++) {
if (((i + 1) - arr[i]) < 0) {
flag = false ;
break ;
}
else {
difference += ((i + 1) - arr[i]);
}
}
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
}
let N = 5;
let arr = [1, 2, 2, 4, 1 ];
arr.sort();
document.write(findWinner(N, arr) == true ? "1" : "2" );
let N2 = 6;
let arr2 = [3, 2, 4, 9, 7 ];
arr2.sort();
document.write( "<br>" );
document.write(findWinner(N2, arr2) == true ? "1" : "2" );
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
13 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...