Minimum numbers with one’s place as 9 to be added to get N
Last Updated :
10 Mar, 2022
Given an integer N, the task is to find the minimum count of numbers which needs to be added to get N. (All such numbers must have 9 as one’s digit)
Examples:
Input: N = 27
Output: 3
27 = 9 + 9 + 9
Input: N = 109
Output: 1
109 itself has 9 as one’s digit.
Approach:
- Check the one’s digit of N, based on one’s digit the minimum count of numbers which needs to be added can be easily found.
- If one’s digit is 9: The answer will 1 as the number itself has 9 as its one’s place digit.
- If one’s digit is:
- 1: 9 has to be added 9 times i.e. (9 * 9 = 81).
- 2: 9 has to be added 8 times i.e. (9 * 8 = 72).
- 3: 9 has to be added 7 times i.e. (9 * 7 = 63).
- 4: 9 has to be added 6 times i.e. (9 * 6 = 54).
- 5: 9 has to be added 5 times i.e. (9 * 5 = 45).
- 6: 9 has to be added 4 times i.e. (9 * 4 = 36).
- 7: 9 has to be added 3 times i.e. (9 * 3 = 27).
- 8: 9 has to be added 2 times i.e. (9 * 2 = 18).
- 0: 9 has to be added 10 times i.e. (9 * 10 = 90).
- Observation here is that only the minimum multiple count for all the cases above mentioned have to be added. This is because say for one’s digit as 4, all 9 (one’s place) from 6 numbers can be used and the result can be subtracted from N say it is M. Now, M will have 0 in one’s place. So, just use 5 numbers as 9 and sixth number as (M + 9).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMin( int N)
{
int digit = N % 10;
switch (digit) {
case 0:
if (N >= 90)
return 10;
break ;
case 1:
if (N >= 81)
return 9;
break ;
case 2:
if (N >= 72)
return 8;
break ;
case 3:
if (N >= 63)
return 7;
break ;
case 4:
if (N >= 54)
return 6;
break ;
case 5:
if (N >= 45)
return 5;
break ;
case 6:
if (N >= 36)
return 4;
break ;
case 7:
if (N >= 27)
return 3;
break ;
case 8:
if (N >= 18)
return 2;
break ;
case 9:
if (N >= 9)
return 1;
break ;
}
return -1;
}
int main()
{
int N = 27;
cout << findMin(N);
}
|
Java
class GFG
{
static int findMin( int N)
{
int digit = N % 10 ;
switch (digit)
{
case 0 :
if (N >= 90 )
return 10 ;
break ;
case 1 :
if (N >= 81 )
return 9 ;
break ;
case 2 :
if (N >= 72 )
return 8 ;
break ;
case 3 :
if (N >= 63 )
return 7 ;
break ;
case 4 :
if (N >= 54 )
return 6 ;
break ;
case 5 :
if (N >= 45 )
return 5 ;
break ;
case 6 :
if (N >= 36 )
return 4 ;
break ;
case 7 :
if (N >= 27 )
return 3 ;
break ;
case 8 :
if (N >= 18 )
return 2 ;
break ;
case 9 :
if (N >= 9 )
return 1 ;
break ;
}
return - 1 ;
}
public static void main (String[] args)
{
int N = 27 ;
System.out.println(findMin(N));
}
}
|
Python3
def findMin(N: int ):
digit = N % 10
if digit = = 0 and N > = 90 :
return 10
elif digit = = 1 and N > = 81 :
return 9
elif digit = = 2 and N > = 72 :
return 8
elif digit = = 3 and N > = 63 :
return 7
elif digit = = 4 and N > = 54 :
return 6
elif digit = = 5 and N > = 45 :
return 5
elif digit = = 6 and N > = 36 :
return 4
elif digit = = 7 and N > = 27 :
return 3
elif digit = = 8 and N > = 18 :
return 2
elif digit = = 9 and N > = 9 :
return 1
return - 1
if __name__ = = "__main__" :
N = 27
print (findMin(N))
|
C#
using System;
class GFG
{
static int findMin( int N)
{
int digit = N % 10;
switch (digit)
{
case 0:
if (N >= 90)
return 10;
break ;
case 1:
if (N >= 81)
return 9;
break ;
case 2:
if (N >= 72)
return 8;
break ;
case 3:
if (N >= 63)
return 7;
break ;
case 4:
if (N >= 54)
return 6;
break ;
case 5:
if (N >= 45)
return 5;
break ;
case 6:
if (N >= 36)
return 4;
break ;
case 7:
if (N >= 27)
return 3;
break ;
case 8:
if (N >= 18)
return 2;
break ;
case 9:
if (N >= 9)
return 1;
break ;
}
return -1;
}
public static void Main (String[] args)
{
int N = 27;
Console.WriteLine(findMin(N));
}
}
|
Javascript
<script>
function findMin(N)
{
let digit = N % 10;
switch (digit)
{
case 0:
if (N >= 90)
return 10;
break ;
case 1:
if (N >= 81)
return 9;
break ;
case 2:
if (N >= 72)
return 8;
break ;
case 3:
if (N >= 63)
return 7;
break ;
case 4:
if (N >= 54)
return 6;
break ;
case 5:
if (N >= 45)
return 5;
break ;
case 6:
if (N >= 36)
return 4;
break ;
case 7:
if (N >= 27)
return 3;
break ;
case 8:
if (N >= 18)
return 2;
break ;
case 9:
if (N >= 9)
return 1;
break ;
}
return -1;
}
let N = 27;
document.write(findMin(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...