Algorithm


Problem Name: C++ Variadics

Problem Link: https://www.hackerrank.com/challenges/cpp-variadics/problem?isFullScreen=true

In this HackerRank Functions in C++ programming problem solutio

A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). To read more about parameter pack, click here.

Create a template function named reversed_binary_value. It must take an arbitrary number of bool values as template parameters. These booleans represent binary digits in reverse order. Your function must return an integer corresponding to the binary value of the digits represented by the booleans. For example: reversed_binary_value<0,0,1>() should return 4.

Input Format

The first line contains an integer, t , the number of test cases. Each of the t subsequent lines contains a test case. A test case is described as 2 space-separated integers, x and y , respectively.

  • x is the value to compare against.
  • y represents the range to compare: 64 * y to 64 * y + 63

Constraints

  • 0 <= x <= 65535
  • 0 <= y <= 1023
  • The number of template parameters passed to reversed_binary_value will be <= 16

Output Format

Each line of output contains 64 binary characters (i.e., 0 's and 1's). Each character represents one value in the range. The first character corresponds to the first value in the range. The last character corresponds to the last value in the range. The character is 1 if the value in the range matches X ; otherwise, the character is 0.

Sample Input

2
65 1
10 0

Sample Output

0100000000000000000000000000000000000000000000000000000000000000
0000000000100000000000000000000000000000000000000000000000000000

 

 

 

 

 

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <vector>
#include "math.h"
#include <initializer_list>
#include <stdarg.h>

template  < bool...digits>
int reverse(int count,...)
{
    va_list ap;
    int j;
    double sum = 0;
    va_start(ap, count); /* Requires the last fixed parameter (to get the address) */
    for (j = 0; j  <  count; j++)
    {
        sum += va_arg(ap, int)*pow(2,j); /* Increments ap to the next argument. */
    }
    //cout<<sum<<endl;
    va_end(ap);
    return sum;
}

template  < bool...digits>
int reversed_binary_value(...)
{
    return reverse(sizeof...(digits), digits...);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 65 1 10 0

Output

x
+
cmd
0100000000000000000000000000000000000000000000000000000000000000 0000000000100000000000000000000000000000000000000000000000000000
Advertisements

Demonstration


Previous
[Solved] C++ Class Template Specialization in C++ solution in Hackerrank - Hacerrank solution C++
Next
[Solved] Bit Array in C++ solution in Hackerrank - Hacerrank solution C++