You have an unlimited number of 10-rupee coins and exactly one coin of r rupee and you need to buy minimum items each of cost k such that you do not ask for change.
Examples:
Input: k = 15, r = 2
Output: 2
You should buy two cables and pay 2*15=30 rupees. It is obvious that you can pay this sum without any change.
Input: k = 237, r = 7
Output:1
It is enough for you to buy one cable.
It is obvious that we can pay for 10 items without any change (by paying the required amount of 10-rupee coins and not using the coin of r rupee). But perhaps you can buy fewer hammers and pay without any change. Note that you should buy at least one item.
C++
#include <bits/stdc++.h>
using namespace std;
int minItems( int k, int r)
{
for ( int i = 1; i < 10; i++)
if ((i * k - r) % 10 == 0 ||
(i * k) % 10 == 0)
return i;
return 10;
}
int main()
{
int k = 15;
int r = 2;
cout << minItems(k, r);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int minItems( int k, int r)
{
for ( int i = 1 ; i < 10 ; i++)
if ((i * k - r) % 10 == 0 ||
(i * k) % 10 == 0 )
return i;
return 10 ;
}
public static void main(String args[])
{
int k = 15 ;
int r = 2 ;
System.out.println(minItems(k, r));
}
}
|
Python3
def minItems(k, r) :
for i in range ( 1 , 10 ) :
if ((i * k - r) % 10 = = 0 or
(i * k) % 10 = = 0 ) :
return i
return 10 ;
if __name__ = = "__main__" :
k, r = 15 , 2 ;
print (minItems(k, r))
|
C#
using System;
class GFG
{
static int minItems( int k, int r)
{
for ( int i = 1; i < 10; i++)
if ((i * k - r) % 10 == 0 ||
(i * k) % 10 == 0)
return i;
return 10;
}
public static void Main()
{
int k = 15;
int r = 2;
Console.WriteLine(minItems(k, r));
}
}
|
PHP
<?php
function minItems( $k , $r )
{
for ( $i = 1; $i < 10; $i ++)
if (( $i * $k - $r ) % 10 == 0 ||
( $i * $k ) % 10 == 0)
return $i ;
return 10;
}
$k = 15;
$r = 2;
echo minItems( $k , $r );
?>
|
Javascript
<script>
function minItems(k, r)
{
for (let i = 1; i < 10; i++)
if ((i * k - r) % 10 == 0 ||
(i * k) % 10 == 0)
return i;
return 10;
}
let k = 15;
let r = 2;
document.write(minItems(k, r));
</script>
|
Time Complexity : O(10), as 10 is constant so => O(1)
Auxiliary Space : O(1)