Problem Statement: Let’s define a Beautiful Function F(x) in such a way: Add 1 to the value of x, if the result of addition contains any trailing zeros then remove them all.
Example:
F(11) = 12
F(19) = 2 (20 –> 2)
F(99) = 1(100 –> 10 –> 1)
Let’s define a number to be reachable from x , if we can apply Beautiful Function some number of times (possibly zero) to x and get that number as result
Ex. 102 can be reachable from 1099 as F(F(1099)) = F(101) = 102
You are given a number N . Calculate how many numbers are reachable from N.
Input Format:
The first line contains an integer N. denoting the given number.
Constrains: 1 <= N <= 10^9
Sample Input | Sample output | Explanation |
1 | 9 | 1,2,3,4,5,6,7,8,9 are reachable from 1. |
Let’s Understand the problem first: In this problem, you will be given a number you have to find how many numbers can be reachable from the given no using the Beautiful function.
The beautiful function is also very simple you just have to add 1 with the given input number and have to check whether it has any trailing zero or not if it has trailing zeros we have to remove those zeros to get the output number of the function.
Tricks to solve the problem: As reading the question you can guess that we have to use beautiful function multiple times so it can be easily solved using recursion function . But the question is when we have to stop the recursion or what will be the exit condition in case of the problem??
So the exit condition will be we have to make a list of all the visited numbers from the given input number and if any case we get a same number which has already been visited then numbers will be start repeating so in that case we have to stop the recursion and length of the list will be the final output of the program.
Solution: Here is our solution in python. We strongly recommend you to attempt the problem first before moving to our given solution for your betterment.
# HackWithInfy 2021 Sample Question-A (Beautiful Function) Solution
# www.codewindow.in
count=0
r=[]
def func1(c):
global r
global count
if c in r:
return
else:
r.append(c)
c=c+1
c=str(c)
c=c.rstrip('0')
c=int(c)
count=count+1
func1(c)
n=int(input())
count=0
func1(n)
print(count)
Let’s Understand the code line by line: First off all I needed a count variable for counting the output and a list r to store all the values possible to visit from the given input number. We can do it by two ways as passing the list and count variable in each recursive call or we can do it but making them global variable. We have made them global in our solution.
Now coming in the driving code first we have taken input and typecasting it into int. We have made count variable as 0 for initial setting. Then we are calling func1 which is the original beautiful function and passing the initial input value in the function.
Now let’s look into the beautiful function. We have taken the global list r and the count variable inside the function and we are checking if the number is already available in list or not? If the number is previously present in the list we perform break and end the recursive calling. If not present in the list we are adding the new value in the list and then performing the beautiful function operation.
We are adding 1 with the number and changing it to a string by using str() command . Lets understand it by using a example suppose our input is 99 in beautiful function we add 1 then it becomes 100. We typecast it in string then it becomes 100 in string format. Now we are applying rstrip(‘0’) methods in string to remove the right side ‘0’ characters . So the string “100” after removing right side ‘0’ characters will become “1” then we again typecast it into int to get the value. We increase the value of count by 1 and we call again the func1() by giving the newly generated value as input. And at last in the driving code we print the count to get the value of output which is number of values possible to visit from the given input number.
Proof of successful running the code: Here is the proof that our code was successfully accepted by online judge.
- Adobe (1)
- Advanced Coading (1)
- Advanced course (1)
- Ajax (17)
- Algorithm (6)
- Amagi (1)
- Amazon Interview Questions (1)
- Angular JS (23)
- Aptitude (10)
- Aptitude tricks (3)
- Automata Fixing (1)
- Basic Coding (1)
- big data (61)
- Books (1)
- Bridge2i (1)
- C programming (20)
- Campgemini Interview Questions (1)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- Celebal Tech (1)
- Cloud Computing (28)
- code nation (2)
- Coding Questions (240)
- Cognizant Placement (11)
- commvault Systems (1)
- Computer Network (24)
- CSS (44)
- CTS (1)
- Data Science (44)
- Data Structure (1)
- Data Structure and Algorithm (126)
- DBMS (29)
- De Show Interview Questions (1)
- deloitte (2)
- Deutsche Bank Interview questions (1)
- Enhance Communication (1)
- Epam Full Question Paper (6)
- Extempore (1)
- Exxon Mobil interview questions (1)
- filpkart (1)
- Fractal Analytics Interview Questions (1)
- Genpact (1)
- Grab (1)
- GreyB Interview Questions (1)
- Group Discussion (1)
- Guidance for Accenture (3)
- Gupshup (1)
- Hackathon 2024 (1)
- HCL Interview Questions (1)
- Hexaware (1)
- HFCL (1)
- HR Questions (11)
- HTML5 (44)
- IBM Interview questions (1)
- IBM Questions (5)
- Incture Interview Questions (1)
- Infineon Technologies Interview Questions (1)
- Infosys (11)
- Infosys Interview Questions (1)
- Internship (1)
- Interview Experience (19)
- Interview Questions (63)
- ITC Infotech (1)
- itron (1)
- JavaScript (67)
- JECA (1)
- Job Info (93)
- JQuery (15)
- Kantar Interview Questions (1)
- Language Confusion (1)
- language confussion (1)
- Larsen & Turbo (1)
- Latenview AnalyticsInterview questions (1)
- Lexmark International Interview Questions (1)
- Linkedin (1)
- Machine Learning (23)
- Media.net (1)
- Mindtree Interview Questions (1)
- Miscellaneous (61)
- Mock Test Series (2)
- MongoDB (34)
- Morgan Stanly Interview Questions (1)
- nagarro (5)
- navi (1)
- NodeJS (24)
- NTT Data Interview Questions (1)
- NVDIA (1)
- NVDIA interview questions (1)
- Operating System (19)
- Optum (1)
- PayU (2)
- Persistent INterview Questions (1)
- PHP and MYSQL (31)
- Previous Coding Questions (1)
- Programming in C (61)
- Programming in C++ (33)
- Programming in JAVA (154)
- Programming in Python (133)
- Pseudo Code (2)
- pseudocode (5)
- PWC Interview Questions (1)
- Python (61)
- Quiz (9)
- Razorpay (1)
- ReactJS (26)
- Recruiting Companies (34)
- Revature (3)
- salesforce (1)
- Samsung (1)
- Schlumberger (1)
- Seimens (2)
- Slice (1)
- Smart Cube (1)
- Software Engineering (35)
- Study Material (4)
- Tally Solutions (1)
- tata cliq (1)
- TCS (1)
- TCS NQT (69)
- TCS NQT Coding Questions (13)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Technical Preparation (1)
- Teg Analytics (1)
- Tejas Network Interview Questions (1)
- Texas Instrument Interview Questions (1)
- Tiger Analytics (1)
- Uncategorized (66)
- UnDosTres (1)
- Unstop (1)
- Verbal Ability (8)
- Verbal Lesson (1)
- Web Development (208)
- wipro (1)
- Wipro Coding Questions (5)
- Wipro interview Questions (1)
- Wipro NLTH (30)
- WIpro NLTH Coding Solve (19)
- Zenser (1)
- Zoho Interview Questions (1)