2

HackerRank Bitwise Operators

In this challenge, you will use logical bitwise operators. All data is stored in its binary representation. The logical operators, and C language, use 1 to represent true and 0 to represent false. The logical operators compare bits in two numbers and return true or false, 0 or 1, for each bit compared.

  • 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

You will be given an integer n, and a threshold, k. For each number i from 1 through n, find the maximum value of the logical and, or and xor when compared against all integers through n that are greater than i. Consider a value only if the comparison returns a result less than k [else 0].
Print the results of the and, or and exclusive or comparisons on separate lines, in that order.

Example
n = 3
k = 3

The results of the comparisons are below:

a b   and or xor
1 2   0   3  3
1 3   1   3  2
2 3   2   3  1

For the and comparison, the maximum is 2. For the or comparison, none of the values is less than k, so the maximum is 0. For the xor comparison, the maximum value less than k is 2. The function should print:

2
0
2

Function Description

Complete the calculate_the_maximum function in the editor below.

calculate_the_maximum has the following parameters:

  • int n: the highest number to consider
  • int k: the result of a comparison must be lower than this number to be considered

Prints

Print the maximum values for the and, or and xor comparisons, each on a separate line.

Input Format

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

Constraints

  • 2 <= n <= 103
  • 2 <= k <= n

Sample Input 0

5 4

Sample Output 0

2
3
3

So This is My Answer

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


void calculate_the_maximum(int n, int k) {
  int m1=0,m2=0,m3=0;
  for (int x=1; x<n; x++){
      for (int y=2; y<=n; y++){
          //and
          int a=x&y;
          if((a>m1) && (a<k)){
            m1=a;
          };
          //or
          int b=x|y;
          if((b>m2) && (b<k)){
            m2=b;
          };
          //xor
          int c=x^y;
          if((c>m3) && (c<k)){
            m3=c;
          };
      }
  }
  
  printf("%d \n",m1);
  printf("%d \n",m2);
  printf("%d \n",m3);
}

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

And My Output is

3 
3 
3 

Expected Output

2
3
3

What is the mistake of my code?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

3 Answers3

0

This was my answer I first converted to binary then converted it back

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


    void calculate_the_maximum(int n, int k)
    {
        int i,j,a,b;
        int max_and = 0;
        int max_or = 0;
        int max_xor = 0;
        for (i = 1; i < n+1; i++)
        {
            for (j = 1; j < n+1; j++)
            {
                if (j>i)
                {
                    a = i;
                    b = j;
                    int count_1 = 0;
                    int sum_1 = 0;
                    int count_2 = 0;
                    int sum_2 = 0;

                    while (!(a==0))
                    {
                        sum_1 += (a%2)*(pow(10,count_1));
                        a = a/2;
                        count_1++;

                    }
                    while (!(b==0))
                    {
                        sum_2 += (b%2)*(pow(10,count_2));
                        b = b/2;
                        count_2++;
                    }
                    int sum_3 = 0;
                    int sum_4 = 0;
                    int sum_5 = 0;
                    int num_1 = 0;
                    int num_2 = 0;
                    int num_3 =0;
                    int count_3 = 0;
                    while (!((sum_1 == 0)&&(sum_2 == 0)))
                    {
                        if ((sum_1%10 == 1)&&(sum_2%10 == 1))
                        {
                            sum_3 += pow(10,count_3);

                        }
                        if ((sum_1%10 == 1)||(sum_2%10 == 1))
                        {
                            sum_4 += pow(10,count_3);
                        }
                        if (((sum_1%10 == 1)&&(sum_2%10 == 0))||((sum_1%10 == 0)&&(sum_2%10 == 1)))
                        {
                            sum_5 += pow(10,count_3);
                        }
                        sum_1 = sum_1/10;
                        sum_2 = sum_2/10;
                        count_3++;
                    }
                    int count_4 = 0;
                    while (!(sum_3 == 0))
                    {
                        num_1 += (sum_3%10)*(pow(2,count_4));
                        sum_3 = sum_3/10;
                        count_4++;
                    }
                    int count_5 = 0;
                    while (!(sum_4 == 0))
                    {
                        num_2 += (sum_4%10)*(pow(2,count_5));
                        sum_4 = sum_4/10;
                        count_5++;
                    }
                    int count_6 = 0;
                    while (!(sum_5 == 0))
                    {
                        num_3 += (sum_5%10)*(pow(2,count_6));
                        sum_5 = sum_5/10;
                        count_6++;
                    }

                    if ((max_and < num_1)&&(num_1<k))
                    {
                        max_and = num_1;


                    }
                    if ((max_or < num_2)&&(num_2<k))
                    {
                        max_or = num_2;

                    }
                    if ((max_xor < num_3)&&(num_3<k))
                    {
                        max_xor = num_3;
                    }
                }
            }
        }
        printf("%d\n%d\n%d",max_and,max_or,max_xor);
    }

    int main() {
        int n, k;

        scanf("%d %d", &n, &k);
        if (!((n < 2)||(n > 1000)||(k < 2)||(k >n)))
        {
            calculate_the_maximum(n, k);
            return 0;
        }
        else
        {
            printf("Error!\n");
            return 1;
        }
    }
0

You shouldn't initialize "y" at 2. In your case, i and y can be the same values. when i = 2, y can be equal to 2. But you cannot do this for the question. Start y from i+1.

    void calculate_the_maximum(int n, int k)
    {
      int max_or = 0;
      int max_and = 0;
      int max_xor = 0;

      int temp_xor = 0;
      int temp_and = 0;
      int temp_or = 0;
      //or = xor + and;
     for(int i = 1 ; i < n ; i++ )
     {
         for(int j = i+1 ; j <=n ; j++)
         {
              temp_xor = i ^ j;
              temp_and = i & j;
              temp_or = temp_xor + temp_and;

              if (temp_xor > max_xor && temp_xor < k) max_xor = temp_xor;
              if (temp_and > max_and && temp_and < k) max_and = temp_and;
              if (temp_or > max_or && temp_or < k) max_or = temp_or;
         }

    }

    //print
    cout << max_and << endl << max_or << endl << max_xor;
}
    
0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>



void calculate_the_maximum(int n, int k) {
  int m1=0,m2=0,m3=0;
  for (int x=1; x<n; x++){
      for (int y=x+1; y<=n; y++){
          //and
          int a= x & y;
          if((a>m1) && (a<k)){
            m1=a;
          };
          //or
          int b=x|y;
          if((b>m2) && (b<k)){
            m2=b;
          };
          //xor
          int c=x^y;
          if((c>m3) && (c<k)){
            m3=c;
          };
      }
  }
  
  printf("%d \n",m1);
  printf("%d \n",m2);
  printf("%d \n",m3);
}

int main() {
    int n, k;
  
    scanf("%d %d", &n, &k);
    calculate_the_maximum(n, k);
 
    return 0;
}
Mani Teja
  • 16
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 12 '22 at 04:50