codeparrot/xlcost-text-to-code huggingface dataset:
Growable array based stack | C # Program to implement growable array based stack using tight strategy ; constant amount at which stack is increased ; top of the stack ; length of stack ; function to create new stack ; allocate memory for new stack ; copying the content of old stack ; re - sizing the length ; function to push new element ; if stack is full , create new one ; insert element at top of the stack ; function to pop an element ; function to display ; if top is - 1 , that means stack is empty ; Driver Code ; creating initial stack ; pushing element to top of stack ; pushing more element when stack is full ; pushing more element so that stack can grow
using System ; class GFG { static int BOUND = 4 ; static int top = - 1 ; static int length = 0 ; static int [ ] create_new ( int [ ] a ) { int [ ] new_a = new int [ length + BOUND ] ; for ( int i = 0 ; i < length ; i ++ ) new_a [ i ] = a [ i ] ; length += BOUND ; return new_a ; } static int [ ] push ( int [ ] a , int element ) { if ( top == length - 1 ) a = create_new ( a ) ; a [ ++ top ] = element ; return a ; } static void pop ( int [ ] a ) { top -- ; } static void display ( int [ ] a ) { if ( top == - 1 ) Console . WriteLine ( " Stack ▁ is ▁ Empty " ) ; else { Console . Write ( " Stack : ▁ " ) ; for ( int i = 0 ; i <= top ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ ] a = create_new ( new int [ length + BOUND ] ) ; a = push ( a , 1 ) ; a = push ( a , 2 ) ; a = push ( a , 3 ) ; a = push ( a , 4 ) ; display ( a ) ; a = push ( a , 5 ) ; a = push ( a , 6 ) ; display ( a ) ; a = push ( a , 7 ) ; a = push ( a , 8 ) ; display ( a ) ; a = push ( a , 9 ) ; display ( a ) ; } }
Range Queries for Longest Correct Bracket Subsequence Set | 2 | C # code to answer the query in constant time ; Function for precomputation ; Create a stack and push - 1 as initial index to it . ; Traverse all characters of given String ; If opening bracket , push index of it ; If closing bracket , i . e . , str [ i ] = ' ) ' ; If closing bracket , i . e . , str [ i ] = ' ) ' && stack is not empty then mark both " open ▁ & ▁ close " bracket indexs as 1 . Pop the previous opening bracket 's index ; If stack is empty . ; Function return output of each query in O ( 1 ) ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void constructBlanceArray ( int [ ] BOP , int [ ] BCP , String str , int n ) { Stack < int > stk = new Stack < int > ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) stk . Push ( i ) ; else { if ( stk . Count != 0 ) { BCP [ i ] = 1 ; BOP [ stk . Peek ( ) ] = 1 ; stk . Pop ( ) ; } else BCP [ i ] = 0 ; } } for ( int i = 1 ; i < n ; i ++ ) { BCP [ i ] += BCP [ i - 1 ] ; BOP [ i ] += BOP [ i - 1 ] ; } } static int query ( int [ ] BOP , int [ ] BCP , int s , int e ) { if ( BOP [ s - 1 ] == BOP [ s ] ) { return ( BCP [ e ] - BOP [ s ] ) * 2 ; } else { return ( BCP [ e ] - BOP [ s ] + 1 ) * 2 ; } } public static void Main ( String [ ] args ) { String str = " ( ) ) ( ( ) ) ( ( ) ) ( " ; int n = str . Length ; int [ ] BCP = new int [ n + 1 ] ; int [ ] BOP = new int [ n + 1 ] ; constructBlanceArray ( BOP , BCP , str , n ) ; int startIndex = 5 , endIndex = 11 ; Console . Write ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + " STRNEWLINE " ) ; startIndex = 4 ; endIndex = 5 ; Console . Write ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + " STRNEWLINE " ) ; startIndex = 1 ; endIndex = 5 ; Console . Write ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + " STRNEWLINE " ) ; } }
HeapSort | C # program for implementation of Heap Sort ; To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is larger than root ; If right child is larger than largest so far ; If largest is not root ; Recursively heapify the affected sub - tree ; The main function to sort an array of given size ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver code
using System ; public class HeapSort { void heapify ( int [ ] arr , int n , int i ) { int largest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] > arr [ largest ] ) largest = l ; if ( r < n && arr [ r ] > arr [ largest ] ) largest = r ; if ( largest != i ) { int swap = arr [ i ] ; arr [ i ] = arr [ largest ] ; arr [ largest ] = swap ; heapify ( arr , n , largest ) ; } } public void sort ( int [ ] arr ) { int n = arr . Length ; for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . Read ( ) ; } public static void Main ( ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . Length ; HeapSort ob = new HeapSort ( ) ; ob . sort ( arr ) ; Console . WriteLine ( " Sorted ▁ array ▁ is " ) ; printArray ( arr ) ; } }
Iterative HeapSort | C # implementation of Iterative Heap Sort ; function build Max Heap where value of each child is always smaller than value of their parent ; if child is bigger than parent ; swap child and parent until parent is smaller ; swap value of first indexed with last indexed ; maintaining heap property after each swapping ; if left child is smaller than right child point index variable to right child ; if parent is smaller than child then swapping parent with child having higher value ; A utility function to print array of size n ; Driver Code
using System ; class HeapSort { static void buildMaxHeap ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) { int j = i ; while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) { swap ( arr , j , ( j - 1 ) / 2 ) ; j = ( j - 1 ) / 2 ; } } } } static void heapSort ( int [ ] arr , int n ) { buildMaxHeap ( arr , n ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { swap ( arr , 0 , i ) ; int j = 0 , index ; do { index = ( 2 * j + 1 ) ; if ( index < ( i - 1 ) && arr [ index ] < arr [ index + 1 ] ) index ++ ; if ( index < i && arr [ j ] < arr [ index ] ) swap ( arr , j , index ) ; j = index ; } while ( index < i ) ; } } public static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 15 , 17 , 9 , 21 } ; int n = arr . Length ; Console . Write ( " Given ▁ array : ▁ " ) ; printArray ( arr ) ; heapSort ( arr , n ) ; Console . Write ( " Sorted ▁ array : ▁ " ) ; printArray ( arr ) ; } }
Flip Binary Tree | C # program to flip a binary tree ; A binary tree node ; Utility function to create a new Binary Tree Node ; method to flip the binary tree ; Initialization of pointers ; Iterate through all left nodes ; Swapping nodes now , need temp to keep the previous right child Making prev ' s ▁ right ▁ ▁ as ▁ curr ' s left child ; Storing curr 's right child ; Making prev as curr 's  right child ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current lelvel . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } public static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static Node flipBinaryTree ( Node root ) { Node curr = root ; Node next = null ; Node temp = null ; Node prev = null ; while ( curr != null ) { next = curr . left ; curr . left = temp ; temp = curr . right ; curr . right = prev ; prev = curr ; curr = next ; } return prev ; } public static void printLevelOrder ( Node root ) { if ( root == null ) { return ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( root ) ; while ( true ) { int nodeCount = q . Count ; if ( nodeCount == 0 ) { break ; } while ( nodeCount > 0 ) { Node node = q . First . Value ; Console . Write ( node . data + " ▁ " ) ; q . RemoveFirst ( ) ; if ( node . left != null ) { q . AddLast ( node . left ) ; } if ( node . right != null ) { q . AddLast ( node . right ) ; } nodeCount -- ; } Console . WriteLine ( ) ; } } public static void Main ( string [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; Console . Write ( " Level ▁ order ▁ traversal ▁ " + " of ▁ given ▁ tree STRNEWLINE " ) ; printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; Console . Write ( " STRNEWLINE Level ▁ order ▁ traversal ▁ " + " of ▁ the ▁ flipped ▁ tree STRNEWLINE " ) ; printLevelOrder ( root ) ; } }
Foldable Binary Trees | C # program to check foldable binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Returns true if the given tree can be folded ; A utility function that checks if trees with roots as n1 and n2 are mirror of each other ; If both left and right subtrees are NULL , then return true ; If one of the trees is NULL and other is not , then return false ; Otherwise check if left and right subtrees are mirrors of their counterparts ; Driver code ; The constructed binary tree is 1 / \ 2 3 \ / 4 5
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; bool IsFoldable ( Node node ) { if ( node == null ) return true ; return IsFoldableUtil ( node . left , node . right ) ; } bool IsFoldableUtil ( Node n1 , Node n2 ) { if ( n1 == null && n2 == null ) return true ; if ( n1 == null n2 == null ) return false ; return IsFoldableUtil ( n1 . left , n2 . right ) && IsFoldableUtil ( n1 . right , n2 . left ) ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; if ( tree . IsFoldable ( tree . root ) ) Console . WriteLine ( " tree ▁ is ▁ foldable " ) ; else Console . WriteLine ( " Tree ▁ is ▁ not ▁ foldable " ) ; } }
Check for Children Sum Property in a Binary Tree | C # program to check children sum property ; A binary tree node has data , pointer to left child and a pointer to right child ; returns 1 if children sum property holds for the given node and both of its children ; left_data is left child data and right_data is for right child data ; If node is NULL or it 's a leaf   node then return true  ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; if the node and both of its children satisfy the property return 1 else 0 ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } class GFG { public Node root ; public virtual int isSumProperty ( Node node ) { int left_data = 0 , right_data = 0 ; if ( node == null || ( node . left == null && node . right == null ) ) { return 1 ; } else { if ( node . left != null ) { left_data = node . left . data ; } if ( node . right != null ) { right_data = node . right . data ; } if ( ( node . data == left_data + right_data ) && ( isSumProperty ( node . left ) != 0 ) && isSumProperty ( node . right ) != 0 ) { return 1 ; } else { return 0 ; } } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 2 ) ; if ( tree . isSumProperty ( tree . root ) != 0 ) { Console . WriteLine ( " The ▁ given ▁ tree ▁ satisfies " + " ▁ children ▁ sum ▁ property " ) ; } else { Console . WriteLine ( " The ▁ given ▁ tree ▁ does ▁ not " + " ▁ satisfy ▁ children ▁ sum ▁ property " ) ; } } }
How to check if a given array represents a Binary Heap ? | C # program to check whether a given array represents a max - heap or not ; Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver Code
using System ; class GFG { static bool isHeap ( int [ ] arr , int i , int n ) { if ( i >= ( n - 2 ) / 2 ) { return true ; } if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) { return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . Length - 1 ; if ( isHeap ( arr , 0 , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
How to check if a given array represents a Binary Heap ? | C # program to check whether a given array represents a max - heap or not ; Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver Code
using System ; class GFG { static bool isHeap ( int [ ] arr , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) { return false ; } if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) { return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . Length ; if ( isHeap ( arr , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Connect n ropes with minimum cost | C # program to connect n ropes with minimum cost ; Create a priority queue ; Initialize result ; While size of priority queue is more than 1 ; Extract shortest two ropes from pq ; Connect the ropes : update result and insert the new rope to pq ; Driver program to test above function
using System ; using System . Collections . Generic ; public class ConnectRopes { static int minCost ( int [ ] arr , int n ) { List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . Add ( arr [ i ] ) ; } int res = 0 ; while ( pq . Count > 1 ) { pq . Sort ( ) ; int first = pq [ 0 ] ; int second = pq [ 1 ] ; pq . RemoveRange ( 0 , 2 ) ; res += first + second ; pq . Add ( first + second ) ; } return res ; } public static void Main ( String [ ] args ) { int [ ] len = { 4 , 3 , 2 , 6 } ; int size = len . Length ; Console . WriteLine ( " Total ▁ cost ▁ for ▁ connecting " + " ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } }
Merge k sorted arrays | Set 1 | C # program to merge k sorted arrays of size n each . ; This function takes an array of arrays as an argument and All arrays are assumed to be sorted . It merges them together and prints the readonly sorted output . ; traverse the matrix ; sort the array ; A utility function to print array elements ; Driver program to test above functions
using System ; public class GFG { public static void mergeKArrays ( int [ , ] arr , int a , int [ ] output ) { int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) output [ c ++ ] = arr [ i , j ] ; } Array . Sort ( output ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = 4 ; int n = 3 ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , n , output ) ; Console . WriteLine ( " Merged ▁ array ▁ is ▁ " ) ; printArray ( output , n * k ) ; } }
Smallest Derangement of Sequence | C # program to generate smallest derangement using priority queue . ; Generate Sequence and insert into a priority queue . ; Generate Least Derangement ; Print Derangement ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void generate_derangement ( int N ) { int [ ] S = new int [ N + 1 ] ; List < int > PQ = new List < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { S [ i ] = i ; PQ . Add ( S [ i ] ) ; } int [ ] D = new int [ N + 1 ] ; PQ . Sort ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { PQ . Sort ( ) ; int d = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; if ( d != S [ i ] i == N ) { D [ i ] = d ; } else { PQ . Sort ( ) ; D [ i ] = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; PQ . Add ( d ) ; } } if ( D [ N ] == S [ N ] ) { int t = D [ N - 1 ] ; D [ N - 1 ] = D [ N ] ; D [ N ] = t ; } for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( D [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { generate_derangement ( 10 ) ; } }
Smallest Derangement of Sequence | Efficient C # program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Print Derangement ; Driver Program
using System ; class GFG { static void generate_derangement ( int N ) { int [ ] S = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int [ ] D = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N ) { D [ N ] = S [ N - 1 ] ; D [ N - 1 ] = S [ N ] ; } else { D [ i ] = i + 1 ; D [ i + 1 ] = i ; } } for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( D [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { generate_derangement ( 10 ) ; } }
Largest Derangement of a Sequence | C # program to find the largest derangement ; Stores result ; Insert all elements into a priority queue ; Fill Up res [ ] from left to right ; New Element poped equals the element in original sequence . Get the next largest element ; If given sequence is in descending order then we need to swap last two elements again ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printLargest ( int [ ] seq , int N ) { int [ ] res = new int [ N ] ; List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) pq . Add ( seq [ i ] ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Sort ( ) ; pq . Reverse ( ) ; int d = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( d != seq [ i ] i == N - 1 ) { res [ i ] = d ; } else { res [ i ] = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; pq . Add ( d ) ; } } if ( res [ N - 1 ] == seq [ N - 1 ] ) { res [ N - 1 ] = res [ N - 2 ] ; res [ N - 2 ] = seq [ N - 1 ] ; } Console . WriteLine ( " Largest ▁ Derangement " ) ; for ( int i = 0 ; i < N ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] seq = { 92 , 3 , 52 , 13 , 2 , 31 , 1 } ; int n = seq . Length ; printLargest ( seq , n ) ; } }
Program to calculate Profit Or Loss | C # code to demonstrate Profit and Loss ; Function to calculate Profit ; Function to calculate Loss ; Driver Code
using System ; class GFG { static int Profit ( int costPrice , int sellingPrice ) { int profit = ( sellingPrice - costPrice ) ; return profit ; } static int Loss ( int costPrice , int sellingPrice ) { int Loss = ( costPrice - sellingPrice ) ; return Loss ; } public static void Main ( ) { int costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) Console . Write ( " No ▁ profit ▁ nor ▁ Loss " ) ; else if ( sellingPrice > costPrice ) Console . Write ( Profit ( costPrice , sellingPrice ) + " ▁ Profit ▁ " ) ; else Console . Write ( Loss ( costPrice , sellingPrice ) + " ▁ Loss ▁ " ) ; } }
Find the Next perfect square greater than a given number | C # implementation of above approach ; Function to find the next perfect square ; Driver Code
using System ; class GFG { static int nextPerfectSquare ( int N ) { int nextN = ( int ) Math . Floor ( Math . Sqrt ( N ) ) + 1 ; return nextN * nextN ; } public static void Main ( ) { int n = 35 ; Console . WriteLine ( nextPerfectSquare ( n ) ) ; } }
Print all substring of a number without any conversion | C # implementation of above approach ; Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code
using System ; class GFG { static void printSubstrings ( int n ) { int s = ( int ) Math . Log10 ( n ) ; int d = ( int ) ( Math . Pow ( 10 , s ) + 0.5 ) ; int k = d ; while ( n > 0 ) { while ( d > 0 ) { Console . WriteLine ( n / d ) ; d = d / 10 ; } n = n % k ; k = k / 10 ; d = k ; } } public static void Main ( ) { int n = 123 ; printSubstrings ( n ) ; } }
Modulo power for large numbers represented as strings | C # program to find ( a ^ b ) % MOD where a and b may be very large and represented as strings . ; Returns modulo exponentiation for two numbers represented as long long int . It is used by powerStrings ( ) . Its complexity is log ( n ) ; Returns modulo exponentiation for two numbers represented as strings . It is used by powerStrings ( ) ; We convert strings to number ; calculating a % MOD ; calculating b % ( MOD - 1 ) ; Now a and b are long long int . We calculate a ^ b using modulo exponentiation ; Driver code ; As numbers are very large that is it may contains upto 10 ^ 6 digits . So , we use string .
using System ; class GFG { static long MOD = ( long ) ( 1e9 + 7 ) ; static long powerLL ( long x , long n ) { long result = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { result = result * x % MOD ; } n = n / 2 ; x = x * x % MOD ; } return result ; } static long powerStrings ( String sa , String sb ) { long a = 0 , b = 0 ; for ( int i = 0 ; i < sa . Length ; i ++ ) { a = ( a * 10 + ( sa [ i ] - '0' ) ) % MOD ; } for ( int i = 0 ; i < sb . Length ; i ++ ) { b = ( b * 10 + ( sb [ i ] - '0' ) ) % ( MOD - 1 ) ; } return powerLL ( a , b ) ; } public static void Main ( String [ ] args ) { String sa = "2" , sb = "3" ; Console . WriteLine ( powerStrings ( sa , sb ) ) ; } }
Check if a number can be expressed as 2 ^ x + 2 ^ y | C # code to check if a number can be expressed as 2 ^ x + 2 ^ y ; Utility function to check if a number is power of 2 or not ; Utility function to determine the value of previous power of 2 ; function to check if n can be expressed as 2 ^ x + 2 ^ y or not ; if value of n is 0 or 1 it can not be expressed as 2 ^ x + 2 ^ y ; if a number is power of it can be expressed as 2 ^ x + 2 ^ y ; if the remainder after subtracting previous power of 2 is also a power of 2 then it can be expressed as 2 ^ x + 2 ^ y ; driver code
using System ; class GFG { static bool isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int previousPowerOfTwo ( int n ) { while ( ( n & n - 1 ) > 1 ) { n = n & n - 1 ; } return n ; } static bool checkSum ( int n ) { if ( n == 0 n == 1 ) { Console . WriteLine ( " No " ) ; return false ; } else if ( isPowerOfTwo ( n ) ) { Console . WriteLine ( n / 2 + " ▁ " + n / 2 ) ; return true ; } else { int x = previousPowerOfTwo ( n ) ; int y = n - x ; if ( isPowerOfTwo ( y ) ) { Console . WriteLine ( x + " ▁ " + y ) ; return true ; } else { return false ; } } } public static void Main ( ) { int n1 = 20 ; if ( checkSum ( n1 ) == false ) Console . WriteLine ( " No " ) ; Console . WriteLine ( ) ; int n2 = 11 ; if ( checkSum ( n2 ) == false ) Console . WriteLine ( " No " ) ; } }