Minimum value to be added to X such that it is at least Y percent of N
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++
#include <bits/stdc++.h>
using namespace std;
int minValue( int n, int x, int y)
{
float val = (y * n) / 100;
if (x >= val)
return 0;
else
return ( ceil (val) - x);
}
int main()
{
int n = 10, x = 2, y = 40;
cout << minValue(n, x, y);
}
|
Java
import java.lang.Math;
class GFG
{
static int minValue( int n, int x, int y)
{
float val = (y * n) / 100 ;
if (x >= val)
return 0 ;
else
return ( int )(Math.ceil(val)-x);
}
public static void main(String[] args)
{
int n = 10 , x = 2 , y = 40 ;
System.out.println(minValue(n, x, y));
}
}
|
Python3
import math
def minValue(n, x, y):
val = (y * n) / 100
if x > = val:
return 0
else :
return math.ceil(val) - x
n = 10 ; x = 2 ; y = 40
print (minValue(n, x, y))
|
C#
using System;
class GFG
{
static int minValue( int n, int x, int y)
{
float val = (y * n) / 100;
if (x >= val)
return 0;
else
return ( int )(Math.Ceiling(val)-x) ;
}
public static void Main()
{
int n = 10, x = 2, y = 40;
Console.WriteLine(( int )minValue(n, x, y));
}
}
|
PHP
<?php
function minValue( $n , $x , $y )
{
$val = ( $y * $n ) / 100;
if ( $x >= $val )
return 0;
else
return ( ceil ( $val ) - $x );
}
{
$n = 10; $x = 2; $y = 40;
echo (minValue( $n , $x , $y ));
}
|
Javascript
<script>
function minValue(n, x, y)
{
let val = (y * n) / 100;
if (x >= val)
return 0;
else
return (Math.ceil(val) - x);
}
let n = 10, x = 2, y = 40;
document.write(minValue(n, x, y));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...