Tennis Tournament | codechef solution

 Problem:-

A tennis tournament is about to take place with � players participating in it. Every player plays with every other player exactly once and there are no ties. That is, every match has a winner and a loser.

With Naman's birthday approaching, he wants to make sure that each player wins the same number of matches so that nobody gets disheartened.

Your task is to determine if such a scenario can take place and if yes find one such scenario.

###Input:

  • First line will contain �, number of testcases. Then the testcases follow.
  • Each testcase contains of a single integer � denoting number of players.

###Output:

  • If it's impossible for everyone to win the same number of matches, print "NO" (without quotes).
  • Otherwise print "YES" (without quotes) and then print � lines , each line should consist of a string containing only 0s and 1s and should be of size �.
  • If the jth character in the ith line is 1 then it means in the match between � and � , � wins.
  • You will get a WA if the output does not correspond to a valid tournament, or if the constraints are not satisfied.
  • You will get also WA verdict if any 2 lines have contradicting results or if a player beats himself.

###Constraints

  • 1≤�≤100
  • 2≤�≤100

###Subtasks

  • 10 points : 2≤�≤6
  • 90 points : Original Constraints.

Sample 1:

Input
Output
2
3
2
YES
010
001
100
NO

Explanation:

One such scenario for � = 3 is when player 1 beats player 2, player 2 to beats player 3 and player 3 beats player 1. Here all players win exactly 1 match.


Code(c++):-


#include <bits/stdc++.h>
using namespace std;
void tournament()
{
int players, y_n, wins;
cin >> players;
vector <char> str (players, '0');
vector <char> zeroes (players, '0');
if (players%2==1)
{
cout << "YES" <<endl;
for (int i = 0; i < players; i++)
{
wins = (players-1)/2;
for (int j = 1; j <= (players-1)/2; j++)
{
if (i+j<players)
{
str[i+j]='1';
wins--;
}else{
for (int l = 0; l < wins; l++)
{
str[l]='1';
}
break;
}
}
for (int k = 0; k < players; k++)
{
cout << str[k];
}
cout << "\n";
str = zeroes;
}
}else{
cout << "NO" <<endl;
}
}
int main() {
    int t;
    cin >> t;
    while (t--)
    {
     tournament();
    }
    return 0;
}


Code(java):-


import java.util.*;
import java.lang.*;
import java.io.*;


class Codechef
{
  public static void patternMaker (int n)
  {
  
   int m = n/2;
   int [] arr = new int [n];
   arr[0]=0;
   for(int i = 1;i <= m;i++){
   arr[i] = 1;
   }
   for(int i = m + 1;i < n;i++){
   arr[i] = 0;
   }
   for (int i = 0;i < n;i++)
   {
   System.out.print(arr[i]);
   }
   System.out.println("");
   for(int i = n - 1;i > 0;i--){
   int ptr = i;
   for(int j = 0;j < n;j++){
   if(ptr == n)
   ptr = 0;
   System.out.print(arr[ptr]);
   ptr++;
   }
   System.out.println("");
   }
  
  }
  public static void main (String[] args) throws java.lang.Exception
  {

    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    for (int i=0;i<t ;i++ )
    {
     int n = sc.nextInt();
     if(n%2==0)
     System.out.println("NO");
     else
     {
     System.out.println("YES");
     patternMaker(n);
     }
    
    
    }
    
    
  }
}


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-