POSSIBLEPAIR Editorial

PROBLEM LINK:

Practice

Author: Ram Agrawal
Tester: Yogesh Deolalkar
Editorialist:Prathamesh Sogale

DIFFICULTY:

CAKEWALK, SIMPLE.

PREREQUISITES:

Greedy, Math

PROBLEM:

Prathamesh is supervising a game in which each player is given an id. In this game, each player has to find another player to form a perfect pair. A perfect pair is a pair in which the addition of the id of both the players is fully divisible by k. A player cannot form pairs with multiple players. There is always an even number of players. Help Prathamesh to find that is it possible to get every player a partner.

Given an array of integers of length N and a number k, write a program to print True if the given array can be divided into N/2 perfect pairs else print False.

     Note!- Test cases are case sensitive TrueTrue and truetrue are different

SOLUTIONS:

Setter's Solution

#include
using namespace std;
//using ll=long long;

int main() {
// your code goes here
long long int n,k;
std::cin >> n>>k;
long long int arr[n];
for(long long int i=0;i<n;i++){
std::cin >> arr[i];
}
long long int sum=0;
for(long long int i=0;i<n;i++){
sum=sum+arr[i];
}
if(n%2==0){
if(sum%k==0){
std::cout << “True” << std::endl;
}
else{
std::cout << “False” << std::endl;
}
}
else{
std::cout << “False” << std::endl;
}
return 0;
}

Tester's Solution

public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int arr[]=new int[n];
int total=0;
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
total += arr[i];
}
if(total%k==0){
System.out.println(“True”);
}
else{
System.out.println(“False”);
}
}