Question: BlackRock OA | Minimum Steps | Shopping Spree | 2023
4
Entering edit mode

Question 1. 
 

Minimum steps

You have a starting number X. You can perform one of the following operations on it any number of times:

  • Multiply X by 2
  • Divide X by 2. (X must be even to perform this operation)

Find out the minimum number of steps required to reach Y starting from X, or print -1 if it is not possible

Function description

Complete the function solve(). This function takes the following 2 parameters and returns the required
answer.

  • X: Represents the value of X
  • Y: Represents the value of Y

Input format for custom testing

Note: Use this input format if you are testing against custom input or writing code in a  language where we don't provide boilerplate code

  • The first line contains T, which represents the number of test cases.
  • For each test case:
    • The first line contains a single integer X denoting the value of X.
    • The second line contains a single integer Y denoting the value of Y.

Output format

For each test case, print a single line containing a single integer representing the answer.

Constraints

1 ≤ T ≤ 10^5

1 ≤ X, Y ≤ 10^18

Sample input         Sample Output

2                           2

24                          -1

6

24

16

 

Explanation

The first test case is explained in the statement.

For the second test case

Given

  • X = 24
  • Y = 16

Approach

• It is impossible to reach 16 by performing operations, so the answer is -1.

Sample Input 1                          Sample Output 1

2                                             2                                                 
24                                           -1
6
24
16                  


Sample Input 2                          Sample Output 2

100                                            16
50                                             26
3276800                                        2
6308233216                                     21
94                                             5
12                                             4
48                                             18
55                                             23
115343360                                      26 
2464                                           12


Question 2.
 

Shopping Spree

Steve goes to a shop to buy N candies. After his purchase, the shopkeeper offers him a crazy refund on his bill.

The shopkeeper tells Steve that he can select any of the [N/2] candies and the shopkeeper will refund the sum of the square of the prices of those candies.

Now, before buying the candies, Steve can perform an infinite number of the following operations:

  • Select any pair of two candies
  •  Let's say their cost is A and B respectively.
  •  Update the cost of the first candy as (A OR B)
  •  Update the cost of the second cany to (A AND B)

Now, Steve wonders what is the maximum refund he can get if the number and cost of the candies are already decided. Help Steve get the maximum benefit of this craziness.

Since the answer can be too great, output the answer modulo 10^9 +7

Function description

Complete the function solution(). This function takeş 2 parameters and returns the answer:

  • N: Represents the number of candies Steve purchases
  • candyPrice: Represents the array of size N representing the N candies' cost.


Input format for custom testing

Note:Use this input format if you are testing against custom input or writing code in a language where we don't provide boilerplate code.

  • The first line contains an integer representing N, the number of candies. 
  • The second line contains N integers denoting the cost of those N candies.

Output format
In a single line, print the maximum refund Steve can get

Constraints
1≤N≤ 10^5
1≤candyPrice ≤ 10^8 

Sample Input        Sample Output

5                       98
1 6 7 5 3

Explanation

Let's perform the operation on the cost of Candy 1 and Candy 2.
The cost of Candy 1becomes (3 | 6)= 7.
The cost of Candy 2becomes (3 & 6) = 2.
Now, we can pick Candy 1 and Candy 3, and ask for the sum of squares of their prices as a refund.

That is 7^2 + 7^2 = 49 +49 = 98

The following test cases are the actual test cases of this question that may be used to evaluate your submission.

 

Sample input 1               Sample output 1
                                
5                                 98
3 6 7 5 3

 

 Sample input 2                                      Sample Output 2
 
    10                                                     56075
    84 87 78 16 94 36 87 93 50 22



Note:

Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your cod these hidden test cases to solve the problem statement.

Scoring
Score is assigned if any testcase passes

Allowed Languages
C#, Java 8, Java 14, JavaScript(Node.js) 


3
Entering edit mode
 public static void main(String args[]) throws IOException {
        int t=in.nextInt();
        while(t-->0){
            long x=in.nextLong();
            long y=in.nextLong();
            if(x>y){
                if(x%y!=0)print(-1);
                else{
                    print(f((long)(x/y)));
                }
            }
            else{
                if(y%x!=0)print(-1);
                else{
                    print(f((long)(y/x)));
                }
            }
        }
    }
    public static long f(long p){
        for(long i=0;i<63;i++){
            if((long)(1l<<i)==p)return i;
        }
        return -1;
    }

Q1 solution can be this

ADD COMMENTlink 17 months ago ROHIT KONER • 120
1
Entering edit mode

how to approach the second problem?

ADD COMMENTlink 17 months ago Shivansh Gupta • 10
0
Entering edit mode

2nd approach anyone ?

 

ADD COMMENTlink 16 months ago mwa_coder • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts