Monday, August 5, 2019

Sports Tournament

Sports Tournament 


The company XYZ is hosting a cricket tournament for its employees. The teams are to be built by different departments and the format of the tournament will be knockout. The HR lead is busy with preparations and has done all the promotions. The sponsors are very keen for the tournament and have raised queries about it. The sponsors have asked about the number of matches that needs to be played. Here is where the HR executive is unable to help as he made all the preparations but forgot about the basic questions. He badly needs your help to find the number of matches they have to organize.


The HR has provided you with the total number of teams(N) that registered for the tournament. If there is any team left out to be paired then it automatically gets promoted to the next round. Now it is only you who can save the HR from embarrassment by solving the problem.


Input Format
The first line of input consists of the number of test cases T.
Next T lines consist of the number of teams participating in the tournament, N.

Constraints
1<= T <=100
2<= N <=100000

Output Format
For each test case, print the number of matches that have to be played.

Sample TestCase 1
Input
2
4
7
Output
3
6
Explanation
Test Case 1: N = 4 = 2 + 1 = 3
Test Case 2: N = 7 = 3 + 2 + 1 = 6

PROGRAM IN C:

#include <stdio.h> int main() { int cases,arr1[100],k=0,arr2[100]; //printf("no.of cases"); scanf("%d",&cases); for(int x=0;x<cases;x++) arr2[x]=0; for(int i=0;i<cases;i++) { scanf("%d",&arr1[i]); } for(int j=0;j<cases;j++) { while(arr1[j]>1) { if(arr1[j]%2==0) { arr2[k]=arr2[k]+arr1[j]/2; arr1[j]=arr1[j]/2; } else { arr2[k]=arr2[k]+arr1[j]/2; arr2[k]++; arr1[j]=arr1[j]/2; } } k++; } for(int l=0;l<cases;l++) printf("%d\n",arr2[l]); return 0; }

13 comments:

  1. Can you provide this code in python.

    ReplyDelete
  2. Can you provide the code in java

    ReplyDelete
  3. Hi,
    Here is the Java code...

    package demo;

    import java.util.Scanner;

    public class DemoTranslation {
    public static void main(String[] args) {
    int cases, k = 0;
    int[] arr1 = new int[100], arr2 = new int[100];
    //printf("no.of cases");
    cases = STDIN_SCANNER.nextInt();
    for(int x = 0; x < cases; x++) {
    arr2[x] = 0;
    }
    for(int i = 0; i < cases; i++) {
    arr1[i] = STDIN_SCANNER.nextInt();
    }
    for(int j = 0; j < cases; j++) {
    while(arr1[j] > 1) {
    if(arr1[j] % 2 == 0) {
    arr2[k] = arr2[k] + arr1[j] / 2;
    arr1[j] = arr1[j] / 2;
    } else {
    arr2[k] = arr2[k] + arr1[j] / 2;
    arr2[k] = arr2[k] + 1;
    arr1[j] = arr1[j] / 2;
    }
    }
    k++;
    }
    for(int l = 0; l < cases; l++) {
    System.out.println(arr2[l]);
    }
    }

    public final static Scanner STDIN_SCANNER = new Scanner(System.in);
    }

    Thanks
    Nagendra

    ReplyDelete
  4. onle line code n-1 no of matches for n teams

    ReplyDelete
  5. Replies
    1. using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      class CandidateCode {
      static void Main(String[] args) {
      //Write code here
      int numberOfTests = Convert.ToInt32(Console.ReadLine());
      int[] numberOfTeams= new int[numberOfTests];


      for(int i=0;i<numberOfTeams.Length; i++)
      {
      numberOfTeams[i] = Convert.ToInt32(Console.ReadLine());
      }

      for(int i=0;i<numberOfTeams.Length; i++)
      {
      Console.WriteLine(numberOfTeams[i] - 1);
      }

      }
      }

      Delete
  6. Hello there, just became alert to your blog through Google, and found that it’s really informative. I’m going to watch out for brussels. I will be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers! login sbobet

    ReplyDelete