Strange Counter

Strange Counter

There is a strange counter. At the first second, it displays the number 3. Each second, the number displayed by decrements by 1 until it reaches 1. In next second, the timer resets to 2 X the initial number for the prior cycle and continues counting down. The diagram below shows the counter values for each time t in the first three cycles:

Find and print the value displayed by the counter at time t.

Function Description

Complete the strangeCounter function in the editor below.

strangeCounter has the following parameter(s):

  • int t: an integer

Returns

  • int: the value displayed at time t

Input Format

A single integer, the value of t.

Constraints

  • 1 <= t <= 1012

Subtask

  • 1 <= t <= 105 for 60% of the maximum score.

Sample Input

1
4

Sample Output

1
6

Explanation

Time t = 4 marks the beginning of the second cycle. It is double the number displayed at the beginning of the first cycle: . This is shown in the diagram in the problem statement.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* Complete the 'strangeCounter' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER t as parameter.
*/

function strangeCounter(t) {
// Write your code here

let time = 3;

while (2 * time - 2 <= t) {
time *= 2;
}

return time - (t - (time - 2));
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×