반응형

For a = 2 and b = 7, the output should be
rangeBitCount(a, b) = 11.

Given a = 2 and b = 7 the array is: [2, 3, 4, 5, 6, 7]. Converting the numbers to binary, we get [10, 11, 100, 101, 110, 111], which contains 1 + 2 + 1 + 2 + 2 + 3 = 11 1s.

Input/Output

[execution time limit] 0.5 seconds (cpp)

[input] integer a

Guaranteed constraints:
0 ≤ a ≤ b.

[input] integer b

Guaranteed constraints:
a ≤ b ≤ 10.

 

code>>

 

int rangeBitCount(int aint b) {

    auto count = 0;

    for (int i = ai <= b; ++i)

    {

        std::bitset<4bit(i);

        count += bit.count();

    }

    return count;

}

 

반응형

'알고리즘 > codefights' 카테고리의 다른 글

18>arrayPacking  (0) 2021.04.17
17>Kill K-th Bit  (0) 2021.04.17
Challenge>formatString  (0) 2019.03.06
Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
반응형

You are given an array of up to four non-negative integers, each less than 256.

Your task is to pack these integers into one number M in the following way:

The first element of the array occupies the first 8 bits of M;
The second element occupies next 8 bits, and so on.
Return the obtained integer M.

Note: the phrase "first bits of M" refers to the least significant bits of M - the right-most bits of an integer. For further clarification see the following example.

Example

For a = [24, 85, 0], the output should be
arrayPacking(a) = 21784.

An array [24, 85, 0] looks like [00011000, 01010101, 00000000] in binary.
After packing these into one number we get 00000000 01010101 00011000 (spaces are placed for convenience), which equals to 21784.

 

code>>

 

int arrayPacking(vector<inta) {

    auto result = 0;

    for(int i=0ia.size(); ++i)

    {

        result += a[i] << i * 8;

    }

    return result;

}

 

반응형

'알고리즘 > codefights' 카테고리의 다른 글

19>rangeBitCount  (0) 2021.04.17
17>Kill K-th Bit  (0) 2021.04.17
Challenge>formatString  (0) 2019.03.06
Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
반응형

In order to stop the Mad Coder evil genius you need to decipher the encrypted message he sent to his minions. The message contains several numbers that, when typed into a supercomputer, will launch a missile into the sky blocking out the sun, and making all the people on Earth grumpy and sad.

You figured out that some numbers have a modified single digit in their binary representation. More specifically, in the given number n the kth bit from the right was initially set to 0, but its current value might be different. It's now up to you to write a function that will change the kth bit of n back to 0.In order to stop the Mad Coder evil genius you need to decipher the encrypted message he sent to his minions. The message contains several numbers that, when typed into a supercomputer, will launch a missile into the sky blocking out the sun, and making all the people on Earth grumpy and sad.

You figured out that some numbers have a modified single digit in their binary representation. More specifically, in the given number n the kth bit from the right was initially set to 0, but its current value might be different. It's now up to you to write a function that will change the kth bit of n back to 0.

Example

For n = 37 and k = 3, the output should be
killKthBit(n, k) = 33.

3710 = 1001012 ~> 1000012 = 3310.

For n = 37 and k = 4, the output should be
killKthBit(n, k) = 37.

The 4th bit is 0 already (looks like the Mad Coder forgot to encrypt this number), so the answer is still 37.

 

code>>

int killKthBit(int nint k) {

  return n & ~(1 << (k - 1));

}



 

반응형

'알고리즘 > codefights' 카테고리의 다른 글

19>rangeBitCount  (0) 2021.04.17
18>arrayPacking  (0) 2021.04.17
Challenge>formatString  (0) 2019.03.06
Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
반응형

Remove extra white-spaces from the given string.


Example


For input = " abc   a aa  a a ", the output should be

formatString(input) = "abc a aa a a".


code>>


string formatString(string i) {

    return i.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Aggregate((l,r)=> l+" "+r);

}



문자열 처리에 C++은 최악...


반응형

'알고리즘 > codefights' 카테고리의 다른 글

18>arrayPacking  (0) 2021.04.17
17>Kill K-th Bit  (0) 2021.04.17
Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
15>willYou  (0) 2019.02.24
반응형

Consider the Fibonacci sequence: 0 1 1 2 3 5 8 13 21 ...


We can see that 7 is the smallest 0-based index k for which F(k) has exactly 2 decimal digits.

What is the smallest index k for which F(k) has exactly n decimal digits?


Example


For n = 1, the output should be

fibonacciIndex(n) = 0;

For n = 2, the output should be

fibonacciIndex(n) = 7. 


code>>


