Midori and chocolates | Practice | GeeksforGeeks

Midori like chocolates and he loves to try different ones. There are N shops in a market labelled from 1 to N and each shop offers a different variety of chocolate. Midori starts from ith shop and goes ahead to each and every shop. But as there are too many shops Midori would like to know how many more shops he has to visit. You have been given L denoting number of bits required to represent N. You need to return the maximum number of shops he can visit.

Example 1:
Input: i = 2, L = 3
Output: 6
Explanation: There are 3 bits So N = 8
Hence midori can visit 8 - 2 = 6 more shops.

Example 2:
Input: i = 1, L = 2
Output: 3
Explanation: There are 2 bits so N = 4
Hence midori can visit 4 - 1 = 3 more shops.

Your Task:  
You dont need to read input or print anything. Complete the function leftShops() which takes i and L as input parameter and returns the maximum number of shops that midori can visit.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Constraints:
1<= i <=N
1<= L <=30
0
rb0014 days ago
class Solution {   static long leftShops(long i, long L){       // code here       //  here you are using bitwise Operator        // we are shifting the 1 to L       // beacause 1 is the first shop and        //when we will shift it it by L ,       // we will reach at the last shop or N th shop        // because she is at ith shop so we will subtract her position by N;       int maxShift = 1<< L;       return maxShift -i;   }}
0
yogeshnarendratiwari1 month ago
Simple one line c++ code
long long int leftShops(long long int i, long long int L){       return ((1<<L)-i);   }
0
mankesh0161 month ago
return (1<<L)-i;
0
ankitparashxr3 months ago
long res =0;       long bit = (long)Math.pow(2,l);       return (bit-i);
0
saisb19193 months ago
 static long leftShops(long i, long L){       // code here       L=1<<L;       i=L-i;       return i;           }
0
shreyanshikunda3 months ago
 Python
 def leftShops (self, i, L):       N=2**L       v=N-i       return (v)       
0
vishallodha20094 months ago
C++
long long int leftShops(long long int i, long long int L){
        // code here
        return ((1<<L) - i);
    }
0
vasasainivas184 months ago
Python One Line Code
 
class Solution:   def leftShops (self, i, L):       return(2**L-i) 
0
surabhichoubey554 months ago
class Solution { public:   long long int leftShops(long long int i, long long int L){       // code here       long long int ans1 = pow(2,L);       long long int output = ans1-i;       return output;   }}; 
0
pranshujain0111Premium4 months ago
This is the code in which we find number of bit required to represent the number as we have given the how many bit is required and we find total bits by taking power of 2 and then simply minus the required number.
long long int leftShops(long long int i, long long int L){

        // code here

        long long int ans= pow(2,L);
        return (ans-i);
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.