Question: HCL | 6th October | Online Assessments | Pythagorean Theorem | Reverse Bits
0
Entering edit mode

Question 1

Click here to Practice

Pythagorean Theorem

You are given two integers A and B. A represents a coordinate on the x-axis(0,A) and B represents a coordinate on the y-axis (B,0).

These are two coordinates points of a rihjt angled trisngle, the third point being the origin(0,0). You will be given N such triangles in the input. Find out and print the length of the hypotenuse of all the triangles.

Note:

  • The formula of the length of a hypotenuse = root(a^2 + b^2) where a and b represent the length of the other two sides of the triangle.
  • If the length of the hypotenuse is in decimal, round it to the next greater integer.

Input Format:

The input consist is given in the following format:

  • The first line contains an integer N denoting the number of trtiangles.
  • The next N lines contain two space-separated integers representing A and B respectively.

The input will be read from the STDIN by the candidate.

Output Format:

The output consists of N lines:

  • Each line representing the length of the hypotenuse of the ith triangle.

The output will be matched to the candidate's output printed on the STDOUT.

Constraints:

  • 1 <= A,B <= 10^9

Example

Input:

2
20 21
8 15

Output:

29
17

Explanation:

The test case contains two triangles:

First triangle:

The hypotenuse = root (20^2+21^2) = 29

Second triangle:

The hypotenuse = root (8^2+15^2) = 17

Question 2

Click here to Practice

Reverse Bits

Reverse the bits of an 32 bit unsigned integer A.

Problem Constraints 0 <= A <= 232

Input Format First and only argument of input contains an integer A.

Output Format Return a single unsigned integer denoting the decimal value of reversed bits.

Example 1

Input:

0

Output:

0

Explanation 1:

    00000000000000000000000000000000
    00000000000000000000000000000000

Example 2

Input:

3

Output:

3221225472

Explanation:

    00000000000000000000000000000011    
    11000000000000000000000000000000

Question 3

Click here to Practice

3.1

3.2

Problem Statement


You are given four integers a, b, c, and d. Find the sum of negative numbers out of these four numbers and print the same.


Note: Print 0 if no negative number is present.


Input Format:


The input consists offi single line:
The line contains four integers a, b, c, and d.
The input will be read from the STDIN by the candidate


Output Format:


Print the sum of negative numbers out of these four numbers.
The output will be matched to the candidate's output printed on the STDOUT


Constraints:


-103  a 103.
-103 b. 103
-103 c. 103
-103 d. 103

 

 

Input:


2 -3 -14 7


Output:


-17


Explanation:


Sum of '-3' and '-14' is '-17', thus output is '-17'.


Sample input


-1 -10 2 -25


Sample Output


- 36


Instructions :

 

  • Program should take input from standard input and print output to standard output.
  • Your code is judged by  an automated system, do not write any additional welcome/greeting messages.
  • "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.
  • Additional score will be given for writing optimized code both in terms of memory and execution time
 
 
0
Entering edit mode

Question 1

Overview

  • Given two integers A, B where coordinators A represent the coordinate of the x-axis (0, A) and B represents the coordination of the y-axis (B,0).
    • Given that these two coordinates A and B are points on a right-angled triangle state and the origin being (0,0).
    • We need to print the length of the hypotenuse triangle.

Solution

  • For finding the length of the hypotenuse of the triangle we need to know the formula that is the square root of( A^2+B^2).
    • Another thing in the notes we are given is that length can be in decimal too. So we need to round to the next greater integer.
    • For C++ we can simply use inbuilt function of maths header file that is hypot(adjacent,opposite) but for those who code in java or c++ can easily compute the length by hypotenuse= Math.sqrt((adjacent*adjacent)+(opposite*opposite)).
  • Time Complexity: O(1).
ADD COMMENTlink 2.2 years ago Akshay Sharma 990
0
Entering edit mode

Question 2

Overview

  • We are given an integer N of 32 bits unsigned int
    • In some languages like java there is no concept of unsigned int So in that case both input and output will be signed integer type only
    • we have to reverse the bits of 32 bits unsigned int
    • After that, we have to return the decimal value of the reverse bits

Solution

  • The idea is to keep putting set bits of the N in N_reverse until N becomes zero.
  • After N becomes zero, shift the remaining bits of N_reverse.
  • N is stored using 8 bits and N will be 00000110.
  • After the loop you will get N_reverse as 00000011.
  • Now you need to left shift N_reverse 5 more times and you get the exact reverse that is 01100000.

    ` 
    
      while (num) {
      reverse_num &lt;&lt;= 1;
      reverse_num |= num &amp; 1;
      num &gt;&gt;= 1;
      count--;
     }`
    
  • time complexity :(logN)

ADD COMMENTlink 2.2 years ago Akshay Sharma 990
0
Entering edit mode

Question 3

Overview

  • Given 4 integer values either negative or positive.
  • we need to return the sum of the negative value only.

Solution

  • Solution is quite easier for each value of a,b,c, and d check if the value is negative or positive.
  • If the value is negative maintain a sum variable initialized to 0 and all negative values one by one. If the value of any integer among 4 is positive ignore them.
  • Time complexity : 0(1)
ADD COMMENTlink 2.2 years ago Akshay Sharma 990

Login before adding your answer.

Similar Posts
Loading Similar Posts