Given three integers **N**, **X** and **Y**, the task is to find the minimum integer that should be added to **X** to make it at least **Y** percent of **N**.

**Examples:**

Input:N = 10, X = 2, Y = 40

Output:2

Adding 2 to X gives 4 which is 40% of 10

Input:N = 10, X = 2, Y = 20

Output:0

X is already 20% of 10

**Approach:** Find **val = (N * Y) / 100 ** which is the **Y** percent of **N**. Now in order for **X** to be equal to **val**, **val – X** must be added to **X** only if **X < val**.

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 required value ` `// that must be added to x so that ` `// it is at least y percent of n ` `int` `minValue(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Required value ` ` ` `float` `val = (y * n) / 100; ` ` ` ` ` `// If x is already >= y percent of n ` ` ` `if` `(x >= val) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `(` `ceil` `(val) - x); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10, x = 2, y = 40; ` ` ` `cout << minValue(n, x, y); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.lang.Math; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the required value ` `// that must be added to x so that ` `// it is at least y percent of n ` `static` `int` `minValue(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// Required value ` ` ` `float` `val = (y * n) / ` `100` `; ` ` ` ` ` `// If x is already >= y percent of n ` ` ` `if` `(x >= val) ` ` ` `return` `0` `; ` ` ` `else` ` ` `return` `(` `int` `)(Math.ceil(val)-x); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `10` `, x = ` `2` `, y = ` `40` `; ` ` ` `System.out.println(minValue(n, x, y)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`import` `math ` ` ` `# Function to return the required value ` `# that must be added to x so that ` `# it is at least y percent of n ` `def` `minValue(n, x, y): ` ` ` ` ` `# Required value ` ` ` `val ` `=` `(y ` `*` `n)` `/` `100` ` ` ` ` `# If x is already >= y percent of n ` ` ` `if` `x >` `=` `val: ` ` ` `return` `0` ` ` `else` `: ` ` ` `return` `math.ceil(val) ` `-` `x ` ` ` `# Driver code ` `n ` `=` `10` `; x ` `=` `2` `; y ` `=` `40` `print` `(minValue(n, x, y)) ` ` ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the required value ` ` ` `// that must be added to x so that ` ` ` `// it is at least y percent of n ` ` ` `static` `int` `minValue(` `int` `n, ` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` ` ` `// Required value ` ` ` `float` `val = (y * n) / 100; ` ` ` ` ` `// If x is already >= y percent of n ` ` ` `if` `(x >= val) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `(` `int` `)(Math.Ceiling(val)-x) ; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 10, x = 2, y = 40; ` ` ` `Console.WriteLine((` `int` `)minValue(n, x, y)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// php implementation of the approach ` `// Function to return the required value ` `// that must be added to x so that ` `// it is at least y percent of n ` `function` `minValue(` `$n` `, ` `$x` `, ` `$y` `) ` `{ ` ` ` ` ` `// Required value ` ` ` `$val` `= (` `$y` `* ` `$n` `) / 100; ` ` ` ` ` `// If x is already >= y percent of n ` ` ` `if` `(` `$x` `>= ` `$val` `) ` ` ` `return` `0; ` ` ` `else` ` ` `return` `(` `ceil` `(` `$val` `) - ` `$x` `); ` `} ` ` ` `// Driver code ` `{ ` ` ` `$n` `= 10; ` `$x` `= 2; ` `$y` `= 40; ` ` ` `echo` `(minValue(` `$n` `, ` `$x` `, ` `$y` `)); ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Least number to be added to or subtracted from N to make it a Perfect Square
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Minimum value of X that can be added to N to minimize sum of the digits to ≤ K
- Count pairs in an array such that frequency of one is at least value of other
- Minimum increments by index value required to obtain at least two equal Array elements
- Minimum number of integers required such that each Segment contains at least one of them
- Count possible splits of sum N into K integers such that the minimum is at least P
- Minimum number of Parentheses to be added to make it valid
- Minimum elements to be added in a range so that count of elements is divisible by K
- Minimum elements to be added so that two matrices can be multiplied
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Minimum numbers with one's place as 9 to be added to get N
- Minimum number to be added to all digits of X to make X > Y
- Find the minimum number to be added to N to make it a prime number
- Find the minimum number to be added to N to make it a power of K
- Count pairs in an array containing at least one even value
- Least root of given quadratic equation for value greater than equal to K
- Convert a number of length N such that it contains any one digit at least 'K' times
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Ways to choose balls such that at least one ball is chosen

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.