Open In App

Minimum value to be added to X such that it is at least Y percent of N

Improve
Improve
Like Article
Like
Save
Share
Report

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:
Adding 2 to X gives 4 which is 40% of 10

Input: N = 10, X = 2, Y = 20 
Output:
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);
}


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.


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


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.


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.


Javascript




<script>
 
// Javascript 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
    let 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
let n = 10, x = 2, y = 40;
 
document.write(minValue(n, x, y));
 
// This code is contributed by souravmahato348
 
</script>


Output: 

2

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 



Last Updated : 23 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads