Calculate the value of 2 raised to the power of twice the binary representation of N | Java program to implement the above approach ; Function to find the value of power ( X , Y ) in O ( log Y ) ; Stores power ( X , Y ) ; Update X ; Base Case ; Calculate power ( X , Y ) ; If Y is an odd number ; Update res ; Update Y ; Update X ; Function to calculate ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Stores binary representation of n ; Stores power of 10 ; Calculate the binary representation of n ; If n is an odd number ; Update X ; Update pow_10 ; Update n ; Double the value of X ; Stores the value of ( 2 ^ ( 2 * x ) ) % ( 10 ^ 9 + 7 ) ; Driver code
import java . util . * ; class GFG { static int M = 1000000007 ; static int power ( int X , int Y ) { int res = 1 ; X = X % M ; if ( X == 0 ) return 0 ; while ( Y > 0 ) { if ( ( Y & 1 ) != 0 ) { res = ( res * X ) % M ; } Y = Y >> 1 ; X = ( X * X ) % M ; } return res ; } static int findValue ( int n ) { int X = 0 ; int pow_10 = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) { X += pow_10 ; } pow_10 *= 10 ; n /= 2 ; } X = ( X * 2 ) % M ; int res = power ( 2 , X ) ; return res ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( findValue ( n ) ) ; } }