Open In App

Minimum count of numbers required ending with 7 to sum as a given number

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:
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:
7 + 7 + 7 + 7 + 187 

Approach:  

Below is the implementation of the above approach:  




// 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;
}




// 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____




# 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____




// 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____




<script>
 
// Javascript 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
function minCount(n)
{
      
    // hasharr[i] will store the minimum
    // numbers ending with 7 so that it
    // sums to number ending with digit i
    let 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
     
      let n = 38;
      
    document.write(minCount(n));
 
// This code is contributed by code_hunt.
</script>

Output: 
4

 

Time Complexity: O(1)

Auxiliary Space: O(1)


Article Tags :