int fibonacciIndex(int n) {

if (n == 1) return 0;

int f = 0, s = 1, t = 1, idx = 1;

while (std::to_string(t).size() != n)

{

t = f + s;

f = s;

s = t;

idx++;

}

return idx;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

17>Kill K-th Bit  (0) 2021.04.17
Challenge>formatString  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
15>willYou  (0) 2019.02.24
14>tennisSet  (0) 2019.02.24
반응형

You just bought a public transit card that allows you to ride the Metro for a certain number of days.


Here is how it works: upon first receiving the card, the system allocates you a 31-day pass, which equals the number of days in January. The second time you pay for the card, your pass is extended by 28 days, i.e. the number of days in February (note that leap years are not considered), and so on. The 13th time you extend the pass, you get 31 days again.


You just ran out of days on the card, and unfortunately you've forgotten how many times your pass has been extended so far. However, you do remember the number of days you were able to ride the Metro during this most recent month. Figure out the number of days by which your pass will now be extended, and return all the options as an array sorted in increasing order.


code>>


std::vector<int> metroCard(int lastNumberOfDays) {

    if(lastNumberOfDays == 30) return std::vector<int> { 31 };

    else if(lastNumberOfDays == 31) return std::vector<int> { 28, 30, 31 };

    else return std::vector<int> { 31 };

}



반응형

'알고리즘 > codefights' 카테고리의 다른 글

Challenge>formatString  (0) 2019.03.06
Challenge>fibonacciIndex  (0) 2019.03.06
15>willYou  (0) 2019.02.24
14>tennisSet  (0) 2019.02.24
13>arithmeticExpression  (0) 2018.05.20
반응형

Once Mary heard a famous song, and a line from it stuck in her head. That line was "Will you still love me when I'm no longer young and beautiful?". Mary believes that a person is loved if and only if he/she is both young and beautiful, but this is quite a depressing thought, so she wants to put her belief to the test.


Knowing whether a person is young, beautiful and loved, find out if they contradict Mary's belief.


A person contradicts Mary's belief if one of the following statements is true:


they are young and beautiful but not loved;

they are loved but not young or not beautiful.


code>>


bool willYou(bool young, bool beautiful, bool loved) {


    return (young & beautiful) ^ loved;

}



반응형

'알고리즘 > codefights' 카테고리의 다른 글

Challenge>fibonacciIndex  (0) 2019.03.06
16>metroCard  (0) 2019.02.24
14>tennisSet  (0) 2019.02.24
13>arithmeticExpression  (0) 2018.05.20
12>isInfiniteProcess  (0) 2018.05.19
반응형

In tennis, the winner of a set is based on how many games each player wins. The first player to win 6 games is declared the winner unless their opponent had already won 5 games, in which case the set continues until one of the players has won 7 games.


Given two integers score1 and score2, your task is to determine if it is possible for a tennis set to be finished with a final score of score1 : score2. 


code>>


bool tennisSet(int score1, int score2) {

if (score1 == 7 || score2 == 7)

return score1 == 7 && (score2 == 5 || score2 == 6) || score2 == 7 && (score1 == 5 || score2 == 6);

return score1 < 5 && score2 == 6 || score2 < 5 && score1 == 6;

}


테니스 룰을 몰라서 테스트 케이스 열어서 어거지로 맞춤

반응형

'알고리즘 > codefights' 카테고리의 다른 글

16>metroCard  (0) 2019.02.24
15>willYou  (0) 2019.02.24
13>arithmeticExpression  (0) 2018.05.20
12>isInfiniteProcess  (0) 2018.05.19
11>extraNumber  (0) 2018.05.19
반응형

Consider an arithmetic expression of the form a#b=c. Check whether it is possible to replace # with one of the four signs: +, -, * or / to obtain a correct expression.


Example


For a = 2, b = 3 and c = 5, the output should be

arithmeticExpression(a, b, c) = true.


We can replace # with a + to obtain 2 + 3 = 5, so the answer is true.


For a = 8, b = 2 and c = 4, the output should be

arithmeticExpression(a, b, c) = true.


We can replace # with a / to obtain 8 / 2 = 4, so the answer is true.


For a = 8, b = 3 and c = 2, the output should be

arithmeticExpression(a, b, c) = false.


8 + 3 = 11 ≠ 2;

8 - 3 = 5 ≠ 2;

8 * 3 = 24 ≠ 2;

8 / 3 = 2.(6) ≠ 2.

So the answer is false.


code>>


bool arithmeticExpression(int a, int b, int c) {

return a + b == c || a - b == c || (a / b == c && a % b == 0) || a * b == c;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

15>willYou  (0) 2019.02.24
14>tennisSet  (0) 2019.02.24
12>isInfiniteProcess  (0) 2018.05.19
11>extraNumber  (0) 2018.05.19
10>knapsackLight  (0) 2018.05.19
반응형

Given integers a and b, determine whether the following pseudocode results in an infinite loop


while a is not equal to b do

  increase a by 1

  decrease b by 1

Assume that the program is executed on a virtual machine which can store arbitrary long numbers and execute forever.


Example


For a = 2 and b = 6, the output should be

isInfiniteProcess(a, b) = false;

For a = 2 and b = 3, the output should be

isInfiniteProcess(a, b) = true. 


code>>


bool isInfiniteProcess(int a, int b) {

return ((a - b) & 1) || a > b;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

14>tennisSet  (0) 2019.02.24
13>arithmeticExpression  (0) 2018.05.20
11>extraNumber  (0) 2018.05.19
10>knapsackLight  (0) 2018.05.19
9>reachNextLevel  (0) 2018.05.19

+ Recent posts