Given two positive integer x and y. we have to find the value of y mod 2x. That is remainder when y is divided by 2x.
Input : x = 3, y = 14 Output : 6 Explanation : 14 % 23 = 14 % 8 = 6. Input : x = 4, y = 14 Output : 14 Explanation : 14 % 24 = 14 % 16 = 14.
To solve this question we can use pow() and modulo operator and can easily find the remainder.
But there are some points we should care about:
- For higher value of x such that 2x is greater than long long int range, we can not obtain proper result.
- Whenever y < 2x the remainder will always be y. So, in that case we can restrict ourselves to calculate value of 2x as:
y < 2x log y < x // means if log y is less than x, then // we can print y as remainder.
- The maximum value of 2x for which we can store 2x in a variable is 263. This means for x > 63, y is always going to be smaller than 2x and in that case remainder of y mod 2x is y itself.
keeping in mind the above points we can approach this problem as :
if (log y < x) return y; else if (x < 63) return y; else return (y % (pow(2, x)))
Note: As python is limit free we can directly use mod and pow() function
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Find power of power under mod of a prime
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find unit digit of x raised to power y
- Find last five digits of a given five digit number raised to power five
- Check if a number can be expressed as x^y (x raised to power y)
- Print last k digits of a^b (a raised to power b)
- K-th digit in 'a' raised to power 'b'
- Number of digits in 2 raised to power n
- GCD of a number raised to some power and another number
- Minimum removals in a number to be divisible by 10 power raised to K
- Sum of absolute difference of all pairs raised to power K
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Check if given number is a power of d where d is a power of 2
- Compute power of power k times % m
- Sum of each element raised to (prime-1) % prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1