codeparrot/xlcost-text-to-code huggingface dataset:
Remove nodes on root to leaf paths of length < K | C ++ program to remove nodes on root to leaf paths of length < K ; New node of a tree ; Utility method that actually removes the nodes which are not on the pathLen >= k . This method can change the root as well . ; Base condition ; Traverse the tree in postorder fashion so that if a leaf node path length is shorter than k , then that node and all of its descendants till the node which are not on some other path are removed . ; If root is a leaf node and it 's level is less than k then  remove this node.  This goes up and check for the ancestor nodes also for the  same condition till it finds a node which is a part of other  path(s) too. ; Return root ; ; Method which calls the utitlity method to remove the short path nodes . ; Method to print the tree in inorder fashion . ; Driver method .
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } Node * removeShortPathNodesUtil ( Node * root , int level , int k ) { if ( root == NULL ) return NULL ; root -> left = removeShortPathNodesUtil ( root -> left , level + 1 , k ) ; root -> right = removeShortPathNodesUtil ( root -> right , level + 1 , k ) ; if ( root -> left == NULL && root -> right == NULL && level < k ) { delete root ; return NULL ; } return root ; } Node * removeShortPathNodes ( Node * root , int k ) { int pathLen = 0 ; return removeShortPathNodesUtil ( root , 1 , k ) ; } void printInorder ( Node * root ) { if ( root ) { printInorder ( root -> left ) ; cout << root -> data << " ▁ " ; printInorder ( root -> right ) ; } } int main ( ) { int k = 4 ; Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> left -> left -> left = newNode ( 7 ) ; root -> right -> right = newNode ( 6 ) ; root -> right -> right -> left = newNode ( 8 ) ; cout << " Inorder ▁ Traversal ▁ of ▁ Original ▁ tree " << endl ; printInorder ( root ) ; cout << endl ; cout << " Inorder ▁ Traversal ▁ of ▁ Modified ▁ tree " << endl ; Node * res = removeShortPathNodes ( root , k ) ; printInorder ( res ) ; return 0 ; }
Remove duplicates from a given string | CPP program to remove duplicate character from character array and print in sorted order ; create a set using string characters excluding ' \0' ; print content of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * removeDuplicate ( char str [ ] , int n ) { set < char > s ( str , str + n - 1 ) ; int i = 0 ; for ( auto x : s ) str [ i ++ ] = x ; str [ i ] = ' \0' ; return str ; } int main ( ) { char str [ ] = " geeksforgeeks " ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; cout << removeDuplicate ( str , n ) ; return 0 ; }
Check if K can be obtained by performing arithmetic operations on any permutation of an Array | C ++ program for the above approach ; Function that finds the string of possible combination of operations ; If only one number left then check for result ; If resultant value is K ; Else return 0 ; Choose all combination of numbers and operators and operate them ; Choose the first two and operate it with ' + ' ; Place it to 0 th position ; Place ( n - 1 ) th element on 1 st position ; Evaluate the expression with current combination ; Now , we have N - 1 elements ; Try ' - ' operation ; Evaluate the expression with current combination ; Try reverse ' - ' ; Evaluate the expression with current combination ; Try ' * ' operation ; Evaluate the expression with current combination ; Try ' / ' ; Evaluate the expression with current combination ; Try reverse ' / ' ; Evaluate the expression with current combination ; Backtracking Step ; Return 0 if there doesnt exist any combination that gives K ; Function that finds the possible combination of operation to get the resultant value K ; Store the resultant operation ; Function Call ; Driver Code ; Given array arr [ ] ; Resultant value K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool find ( vector < double > & v , int n , double dest , string s ) { if ( n == 1 ) { if ( abs ( v [ 0 ] - dest ) <= 0.0000001 ) { cout << s + to_string ( int ( dest ) ) << " ▁ " ; return 1 ; } return 0 ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { double a = v [ i ] , b = v [ j ] ; string p = s ; v [ i ] = a + b ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( a ) ) + ' + ' + to_string ( int ( b ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; s = p ; v [ i ] = a - b ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( a ) ) + ' - ' + to_string ( int ( b ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; s = p ; v [ i ] = b - a ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( b ) ) + ' - ' + to_string ( int ( a ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; s = p ; v [ i ] = a * b ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( a ) ) + ' * ' + to_string ( int ( b ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; s = p ; if ( b != 0 ) { v [ i ] = a / b ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( a ) ) + ' / ' + to_string ( int ( b ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; } s = p ; if ( a != 0 ) { v [ i ] = b / a ; v [ j ] = v [ n - 1 ] ; s = ( s + to_string ( int ( b ) ) + ' / ' + to_string ( int ( a ) ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return 1 ; } s = p ; v [ i ] = a ; v [ j ] = b ; } } return 0 ; } void checkPossibleOperation ( vector < double > & arr , double K ) { string s = " " ; if ( ! find ( arr , arr . size ( ) , K , s ) ) { cout << " - 1" ; } } int main ( ) { vector < double > arr = { 2 , 0 , 0 , 2 } ; double K = 4 ; checkPossibleOperation ( arr , K ) ; return 0 ; }
Sudoku | Backtracking |  ; N is the size of the 2D matrix N * N ; A utility function to print grid ; Checks whether it will be legal to assign num to the given row , col ; Check if we find the same num in the similar row , we return false ; Check if we find the same num in the similar column , we return false ; Check if we find the same num in the particular 3 * 3 matrix , we return false ; Takes a partially filled - in grid and attempts to assign values to all unassigned locations in such a way to meet the requirements for Sudoku solution ( non - duplication across rows , columns , and boxes ) ; Check if we have reached the 8 th row and 9 th column ( 0 indexed matrix ) , we are returning true to avoid further backtracking ; Check if column value becomes 9 , we move to next row and column start from 0 ; Check if the current position of the grid already contains value > 0 , we iterate for next column ; Check if it is safe to place the num ( 1 - 9 ) in the given row , col -> we move to next column ; Assigning the num in the current ( row , col ) position of the grid and assuming our assigned num in the position is correct ; Checking for next possibility with next column ; Removing the assigned num , since our assumption was wrong , and we go for next assumption with diff num value ; Driver Code ; 0 means unassigned cells
#include <iostream> NEW_LINE using namespace std ; #define N  9 NEW_LINE void print ( int arr [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) cout << arr [ i ] [ j ] << " ▁ " ; cout << endl ; } } bool isSafe ( int grid [ N ] [ N ] , int row , int col , int num ) { for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ row ] [ x ] == num ) return false ; for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ x ] [ col ] == num ) return false ; int startRow = row - row % 3 , startCol = col - col % 3 ; for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) if ( grid [ i + startRow ] [ j + startCol ] == num ) return false ; return true ; } bool solveSuduko ( int grid [ N ] [ N ] , int row , int col ) { if ( row == N - 1 && col == N ) return true ; if ( col == N ) { row ++ ; col = 0 ; } if ( grid [ row ] [ col ] > 0 ) return solveSuduko ( grid , row , col + 1 ) ; for ( int num = 1 ; num <= N ; num ++ ) { if ( isSafe ( grid , row , col , num ) ) { grid [ row ] [ col ] = num ; if ( solveSuduko ( grid , row , col + 1 ) ) return true ; } grid [ row ] [ col ] = 0 ; } return false ; } int main ( ) { int grid [ N ] [ N ] = { { 3 , 0 , 6 , 5 , 0 , 8 , 4 , 0 , 0 } , { 5 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 8 , 7 , 0 , 0 , 0 , 0 , 3 , 1 } , { 0 , 0 , 3 , 0 , 1 , 0 , 0 , 8 , 0 } , { 9 , 0 , 0 , 8 , 6 , 3 , 0 , 0 , 5 } , { 0 , 5 , 0 , 0 , 9 , 0 , 6 , 0 , 0 } , { 1 , 3 , 0 , 0 , 0 , 0 , 2 , 5 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 4 } , { 0 , 0 , 5 , 2 , 0 , 6 , 3 , 0 , 0 } } ; if ( solveSuduko ( grid , 0 , 0 ) ) print ( grid ) ; else cout << " no ▁ solution ▁ exists ▁ " << endl ; return 0 ; }
Subset Sum | Backtracking |  ; prints subset found ; qsort compare function ; inputs s - set vector t - tuplet vector s_size - set size t_size - tuplet size so far sum - sum so far ite - nodes count target_sum - sum to be found ; We found sum ; constraint check ; Exclude previous added item and consider next candidate ; constraint check ; generate nodes along the breadth ; consider next level node ( along depth ) ; Wrapper that prints subsets that sum to target_sum ; sort the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ARRAYSIZE ( a )  (sizeof(a))/(sizeof(a[0])) NEW_LINE static int total_nodes ; void printSubset ( int A [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { cout << " ▁ " << A [ i ] ; } cout << " STRNEWLINE " ; } int comparator ( const void * pLhs , const void * pRhs ) { int * lhs = ( int * ) pLhs ; int * rhs = ( int * ) pRhs ; return * lhs > * rhs ; } void subset_sum ( int s [ ] , int t [ ] , int s_size , int t_size , int sum , int ite , int const target_sum ) { total_nodes ++ ; if ( target_sum == sum ) { printSubset ( t , t_size ) ; if ( ite + 1 < s_size && sum - s [ ite ] + s [ ite + 1 ] <= target_sum ) { subset_sum ( s , t , s_size , t_size - 1 , sum - s [ ite ] , ite + 1 , target_sum ) ; } return ; } else { if ( ite < s_size && sum + s [ ite ] <= target_sum ) { for ( int i = ite ; i < s_size ; i ++ ) { t [ t_size ] = s [ i ] ; if ( sum + s [ i ] <= target_sum ) { subset_sum ( s , t , s_size , t_size + 1 , sum + s [ i ] , i + 1 , target_sum ) ; } } } } } void generateSubsets ( int s [ ] , int size , int target_sum ) { int * tuplet_vector = ( int * ) malloc ( size * sizeof ( int ) ) ; int total = 0 ; qsort ( s , size , sizeof ( int ) , & comparator ) ; for ( int i = 0 ; i < size ; i ++ ) { total += s [ i ] ; } if ( s [ 0 ] <= target_sum && total >= target_sum ) { subset_sum ( s , tuplet_vector , size , 0 , 0 , 0 , target_sum ) ; } free ( tuplet_vector ) ; } int main ( ) { int weights [ ] = { 15 , 22 , 14 , 26 , 32 , 9 , 16 , 8 } ; int target = 53 ; int size = ARRAYSIZE ( weights ) ; generateSubsets ( weights , size , target ) ; cout << " Nodes ▁ generated ▁ " << total_nodes ; return 0 ; }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | C ++ program to check if given array has 2 elements whose sum is equal to the given value ; function to check for the given sum in the array ; checking for condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPairs ( int arr [ ] , int arr_size , int sum ) { unordered_set < int > s ; for ( int i = 0 ; i < arr_size ; i ++ ) { int temp = sum - arr [ i ] ; if ( s . find ( temp ) != s . end ( ) ) cout << " Pair ▁ with ▁ given ▁ sum ▁ " << sum << " ▁ is ▁ ( " << arr [ i ] << " , " << temp << " ) " << endl ; s . insert ( arr [ i ] ) ; } } int main ( ) { int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int n = 16 ; int arr_size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; printPairs ( A , arr_size , n ) ; return 0 ; }
Longest consecutive sequence in Binary tree | C / C ++ program to find longest consecutive sequence in binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A utility function to create a node ; Utility method to return length of longest consecutive sequence of tree ; if root data has one more than its parent then increase current length ; update the maximum by current length ; recursively call left and right subtree with expected value 1 more than root data ; method returns length of longest consecutive sequence rooted at node root ; call utility method with current length 0 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } void longestConsecutiveUtil ( Node * root , int curLength , int expected , int & res ) { if ( root == NULL ) return ; if ( root -> data == expected ) curLength ++ ; else curLength = 1 ; res = max ( res , curLength ) ; longestConsecutiveUtil ( root -> left , curLength , root -> data + 1 , res ) ; longestConsecutiveUtil ( root -> right , curLength , root -> data + 1 , res ) ; } int longestConsecutive ( Node * root ) { if ( root == NULL ) return 0 ; int res = 0 ; longestConsecutiveUtil ( root , 0 , root -> data , res ) ; return res ; } int main ( ) { Node * root = newNode ( 6 ) ; root -> right = newNode ( 9 ) ; root -> right -> left = newNode ( 7 ) ; root -> right -> right = newNode ( 10 ) ; root -> right -> right -> right = newNode ( 11 ) ; printf ( " % d STRNEWLINE " , longestConsecutive ( root ) ) ; return 0 ; }
Find remainder when a number A raised to N factorial is divided by P | C ++ program for above approach ; Function to calculate ( A ^ N ! ) % P in O ( log y ) ; Initialize result ; Update x if it is more than or Equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now ; Returning modular power ; Function to calculate resultant remainder ; Initializing ans to store final remainder ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int power ( long long x , long long int y , long long int p ) { long long int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } long long int remainder ( long long int n , long long int a , long long int p ) { long long int ans = a % p ; for ( long long int i = 1 ; i <= n ; i ++ ) ans = power ( ans , i , p ) ; return ans ; } int main ( ) { long long int A = 2 , N = 1 , P = 2 ; cout << remainder ( N , A , P ) << endl ; }
Find all array elements occurring more than âŒŠN / 3 âŒ ‹ times | C ++ program to find Majority element in an array ; Function to find Majority element in an array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; gain traverse the array and find the actual counts . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMajority ( int arr [ ] , int n ) { int count1 = 0 , count2 = 0 ; int first = INT_MAX , second = INT_MAX ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) { cout << first << " ▁ " ; flag = 1 ; } if ( count2 > n / 3 ) { cout << second << " ▁ " ; flag = 1 ; } if ( flag == 0 ) { cout << " No ▁ Majority ▁ Element " << endl ; } } int main ( ) { int arr [ ] = { 2 , 2 , 3 , 1 , 3 , 2 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMajority ( arr , n ) ; return 0 ; }
Modular Exponentiation of Complex Numbers |  ; Function to multiply two complex numbers modulo M ; Multiplication of two complex numbers is ( a + ib ) ( c + id ) = ( ac - bd ) + i ( ad + bc ) ; Return the multiplied value ; Function to calculate the complex modular exponentiation ; Here , res is initialised to ( 1 + i0 ) ; If k is odd ; Multiply ' complex ' with ' res ' ; Make complex as complex * complex ; Make k as k / 2 ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > Multiply ( pair < int , int > p , pair < int , int > q , int M ) { int x = ( ( p . first * q . first ) % M - ( p . second * q . second ) % M + M ) % M ; int y = ( ( p . first * q . second ) % M + ( p . second * q . first ) % M ) % M ; return { x , y } ; } pair < int , int > compPow ( pair < int , int > complex , int k , int M ) { pair < int , int > res = { 1 , 0 } ; while ( k > 0 ) { if ( k & 1 ) { res = Multiply ( res , complex , M ) ; } complex = Multiply ( complex , complex , M ) ; k = k >> 1 ; } return res ; } int main ( ) { int A = 7 , B = 3 , k = 10 , M = 97 ; pair < int , int > ans = compPow ( { A , B } , k , M ) ; cout << ans . first << " ▁ + ▁ i " << ans . second ; return 0 ; }
Sum of maximum of all subarrays | Divide and Conquer | C ++ implementation of the above approach ; Array to store segment tree . In first we will store the maximum of a range In second , we will store index of that range ; Size of array declared global to maintain simplicity in code ; Function to build segment tree ; Base case ; Finding the maximum among left and right child ; Returning the maximum to parent ; Function to perform range - max query in segment tree ; Base cases ; Finding the maximum among left and right child ; Function to find maximum sum subarray ; base case ; range - max query to determine largest in the range . ; divide the array in two parts ; Driver Code ; Input array ; Size of array ; Builind the segment - tree
#include <bits/stdc++.h> NEW_LINE #define seg_max  51 NEW_LINE using namespace std ; pair < int , int > seg_tree [ seg_max ] ; int n ; pair < int , int > buildMaxTree ( int l , int r , int i , int arr [ ] ) { if ( l == r ) { seg_tree [ i ] = { arr [ l ] , l } ; return seg_tree [ i ] ; } seg_tree [ i ] = max ( buildMaxTree ( l , ( l + r ) / 2 , 2 * i + 1 , arr ) , buildMaxTree ( ( l + r ) / 2 + 1 , r , 2 * i + 2 , arr ) ) ; return seg_tree [ i ] ; } pair < int , int > rangeMax ( int l , int r , int arr [ ] , int i = 0 , int sl = 0 , int sr = n - 1 ) { if ( sr < l sl > r ) return { INT_MIN , -1 } ; if ( sl >= l and sr <= r ) return seg_tree [ i ] ; return max ( rangeMax ( l , r , arr , 2 * i + 1 , sl , ( sl + sr ) / 2 ) , rangeMax ( l , r , arr , 2 * i + 2 , ( sl + sr ) / 2 + 1 , sr ) ) ; } int maxSumSubarray ( int arr [ ] , int l = 0 , int r = n - 1 ) { if ( l > r ) return 0 ; pair < int , int > a = rangeMax ( l , r , arr ) ; return a . first * ( r - a . second + 1 ) * ( a . second - l + 1 ) + maxSumSubarray ( arr , l , a . second - 1 ) + maxSumSubarray ( arr , a . second + 1 , r ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 7 } ; n = sizeof ( arr ) / sizeof ( int ) ; buildMaxTree ( 0 , n - 1 , 0 , arr ) ; cout << maxSumSubarray ( arr ) ; return 0 ; }
Modular exponentiation ( Recursive ) | Recursive C ++ program to compute modular power ; Base cases ; If B is even ; If B is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int exponentMod ( int A , int B , int C ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 , C ) ; y = ( y * y ) % C ; } else { y = A % C ; y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C ; } return ( int ) ( ( y + C ) % C ) ; } int main ( ) { int A = 2 , B = 5 , C = 13 ; cout << " Power ▁ is ▁ " << exponentMod ( A , B , C ) ; return 0 ; }
Find frequency of each element in a limited range array in less than O ( n ) time | C ++ program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findFrequencies ( int ele [ ] , int n ) { int freq = 1 ; int idx = 1 ; int element = ele [ 0 ] ; while ( idx < n ) { if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { cout << element << " ▁ " << freq << endl ; element = ele [ idx ] ; idx ++ ; freq = 1 ; } } cout << element << " ▁ " << freq ; } int main ( ) { cout << " - - - frequencies ▁ in ▁ a ▁ sorted ▁ array - - - - " << endl ; int arr [ ] = { 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findFrequencies ( arr , n ) ; }
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize answer ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2
int power ( int x , int y ) { int res = 1 ; while ( y ) { if ( y % 2 == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; }
Modular Exponentiation ( Power in Modular Arithmetic ) | Iterative C ++ program to compute modular power ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Driver code
#include <iostream> NEW_LINE using namespace std ; int power ( long long x , unsigned int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; y = y / 2 x = ( x * x ) % p ; } return res ; } int main ( ) { int x = 2 ; int y = 5 ; int p = 13 ; cout << " Power ▁ is ▁ " << power ( x , y , p ) ; return 0 ; }