"Paths requiring minimum number of jumps to reach end of array | C # program to implement the above approach ; Pair Struct ; Stores the current index ; Stores the path travelled so far ; Stores the minimum jumps required to reach the last index from current index ; Constructor ; Minimum jumps required to reach end of the array ; Stores the maximum number of steps that can be taken from the current index ; Checking if index stays within bounds ; Stores the minimum number of jumps required to jump from ( i + j ) - th index ; Function to find all possible paths to reach end of array requiring minimum number of steps ; Storing the neighbours of current index element ; Function to find the minimum steps and corresponding paths to reach end of an array ; Stores the minimum jumps from each position to last position ; Driver Code"
Solution in C#:

using System ; using System . Collections ; public struct Pair { public int idx ; public string psf ; public int jmps ; public Pair ( int idx , String psf , int jmps ) { this . idx = idx ; this . psf = psf ; this . jmps = jmps ; } } class GFG { public static int [ ] minJumps ( int [ ] arr ) { int [ ] dp = new int [ arr . Length ] ; int n = dp . Length ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = int . MaxValue ; dp [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int steps = arr [ i ] ; int min = int . MaxValue ; for ( int j = 1 ; j <= steps && i + j < n ; j ++ ) { if ( dp [ i + j ] != int . MaxValue && dp [ i + j ] < min ) { min = dp [ i + j ] ; } } if ( min != int . MaxValue ) dp [ i ] = min + 1 ; } return dp ; } public static void possiblePath ( int [ ] arr , int [ ] dp ) { Queue queue = new Queue ( ) ; queue . Enqueue ( new Pair ( 0 , "0" , dp [ 0 ] ) ) ; while ( queue . Count > 0 ) { Pair tmp = ( Pair ) queue . Dequeue ( ) ; if ( tmp . jmps == 0 ) { Console . WriteLine ( tmp . psf ) ; continue ; } for ( int step = 1 ; step <= arr [ tmp . idx ] ; step ++ ) { if ( tmp . idx + step < arr . Length && tmp . jmps - 1 == dp [ tmp . idx + step ] ) { queue . Enqueue ( new Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + ( tmp . idx + step ) , tmp . jmps - 1 ) ) ; } } } } public static void Solution ( int [ ] arr ) { int [ ] dp = minJumps ( arr ) ; possiblePath ( arr , dp ) ; } static public void Main ( ) { int [ ] arr = { 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 } ; int size = arr . Length ; Solution ( arr ) ; } }


"Paths with maximum number of ' a ' from ( 1 , 1 ) to ( X , Y ) vertically or horizontally | C # program to find paths with maximum number of ' a ' from ( 1 , 1 ) to ( X , Y ) vertically or horizontally ; Function to answer queries ; Iterate till query ; Decrease to get 0 - based indexing ; Print answer ; Function that pre - computes the dp array ; Check fo the first character ; Iterate in row and columns ; If not first row or not first column ; Not first row ; Not first column ; If it is not ' a ' then increase by 1 ; Driver code ; character N X N array ; queries ; number of queries ; function call to pre - compute ; function call to answer every query"
Solution in C#:

using System ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int n = 3 ; static int [ , ] dp = new int [ n , n ] ; static void answerQueries ( pair [ ] queries , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int x = queries [ i ] . first ; x -- ; int y = queries [ i ] . second ; y -- ; Console . WriteLine ( dp [ x , y ] ) ; } } static void pre_compute ( char [ , ] a ) { if ( a [ 0 , 0 ] == ' a ' ) dp [ 0 , 0 ] = 0 ; else dp [ 0 , 0 ] = 1 ; for ( int row = 0 ; row < n ; row ++ ) { for ( int col = 0 ; col < n ; col ++ ) { if ( row != 0 col != 0 ) dp [ row , col ] = int . MaxValue ; if ( row != 0 ) { dp [ row , col ] = Math . Min ( dp [ row , col ] , dp [ row - 1 , col ] ) ; } if ( col != 0 ) { dp [ row , col ] = Math . Min ( dp [ row , col ] , dp [ row , col - 1 ] ) ; } if ( a [ row , col ] != ' a ' && ( row != 0 col != 0 ) ) dp [ row , col ] += 1 ; } } } public static void Main ( String [ ] args ) { char [ , ] a = { { ' a ' , ' b ' , ' a ' } , { ' a ' , ' c ' , ' d ' } , { ' b ' , ' a ' , ' b ' } } ; pair [ ] queries = { new pair ( 1 , 3 ) , new pair ( 3 , 3 ) } ; int q = 2 ; pre_compute ( a ) ; answerQueries ( queries , q ) ; } }


"Pen Distribution Problem | C # implementation of the above approach ; Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code"
Solution in C#:

using System ; class GFG { static void solve ( int N , int P1 , int P2 , int X , int Move , bool QuitP1 , bool QuitP2 ) { if ( N == 0 || ( QuitP1 && QuitP2 ) ) { Console . WriteLine ( " Number ▁ of ▁ pens ▁ remaining " + " ▁ in ▁ the ▁ box : ▁ " + N ) ; Console . WriteLine ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P1 : ▁ " + P1 ) ; Console . WriteLine ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P2 : ▁ " + P2 ) ; return ; } if ( Move == 0 && QuitP1 == false ) { int req_P1 = ( int ) ( Math . Pow ( 2 , X ) ) ; if ( req_P1 <= N ) { P1 += req_P1 ; N -= req_P1 ; } else { QuitP1 = true ; } } else if ( Move == 1 && QuitP2 == false ) { int req_P2 = ( int ) ( Math . Pow ( 3 , X ) ) ; if ( req_P2 <= N ) { P2 += req_P2 ; N -= req_P2 ; } else { QuitP2 = true ; } } X ++ ; Move = ( ( Move == 1 ) ? 0 : 1 ) ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } static void PenGame ( int N ) { int P1 = 0 ; int P2 = 0 ; int X = 0 ; int Move = 0 ; bool QuitP1 = false ; bool QuitP2 = false ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } public static void Main ( ) { int N = 22 ; PenGame ( N ) ; } }


"Pentacontagon number | C # program for above approach ; Finding the nth pentacontagon number ; Driver code"
Solution in C#:

using System ; class GFG { static int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } public static void Main ( string [ ] args ) { int n = 3 ; Console . Write ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " + pentacontagonNum ( n ) ) ; } }


"Pentacontahenagon Number | C # program for the above approach ; Function to find the N - th Pentacontahenagon Number ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int PentacontahenagonNum ( int N ) { return ( 49 * N * N - 47 * N ) / 2 ; } public static void Main ( ) { int N = 3 ; Console . Write ( "3rd ▁ Pentacontahenagon ▁ Number ▁ is ▁ " + PentacontahenagonNum ( N ) ) ; } }


"Pentadecagonal Number | C # program to find Nth pentadecagonal number ; Function to find N - th pentadecagonal number ; Formula to calculate nth Pentadecagonal number ; Driver code"
Solution in C#:

 using System ; class GFG { static int Pentadecagonal_num ( int n ) { return ( 13 * n * n - 11 * n ) / 2 ; } public static void Main ( string [ ] args ) { int n = 3 ; Console . Write ( Pentadecagonal_num ( n ) + "
 " ) ; n = 10 ; Console . Write ( Pentadecagonal_num ( n ) + "
 " ) ; } }


"Pentagonal Pyramidal Number | C # Program to get nth Pentagonal pyramidal number . ; function to get nth Pentagonal pyramidal number . ; Driver Code"
Solution in C#:

using System ; class GFG { static int pentagon_pyramidal ( int n ) { return n * n * ( n + 1 ) / 2 ; } static public void Main ( ) { int n = 4 ; Console . WriteLine ( pentagon_pyramidal ( n ) ) ; } }


"Pentagonal Pyramidal Number | C # Program to get nth Pentagonal pyramidal number . ; function to get nth Pentagonal pyramidal number . ; Running loop from 1 to n ; get nth pentagonal number ; add to sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int pentagon_pyramidal ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int p = ( 3 * i * i - i ) / 2 ; sum = sum + p ; } return sum ; } static public void Main ( ) { int n = 4 ; Console . WriteLine ( pentagon_pyramidal ( n ) ) ; } }


"Pentanacci Numbers | A simple recursive program to print Nth Pentanacci number ; Recursive function to find the Nth Pentanacci number ; Function to print the Nth Pentanacci number ; Driver code"
Solution in C#:

using System ; class GFG { static int printpentaRec ( int n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 ) return 0 ; else if ( n == 5 ) return 1 ; else return ( printpentaRec ( n - 1 ) + printpentaRec ( n - 2 ) + printpentaRec ( n - 3 ) + printpentaRec ( n - 4 ) + printpentaRec ( n - 5 ) ) ; } static void printPenta ( int n ) { Console . WriteLine ( printpentaRec ( n ) ) ; } static void Main ( ) { int n = 10 ; printPenta ( n ) ; } }


"Pentanacci Numbers | C # implementation to print Nth Pentanacci numbers . ; Function to print Nth Pentanacci number ; Initialize first five numbers to base cases ; Declare a current variable ; Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth to second , third , fourth and curr to fifth respectively ; Driver code"
Solution in C#:

 using System ; class GFG { static void printpenta ( int n ) { if ( n < 0 ) return ; int first = 0 ; int second = 0 ; int third = 0 ; int fourth = 0 ; int fifth = 1 ; int curr = 0 ; if ( n == 0 n == 1 n == 2 n == 3 ) Console . Write ( first + "
 " ) ; else if ( n == 5 ) Console . Write ( fifth + "
 " ) ; else { for ( int i = 5 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = curr ; } Console . Write ( curr + "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 10 ; printpenta ( n ) ; } }


"Pentatope number | C # Program to find the nth Pentatope Number ; Function that returns nth pentatope number ; Driver Code ; For 5 th PentaTope Number ; For 11 th PentaTope Number"
Solution in C#:

using System ; class GFG { static int pentatopeNum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) ) / 24 ; } static public void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( pentatopeNum ( n ) ) ; n = 11 ; Console . WriteLine ( pentatopeNum ( n ) ) ; } }


"Pentatope number | C # Program to find the nth Pentatope number ; function for Pentatope number ; formula for find Pentatope nth term ; Driver Code"
Solution in C#:

using System ; class GFG { static int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } public static void Main ( ) { int n = 7 ; Console . WriteLine ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; n = 12 ; Console . WriteLine ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; } }


"Percentage change in Hemisphere volume if radius is changed | C # program to find percentage change in hemisphere volume wrt change in radius ; Function to find the change in hemispheric volume ; Driver code ; Get the change in radius ; Calculate the change in hemispheric volume"
Solution in C#:

 using System ; class GFG { static void new_vol ( double x ) { if ( x > 0 ) { Console . Write ( " % ▁ change ▁ in ▁ the ▁ " + " volume ▁ of ▁ the ▁ hemisphere : ▁ " + ( Math . Pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . Pow ( x , 2 ) ) / 100 ) + " % " + " ▁ increase
 " ) ; } else if ( x < 0 ) { Console . Write ( " % ▁ change ▁ in ▁ the ▁ " + " volume ▁ of ▁ the ▁ hemisphere : ▁ " + ( Math . Pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . Pow ( x , 2 ) ) / 100 ) + " % ▁ decrease
 " ) ; } else { Console . Write ( " Volume ▁ remains ▁ the ▁ same . " ) ; } } public static void Main ( ) { double x = - 10.0 ; new_vol ( x ) ; } }


"Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant | C # program to find percentage increase in the cylinder if the height is increased by given percentage but radius remains constant ; Driver code"
Solution in C#:

using System ; class GFG { static void newvol ( double x ) { Console . WriteLine ( " percentage ▁ increase ▁ " + " in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " + x + " % " ) ; } public static void Main ( ) { double x = 10 ; newvol ( x ) ; } }


"Percentage increase in the volume of cuboid if length , breadth and height are increased by fixed percentages | C # implementation of the approach ; Function to return the percentage increase in the volume of the cuboid ; Driver code"
Solution in C#:

using System ; class GFG { static double increaseInVol ( double l , double b , double h ) { double percentInc = ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ; percentInc -= 1 ; percentInc *= 100 ; return percentInc ; } public static void Main ( ) { double l = 50 , b = 20 , h = 10 ; Console . WriteLine ( increaseInVol ( l , b , h ) + " % " ) ; } }


"Percentage increase in volume of the cube if a side of cube is increased by a given percentage | C # program to find percentage increase in the volume of the cube if a side of cube is increased by a given percentage ; Driver code"
Solution in C#:

using System ; class GFG { static void newvol ( double x ) { Console . Write ( " percentage ▁ increase ▁ " + " in ▁ the ▁ volume ▁ of ▁ the ▁ cube ▁ is ▁ " + ( Math . Pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . Pow ( x , 2 ) ) / 100 ) ) ; Console . Write ( " % " ) ; } public static void Main ( ) { double x = 10 ; newvol ( x ) ; } }


"Perfect Binary Tree Specific Level Order Traversal | C # program for special level order traversal ; Class containing left and right child of current node and key value ; Given a perfect binary tree , print its nodes in specific level order ; Let us print root and next level first ; Since it is perfect Binary Tree , right is not checked ; Do anything more if there are nodes at next level in given perfect Binary Tree ; Create a queue and enqueue left and right children of root ; We process two nodes at a time , so we need two variables to store two front items of queue ; traversal loop ; Pop two items from queue ; Print children of first and second in reverse order ; If first and second have grandchildren , enqueue them in reverse order ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void printSpecificLevelOrder ( Node node ) { if ( node == null ) { return ; } Console . Write ( node . data ) ; if ( node . left != null ) { Console . Write ( " ▁ " + node . left . data + " ▁ " + node . right . data ) ; } if ( node . left . left == null ) { return ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( node . left ) ; q . AddLast ( node . right ) ; Node first = null , second = null ; while ( q . Count > 0 ) { first = q . First . Value ; q . RemoveFirst ( ) ; second = q . First . Value ; q . RemoveFirst ( ) ; Console . Write ( " ▁ " + first . left . data + " ▁ " + second . right . data ) ; Console . Write ( " ▁ " + first . right . data + " ▁ " + second . left . data ) ; if ( first . left . left != null ) { q . AddLast ( first . left ) ; q . AddLast ( second . right ) ; q . AddLast ( first . right ) ; q . AddLast ( second . left ) ; } } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . left . left = new Node ( 12 ) ; tree . root . right . left . right = new Node ( 13 ) ; tree . root . right . right . left = new Node ( 14 ) ; tree . root . right . right . right = new Node ( 15 ) ; tree . root . left . left . left . left = new Node ( 16 ) ; tree . root . left . left . left . right = new Node ( 17 ) ; tree . root...


"Perfect Binary Tree Specific Level Order Traversal | Set 2 | C # program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Function body ; Create a queue and enqueue left and right children of root ; We process two nodes at a time , so we need two variables to store two front items of queue ; traversal loop ; Pop two items from queue ; Push first and second node 's  children in reverse order ; If first and second have grandchildren , enqueue them in specific order ; Given a perfect binary tree , print its nodes in specific level order ; create a stack and push root ; Push level 1 and level 2 nodes in stack ; Since it is perfect Binary Tree , right is not checked ; Do anything more if there are nodes at next level in given perfect Binary Tree ; Finally pop all Nodes from stack and prints them . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public Node root ; public virtual void printSpecificLevelOrderUtil ( Node root , Stack < Node > s ) { if ( root == null ) { return ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( root . left ) ; q . AddLast ( root . right ) ; Node first = null , second = null ; while ( q . Count > 0 ) { first = q . First . Value ; q . RemoveFirst ( ) ; second = q . First . Value ; q . RemoveFirst ( ) ; s . Push ( second . left ) ; s . Push ( first . right ) ; s . Push ( second . right ) ; s . Push ( first . left ) ; if ( first . left . left != null ) { q . AddLast ( first . right ) ; q . AddLast ( second . left ) ; q . AddLast ( first . left ) ; q . AddLast ( second . right ) ; } } } public virtual void printSpecificLevelOrder ( Node root ) { Stack < Node > s = new Stack < Node > ( ) ; s . Push ( root ) ; if ( root . left != null ) { s . Push ( root . right ) ; s . Push ( root . left ) ; } if ( root . left . left != null ) { printSpecificLevelOrderUtil ( root , s ) ; } while ( s . Count > 0 ) { Console . Write ( s . Peek ( ) . data + " ▁ " ) ; s . Pop ( ) ; } } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; Console . WriteLine ( " Specific ▁ Level ▁ Order ▁ Traversal ▁ " + " of ▁ Binary ▁ Tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; }


"Perfect Binary Tree Specific Level Order Traversal | Set 2 | C # program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a perfect binary tree , print its nodes in specific level order ; for level order traversal ; Stack to print reverse ; vector to store the level ; considering size of the level ; push data of the node of a particular level to vector ; push vector containing a level in Stack ; print the Stack ; Finally pop all Nodes from Stack and prints them . ; finally print root ; ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left ; public Node right ; public Node ( int value ) { data = value ; left = null ; right = null ; } } ; static void specific_level_order_traversal ( Node root ) { Queue < Node > q = new Queue < Node > ( ) ; Stack < List < int > > s = new Stack < List < int > > ( ) ; q . Enqueue ( root ) ; int sz ; while ( q . Count > 0 ) { List < int > v = new List < int > ( ) ; sz = q . Count ; for ( int i = 0 ; i < sz ; ++ i ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; v . Add ( temp . data ) ; if ( temp . left != null ) q . Enqueue ( temp . left ) ; if ( temp . right != null ) q . Enqueue ( temp . right ) ; } s . Push ( v ) ; } while ( s . Count > 0 ) { List < int > v = s . Peek ( ) ; s . Pop ( ) ; for ( int i = 0 , j = v . Count - 1 ; i < j ; ++ i ) { Console . Write ( v [ i ] + " ▁ " + v [ j ] + " ▁ " ) ; j -- ; } } Console . WriteLine ( root . data ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; Console . WriteLine ( " Specific ▁ Level ▁ Order ▁ traversal " + " ▁ of ▁ binary ▁ tree ▁ is " ) ; specific_level_order_traversal ( root ) ; } }


"Perfect Cube String | C # program to find if String is a perfect cube or not . ; Finding ASCII values of each character and finding its sum ; Find the cube root of sum ; Check if sum is a perfect cube ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPerfectCubeString ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) sum += ( int ) str [ i ] ; double cr = Math . Round ( Math . Pow ( sum , ( double ) 1 / 3 ) ) ; return ( cr * cr * cr == sum ) ; } public static void Main ( String [ ] args ) { String str = " ll " ; if ( isPerfectCubeString ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Perfect Cube factors of a Number | C # program for the above approach ; Function that returns the count of factors that are perfect cube ; To store the count of number of times a prime number divides N . ; To store the number of factors that are perfect cube ; Count number of 2 's that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 3 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 3 + 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 216 ; Console . Write ( noOfFactors ( N ) ) ; } }


"Perfect Cube | C # program to check if a number is a perfect cube using prime factors ; Inserts the prime factor in the Hash Map if not present If present updates it 's frequency ; A utility function to find all prime factors of a given number N ; Insert the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n , insert i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is a perfect cube ; Using values ( ) for iteration over keys ; Driver Code ; Function to check if N is perfect cube or not"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static Dictionary < int , int > insertPF ( Dictionary < int , int > primeFact , int fact ) { if ( primeFact . ContainsKey ( fact ) ) { int freq ; freq = primeFact [ fact ] ; primeFact [ fact ] = ++ freq ; } else { primeFact . Add ( fact , 1 ) ; } return primeFact ; } public static Dictionary < int , int > primeFactors ( int n ) { Dictionary < int , int > primeFact = new Dictionary < int , int > ( ) ; while ( n % 2 == 0 ) { primeFact = insertPF ( primeFact , 2 ) ; n /= 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { primeFact = insertPF ( primeFact , i ) ; n /= i ; } } if ( n > 2 ) primeFact = insertPF ( primeFact , n ) ; return primeFact ; } public static String perfectCube ( int n ) { Dictionary < int , int > primeFact ; primeFact = primeFactors ( n ) ; foreach ( int freq in primeFact . Values ) { if ( freq % 3 != 0 ) return " No " ; } return " Yes " ; } public static void Main ( String [ ] args ) { int N = 216 ; Console . WriteLine ( perfectCube ( N ) ) ; } }


"Perfect Cube | C # program to check whether the given number N is the perfect cube or not ; Function to check if a number is a perfect Cube or not ; Iterate from 1 - N ; Find the cube of every number ; Check if cube equals N or not ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void perfectCube ( int N ) { int cube ; for ( int i = 0 ; i <= N ; i ++ ) { cube = i * i * i ; if ( cube == N ) { Console . WriteLine ( " Yes " ) ; return ; } else if ( cube > N ) { Console . WriteLine ( " NO " ) ; return ; } } } public static void Main ( string [ ] args ) { int N = 216 ; perfectCube ( N ) ; } }


"Perfect Cube | C # program to check whether the given number N is the perfect cube or not ; Function to check if a number is a perfect Cube using inbuilt function ; If cube of cube_root is equals to N , then print Yes Else print No ; Driver 's code ; Function call to check N is cube or not"
Solution in C#:

using System ; class GFG { static void perfectCube ( int N ) { int cube_root ; cube_root = ( int ) Math . Round ( Math . Cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { Console . WriteLine ( " Yes " ) ; return ; } else { Console . WriteLine ( " NO " ) ; return ; } } public static void Main ( string [ ] args ) { int N = 125 ; perfectCube ( N ) ; } }


"Perfect Digital Invariants number | C # program for the above approach ; Function to calculate x raised to the power y ; Function to check whether the given number is Perfect Digital Invariant number or not ; For each digit in temp ; If satisfies Perfect Digital Invariant condition ; If sum exceeds n , then not possible ; Driver Code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) { return 1 ; } if ( y % 2 == 0 ) { return ( power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } return ( x * power ( x , y / 2 ) * power ( x , y / 2 ) ) ; } static bool isPerfectDigitalInvariant ( int x ) { for ( int fixed_power = 1 ; ; fixed_power ++ ) { int temp = x , sum = 0 ; while ( temp > 0 ) { int r = temp % 10 ; sum += power ( r , fixed_power ) ; temp = temp / 10 ; } if ( sum == x ) { return true ; } if ( sum > x ) { return false ; } } } public static void Main ( String [ ] args ) { int N = 4150 ; if ( isPerfectDigitalInvariant ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Perfect Number | C # program to check if a given number is perfect or not ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Driver program"
Solution in C#:

class GFG { static bool isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } static void Main ( ) { System . Console . WriteLine ( " Below ▁ are ▁ all ▁ perfect " + " numbers ▁ till ▁ 10000" ) ; for ( int n = 2 ; n < 10000 ; n ++ ) if ( isPerfect ( n ) ) System . Console . WriteLine ( n + " ▁ is ▁ a ▁ perfect ▁ number " ) ; } }


"Perfect Square String | C # program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ASCII value of the string ; Find floating point value of square root of x . ; If square root is an integer ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPerfectSquareString ( string str ) { int sum = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) sum += ( int ) str [ i ] ; double squareRoot = Math . Sqrt ( sum ) ; double F = Math . Floor ( squareRoot ) ; return ( ( squareRoot - F ) == 0 ) ; } public static void Main ( ) { string str = " d " ; if ( isPerfectSquareString ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Perfect Square factors of a Number | C # program to implement the above approach ; Function that returns the count of factors that are perfect squares ; Stores the count of number of times a prime number divides N . ; Stores the number of factors that are perfect square ; Count number of 2 's  that divides N ; Calculate ans according to above formula ; Check for all the possible numbers that can divide it ; Check the number of times prime number i divides it ; Calculate ans according to above formula ; Return final count ; Driver Code"
Solution in C#:

using System ; class GFG { static int noOfFactors ( int N ) { if ( N == 1 ) return 1 ; int count = 0 ; int ans = 1 ; while ( N % 2 == 0 ) { count ++ ; N = N / 2 ; } ans *= ( count / 2 + 1 ) ; for ( int i = 3 ; i * i <= N ; i = i + 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } ans *= ( count / 2 + 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . Write ( noOfFactors ( N ) ) ; } }


"Perfect Sum Problem | C # implementation of the above approach ; Function to print the subsets whose sum is equal to the given target K ; Create the new array with size equal to array set [ ] to create binary array as per n ( decimal number ) ; Convert the array into binary array ; Calculate the sum of this subset ; Check whether sum is equal to target if it is equal , then print the subset ; Function to find the subsets with sum K ; Calculate the total no . of subsets ; Run loop till total no . of subsets and call the function for each subset ; Driver code"
Solution in C#:

using System ; class GFG { public static void sumSubsets ( int [ ] set , int n , int target ) { int [ ] x = new int [ set . Length ] ; int j = set . Length - 1 ; while ( n > 0 ) { x [ j ] = n % 2 ; n = n / 2 ; j -- ; } int sum = 0 ; for ( int i = 0 ; i < set . Length ; i ++ ) if ( x [ i ] == 1 ) sum = sum + set [ i ] ; if ( sum == target ) { Console . Write ( " { " ) ; for ( int i = 0 ; i < set . Length ; i ++ ) if ( x [ i ] == 1 ) Console . Write ( set [ i ] + " , ▁ " ) ; Console . Write ( " } , ▁ " ) ; } } public static void findSubsets ( int [ ] arr , int K ) { int x = ( int ) Math . Pow ( 2 , arr . Length ) ; for ( int i = 1 ; i < x ; i ++ ) sumSubsets ( arr , i , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 10 , 12 , 13 , 15 , 18 } ; int K = 30 ; findSubsets ( arr , K ) ; } }


"Perfect cubes in a range | A Simple Method to count cubes between a and b ; Traverse through all numbers in given range and one by one check if number is prime ; Check if current number ' i ' is perfect cube ; Driver method"
Solution in C#:

using System ; class GFG { static void printCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { for ( int j = 1 ; j * j * j <= i ; j ++ ) { if ( j * j * j == i ) { Console . Write ( j * j * j + " ▁ " ) ; break ; } } } } public static void Main ( ) { int a = 1 , b = 100 ; Console . WriteLine ( " Perfect ▁ cubes ▁ in " + " ▁ given ▁ range : " ) ; printCubes ( a , b ) ; } }


"Perfect cubes in a range | C # progroam for Efficient method to print cubes between a and b ; An efficient solution to print perfect cubes between a and b ; Find cube root of both a and b ; Print cubes between acrt and bcrt ; Driver Code"
Solution in C#:

using System ; class GFG { static void printCubes ( int a , int b ) { int acrt = ( int ) Math . Pow ( a , ( double ) 1 / 3 ) ; int bcrt = ( int ) Math . Pow ( b , ( double ) 1 / 3 ) ; for ( int i = acrt ; i <= bcrt ; i ++ ) if ( i * i * i >= a && i * i * i <= b ) Console . Write ( i * i * i + " ▁ " ) ; } static public void Main ( ) { int a = 24 ; int b = 576 ; Console . WriteLine ( " Perfect ▁ cubes ▁ " + " in ▁ given ▁ range : " ) ; printCubes ( a , b ) ; } }


"Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | C # program to count number of numbers from 1 to n are of type x ^ y where x > 0 and y > 1 ; Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; Return sum of odd and even powers . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long powerNumbers ( int n ) { HashSet < long > v = new HashSet < long > ( ) ; for ( long i = 2 ; i * i * i <= n ; i ++ ) { long j = i * i ; while ( j * i <= n ) { j *= i ; long s = ( long ) Math . Sqrt ( j ) ; if ( s * s != j ) v . Add ( j ) ; } } return v . Count + ( long ) Math . Sqrt ( n ) ; } static void Main ( ) { Console . Write ( powerNumbers ( 50 ) ) ; } }


"Perfect reversible string | C # program to check if a string is perfect reversible or nor ; This function basically checks if string is palindrome or not ; iterate from left and right ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool isReversible ( string str ) { int i = 0 , j = str . Length - 1 ; while ( i < j ) { if ( str [ i ] != str [ j ] ) { return false ; } i ++ ; j -- ; } return true ; } public static void Main ( string [ ] args ) { string str = " aba " ; if ( isReversible ( str ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }


"Perfect totient number | C # implementation to find the number of digits in a Nth fibonacci number ; Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; Store original value of n ; Loop to calculate sum of iterated totients ; Condition for Perfect Totient Number ; Driver Code"
Solution in C#:

using System ; class GFG { static int phi ( int n ) { int result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) { n /= p ; } result -= result / p ; } } if ( n > 1 ) result -= result / n ; return result ; } static bool isPerfectTotientNum ( int n ) { int temp = n ; int sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } public static void Main ( ) { int n = 9 ; if ( isPerfectTotientNum ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | C # program to convert number to the format [ count ] [ digit ] at every step ; Function to perform every step ; perform N steps ; Traverse in the string ; for last digit ; recur for current string ; Driver Code"
Solution in C#:

using System ; class GFG { public static void countDigits ( string st , int n ) { if ( n > 0 ) { int cnt = 1 , i ; string st2 = " " ; for ( i = 1 ; i < st . Length ; i ++ ) { if ( st [ ( i ) ] == st [ ( i - 1 ) ] ) cnt ++ ; else { st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; cnt = 1 ; } } st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st [ ( i - 1 ) ] ; countDigits ( st2 , -- n ) ; } else Console . Write ( st ) ; } public static void Main ( ) { string num = "123" ; int n = 3 ; countDigits ( num , n ) ; } }


"Perform range sum queries on string as per given condition | C # program for the above approach ; Function to perform range sum queries on String as per the given condition ; Initialize N by String size ; Create array [ ] A for prefix sum ; Iterate till N ; Traverse the queries ; Check if if L == 1 range sum will be A [ R - 1 ] ; Condition if L > 1 range sum will be A [ R - 1 ] - A [ L - 2 ] ; Driver Code ; Given String ; Given Queries ; Function call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void Range_sum_query ( String S , List < pair > Query ) { int N = S . Length ; int [ ] A = new int [ N ] ; A [ 0 ] = S [ 0 ] - ' a ' + 1 ; for ( int i = 1 ; i < N ; i ++ ) { A [ i ] = S [ i ] - ' a ' + 1 ; A [ i ] = A [ i ] + A [ i - 1 ] ; } for ( int i = 0 ; i < Query . Count ; i ++ ) { if ( Query [ i ] . first == 1 ) { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] + "
 " ) ; } else { Console . Write ( A [ ( Query [ i ] . second ) - 1 ] - A [ ( Query [ i ] . first ) - 2 ] + "
 " ) ; } } } public static void Main ( String [ ] args ) { String S = " abcd " ; List < pair > Query = new List < pair > ( ) ; Query . Add ( new pair ( 2 , 4 ) ) ; Query . Add ( new pair ( 1 , 3 ) ) ; Range_sum_query ( S , Query ) ; } }


"Perimeter and Area of Varignon 's Parallelogram | C # code to find the perimeter and area ; Function to find the perimeter ; Function to find the area ; Driver code"
Solution in C#:

using System ; class GFG { public static double per ( double a , double b ) { return ( a + b ) ; } public static double area ( double s ) { return ( s / 2 ) ; } public static void Main ( ) { double a = 7.0 , b = 8.0 , s = 10.0 ; Console . WriteLine ( per ( a , b ) ) ; Console . Write ( area ( s ) ) ; } }


"Perimeter of an Ellipse | C # program to find perimeter of an Ellipse . ; Function to find perimeter of an ellipse . ; formula to find the Perimeter of an Ellipse . ; Display the result ; Driver code"
Solution in C#:

using System ; class GFG1 { static void Perimeter ( double a , double b ) { double Perimeter ; Perimeter = ( double ) 2 * 3.14 * Math . Sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ; Console . WriteLine ( " Perimeter : ▁ " + Perimeter ) ; } public static void Main ( String [ ] args ) { double a = 3 , b = 2 ; Perimeter ( a , b ) ; } }


"Perimeter of the Union of Two Rectangles | C # program for the above approach ; Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code"
Solution in C#:

using System ; using System . Linq ; public class GFG { static bool doIntersect ( int [ ] X , int [ ] Y ) { if ( X [ 0 ] > X [ 3 ] X [ 2 ] > X [ 1 ] ) return false ; if ( Y [ 0 ] > Y [ 3 ] Y [ 2 ] > Y [ 1 ] ) return false ; return true ; } static int getUnionPerimeter ( int [ ] X , int [ ] Y ) { int perimeter = 0 ; if ( ! doIntersect ( X , Y ) ) { perimeter += 2 * ( Math . Abs ( X [ 1 ] - X [ 0 ] ) + Math . Abs ( Y [ 1 ] - Y [ 0 ] ) ) ; perimeter += 2 * ( Math . Abs ( X [ 3 ] - X [ 2 ] ) + Math . Abs ( Y [ 3 ] - Y [ 2 ] ) ) ; } else { int w = X . Max ( ) - X . Min ( ) ; int l = X . Max ( ) - Y . Min ( ) ; perimeter = 2 * ( l + w ) ; } return perimeter ; } public static void Main ( String [ ] args ) { int [ ] X = { - 1 , 2 , 4 , 6 } ; int [ ] Y = { 2 , 5 , 3 , 7 } ; Console . Write ( getUnionPerimeter ( X , Y ) ) ; } }


"Periodic Binary String With Minimum Period and a Given Binary String as Subsequence . | C # implementation to find the periodic string with minimum period ; Function to find the periodic string with minimum period ; Print the string S if it consists of similar elements ; Find the required periodic string with period 2 ; Driver code"
Solution in C#:

using System ; class GFG { static void findPeriodicString ( string S ) { int l = 2 * S . Length ; int count = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( S [ i ] == '1' ) count ++ ; } if ( count == S . Length count == 0 ) Console . WriteLine ( S ) ; else { char [ ] arr = new char [ l ] ; for ( int i = 0 ; i < l ; i += 2 ) { arr [ i ] = '1' ; arr [ i + 1 ] = '0' ; } for ( int i = 0 ; i < l ; i ++ ) Console . Write ( arr [ i ] ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { string S = "1111001" ; findPeriodicString ( S ) ; } }


"Permutation of Array such that products of all adjacent elements are even | C # program to permutation of array such that product of all adjacent elements is even ; Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; Else print the permutation ; Print remaining odds are even . and even elements ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printPermutation ( int [ ] arr , int n ) { List < int > odd = new List < int > ( ) ; List < int > even = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . Add ( arr [ i ] ) ; else odd . Add ( arr [ i ] ) ; } int size_odd = odd . Count ; int size_even = even . Count ; if ( size_odd > size_even + 1 ) Console . WriteLine ( " - 1" ) ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { Console . Write ( odd [ i ] + " ▁ " ) ; ++ i ; Console . Write ( even [ j ] + " ▁ " ) ; ++ j ; } while ( i < size_odd ) { Console . Write ( odd [ i ] + " ▁ " ) ; ++ i ; } while ( j < size_even ) { Console . Write ( even [ j ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = arr . Length ; printPermutation ( arr , N ) ; } }


"Permutation of Array such that sum of adjacent elements are not divisible by 3 | C # implementation to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Function to segregate numbers based on their remainder when divided by three ; Loop to iterate over the elements of the given array ; Condition to check the remainder of the number ; Function to find the permutation of the array such that sum of adjacent elements is not divisible by 3 ; Condition to check when it 's impossible to arrange ; Condition to check when there are no zeros , and only ones or only twos ; Array to store the permutation ; Place the ones on alternate places in the answer array , leaving spaces for zeros remainder elements in the array ; Adding a zero to connect it with a two ; Place the twos on alternate places in the answer array , leaving spaces for zeros ; Fill the zeros finally , between the ones and the twos ; Print the arrangement of the array ; Function to solve the problem ; As there can be only 3 remainders ; Function Call ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int hell = 1000000007 ; static readonly int N = 100005 ; static int c_0 , c_1 , c_2 ; static void count_k ( int [ ] arr , Stack < int > ones , Stack < int > twos , Stack < int > zeros ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] % 3 == 0 ) { c_0 ++ ; zeros . Push ( arr [ i ] ) ; } else if ( arr [ i ] % 3 == 1 ) { c_1 ++ ; ones . Push ( arr [ i ] ) ; } else { c_2 ++ ; twos . Push ( arr [ i ] ) ; } } return ; } static void printArrangement ( int [ ] arr , Stack < int > ones , Stack < int > twos , Stack < int > zeros ) { if ( ( c_0 == 0 && c_1 != 0 && c_2 != 0 ) c_0 > c_1 + c_2 + 1 ) { Console . Write ( " - 1" ) ; return ; } int i ; if ( c_0 == 0 ) { for ( i = 0 ; i < arr . Length ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } return ; } int j ; int [ ] ans = new int [ N ] ; for ( i = 0 ; i < ans . Length ; i ++ ) ans [ i ] = - 1 ; for ( i = 1 , j = 0 ; j < c_1 ; i += 2 , j ++ ) { ans [ i ] = ones . Peek ( ) ; ones . Pop ( ) ; } ans [ i - 1 ] = zeros . Peek ( ) ; zeros . Pop ( ) ; c_0 -- ; for ( j = 0 ; j < c_2 ; j ++ , i += 2 ) { ans [ i ] = twos . Peek ( ) ; twos . Pop ( ) ; } for ( int k = 0 ; c_0 > 0 ; k += 2 ) { if ( ans [ k ] == - 1 ) { ans [ k ] = zeros . Peek ( ) ; c_0 -- ; } } for ( int i1 = 0 ; i1 < N ; i1 ++ ) { if ( ans [ i1 ] != - 1 ) Console . Write ( ans [ i1 ] + " ▁ " ) ; } return ; } static void solve ( int n , int [ ] arr ) { Stack < int > ones = new Stack < int > ( ) ; Stack < int > zeros = new Stack < int > ( ) ; Stack < int > twos = new Stack < int > ( ) ; c_0 = 0 ; c_1 = 0 ; c_2 = 0 ; count_k ( arr , ones , twos , zeros ) ; printArrangement ( arr , ones , twos , zeros ) ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; solve ( n , arr ) ; } }


"Permutation of first N positive integers such that prime numbers are at prime indices | Set 2 | C # program to count permutations from 1 to N such that prime numbers occurr at prime indices ;  ; Computing count of prime numbers using sieve ; Computing permutations for primes ; Computing permutations for non - primes ; Driver code"
Solution in C#:

using System ; class GFG { static int numPrimeArrangements ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) { for ( int factor = 2 ; factor * i <= n ; factor ++ ) prime [ factor * i ] = false ; } } int primeIndices = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( prime [ i ] ) primeIndices ++ ; int mod = ( int ) ( 1e9 + 7 ) , res = 1 ; for ( int i = 1 ; i <= primeIndices ; i ++ ) res = ( int ) ( ( 1L * res * i ) % mod ) ; for ( int i = 1 ; i <= ( n - primeIndices ) ; i ++ ) res = ( int ) ( ( 1L * res * i ) % mod ) ; return res ; } public static void Main ( String [ ] args ) { int N = 5 ; Console . Write ( numPrimeArrangements ( N ) ) ; } }


"Permutations of an array having sum of Bitwise AND of adjacent elements at least K | C # program for the above approach ; Function to print all permutations of [ ] arr such that the sum of Bitwise AND of all adjacent element is at least K ; To check if any permutation exists or not ; Sort the given array ; Find all possible permutations ; Stores the sum of bitwise AND of adjacent elements of the current permutation ; Traverse the current permutation of [ ] arr ; Update the sum ; If the sum is at least K , then print the current permutation ; Set the flag variable ; Print the current permutation ; If flag is unset , then print - 1 ; Driver Code ; Function Call"
Solution in C#:

 using System ; class GFG { static void printPermutation ( int [ ] arr , int n , int k ) { bool flag = false ; Array . Sort ( arr ) ; do { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] & arr [ i + 1 ] ; } if ( sum >= k ) { flag = true ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . Write ( "
 " ) ; } } while ( next_permutation ( arr ) ) ; if ( flag == false ) { Console . Write ( " - 1" ) ; } } static bool next_permutation ( int [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int K = 8 ; int N = arr . Length ; printPermutation ( arr , N , K ) ; } }


"Permutations of n things taken r at a time with k things together | C # program to find the number of permutations of n different things taken r at a time with k things grouped together ; Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int npr ( int n , int r ) { int pnr = factorial ( n ) / factorial ( n - r ) ; return pnr ; } static int countPermutations ( int n , int r , int k ) { return factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ; } static void Main ( ) { int n = 8 ; int r = 5 ; int k = 2 ; Console . WriteLine ( countPermutations ( n , r , k ) ) ; } }


"Permutations of string such that no two vowels are adjacent | C # program to count permutations of string such that no two vowels are adjacent ; Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } static int countWays ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { freq [ i ] = 0 ; } int nvowels = 0 , nconsonants = 0 ; int vplaces , cways , vways ; for ( int i = 0 ; i < str . Length ; i ++ ) ++ freq [ str [ i ] - ' a ' ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) nvowels += freq [ i ] ; else nconsonants += freq [ i ] ; } vplaces = nconsonants + 1 ; cways = factorial ( nconsonants ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 && freq [ i ] > 1 ) { cways = cways / factorial ( freq [ i ] ) ; } } vways = ncr ( vplaces , nvowels ) * factorial ( nvowels ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 && freq [ i ] > 1 ) { vways = vways / factorial ( freq [ i ] ) ; } } return cways * vways ; } public static void Main ( ) { String str = " permutation " ; Console . WriteLine ( countWays ( str ) ) ; } }


"Permutations to arrange N persons around a circular table | C # code to demonstrate Circular Permutation ; Function to find no . of permutations ; Driver Code"
Solution in C#:

using System ; public class GFG { static int Circular ( int n ) { int Result = 1 ; while ( n > 0 ) { Result = Result * n ; n -- ; } return Result ; } public static void Main ( ) { int n = 4 ; Console . Write ( Circular ( n - 1 ) ) ; } }


"Perpendicular distance between a point and a Line in 2 D | C # program to find the distance between a given point and a given line in 2 D . ; Function to find distance ; Driver code"
Solution in C#:

using System ; class GFG { static void shortest_distance ( float x1 , float y1 , float a , float b , float c ) { double d = Math . Abs ( ( ( a * x1 + b * y1 + c ) ) / ( Math . Sqrt ( a * a + b * b ) ) ) ; Console . WriteLine ( " Perpendicular ▁ " + " distance ▁ is ▁ " + d ) ; return ; } public static void Main ( ) { float x1 = 5 ; float y1 = 6 ; float a = - 2 ; float b = 3 ; float c = 4 ; shortest_distance ( x1 , y1 , a , b , c ) ; } }


"Philaland Coin | TCS Mockvita 2020 | C # implementation to find the minimum number of denominations required for any number ; Function to find the minimum number of denomminations required ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int findMinDenomin ( int n ) { return ( ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ) ; } static public void Main ( ) { int n = 10 ; Console . WriteLine ( findMinDenomin ( n ) ) ; } }


"Pierpont Prime | C # program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driven Program"
Solution in C#:

using System ; using System . Collections ; class GFG { static void printPierpont ( int n ) { bool [ ] arr = new bool [ n + 1 ] ; int two = 1 , three = 1 ; while ( two + 1 < n ) { arr [ two ] = true ; while ( two * three + 1 < n ) { arr [ three ] = true ; arr [ two * three ] = true ; three *= 3 ; } three = 1 ; two *= 2 ; } ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] ) v . Add ( i + 1 ) ; arr = new bool [ n + 1 ] ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( arr [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) arr [ i ] = true ; } for ( int i = 0 ; i < v . Count ; i ++ ) if ( ! arr [ ( int ) v [ i ] ] ) Console . Write ( v [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 200 ; printPierpont ( n ) ; } }


"Pizza cut problem ( Or Circle Division by Lines ) | C # program to find maximum no of pieces by given number of cuts ; Function for finding maximum pieces with n cuts . ; Driver Program to test above function"
Solution in C#:

using System ; class GFG { static int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; } public static void Main ( ) { Console . Write ( findMaximumPieces ( 3 ) ) ; } }


"Place N ^ 2 numbers in matrix such that every row has an equal sum | C # program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; Using modulo to go to the firs column after the last column ; Making a 2D array containing numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int [ , ] solve ( int [ , ] arr , int n ) { int [ , ] ans = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i , j ] = arr [ j , ( i + j ) % n ] ; } } return ans ; } static void show ( int [ , ] arr , int n ) { int [ , ] res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( res [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static int [ , ] makeArray ( int n ) { int [ , ] arr = new int [ n , n ] ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) arr [ i , j ] = c ++ ; } return arr ; } static void Main ( ) { int n = 5 ; int [ , ] arr = makeArray ( n ) ; show ( arr , n ) ; } }


"Place N boys and M girls in different rows such that count of persons placed in each row is maximized | C # Program to implement the above approach ; Function to calculate GCD of two numbers ; Function to count maximum persons that can be placed in a row ; Driver Code ; Input ; Function to count maximum persons that can be placed in a row"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumRowValue ( int n , int m ) { return gcd ( n , m ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int M = 2 ; Console . WriteLine ( maximumRowValue ( N , M ) ) ; } }


"Place Value of a given digit in a number | C # implementation to find place value of a number ; function to find place value ; Driver Code ; Digit , which we want to find place value . ; Number from where we want to find place value ."
Solution in C#:

using System ; class GFG { static int placeValue ( int N , int num ) { int total = 1 , value = 0 , rem = 0 ; while ( true ) { rem = N % 10 ; N = N / 10 ; if ( rem == num ) { value = total * rem ; break ; } total = total * 10 ; } return value ; } public static void Main ( ) { int D = 5 ; int N = 85932 ; Console . Write ( placeValue ( N , D ) ) ; } }


"Place first N natural numbers at indices not equal to their values in an array | C # program for the above approach ; Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code"
Solution in C#:

using System ; class GFG { static void generatepermutation ( int N ) { int [ ] answer = new int [ N ] ; answer [ 0 ] = N ; for ( int i = 1 ; i < N ; i ++ ) { answer [ i ] = i ; } foreach ( int i in answer ) Console . Write ( i + " ▁ " ) ; } static public void Main ( ) { int N = 4 ; generatepermutation ( N ) ; } }


"Place k elements such that minimum distance is maximized | C # program to find largest minimum distance among k points . ; Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid if all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; driver code"
Solution in C#:

using System ; public class GFG { static bool isFeasible ( int mid , int [ ] arr , int n , int k ) { int pos = arr [ 0 ] ; int elements = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - pos >= mid ) { pos = arr [ i ] ; elements ++ ; if ( elements == k ) return true ; } } return false ; } static int largestMinDist ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int res = - 1 ; int left = 1 , right = arr [ n - 1 ] ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isFeasible ( mid , arr , n , k ) ) { res = Math . Max ( res , mid ) ; left = mid + 1 ; } else right = mid ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 8 , 4 , 9 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( largestMinDist ( arr , n , k ) ) ; } }


"Place the prisoners into cells to maximize the minimum difference between any two | C # implementation of the approach ; Function that returns true if the prisoners can be placed such that the minimum distance between any two prisoners is at least sep ; Considering the first prisoner is placed at 1 st cell ; If the first prisoner is placed at the first cell then the last_prisoner_placed will be the first prisoner placed and that will be in cell [ 0 ] ; Checking if the prisoner can be placed at ith cell or not ; If all the prisoners got placed then return true ; Function to return the maximized distance ; Sort the array so that binary search can be applied on it ; Minimum possible distance for the search space ; Maximum possible distance for the search space ; To store the result ; Binary search ; If the prisoners can be placed such that the minimum distance between any two prisoners is at least mid ; Update the answer ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static bool canPlace ( int [ ] a , int n , int p , int sep ) { int prisoners_placed = 1 ; int last_prisoner_placed = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int current_cell = a [ i ] ; if ( current_cell - last_prisoner_placed >= sep ) { prisoners_placed ++ ; last_prisoner_placed = current_cell ; if ( prisoners_placed == p ) { return true ; } } } return false ; } static int maxDistance ( int [ ] cell , int n , int p ) { Array . Sort ( cell ) ; int start = 0 ; int end = cell [ n - 1 ] - cell [ 0 ] ; int ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( canPlace ( cell , n , p , mid ) ) { ans = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } public static void Main ( ) { int [ ] cell = { 1 , 2 , 8 , 4 , 9 } ; int n = cell . Length ; int p = 3 ; Console . WriteLine ( maxDistance ( cell , n , p ) ) ; } }


"Point arbit pointer to greatest value right side node in a linked list | C # program to point arbit pointers to highest value on its right ; Link list node ; Function to reverse the linked list ; This function populates arbit pointer in every node to the greatest value to its right . ; Reverse given linked list ; Initialize pointer to maximum value node ; Traverse the reversed list ; Connect max through arbit pointer ; Update max if required ; Move ahead in reversed list ; Reverse modified linked list and return head . ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code"
Solution in C#:

using System ; class GfG { class Node { public int data ; public Node next , arbit ; } static Node reverse ( Node head ) { Node prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node populateArbit ( Node head ) { head = reverse ( head ) ; Node max = head ; Node temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } static void printNextArbitPointers ( Node node ) { Console . WriteLine ( " Node 	 Next ▁ Pointer 	 Arbit ▁ Pointer " ) ; while ( node != null ) { Console . Write ( node . data + " 	 	 " ) ; if ( node . next != null ) Console . Write ( node . next . data + " 	 	 " ) ; else Console . Write ( " NULL " + " 	 	 " ) ; if ( node . arbit != null ) Console . Write ( node . arbit . data ) ; else Console . Write ( " NULL " ) ; Console . WriteLine ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void Main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; head = populateArbit ( head ) ; Console . WriteLine ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }


"Point arbit pointer to greatest value right side node in a linked list | C # program to point arbit pointers to highest value on its right ; Link list node ; This function populates arbit pointer in every node to the greatest value to its right . ; if head is null simply return the list ; if head -> next is null it means we reached at the last node just update the max and maxNode ; Calling the populateArbit to the next node ; updating the arbit node of the current node with the maximum value on the right side ; if current Node value id greater then the previous right node then update it ; Utility function to print result linked list ; Function to create a new node with given data ; Driver code"
Solution in C#:

using System ; class GfG { public class Node { public int data ; public Node next , arbit ; } static Node maxNode ; static void populateArbit ( Node head ) { if ( head == null ) return ; if ( head . next == null ) { maxNode = head ; return ; } populateArbit ( head . next ) ; head . arbit = maxNode ; if ( head . data > maxNode . data ) maxNode = head ; return ; } static void printNextArbitPointers ( Node node ) { Console . WriteLine ( " Node 	 Next ▁ Pointer 	 Arbit ▁ Pointer " ) ; while ( node != null ) { Console . Write ( node . data + " 	 	 	 " ) ; if ( node . next != null ) Console . Write ( node . next . data + " 	 	 	 	 " ) ; else Console . Write ( " NULL " + " 	 	 	 " ) ; if ( node . arbit != null ) Console . Write ( node . arbit . data ) ; else Console . Write ( " NULL " ) ; Console . WriteLine ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void Main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; populateArbit ( head ) ; Console . WriteLine ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }


"Pointer | C # program for above approach ; Function to find the product of digits of a number N ; Function that returns true if n is prime else returns false ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the smallest prime number greater than N ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; Function to check Pointer - Prime numbers ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

using System ; class GFG { static int digProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; bool found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } static bool isPointerPrime ( int n ) { if ( isPrime ( n ) && ( n + digProduct ( n ) == nextPrime ( n ) ) ) return true ; else return false ; } public static void Main ( ) { int N = 23 ; if ( isPointerPrime ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Polybius Square Cipher | C # Program to implement polybius cipher ; Function to display polybius cipher text ; convert each character to its encrypted code ; finding row of the table ; finding column of the table ; if character is ' k ' ; if character is greater than ' j ' ; Driver code ; print the cipher of "geeksforgeeks"
Solution in C#:

using System ; class GFG { static void polybiusCipher ( string s ) { int row , col ; for ( int i = 0 ; i < s . Length ; i ++ ) { row = ( int ) Math . Floor ( ( s [ i ] - ' a ' ) / 5.0 ) + 1 ; col = ( ( s [ i ] - ' a ' ) % 5 ) + 1 ; if ( s [ i ] == ' k ' ) { row = row - 1 ; col = 5 - col + 1 ; } else if ( s [ i ] >= ' j ' ) { if ( col == 1 ) { col = 6 ; row = row - 1 ; } col = col - 1 ; } Console . Write ( row + " " + col ) ; } Console . WriteLine ( ) ; } static void Main ( ) { string s = " geeksforgeeks " ; polybiusCipher ( s ) ; } }


"Populate Inorder Successor for all nodes | C # program to populate inorder traversal of all nodes ; A binary tree node ; Set next of p and all descendants of p by traversing them in reverse Inorder ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in left subtree ; Constructed binary tree is 10 / \ 8 12 / 3 ; Populates nextRight pointer in all nodes ; Let us see the populated values ; - 1 is printed if there is no successor"
Solution in C#:

using System ; class BinaryTree { class Node { public int data ; public Node left , right , next ; public Node ( int item ) { data = item ; left = right = next = null ; } } Node root ; static Node next = null ; void populateNext ( Node node ) { if ( node != null ) { populateNext ( node . right ) ; node . next = next ; next = node ; populateNext ( node . left ) ; } } static public void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 3 ) ; tree . populateNext ( tree . root ) ; Node ptr = tree . root . left . left ; while ( ptr != null ) { int print = ptr . next != null ? ptr . next . data : - 1 ; Console . WriteLine ( " Next ▁ of ▁ " + ptr . data + " ▁ is : ▁ " + print ) ; ptr = ptr . next ; } } }


"Populate Inorder Successor for all nodes | C # program to populate inorder traversal of all nodes ; A wrapper over populateNextRecur ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; Set next of all descendants of p by traversing them in reverse Inorder ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in right subtree"
Solution in C#:

using System ; class Node { public int data ; public Node left , right , next ; public Node ( int item ) { data = item ; left = right = next = null ; } } Node root ; static Node next = null ; void populateNext ( Node node ) { populateNextRecur ( node , next ) ; } void populateNextRecur ( Node p , Node next_ref ) { if ( p != null ) { populateNextRecur ( p . right , next_ref ) ; p . next = next_ref ; next_ref = p ; populateNextRecur ( p . left , next_ref ) ; } }


"Position after taking N steps to the right and left in an alternate manner | C # program to find the last coordinate where it ends his journey ; Function to return the last destination ; Driver Code"
Solution in C#:

using System ; class GFG { public static int lastCoordinate ( int n , int a , int b ) { return ( ( n + 1 ) / 2 ) * a - ( n / 2 ) * b ; } public static void Main ( string [ ] args ) { int n = 3 , a = 5 , b = 2 ; Console . WriteLine ( lastCoordinate ( n , a , b ) ) ; } }


"Position of a person diametrically opposite on a circle | C # implementation of the approach ; Function to return the required position ; Driver code"
Solution in C#:

using System ; class GFG { static int getPosition ( int n , int m ) { if ( m > ( n / 2 ) ) return ( m - ( n / 2 ) ) ; return ( m + ( n / 2 ) ) ; } static public void Main ( ) { int n = 8 , m = 5 ; Console . WriteLine ( getPosition ( n , m ) ) ; } }


"Position of an element after stable sort | C # program to get index of array element in sorted array ; Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver code to test above methods"
Solution in C#:

using System ; class ArrayIndex { static int getIndexInSortedArray ( int [ ] arr , int n , int idx ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ idx ] ) result ++ ; if ( arr [ i ] == arr [ idx ] && i < idx ) result ++ ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 } ; int n = arr . Length ; int idxOfEle = 5 ; Console . WriteLine ( getIndexInSortedArray ( arr , n , idxOfEle ) ) ; } }


"Position of rightmost bit with first carry in sum of two binary | C # implementation to find the position of rightmost bit where a carry is generated first ; function to find the position of rightmost set bit in ' n ' ; function to find the position of rightmost bit where a carry is generated first ; Driver code"
Solution in C#:

using System ; class GFG { static int posOfRightmostSetBit ( int n ) { return ( int ) ( Math . Log ( n & - n ) / Math . Log ( 2 ) ) + 1 ; } static int posOfCarryBit ( int a , int b ) { return posOfRightmostSetBit ( a & b ) ; } public static void Main ( ) { int a = 10 , b = 2 ; Console . Write ( posOfCarryBit ( a , b ) ) ; } }


"Position of rightmost common bit in two numbers | C # implementation to find the position of rightmost same bit ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; Initialize loop counter ; Check whether the value ' m ' is odd ; Check whether the value ' n ' is odd ; Below ' if ' checks for both values to be odd or even ; Right shift value of m ; Right shift value of n ; When no common set is found ; Driver code"
Solution in C#:

using System ; class GFG { static int posOfRightMostSameBit ( int m , int n ) { int loopCounter = 1 ; while ( m > 0 n > 0 ) { Boolean a = m % 2 == 1 ; Boolean b = n % 2 == 1 ; if ( ! ( a ^ b ) ) { return loopCounter ; } m = m >> 1 ; n = n >> 1 ; loopCounter ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { int m = 16 , n = 7 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }


"Position of rightmost common bit in two numbers | C # implementation to find the position of rightmost same bit ; Function to find the position of rightmost set bit in ' n ' ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; position of rightmost same bit ; Driver code"
Solution in C#:

using System ; class GFG { static int getRightMostSetBit ( int n ) { return ( int ) ( ( Math . Log ( n & - n ) ) / ( Math . Log ( 2 ) ) ) + 1 ; } static int posOfRightMostSameBit ( int m , int n ) { return getRightMostSetBit ( ~ ( m ^ n ) ) ; } public static void Main ( ) { int m = 16 , n = 7 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }


"Position of rightmost different bit | C # implementation to find the position of rightmost different bit ; Function to find the position of rightmost set bit in ' n ' ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' ; position of rightmost different bit ; Driver code"
Solution in C#:

using System ; class GFG { static int getRightMostSetBit ( int n ) { if ( n == 0 ) return 0 ; return ( int ) ( ( Math . Log10 ( n & - n ) ) / Math . Log10 ( 2 ) ) + 1 ; } static int posOfRightMostDiffBit ( int m , int n ) { return getRightMostSetBit ( m ^ n ) ; } public static void Main ( ) { int m = 52 , n = 4 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }


"Position of rightmost different bit | C # implementation to find the position of rightmost different bit in two number . ; function to find rightmost different bit in two numbers . ; Driver code"
Solution in C#:

using System ; class GFG { static int posOfRightMostDiffBit ( int m , int n ) { return ( int ) Math . Floor ( Math . Log10 ( Math . Pow ( m ^ n , 2 ) ) ) + 2 ; } public static void Main ( String [ ] args ) { int m = 52 , n = 4 ; Console . Write ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }


"Position of rightmost set bit | C # Code for Position of rightmost set bit ; Driver method"
Solution in C#:

using System ; class GFG { public static int getFirstSetBitPos ( int n ) { return ( int ) ( ( Math . Log10 ( n & - n ) ) / Math . Log10 ( 2 ) ) + 1 ; } public static void Main ( ) { int n = 12 ; Console . WriteLine ( getFirstSetBitPos ( n ) ) ; } }


"Position of rightmost set bit | C # implementation of above approach ; counting the position of first set bit ; Driver code"
Solution in C#:

using System ; class GFG { static int INT_SIZE = 32 ; static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } static public void Main ( ) { int num = 18 ; int pos = Right_most_setbit ( num ) ; Console . WriteLine ( pos ) ; } }


"Position of rightmost set bit | C # program for above approach ; Function to find position of rightmost set bit ; Iterate till number > 0 ; Checking if last bit is set ; Increment position and right shift number ; Set bit not found . ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { public static int Last_set_bit ( int n ) { int p = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) { return p ; } p ++ ; n = n >> 1 ; } return - 1 ; } public static void Main ( string [ ] args ) { int n = 18 ; int pos = Last_set_bit ( n ) ; if ( pos != - 1 ) Console . WriteLine ( pos ) ; else Console . WriteLine ( "0" ) ; } }


"Position of rightmost set bit | C # program to find the first rightmost set bit using XOR operator ; function to find the rightmost set bit ; Position variable initialize with 1 m variable is used to check the set bit ; left shift ; Driver Code ; function call"
Solution in C#:

using System ; class GFG { static int PositionRightmostSetbit ( int n ) { int position = 1 ; int m = 1 ; while ( ( n & m ) == 0 ) { m = m << 1 ; position ++ ; } return position ; } static public void Main ( ) { int n = 16 ; Console . WriteLine ( PositionRightmostSetbit ( n ) ) ; } }


"Position of robot after given movements | C # implementation to find final position of robot after the complete movement ; function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver code"
Solution in C#:

using System ; class GFG { static void finalPosition ( String move ) { int l = move . Length ; int countUp = 0 , countDown = 0 ; int countLeft = 0 , countRight = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' U ' ) countUp ++ ; else if ( move [ i ] == ' D ' ) countDown ++ ; else if ( move [ i ] == ' L ' ) countLeft ++ ; else if ( move [ i ] == ' R ' ) countRight ++ ; } Console . WriteLine ( " Final ▁ Position : ▁ ( " + ( countRight - countLeft ) + " , ▁ " + ( countUp - countDown ) + " ) " ) ; } public static void Main ( ) { String move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( move ) ; } }


"Position of the K | C # program to implement the above approach ; Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static int FindIndexKthBit ( int n , int k ) { int cnt = 0 ; int ind = 0 ; while ( n > 0 ) { if ( ( n & 1 ) != 0 ) cnt ++ ; if ( cnt == k ) return ind ; ind ++ ; n = n >> 1 ; } return - 1 ; } public static void Main ( ) { int n = 15 , k = 3 ; int ans = FindIndexKthBit ( n , k ) ; if ( ans != - 1 ) Console . WriteLine ( ans ) ; else Console . WriteLine ( " No ▁ k - th ▁ set ▁ bit " ) ; } }


"Positive elements at even and negative at odd positions ( Relative order not maintained ) | C # program to rearrange positive and negative numbers ; Move forward the positive pointer till negative number number not encountered ; Move forward the negative pointer till positive number number not encountered ; Swap array elements to fix their position . ; Break from the while loop when any index exceeds the size of the array ; Driver Code"
Solution in C#:

using System ; class GFG { static void rearrange ( int [ ] a , int size ) { int positive = 0 , negative = 1 , temp ; while ( true ) { while ( positive < size && a [ positive ] >= 0 ) positive += 2 ; while ( negative < size && a [ negative ] <= 0 ) negative += 2 ; if ( positive < size && negative < size ) { temp = a [ positive ] ; a [ positive ] = a [ negative ] ; a [ negative ] = temp ; } else break ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . Length ; rearrange ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Positive elements at even and negative at odd positions ( Relative order not maintained ) | C # program to rearrange positive and negative numbers ; Swap function ; Print array function ; Driver code ; before modification ; out of order positive element ; find out of order negative element in remaining array ; out of order negative element ; find out of order positive element in remaining array ; after modification"
Solution in C#:

using System ; class GFG { static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . Length ; printArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && i % 2 == 1 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < 0 && j % 2 == 0 ) { swap ( arr , i , j ) ; break ; } } } else if ( arr [ i ] < 0 && i % 2 == 0 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] >= 0 && j % 2 == 1 ) { swap ( arr , i , j ) ; break ; } } } } printArray ( arr , n ) ; } }


"Possibility of a word from a given set of characters | C # program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; driver program"
Solution in C#:

using System ; public class GFG { static int MAX_CHAR = 256 ; static bool isPresent ( string s , string q ) { int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < s . Length ; i ++ ) freq [ s [ i ] ] ++ ; for ( int i = 0 ; i < q . Length ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } return true ; } static public void Main ( ) { string s = " abctd " ; string q = " cat " ; if ( isPresent ( s , q ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Possibility of moving out of maze | C # Possibility of moving out of maze ; Function to check whether it will stay inside or come out ; marks all the positions that is visited ; Initial starting point ; initial assumption is it comes out ; runs till it is inside or comes out ; if the movement is towards left then we move left . The start variable and mark that position as visited if not visited previously . Else we break out ; It will be inside forever ; If the movement is towards right , then we move right . The start variable and mark that position as visited if not visited previously else we break out ; it will be inside forever ; Driver code"
Solution in C#:

using System ; class GFG { static void checkingPossibility ( int [ ] a , int n , String s ) { int [ ] mark = new int [ a [ 0 ] * n ] ; int start = 0 ; int possible = 1 ; while ( start >= 0 && start < n ) { if ( s == " < " ) { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start -= a [ start ] ; } else { possible = 0 ; break ; } } else { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start += a [ start ] ; } else { possible = 0 ; break ; } } } if ( possible == 0 ) Console . Write ( " it ▁ stays ▁ " + " inside ▁ forever " ) ; else Console . Write ( " comes ▁ out " ) ; } public static void Main ( ) { int n = 2 ; String s = " > < " ; int [ ] a = { 1 , 2 } ; checkingPossibility ( a , n , s ) ; } }


"Possible arrangement of persons waiting to sit in a hall | C # program for the above approach ; Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Driver code ; Given N ; Given arr [ ] ; Given string ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findTheOrder ( int [ ] arr , string s , int N ) { List < int > ans = new List < int > ( ) ; Tuple < int , int > [ ] A = new Tuple < int , int > [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = new Tuple < int , int > ( arr [ i ] , i + 1 ) ; Array . Sort ( A ) ; List < Tuple < int , int > > q = new List < Tuple < int , int > > ( ) ; int index = 0 ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == '0' ) { ans . Add ( A [ index ] . Item2 ) ; q . Add ( A [ index ] ) ; q . Sort ( ) ; q . Reverse ( ) ; index ++ ; } else { ans . Add ( q [ 0 ] . Item2 ) ; q . RemoveAt ( 0 ) ; } } foreach ( int i in ans ) { Console . Write ( i + " ▁ " ) ; } } static void Main ( ) { int N = 3 ; int [ ] arr = { 2 , 1 , 3 } ; string s = "001011" ; findTheOrder ( arr , s , N ) ; } }


"Possible cuts of a number such that maximum parts are divisible by 3 | C # program to find the maximum number of numbers divisible by 3 in a large number . ; Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code"
Solution in C#:

using System ; class GFG { static int MaximumNumbers ( String s ) { int n = s . Length ; int [ ] remIndex = { - 1 , - 1 , - 1 } ; remIndex [ 0 ] = 0 ; int [ ] res = new int [ n + 1 ] ; int r = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { r = ( r + s [ i - 1 ] - '0' ) % 3 ; res [ i ] = res [ i - 1 ] ; if ( remIndex [ r ] != - 1 ) res [ i ] = Math . Max ( res [ i ] , res [ remIndex [ r ] ] + 1 ) ; remIndex [ r ] = i + 1 ; } return res [ n ] ; } public static void Main ( String [ ] args ) { String s = "12345" ; Console . WriteLine ( MaximumNumbers ( s ) ) ; } }


"Possible cuts of a number such that maximum parts are divisible by 3 | C # program to find the maximum number of numbers divisible by 3 in large number ; This will contain the count of the splits ; This will keep sum of all successive integers , when they are indivisible by 3 ; This is the condition of finding a split ; Driver code"
Solution in C#:

 using System ; class GFG { static int get_max_splits ( String num_String ) { int count = 0 , current_num ; int running_sum = 0 ; for ( int i = 0 ; i < num_String . Length ; i ++ ) { current_num = num_String [ i ] - '0' ; running_sum += current_num ; if ( current_num % 3 == 0 || ( running_sum != 0 && running_sum % 3 == 0 ) ) { count += 1 ; running_sum = 0 ; } } return count ; } public static void Main ( String [ ] args ) { Console . Write ( get_max_splits ( "12345" ) + "
 " ) ; } }


"Possible edges of a tree for given diameter , height and vertices | C # program to construct tree for given count width and height . ; Function to construct the tree ; Special case when d == 2 , only one edge ; Tree is not possible ; Satisfy the height condition by add edges up to h ; Add d - h edges from 1 to satisfy diameter condition ; Remaining edges at vertex 1 or 2 ( d == h ) ; Driver Code"
Solution in C#:

using System ; class GfG { static void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { Console . WriteLine ( "1 ▁ 2" ) ; return ; } Console . WriteLine ( " - 1" ) ; return ; } if ( d > 2 * h ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 1 ; i <= h ; i ++ ) Console . WriteLine ( i + " ▁ " + ( i + 1 ) ) ; if ( d > h ) { Console . WriteLine ( "1" + " ▁ " + ( h + 2 ) ) ; for ( int i = h + 2 ; i <= d ; i ++ ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) ) ; } } for ( int i = d + 1 ; i < n ; i ++ ) { int k = 1 ; if ( d == h ) k = 2 ; Console . WriteLine ( k + " ▁ " + ( i + 1 ) ) ; } } public static void Main ( String [ ] args ) { int n = 5 , d = 3 , h = 2 ; constructTree ( n , d , h ) ; } }


"Possible moves of knight | C # program to find number of possible moves of knight ; To calculate possible moves ; All possible moves of a knight ; Check if each possible move is valid or not ; Position of knight after move ; count valid moves ; Return number of possible moves ; Driver Code"
Solution in C#:

using System ; class GFG { static int n = 4 ; static int m = 4 ; static int findPossibleMoves ( int [ , ] mat , int p , int q ) { int [ ] X = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int [ ] Y = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; int count = 0 ; for ( int i = 0 ; i < 8 ; i ++ ) { int x = p + X [ i ] ; int y = q + Y [ i ] ; if ( x >= 0 && y >= 0 && x < n && y < m && mat [ x , y ] == 0 ) count ++ ; } return count ; } static public void Main ( ) { int [ , ] mat = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 } } ; int p = 2 , q = 2 ; Console . WriteLine ( findPossibleMoves ( mat , p , q ) ) ; } }


"Possible number of Trees having N vertex | C # program for the above approach ; Function that finds the value of x ^ y using Modular Exponentiation ; Base Case ; If y is odd , multiply x with result ; Return the value ; Function that counts the total number of trees possible ; Find the max element in array ; Level array store the number nodes on level i initially all values are 0 ; In this case tree can not be created ; Calculate level [ i ] ^ level [ i + 1 ] ; Return the readonly count ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; using System . Linq ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = power ( x , y / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( y & 1 ) != 0 ) p = ( x * p ) % mod ; return p ; } static int NumberOfTrees ( int [ ] arr , int N ) { int maxElement = arr . Max ( ) ; int [ ] level = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { ans = ( ans * power ( level [ i ] , level [ i + 1 ] ) ) % mod ; } return ans ; } public static void Main ( String [ ] args ) { int N = 7 ; int [ ] arr = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; Console . Write ( NumberOfTrees ( arr , N ) ) ; } }


"Possible number of Trees having N vertex | C # program for the above approach ; Function to count the total number of trees possible ; Find the max element in the given array ; Level array store the number of nodes on level i initially all values are zero ; In this case tree can not be created ; To store the count of trees ; Iterate until maxElement ; Calculate level [ i ] ^ level [ i + 1 ] ; Update the count of tree ; Return the readonly count of trees ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; using System . Linq ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int NumberOfTrees ( int [ ] arr , int N ) { int maxElement = arr . Max ( ) ; int [ ] level = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { for ( int j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 7 ; int [ ] arr = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; Console . Write ( NumberOfTrees ( arr , N ) ) ; } }


"Possible pairs forming a Pythagorean Triple with a given value | C # program to compute all the possible pairs that forms a pythagorean triple with a given value ; Function to generate all possible pairs ; List to store all the possible pairs ; Checking all the possible pair in the range of [ 1 , c ) ; If the pair satisfies the condition push it in the vector ; Driver code ; If no valid pair exist ; Print all valid pairs"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static List < pair > Pairs ( int C ) { List < pair > ans = new List < pair > ( ) ; for ( int i = 1 ; i < C ; i ++ ) { for ( int j = i + 1 ; j < C ; j ++ ) { if ( ( i * i ) + ( j * j ) == ( C * C ) ) { ans . Add ( new pair ( i , j ) ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int C = 13 ; List < pair > ans = Pairs ( C ) ; if ( ans . Count == 0 ) { Console . Write ( " No ▁ valid ▁ pair ▁ " + " exist " + "
 " ) ; return ; } foreach ( pair i in ans ) { Console . Write ( " ( " + i . first + " , ▁ " + i . second + " ) " + "
 " ) ; } } }


"Possible to form a triangle from array values | C # program to find if it is possible to form a triangle from array values ; Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPossibleTriangle ( int [ ] arr , int N ) { if ( N < 3 ) return false ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } static public void Main ( ) { int [ ] arr = { 5 , 4 , 3 , 1 , 2 } ; int N = arr . Length ; if ( isPossibleTriangle ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Possible two sets from first N natural numbers difference of sums as D | C # program for implementing above approach ; Function returns true if it is possible to split into two sets otherwise returns false ; Driver code"
Solution in C#:

using System ; class GFG { static bool check ( int N , int D ) { int temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } static public void Main ( ) { int N = 5 ; int M = 7 ; if ( check ( N , M ) ) Console . Write ( " yes " ) ; else Console . Write ( " no " ) ; } }


"Possible values of Q such that , for any value of R , their product is equal to X times their sum | C # program for the above approach ; Function to find all possible values of Q ; List initialization to store all numbers satisfying the given condition ; Iterate for all the values of X ; Check if condition satisfied then push the number ; Possible value of Q ; Print all the numbers ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void values_of_Q ( int X ) { List < int > val_Q = new List < int > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { if ( ( ( ( X + i ) * X ) ) % i == 0 ) { val_Q . Add ( X + i ) ; } } Console . WriteLine ( val_Q . Count ) ; for ( int i = 0 ; i < val_Q . Count ; i ++ ) { Console . Write ( val_Q [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int X = 3 ; values_of_Q ( X ) ; } }


"Postfix to Infix | C # program to find infix for a given postfix . ; Get Infix for a given postfix expression ; Push operands ; We assume that input is a valid postfix and expect an operator . ; There must be a single element in stack now which is the required infix . ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static Boolean isOperand ( char x ) { return ( x >= ' a ' && x <= ' z ' ) || ( x >= ' A ' && x <= ' Z ' ) ; } static String getInfix ( String exp ) { Stack s = new Stack ( ) ; for ( int i = 0 ; i < exp . Length ; i ++ ) { if ( isOperand ( exp [ i ] ) ) { s . Push ( exp [ i ] + " " ) ; } else { String op1 = ( String ) s . Peek ( ) ; s . Pop ( ) ; String op2 = ( String ) s . Peek ( ) ; s . Pop ( ) ; s . Push ( " ( " + op2 + exp [ i ] + op1 + " ) " ) ; } } return ( String ) s . Peek ( ) ; } public static void Main ( String [ ] args ) { String exp = " ab * c + " ; Console . WriteLine ( getInfix ( exp ) ) ; } }


"Postorder traversal of Binary Tree without recursion and without stack | C # program or postorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Visited left subtree ; Visited right subtree ; Print node ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = this . right = null ; } } ; class GFG { Node root ; void postorder ( Node head ) { Node temp = root ; HashSet < Node > visited = new HashSet < Node > ( ) ; while ( ( temp != null && ! visited . Contains ( temp ) ) ) { if ( temp . left != null && ! visited . Contains ( temp . left ) ) temp = temp . left ; else if ( temp . right != null && ! visited . Contains ( temp . right ) ) temp = temp . right ; else { Console . Write ( temp . data + " ▁ " ) ; visited . Add ( temp ) ; temp = head ; } } } public static void Main ( String [ ] args ) { GFG gfg = new GFG ( ) ; gfg . root = new Node ( 8 ) ; gfg . root . left = new Node ( 3 ) ; gfg . root . right = new Node ( 10 ) ; gfg . root . left . left = new Node ( 1 ) ; gfg . root . left . right = new Node ( 6 ) ; gfg . root . left . right . left = new Node ( 4 ) ; gfg . root . left . right . right = new Node ( 7 ) ; gfg . root . right . right = new Node ( 14 ) ; gfg . root . right . right . left = new Node ( 13 ) ; gfg . postorder ( gfg . root ) ; } }


"Postorder traversal of Binary Tree without recursion and without stack | C # program or postorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Visited left subtree ; Visited right subtree ; Print node ; Driver code"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node left , right ; public bool visited ; } static void postorder ( Node head ) { Node temp = head ; while ( temp != null && temp . visited == false ) { if ( temp . left != null && temp . left . visited == false ) temp = temp . left ; else if ( temp . right != null && temp . right . visited == false ) temp = temp . right ; else { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp . visited = true ; temp = head ; } } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; node . visited = false ; return ( node ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 4 ) ; root . left . right . right = newNode ( 7 ) ; root . right . right = newNode ( 14 ) ; root . right . right . left = newNode ( 13 ) ; postorder ( root ) ; } }


"Powerful Number | C # program to find if a number is powerful or not . ; function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime numenr . Since prime numbers are not powerful , we return false if n is not 1. ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . Sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } public static void Main ( ) { if ( isPowerful ( 20 ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; if ( isPowerful ( 27 ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Powers of 2 to required sum | C # program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Code here"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void block ( long x ) { List < int > v = new List < int > ( ) ; Console . Write ( " Blocks ▁ for ▁ " + x + " ▁ : ▁ " ) ; while ( x > 0 ) { v . Add ( ( int ) x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . Count ; i ++ ) { if ( v [ i ] == 1 ) { Console . Write ( i ) ; if ( i != v . Count - 1 ) Console . Write ( " , ▁ " ) ; } } Console . WriteLine ( ) ; } public static void Main ( ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; } }