Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Bitwise operator in C Hackerrank Solution

 Objective

This challenge will let you learn about bitwise operators in C.

Inside the CPU, mathematical operations like addition, subtraction, multiplication and division are done in bit-level. To perform bit-level operations in C programming, bitwise operators are used which are explained below.

  • Bitwise AND operator & The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. It is denoted by &.
  • Bitwise OR operator | The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. It is denoted by |.
  • Bitwise XOR (exclusive OR) operator ^ The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by .

For example, for integers 3 and 5,

3 = 00000011 (In Binary)
5 = 00000101 (In Binary)

AND operation        OR operation        XOR operation
  00000011             00000011            00000011
& 00000101           | 00000101          ^ 00000101
  ________             ________            ________
  00000001  = 1        00000111  = 7       00000110  = 6

Task
Given set s={1,2,3,…,n} , find:

  • the maximum value of a&b  which is less than a given integer k, where a and b (where a<b) are two integers from set s.
  • the maximum value of a|b which is less than a given integer k, where a and b (where a<b) are two integers from set s.
  • the maximum value of  which is less than a given integer a^b , where a and b (where a<b) are two integers from set s.

Input Format

The only line contains 2 space-separated integers, n and k, respectively.

Constraints

  • < n < 103
  • < k < n

Output Format

  • The first line of output contains the maximum possible value of  a&b.
  • The second line of output contains the maximum possible value of a|b .
  • The second line of output contains the maximum possible value of a^b.

Sample Input 0

5 4

Sample Output 0

2
3
3

Explanation 0

n = 5,k = 4

s={1,2,3,4,5}

All possible values of a and b are:

  1. a=1,b=2;a&b = 0;a|b =3;a^b=3;
  2. a=1,b=3;a&b = 1;a|b =3;a^b=2;
  3. a=1,b=4;a&b = 0;a|b =5;a^b=5;
  4. a=1,b=5;a&b = 1;a|b =5;a^b=4;
  5. a=1,b=3;a&b = 2;a|b =3;a^b=1;
  6. a=2,b=4;a&b = 0;a|b =6;a^b=6;
  7. a=2,b=5;a&b = 0;a|b =7;a^b=7;
  8. a=3,b=4;a&b = 0;a|b =7;a^b=7;
  9. a=3,b=5;a&b = 1;a|b =7;a^b=6;
  10. a=4,b=5;a&b = 4;a|b =5;a^b=1;
    • The maximum possible value of a&b that is also <(k=4) is 2 , so we print 2 on first line.
    • The maximum possible value of a|b that is also <(k=4) is 3, so we print 3 on second line.
    • The maximum possible value of a^b that is also <(k=4) is 3, so we print  3 on third line.

Code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.


void calculate_the_maximum(int n, int k) {
  //Write your code here.
   printf("%d\n",((k-1)|k)<=n? (k-1):(k-2));
    printf("%d\n",((k-1)&(k-2))==0? (k==3?0:(k-2)) : (k-1) );
    printf("%d\n", k-1);
}

int main() {
    int n, k;
  
    scanf("%d %d", &n, &k);
    calculate_the_maximum(n, k);
 
    return 0;
}

Comments