We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The RUNTIME ERROR COMES FOR SOME TEST CASES SINCE THE THE INPUT SIZE OF THE STRING WHEN MULTIPLIED WITH K IT BECOMES HUGE STRING WHICH IS TOUGH PROCESS. SO AT FIRST JUST SUM ALL THE DIGITS IN NUMBER STRING AND MULTIPLY WITH K.
FOR EXMP:
n='11'
k=2
p=n*k
p='1111' # this could our naive approach
s=4 # sum of all digits.
now,
p= (sum of all digits in n which is equal to 2) * k
=> p=4 #same as our naive approach.
!/bin/python3
import math
import os
import random
import re
import sys
def superDigit(n, k):
# Write your code here
#base condition
if len(n)==1:
return int(n)
s=0
for i in n:
s=s+int(i)
p=s*k if k>1 else s
return superDigit(str(p),1)
if name == 'main':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = first_multiple_input[0]
k = int(first_multiple_input[1])
result = superDigit(n, k)
fptr.write(str(result) + '\n')
fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
The RUNTIME ERROR COMES FOR SOME TEST CASES SINCE THE THE INPUT SIZE OF THE STRING WHEN MULTIPLIED WITH K IT BECOMES HUGE STRING WHICH IS TOUGH PROCESS. SO AT FIRST JUST SUM ALL THE DIGITS IN NUMBER STRING AND MULTIPLY WITH K. FOR EXMP: n='11' k=2 p=n*k p='1111' # this could our naive approach s=4 # sum of all digits. now,
p= (sum of all digits in n which is equal to 2) * k
=> p=4 #same as our naive approach.
!/bin/python3
import math import os import random import re import sys
def superDigit(n, k): # Write your code here #base condition if len(n)==1: return int(n) s=0 for i in n: s=s+int(i) p=s*k if k>1 else s
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')