HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Solving 'decent numbers' that follow specific division rules

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
decentsolvingdivisionnumbersthatfollowspecificrules

Problem

HackerRank Problem Statement

I have to find the largest 'decent number' having N digits. A decent number has the following properties:

  • 3, 5, or both as its digits. No other digit is allowed.



  • Number of times 3 appears is divisible by 5.



  • Number of times 5 appears is divisible by 3.



My code works perfect, but, I am afraid it is far from efficient:

Input:

4 #number of test cases, i.e., the following 4 inputs
1
3
5
11


Output:

-1
555
33333
55555533333


My version of the solution:

T = int(input()) #number of test cases

def computePair(N):
    multiplesOf3 = [i for i in range(N+1) if i%3 == 0]
    if multiplesOf3 != [0]: #reversing a [0] results in a 'NoneType'
        multiplesOf3.reverse()
    multiplesOf5 = [i for i in range(N+1) if i%5 == 0]

    for i in multiplesOf3:
        for j in multiplesOf5:
            if (i+j) == N:
                return (i, j)
    return -1

for testCase in range(T):
    N = int(input()) #test case
    result = computePair(N)
    if result == -1:
        print(result) #If no such combination exists return '-1'
    else:
        print('5' * (result[0]) + '3' * (result[1]))


I assume my code has a time-complexity of \$O(n^2)\$; because of the two for loops. I want this to be more efficient in the order of \$O(n)\$ - linear.

This is a very generic question: also, do you have any resources to time-complexity best practices? I suck at it.

Solution

At the heart of your problem you are trying to solve a single linear Diophantine equation. Because 3 and 5 are coprime, their greatest common divisor is 1, and hence your equation, \$3a + 5b = n\$, will always have an integer solution. It is easy to check that \$a = 7n\$, \$b = -4n\$ is indeed one such solution. Of course you want both \$a\$ and \$b\$ to be positive, so if we take the more general solution to your equation, \$a = 7n - 5k\$, \$b = -4n + 3k\$, and impose \$b \ge 0\$, we get \$k = \lceil 4n / 3 \rceil\$.

Let's put this into a Python function:

def partition35(n):
    k = (4*n - 1) // 3 + 1  # rounded up int division
    a = 21*n - 15*k  # (7*n - 5*k)*3
    b = 15*k - 20*n  # (3*k - 4*n)*5
    return a, b


Notice that there is a single division performed. Division and modulo operations are among the most expensive basic operations you can do on integers, so aside from the fact that this method is constant time, that time is likely going to be pretty small.

To solve your original question:

def max_decent(digits):
    a, b = partition35(digits)
    if a < 0:
        print(-1)
    else:
        print(''.join(('5'*a, '3'*b)))

Code Snippets

def partition35(n):
    k = (4*n - 1) // 3 + 1  # rounded up int division
    a = 21*n - 15*k  # (7*n - 5*k)*3
    b = 15*k - 20*n  # (3*k - 4*n)*5
    return a, b
def max_decent(digits):
    a, b = partition35(digits)
    if a < 0:
        print(-1)
    else:
        print(''.join(('5'*a, '3'*b)))

Context

StackExchange Code Review Q#97445, answer score: 10

Revisions (0)

No revisions yet.