Open In App

Minimum numbers with one’s place as 9 to be added to get N

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:
27 = 9 + 9 + 9

Input: N = 109 
Output:
109 itself has 9 as one’s digit. 
 

Approach: 

Below is the implementation of the above approach: 




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum count
// of numbers(with one's digit 9)
// that sum up to N
int findMin(int N)
{
    // Fetch one's digit
    int digit = N % 10;
 
    // Apply Cases mentioned in approach
    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;
    }
 
    // If no possible answer exists
    return -1;
}
 
// Driver code
int main()
{
    int N = 27;
 
    cout << findMin(N);
}




// Java implementation of the approach
class GFG
{
     
    // Function to find minimum count
    // of numbers(with one's digit 9)
    // that sum up to N
    static int findMin(int N)
    {
        // Fetch one's digit
        int digit = N % 10;
     
        // Apply Cases mentioned in approach
        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;
        }
     
        // If no possible answer exists
        return -1;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 27;
     
        System.out.println(findMin(N));
    }
}
 
// This code is contributed by AnkitRai01




# Python3 implementation of the approach
 
# Function to find minimum count
# of numbers(with one's digit 9)
# that sum up to N
def findMin(N: int):
 
    # Fetch one's digit
    digit = N % 10
 
    # Apply Cases mentioned in approach
    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
 
    # If no possible answer exists
    return -1
 
# Driver Code
if __name__ == "__main__":
    N = 27
    print(findMin(N))
 
# This code is contributed by
# sanjeev2552




// C# implementation of the approach
using System;        
     
class GFG
{
     
    // Function to find minimum count
    // of numbers(with one's digit 9)
    // that sum up to N
    static int findMin(int N)
    {
        // Fetch one's digit
        int digit = N % 10;
     
        // Apply Cases mentioned in approach
        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;
        }
     
        // If no possible answer exists
        return -1;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int N = 27;
     
        Console.WriteLine(findMin(N));
    }
}
 
// This code is contributed by 29AjayKumar




<script>
 
// Javascript implementation of the approach
 
// Function to find minimum count
// of numbers(with one's digit 9)
// that sum up to N
function findMin(N)
{
     
    // Fetch one's digit
    let digit = N % 10;
 
    // Apply Cases mentioned in approach
    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;
    }
 
    // If no possible answer exists
    return -1;
}
 
// Driver code
let N = 27;
 
document.write(findMin(N));
 
// This code is contributed by souravmahato348
 
</script>

Output: 
3

 

Time Complexity: O(1)

Auxiliary Space: O(1)


Article Tags :