![]() |
VOOZH | about |
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.
Examples:
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:
y < 2x log y < x // means if log y is less than x, then // we can print y as remainder.
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
57
Time Complexity: O(x)
Auxiliary Space: O(1)
This approach is called bitwise manipulation. Here are the steps:
The remainder of 14 when divided by 2^3 is 6. The remainder of 14 when divided by 2^4 is 14.
The time complexity: O(1)
The auxiliary space: O(1)