|
U
Pro137. Analyze a Loop That Repeatedly Divides
Easy
Problem Description
Analyze a loop that repeatedly divides N by 3.
Input Format
A single line containing an integer N (0≤N≤106).
Output Format
Print the exact calculated number of executions or metric as a 64-bit integer.
Example 1:
Input:
4
Output:
10
Explanation: For N = 4, iterations evaluated as 1 + 2 + 3 + 4 = 10.
Example 2:
Input:
1
Output:
1
Explanation: For N = 1, exactly 1 execution occurs.
Example 3:
Input:
0
Output:
0
Explanation: For N = 0, no loop iterations are executed.
Constraints:
0 <= N <= 10^6Time complexity target: O(1) closed-form calculationAuxiliary space target: O(1)
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
input =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit