HackerRank Strange Counter Problem Solution
In this post, we will solve HackerRank Strange Counter Problem Solution.
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 × 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.
Sample Input
4
Sample Output
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:2 x 3 = 6. This is shown in the diagram in the problem statement.
Strange Counter C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long unsigned int cnt, t, val, diff;
val = cnt = t = diff = 0;
scanf("%lld", &t);
val = 3;
cnt = 1;
while(cnt <= t) {
diff = val - 1;
if ((cnt+diff) >= t) {
printf ("%lld\n", val - (t - cnt));
break;
} else {
cnt = cnt + diff + 1;
val *= 2;
}
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Strange Counter C++ Solution
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;(i)>=0;(i)--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
typedef long long ll;
int main(){
ll t; cin>>t;
ll ans=1;
ll sum=0;
while(true){
if(sum+ans*3>=t)break;
sum+=ans*3;
ans*=2;
}
ll cnt=t-sum;
cout<<(ans+1)*3-cnt-2<<endl;
return 0;
}
Strange Counter C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
private const int start = 3;
static void Main(string[] args)
{
long t = Convert.ToInt64(Console.ReadLine());
var point = GetStartPoint(t);
var distance = t + 2 - point;
var res = point - distance;
Console.WriteLine(res);
}
private static long GetStartPoint(long time) {
long res = start;
var compare = time + 2;
while (true)
{
if ((compare >= res) && (compare < res * 2)) {
break;
}
res = res * 2;
}
return res;
}
}
Strange Counter Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'strangeCounter' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts LONG_INTEGER t as parameter.
*/
public static long strangeCounter(long t) {
long v=4;
while(v<=t)
{
v=v*2+2;
}
return v-t;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
long t = Long.parseLong(bufferedReader.readLine().trim());
long result = Result.strangeCounter(t);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Strange Counter JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var t = parseInt(readLine());
console.log(kanter(t))
}
__find_kth_pivot_cache = []
function find_kth_pivot(k) {
if (k==0) {
return 1
}
if (__find_kth_pivot_cache[k]) {
return __find_kth_pivot_cache[k]
}
var retVal = find_kth_pivot(k-1) + 3*Math.pow(2,k-1)
__find_kth_pivot_cache[k] = retVal
return retVal
}
//returns m <= n
function find_k_m_lte_n(n) {
var k = 0;
var m = find_kth_pivot(0);
while(m <= n) {
k++
m = find_kth_pivot(k)
if (m > n) {
k--
m = find_kth_pivot(k);
break
}
};
var retVal = {
m: m,
k: k
};
// console.log(n + '=>' + retVal.m + ',' + retVal.k)
return retVal;
}
function calculate_pivot_value(k) {
if (k==0) {
return 3
} else {
return 2 * calculate_pivot_value(k-1)
}
}
function kanter(n) {
var k_and_m = find_k_m_lte_n(n);
var k = k_and_m.k
var m = k_and_m.m
var val_m = calculate_pivot_value(k)
retVal = val_m - (n - m)
//console.log('n', n, 'm',m)
//console.log('n = ', n)
//console.log('f(m) = ' + val_m)
//console.log('n = ', n, 'f(n) = ' + retVal)
return retVal
}
/*
kanter(1)
kanter(2)
kanter(3)
kanter(4)
kanter(5)
kanter(6)
kanter(7)
kanter(8)
kanter(9)
kanter(10)
kanter(20)
kanter(21)
kanter(22)
*/
Strange Counter Python Solution
tf = int(input())
t = []
t.append(1)
t.append(4)
t.append(10)
t.append(22)
t.append(46)
t.append(94)
t.append(190)
t.append(382)
t.append(766)
t.append(1534)
t.append(3070)
t.append(6142)
t.append(12286)
t.append(24574)
t.append(49150)
t.append(98302)
t.append(196606)
t.append(393214)
t.append(786430)
t.append(1572862)
t.append(3145726)
t.append(6291454)
t.append(12582910)
t.append(25165822)
t.append(50331646)
t.append(100663294)
t.append(201326590)
t.append(402653182)
t.append(805306366)
t.append(1610612734)
t.append(3221225470)
t.append(6442450942)
t.append(12884901886)
t.append(25769803774)
t.append(51539607550)
t.append(103079215102)
t.append(206158430206)
t.append(412316860414)
t.append(824633720830)
t.append(1000000000001)
nv = next(x[0] for x in enumerate(t) if x[1] > tf) - 1
#print(nv)
sum = 2*t[nv] + 2 - tf
print(sum)
Other Solutions
Модные заметки по подбору превосходных луков на любой день.
Заметки экспертов, новости, все новинки и мероприятия.
https://biiut.com/read-blog/79584