![]() |
VOOZH | about |
Count the given numbers on your fingers and find the correct finger on which the number ends.
Examples:
Input : 17 Output :1 Input :27 Output :3
Approach: The first number starts from the thumb, second on the index finger, third on the middle finger, fourth on the ring finger, and fifth on the little finger.
4
Time Complexity: O(1), As we are doing only constant time operations.
Auxiliary Space: O(1), As constant extra space is used.
Asked in Paytm Campus Placement August 2017
Total number of fingers used to count 23 is 23
Approach:
1.Define a function count_fingers that takes an integer n as input. The function will return the total number of fingers used to count up to n.
2.Calculate the number of full hands needed to count up to n by dividing n by 10 and using integer division to round down to the nearest whole number. Assign this value to the variable full_hands.
3.Calculate the number of fingers used in the full hands by multiplying full_hands by 10. Assign this value to the variable full_fingers.
4.Calculate the number of remaining fingers needed to count up to n after using all full hands by taking the remainder of n divided by 10. 5.Assign this value to the variable remaining_fingers.
6.Calculate the total number of fingers used to count up to n by adding full_fingers and remaining_fingers. Assign this value to the variable total_fingers.
7.Return the total_fingers value.
8.In the example usage, create an integer n and call the count_fingers function with this argument. Finally, print the total number of fingers used to count up to n.
Time complexity: O(1).
Space complexity: O(1).