Given an integer **N**, the task is to find the minimum count of numbers ending with **7** such that the sum of these numbers is **N**.

**Examples:**

Input:N = 38

Output:4

7 + 7 + 7 + 17

Input:N = 46

Output:-1

46 cannot be represented as the sum

of integers ending with 7.

Input:N = 215

Output:5

7 + 7 + 7 + 7 + 187

**Approach:**

- First observation here is that every number greater than or equal to
**70**can always be written as the sum of numbers all ending with**7**. For example, for**82**the last digit is**2**, so at least 6 numbers ending with 7 are required i.e. (7 * 6 = 42). An array**hasharr[]**can be created where**hasharr[i]**represents the minimum number of numbers required having the last digit as**7**so the resultant sum has the last digit as**i**. - If the number is less than
**70**then**N**has to be checked whether it is less than the sum of the minimum number of numbers ending with digit seven**7**. If it is then it is not possible and print**-1**, otherwise if it is greater or equal than it is possible.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `TEN = 10; ` ` ` `// Function to return the count of ` `// minimum numbers ending with 7 ` `// required such that the sum ` `// of these numbers is n ` `int` `minCount(` `int` `n) ` `{ ` ` ` ` ` `// hasharr[i] will store the minimum ` ` ` `// numbers ending with 7 so that it ` ` ` `// sums to number ending with digit i ` ` ` `int` `hasharr[TEN] = { 10, 3, 6, 9, 2, 5, 8, 1, 4, 7 }; ` ` ` ` ` `// Its always possible to write numbers > 69 ` ` ` `// to write as numbers ending with 7 ` ` ` `if` `(n > 69) ` ` ` `return` `hasharr[n % TEN]; ` ` ` `else` `{ ` ` ` ` ` `// If the number is atleast equal to the ` ` ` `// sum of minimum numbers ending with 7 ` ` ` `if` `(n >= hasharr[n % TEN] * 7) ` ` ` `return` `(hasharr[n % TEN]); ` ` ` `else` ` ` `return` `-1; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 38; ` ` ` ` ` `cout << minCount(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG { ` ` ` `// Function to return the count of ` `// minimum numbers ending with 7 ` `// required such that the sum ` `// of these numbers is n ` `static` `int` `minCount(` `int` `n) ` `{ ` ` ` ` ` `// hasharr[i] will store the minimum ` ` ` `// numbers ending with 7 so that it ` ` ` `// sums to number ending with digit i ` ` ` `int` `[] hasharr = { ` `10` `, ` `3` `, ` `6` `, ` `9` `, ` `2` `, ` ` ` `5` `, ` `8` `, ` `1` `, ` `4` `, ` `7` `}; ` ` ` ` ` `// Its always possible to write ` ` ` `// numbers > 69 to write as ` ` ` `// numbers ending with 7 ` ` ` `if` `(n > ` `69` `) ` ` ` `return` `hasharr[n % ` `10` `]; ` ` ` `else` ` ` `{ ` ` ` ` ` `// If the number is atleast equal ` ` ` `// to the sum of minimum numbers ` ` ` `// ending with 7 ` ` ` `if` `(n >= hasharr[n % ` `10` `] * ` `7` `) ` ` ` `return` `(hasharr[n % ` `10` `]); ` ` ` `else` ` ` `return` `-` `1` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `38` `; ` ` ` ` ` `System.out.println(minCount(n)); ` `} ` `} ` ` ` `// This code is contributed by spp____ ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to return the count of ` `# minimum numbers ending with 7 ` `# required such that the sum ` `# of these numbers is n ` `def` `minCount(n): ` ` ` ` ` `# hasharr[i] will store the minimum ` ` ` `# numbers ending with 7 so that it ` ` ` `# sums to number ending with digit i ` ` ` `hasharr ` `=` `[ ` `10` `, ` `3` `, ` `6` `, ` `9` `, ` `2` `, ` ` ` `5` `, ` `8` `, ` `1` `, ` `4` `, ` `7` `] ` ` ` ` ` `# Its always possible to write ` ` ` `# numbers > 69 to write as ` ` ` `# numbers ending with 7 ` ` ` `if` `(n > ` `69` `): ` ` ` `return` `hasharr[n ` `%` `10` `] ` ` ` `else` `: ` ` ` ` ` `# If the number is atleast equal ` ` ` `# to the sum of minimum numbers ` ` ` `# ending with 7 ` ` ` `if` `(n >` `=` `hasharr[n ` `%` `10` `] ` `*` `7` `): ` ` ` `return` `hasharr[n ` `%` `10` `] ` ` ` `else` `: ` ` ` `return` `-` `1` ` ` `# Driver code ` `n ` `=` `38` `; ` ` ` `print` `(minCount(n)) ` ` ` `# This code is contributed by spp____ ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the count of ` `// minimum numbers ending with 7 ` `// required such that the sum ` `// of these numbers is n ` `static` `int` `minCount(` `int` `n) ` `{ ` ` ` ` ` `// hasharr[i] will store the minimum ` ` ` `// numbers ending with 7 so that it ` ` ` `// sums to number ending with digit i ` ` ` `int` `[] hasharr = { 10, 3, 6, 9, 2, ` ` ` `5, 8, 1, 4, 7 }; ` ` ` ` ` `// Its always possible to write ` ` ` `// numbers > 69 to write as ` ` ` `// numbers ending with 7 ` ` ` `if` `(n > 69) ` ` ` `return` `hasharr[n % 10]; ` ` ` `else` ` ` `{ ` ` ` ` ` `// If the number is atleast equal ` ` ` `// to the sum of minimum numbers ` ` ` `// ending with 7 ` ` ` `if` `(n >= hasharr[n % 10] * 7) ` ` ` `return` `(hasharr[n % 10]); ` ` ` `else` ` ` `return` `-1; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main (String[] args) ` `{ ` ` ` `int` `n = 38; ` ` ` ` ` `Console.WriteLine(minCount(n)); ` `} ` `} ` ` ` `// This code is contributed by spp____ ` |

*chevron_right*

*filter_none*

**Output:**

4

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:

- Count of numbers in Array ending with digits of number N
- Minimum count of numbers required from given array to represent S
- Minimum count of numbers required with unit digit X that sums up to N
- Count the number of carry operations required to add two numbers
- Count of minimum reductions required to get the required sum K
- Minimum operations required to make two numbers equal
- Count minimum factor jumps required to reach the end of an Array
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum number of swaps required to make a number divisible by 60
- Sort numbers based on count of letters required to represent them in words
- Minimum number of given powers of 2 required to represent a number
- Minimum number of changes required to make the given array an AP
- Minimum number of primes required such that their sum is equal to N
- Minimum number of operations required to reduce N to 1
- Minimum number of given operation required to convert n to m
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum number operations required to convert n to m | Set-2
- Count the number of operations required to reduce the given number

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.