Given an integer **N**, the task is to check if **N** can be expressed as a sum of integers having **9** as the last digit (9, 19, 29, 39…), or not. If found to be true, then find the minimum count of such integers required to obtain **N**. Otherwise print **-1**.

**Examples:**

Input:N = 156Output:4Explanation:

156 = 9 + 9 + 9 + 129

Input:N = 60Output:-1Explanation:

No possible way to obtain sum 60 from numbers having 9 as the last digit.

**Naive Approach: **This problem can be viewed as a variation of the Coin change problem. For this problem, the coins can be replaced with [9, 19, 29, 39…. up to the last number smaller than N that ends with 9].

**Time Complexity:** O(N^{2})**Auxiliary Space: **O(N)

**Efficient Approach: **The above approach can be optimized based on the observation that if the **last digit of N is K**, then exactly **(10 – K)** minimum numbers are required to form **N**.

A sum N can be obtained by adding 10 – K numbers, where K is the last digit of N.

Therefore, sum N can be obtained by adding 9, (9 – K) times and adding N – (9 * (9 – K)) finally.

Follow the steps below to solve the problem:

- Extract the last digit of the given number,
**K = N % 10** - Using the above observation, a total of
**(10 – K)**numbers are required. Now, calculate**9 * (9 – K)**, as the first**9 – K**numbers required to obtain**N**is**9**. - Now, calculate
**N – 9 * (9 – K)**and store in a variable, say**z**. If**z**is greater than or equal to**9**and has**9**as its last digit, print**10 – K**as the answer. Otherwise, print**-1**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum count ` `// of numbers ending with 9 to form N ` `int` `minCountOfNumbers(` `int` `N) ` `{ ` ` ` `// Extract last digit of N ` ` ` `int` `k = N % 10; ` ` ` ` ` `// Calculate the last digit ` ` ` `int` `z = N - (9 * (9 - k)); ` ` ` ` ` `// If the last digit ` ` ` `// satisfies the condition ` ` ` `if` `(z >= 9 && z % 10 == 9) { ` ` ` `return` `10 - k; ` ` ` `} ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 156; ` ` ` `cout << minCountOfNumbers(N); ` ` ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum count ` `// of numbers ending with 9 to form N ` `static` `int` `minCountOfNumbers(` `int` `N) ` `{ ` ` ` ` ` `// Extract last digit of N ` ` ` `int` `k = N % ` `10` `; ` ` ` ` ` `// Calculate the last digit ` ` ` `int` `z = N - (` `9` `* (` `9` `- k)); ` ` ` ` ` `// If the last digit ` ` ` `// satisfies the condition ` ` ` `if` `(z >= ` `9` `&& z % ` `10` `== ` `9` `) ` ` ` `{ ` ` ` `return` `10` `- k; ` ` ` `} ` ` ` `else` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `156` `; ` ` ` `System.out.print(minCountOfNumbers(N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find the minimum count ` `# of numbers ending with 9 to form N ` `def` `minCountOfNumbers(N): ` ` ` ` ` `# Extract last digit of N ` ` ` `k ` `=` `N ` `%` `10` ` ` ` ` `# Calculate the last digit ` ` ` `z ` `=` `N ` `-` `(` `9` `*` `(` `9` `-` `k)) ` ` ` ` ` `# If the last digit ` ` ` `# satisfies the condition ` ` ` `if` `(z >` `=` `9` `and` `z ` `%` `10` `=` `=` `9` `): ` ` ` `return` `10` `-` `k ` ` ` `else` `: ` ` ` `return` `-` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `156` ` ` ` ` `print` `(minCountOfNumbers(N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum count ` `// of numbers ending with 9 to form N ` `static` `int` `minCountOfNumbers(` `int` `N) ` `{ ` ` ` ` ` `// Extract last digit of N ` ` ` `int` `k = N % 10; ` ` ` ` ` `// Calculate the last digit ` ` ` `int` `z = N - (9 * (9 - k)); ` ` ` ` ` `// If the last digit ` ` ` `// satisfies the condition ` ` ` `if` `(z >= 9 && z % 10 == 9) ` ` ` `{ ` ` ` `return` `10 - k; ` ` ` `} ` ` ` `else` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 156; ` ` ` ` ` `Console.Write(minCountOfNumbers(N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(1)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Maximum number of distinct positive integers that can be used to represent N
- Minimum count of numbers required ending with 7 to sum as a given number
- Count of numbers in Array ending with digits of number N
- Number of ways to represent a number as sum of k fibonacci numbers
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Represent a number as sum of minimum possible psuedobinary numbers
- Represent the given number as the sum of two composite numbers
- Smallest and Largest N-digit number starting and ending with N
- Number of distinct ways to represent a number as sum of K unique primes
- Represent K^N as the sum of exactly N numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even numbers
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent K as sum of N-bonacci numbers
- Maximum weight path ending at any element of last row in a matrix
- Find start and ending index of an element in an unsorted array
- Count of ways to represent N as sum of a prime number and twice of a square
- Minimum number of given powers of 2 required to represent a number
- Minimum count of numbers required from given array to represent S

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.