# Minimum matches the team needs to win to qualify

Given two integers **X** and **Y** where **X** denotes the **number of points required** to qualify and **Y** denotes the **number of matches left**. The team receives **2 points on winning** the match and **1 point on losing**. The task is to find the minimum number of matches the team needs to win in order to qualify for next round.

**Examples:**

Input:X = 10, Y = 5

Output:5

The team needs to win all the matches in order to get 10 points.

Input :X = 6, Y = 5

Output :1

If the team wins a single match and loses the rest 4 matches, they would still qualify.

A **naive approach** is to check by iterating over all values from **0** to **Y** and find out the first value which gives us **X** points.

An **efficient approach** is to perform a binary search on the number of matches to be won to find out the minimum number of the match. Initially **low = 0** and **high = X**, and then we check for the condition **(mid * 2 + (y – mid)) ≥ x**. If the condition prevails, then check if any lower value exists in the left half i.e. **high = mid – 1** else check in the right half i.e. **low = mid + 1**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum number of ` `// matches to win to qualify for next round ` `int` `findMinimum(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Do a binary search to find ` ` ` `int` `low = 0, high = y; ` ` ` `while` `(low <= high) { ` ` ` ` ` `// Find mid element ` ` ` `int` `mid = (low + high) >> 1; ` ` ` ` ` `// Check for condition ` ` ` `// to qualify for next round ` ` ` `if` `((mid * 2 + (y - mid)) >= x) ` ` ` `high = mid - 1; ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` `return` `low; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `x = 6, y = 5; ` ` ` `cout << findMinimum(x, y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum number of ` `// matches to win to qualify for next round ` `static` `int` `findMinimum(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Do a binary search to find ` ` ` `int` `low = ` `0` `, high = y; ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` ` ` `// Find mid element ` ` ` `int` `mid = (low + high) >> ` `1` `; ` ` ` ` ` `// Check for condition ` ` ` `// to qualify for next round ` ` ` `if` `((mid * ` `2` `+ (y - mid)) >= x) ` ` ` `high = mid - ` `1` `; ` ` ` `else` ` ` `low = mid + ` `1` `; ` ` ` `} ` ` ` `return` `low; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `x = ` `6` `, y = ` `5` `; ` ` ` `System.out.println(findMinimum(x, y)); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function to return the minimum number of ` `# matches to win to qualify for next round ` `def` `findMinimum(x, y): ` ` ` ` ` `# Do a binary search to find ` ` ` `low ` `=` `0` ` ` `high ` `=` `y ` ` ` `while` `(low <` `=` `high): ` ` ` ` ` `# Find mid element ` ` ` `mid ` `=` `(low ` `+` `high) >> ` `1` ` ` ` ` `# Check for condition ` ` ` `# to qualify for next round ` ` ` `if` `((mid ` `*` `2` `+` `(y ` `-` `mid)) >` `=` `x): ` ` ` `high ` `=` `mid ` `-` `1` ` ` `else` `: ` ` ` `low ` `=` `mid ` `+` `1` ` ` `return` `low ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `x ` `=` `6` ` ` `y ` `=` `5` ` ` `print` `(findMinimum(x, y)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum number of ` `// matches to win to qualify for next round ` `static` `int` `findMinimum(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Do a binary search to find ` ` ` `int` `low = 0, high = y; ` ` ` `while` `(low <= high) ` ` ` `{ ` ` ` ` ` `// Find mid element ` ` ` `int` `mid = (low + high) >> 1; ` ` ` ` ` `// Check for condition ` ` ` `// to qualify for next round ` ` ` `if` `((mid * 2 + (y - mid)) >= x) ` ` ` `high = mid - 1; ` ` ` `else` ` ` `low = mid + 1; ` ` ` `} ` ` ` `return` `low; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main() ` `{ ` ` ` `int` `x = 6, y = 5; ` ` ` `Console.WriteLine(findMinimum(x, y)); ` `} ` `} ` ` ` `// This Code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimum number of ` `// matches to win to qualify for next round ` `function` `findMinimum(` `$x` `, ` `$y` `) ` `{ ` ` ` ` ` `// Do a binary search to find ` ` ` `$low` `= 0; ` `$high` `= ` `$y` `; ` ` ` `while` `(` `$low` `<= ` `$high` `) ` ` ` `{ ` ` ` ` ` `// Find mid element ` ` ` `$mid` `= (` `$low` `+ ` `$high` `) >> 1; ` ` ` ` ` `// Check for condition$ ` ` ` `// to qualify for next round ` ` ` `if` `((` `$mid` `* 2 + (` `$y` `- ` `$mid` `)) >= ` `$x` `) ` ` ` `$high` `= ` `$mid` `- 1; ` ` ` `else` ` ` `$low` `= ` `$mid` `+ 1; ` ` ` `} ` ` ` `return` `$low` `; ` `} ` ` ` `// Driver Code ` `$x` `= 6; ` `$y` `= 5; ` `echo` `findMinimum(` `$x` `, ` `$y` `); ` ` ` `// This code has been contributed ` `// by 29AjayKumar ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(log y)

## Recommended Posts:

- Ways of selecting men and women from a group to make a team
- Given number of matches played, find number of teams in tournament
- Longest string in an array which matches with prefix of the given string
- Minimum adjacent swaps to move maximum and minimum to corners
- Minimum LCM and GCD possible among all possible sub-arrays
- Find the value of N when F(N) = f(a)+f(b) where a+b is the minimum possible and a*b = N
- Minimum value possible of a given function from the given set
- Find minimum x such that (x % k) * (x / k) == n
- Find minimum x such that (x % k) * (x / k) == n | Set-2
- Minimum value of N such that xor from 1 to N is equal to K
- Minimum possible number with the given operation
- Minimum value to be added to X such that it is at least Y percent of N
- Add minimum number to an array so that the sum becomes even
- Minimum number with digits as 4 and 7 only and given sum
- Minimum number of bombs

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.