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++ code to implement the approach #include <bits/stdc++.h> using namespace std;
// Function for find winner bool findWinner( int N, long arr[])
{ // A flag is initialized to true
bool flag = true ;
// Variable to calculate absolute difference
long difference = 0;
// Loop for traversing on arr[]
for ( int i = 0; i < N; i++) {
// If an element found such that A[i]>P[i]
if (((i + 1) - arr[i]) < 0) {
// Marking flag as false
// and breaking the loop
flag = false ;
break ;
}
else {
// Adding difference P[i]-A[i] in
// difference variable
difference += ((i + 1) - arr[i]);
}
}
// If flag is true means someone is winner because
// rule A[i]<=P[i] of game followed successfully If
// difference is odd then First player(returning
// true) wins else second player wins
// else condition
// will execute when A[i]<=P[i] not followed,
// Formally after sorting both A[] and P[] an
// element is there such that A[i]>P[i] at any i
// for(1<=i<=N) following 1 based indexing
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
} // Driver Function int main()
{ // Testcase1
// Input value of N
int N = 5;
// Input arr[], On which game will be played
long arr[] = { 1, 2, 2, 4, 1 };
// Sorting arr[] with in-built sort function
sort(arr,arr+N);
// Printing winner by calling findWinner() function
cout<<(
findWinner(N, arr) == true ? "1" : "2" )<<endl;
// Testcase2
// Input value of N
int N2 = 6;
// Input arr[], On which game will be played
long arr2[] = { 3, 2, 4, 9, 7 };
// Sorting arr[] with in-built sort function
sort(arr2,arr2+N2);
// Printing winner by calling findWinner() function
cout<<(
findWinner(N2, arr2) == true ? "1" : "2" )<<endl;
} // This code is contributed by Pushpesh Raj. |
// Java code to implement the approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver Function
public static void main(String[] args)
throws java.lang.Exception
{
// Testcase1
// Input value of N
int N = 5 ;
// Input arr[], On which game will be played
long [] arr = { 1 , 2 , 2 , 4 , 1 };
// Sorting arr[] with in-built sort function
Arrays.sort(arr);
// Printing winner by calling findWinner() function
System.out.println(
findWinner(N, arr) == true ? "1" : "2" );
// Testcase2
// Input value of N
int N2 = 6 ;
// Input arr[], On which game will be played
long [] arr2 = { 3 , 2 , 4 , 9 , 7 };
// Sorting arr[] with in-built sort function
Arrays.sort(arr2);
// Printing winner by calling findWinner() function
System.out.println(
findWinner(N2, arr2) == true ? "1" : "2" );
}
// Function for find winner
static boolean findWinner( int N, long [] arr)
{
// A flag is initialized to true
boolean flag = true ;
// Variable to calculate absolute difference
long difference = 0 ;
// Loop for traversing on arr[]
for ( int i = 0 ; i < N; i++) {
// If an element found such that A[i]>P[i]
if (((i + 1 ) - arr[i]) < 0 ) {
// Marking flag as false
// and breaking the loop
flag = false ;
break ;
}
else {
// Adding difference P[i]-A[i] in
// difference variable
difference += ((i + 1 ) - arr[i]);
}
}
// If flag is true means someone is winner because
// rule A[i]<=P[i] of game followed successfully If
// difference is odd then First player(returning
// true) wins else second player wins
// else condition
// will execute when A[i]<=P[i] not followed,
// Formally after sorting both A[] and P[] an
// element is there such that A[i]>P[i] at any i
// for(1<=i<=N) following 1 based indexing
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
}
} |
# Python code for the above approach # Function for find winner def find_winner(N, arr):
# A flag is initialized to true
flag = True
# Variable to calculate absolute difference
difference = 0
# Loop for traversing on arr[]
for i in range (N):
# If an element found such that A[i]>P[i]
if ((i + 1 ) - arr[i]) < 0 :
# Marking flag as false
# and breaking the loop
flag = False
break
else :
# Adding difference P[i]-A[i] in
# difference variable
difference + = ((i + 1 ) - arr[i])
# If flag is true means someone is winner because
# rule A[i]<=P[i] of game followed successfully If
# difference is odd then First player(returning
# true) wins else second player wins
# else condition
# will execute when A[i]<=P[i] not followed,
# Formally after sorting both A[] and P[] an
# element is there such that A[i]>P[i] at any i
# for(1<=i<=N) following 1 based indexing
if flag:
return difference % 2 ! = 0
else :
return False
# Testcase1 # Input value of N N = 5
# Input arr[], On which game will be played arr = [ 1 , 2 , 2 , 4 , 1 ]
# Sorting arr[] with in-built sort function arr.sort() # Printing winner by calling findWinner() function print ( "1" if find_winner(N, arr) else "2" )
# Testcase2 # Input value of N N2 = 6
# Input arr[], On which game will be played arr2 = [ 3 , 2 , 4 , 9 , 7 ]
# Sorting arr[] with in-built sort function arr2.sort() # Printing winner by calling findWinner() function print ( "1" if find_winner(N2, arr2) else "2" )
# This code is contributed by Potta Lokesh |
// C# code to implement the approach using System;
using System.Collections;
public class GFG {
static public void Main()
{
// Testcase1
// Input value of N
int N = 5;
// Input arr[], On which game will be played
long [] arr = { 1, 2, 2, 4, 1 };
// Sorting arr[] with in-built sort function
Array.Sort(arr);
// Printing winner by calling findWinner() function
Console.WriteLine(findWinner(N, arr) ? "1" : "2" );
// Testcase2
// Input value of N
int N2 = 6;
// Input arr[], On which game will be played
long [] arr2 = { 3, 2, 4, 9, 7 };
// Sorting arr[] with in-built sort function
Array.Sort(arr2);
// Printing winner by calling findWinner() function
Console.WriteLine(findWinner(N2, arr2) ? "1" : "2" );
}
// Function for find winner
static bool findWinner( int N, long [] arr)
{
// A flag is initialized to true
bool flag = true ;
// Variable to calculate absolute difference
long difference = 0;
// Loop for traversing on arr[]
for ( int i = 0; i < N; i++) {
// If an element found such that A[i]>P[i]
if (((i + 1) - arr[i]) < 0) {
// Marking flag as false
// and breaking the loop
flag = false ;
break ;
}
else {
// Adding difference P[i]-A[i] in
// difference variable
difference += ((i + 1) - arr[i]);
}
}
// If flag is true means someone is winner because
// rule A[i]<=P[i] of game followed successfully If
// difference is odd then First player(returning
// true) wins else second player wins
// else condition
// will execute when A[i]<=P[i] not followed,
// Formally after sorting both A[] and P[] an
// element is there such that A[i]>P[i] at any i
// for(1<=i<=N) following 1 based indexing
if (flag)
return difference % 2 != 0;
else
return false ;
}
} // This code is contributed by lokeshmvs21. |
// Javascript code to implement the approach // Function for find winner function findWinner(N, arr)
{ // A flag is initialized to true
let flag = true ;
// Variable to calculate absolute difference
let difference = 0;
// Loop for traversing on arr[]
for (let i = 0; i < N; i++) {
// If an element found such that A[i]>P[i]
if (((i + 1) - arr[i]) < 0) {
// Marking flag as false
// and breaking the loop
flag = false ;
break ;
}
else {
// Adding difference P[i]-A[i] in
// difference variable
difference += ((i + 1) - arr[i]);
}
}
// If flag is true means someone is winner because
// rule A[i]<=P[i] of game followed successfully If
// difference is odd then First player(returning
// true) wins else second player wins
// else condition
// will execute when A[i]<=P[i] not followed,
// Formally after sorting both A[] and P[] an
// element is there such that A[i]>P[i] at any i
// for(1<=i<=N) following 1 based indexing
if (flag)
return difference % 2 != 0 ? true : false ;
else
return false ;
} // Driver Function // Testcase1
// Input value of N
let N = 5;
// Input arr[], On which game will be played
let arr = [1, 2, 2, 4, 1 ];
// Sorting arr[] with in-built sort function
arr.sort();
// Printing winner by calling findWinner() function
document.write(findWinner(N, arr) == true ? "1" : "2" );
// Testcase2
// Input value of N
let N2 = 6;
// Input arr[], On which game will be played
let arr2 = [3, 2, 4, 9, 7 ];
// Sorting arr[] with in-built sort function
arr2.sort();
document.write( "<br>" );
// Printing winner by calling findWinner() function
document.write(findWinner(N2, arr2) == true ? "1" : "2" );
|
1 2
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Related Articles: