test
unknown
java
4 years ago
863 B
9
Indexable
import java.util.*;
class Solution
{
public int solution(int N, int K)
{
int count=0; // Here we take count for counting the number of Glasses use.
for(int i=N;i>0;i--) // In this loop we start from N, and subtract the capacity of each glasses from K, till K is zero
{
if(K>=i)
{
count++;
K-=i;
}
else
{
count++;
K=0;
break;
}
}
if(K!=0) // And if all N glasses are filled but K is not zero, then function return -1.
{
count=-1;
}
return count;
}
}
public class Main
{
public static void main(String[] args) {
Solution s = new Solution();
Scanner sc=new Scanner (System.in);
System.out.print("Enter the value of N = ");
int N = sc.nextInt();
System.out.print("Enter the value of K = ");
int K = sc.nextInt();
System.out.print("The Minimum Number of Bottels required = ");
System.out.println(s.solution(N,K));
}
}Editor is loading...