"Find pair with greatest product in array | C # program to find the largest product number ; Function to find greatest number ; Store occurrences of all elements in hash array ; m [ arr [ i ] ] ++ ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findGreatest ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var a = m [ arr [ i ] ] + 1 ; m . Add ( arr [ i ] , a ) ; } else { m . Add ( arr [ i ] , arr [ i ] ) ; } } Array . Sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m [ result ] == null m [ result ] > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m [ result ] > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . Length ; Console . WriteLine ( findGreatest ( arr , n ) ) ; } }


"Find pair with maximum GCD for integers in range 2 to N | C # program to find a pair of integers less than or equal to N such that their GCD is maximum ; Function to find the required pair whose GCD is maximum ; If N is even ; If N is odd ; Driver Code"
Solution in C#:

 using System ; class GFG { static void solve ( int N ) { if ( N % 2 == 0 ) { Console . Write ( N / 2 + " ▁ " + N + "
 " ) ; } else { Console . Write ( ( N - 1 ) / 2 + " ▁ " + ( N - 1 ) + "
 " ) ; } } public static void Main ( String [ ] args ) { int N = 10 ; solve ( N ) ; } }


"Find pair with maximum GCD in an array | C # Code for Find pair with maximum GCD in an array ; Function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array"
Solution in C#:

using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; int [ ] divisors = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . Sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( int i = high ; i >= 1 ; i -- ) if ( divisors [ i ] > 1 ) return i ; return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }


"Find pair with maximum GCD in an array | C # Code to find pair with maximum GCD in an array ; function to find GCD of pair with max max GCD in the array ; Calculating Max in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1 The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Driver Code ; Array in which pair with max GCD is to be found ; Size of array"
Solution in C#:

using System ; class GFG { public static int findMaxGCD ( int [ ] arr , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . Max ( high , arr [ i ] ) ; int [ ] count = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . Length ; Console . WriteLine ( findMaxGCD ( arr , n ) ) ; } }


"Find pair with maximum difference in any column of a Matrix | C # program to find column with max difference of any pair of elements ; Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; Driver Code"
Solution in C#:

using System ; class GFG { static int colMaxDiff ( int [ , ] mat ) { int max_diff = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int max_val = mat [ 0 , i ] , min_val = mat [ 0 , i ] ; for ( int j = 1 ; j < N ; j ++ ) { max_val = Math . Max ( max_val , mat [ j , i ] ) ; min_val = Math . Min ( min_val , mat [ j , i ] ) ; } max_diff = Math . Max ( max_diff , max_val - min_val ) ; } return max_diff ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 5 , 3 , 5 , 4 , 0 } , { 5 , 6 , 7 , 8 , 9 } , { 0 , 6 , 3 , 4 , 12 } , { 9 , 7 , 12 , 4 , 3 } } ; Console . WriteLine ( " Max ▁ difference ▁ : ▁ " + colMaxDiff ( mat ) ) ; } }


"Find pair with maximum ratio in an Array | C # implementation to find the maximum pair in the array ; Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver code"
Solution in C#:

using System ; class GFG { static float computeMaxValue ( float [ ] arr , int n ) { float ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { float val = Math . Max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = Math . Max ( ans , val ) ; } } return ans ; } public static void Main ( String [ ] args ) { float [ ] arr = { 15 , 10 , 3 , 2 } ; int N = arr . Length ; Console . WriteLine ( computeMaxValue ( arr , N ) ) ; } }


"Find pairs in array whose sum does not exist in Array | C # program to implement the above approach ; Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public static void findPair ( int [ ] arr , int n ) { int i , j ; if ( n < 2 ) { Console . Write ( " - 1" ) ; } HashSet < int > hashMap = new HashSet < int > ( ) ; foreach ( int k in arr ) { hashMap . Add ( k ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . Contains ( sum ) ) { Console . Write ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " )
 " ) ; } } } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 2 , 6 } ; int n = arr . Length ; findPair ( arr , n ) ; } }


"Find pairs in array whose sums already exist in array | A simple C # program to find pair whose sum already exists in array ; Function to find pair whose sum exists in arr [ ] ; Driver code"
Solution in C#:

using System ; public class GFG { static void findPair ( int [ ] arr , int n ) { bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { Console . WriteLine ( arr [ i ] + " ▁ " + arr [ j ] ) ; found = true ; } } } } if ( found == false ) Console . WriteLine ( " Not ▁ exist " ) ; } static public void Main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 8 , 13 , 5 } ; int n = arr . Length ; findPair ( arr , n ) ; } }


"Find pairs with given sum in doubly linked list | C # program to find a pair with given sum x . ; structure of node of doubly linked list ; Function to find pair whose sum equal to given value x . ; Set two pointers , first to the beginning of DLL and second to the end of DLL . ; To track if we find a pair or not ; The loop terminates when they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; move first in forward direction ; move second in backward direction ; if pair is not present ; A utility function to insert a new node at the beginning of doubly linked list ; Driver Code"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node next , prev ; } ; static void pairSum ( Node head , int x ) { Node first = head ; Node second = head ; while ( second . next != null ) second = second . next ; bool found = false ; while ( first != second && second . next != first ) { if ( ( first . data + second . data ) == x ) { found = true ; Console . WriteLine ( " ( " + first . data + " , ▁ " + second . data + " ) " ) ; first = first . next ; second = second . prev ; } else { if ( ( first . data + second . data ) < x ) first = first . next ; else second = second . prev ; } } if ( found == false ) Console . WriteLine ( " No ▁ pair ▁ found " ) ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( head == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return temp ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 7 ; pairSum ( head , x ) ; } }


"Find pairs with given sum such that pair elements lie in different BSTs | C # program to find pairs with given sum such that one element of pair exists in one BST and other in other BST . ; A binary Tree node ; A utility function to create a new BST node with key as given num ; A utility function to insert a given key to BST ; store storeInorder traversal in auxiliary array ; Function to find pair for given sum in different bst vect1 . get ( ) - . stores storeInorder traversal of first bst vect2 . get ( ) - . stores storeInorder traversal of second bst ; Initialize two indexes to two different corners of two Vectors . ; find pair by moving two corners . ; If we found a pair ; If sum is more , move to higher value in first Vector . ; If sum is less , move to lower value in second Vector . ; Prints all pairs with given " sum " such that one element of pair is in tree with root1 and other node is in tree with root2 . ; Store inorder traversals of two BSTs in two Vectors . ; Now the problem reduces to finding a pair with given sum such that one element is in vect1 and other is in vect2 . ; Driver program to run the case ; first BST ; second BST"
Solution in C#:

using System ; using System . Collections . Generic ; public class solution { public class Node { public int data ; public Node left , right ; } public static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } public static Node insert ( Node root , int key ) { if ( root == null ) { return newNode ( key ) ; } if ( root . data > key ) { root . left = insert ( root . left , key ) ; } else { root . right = insert ( root . right , key ) ; } return root ; } public static void storeInorder ( Node ptr , List < int > vect ) { if ( ptr == null ) { return ; } storeInorder ( ptr . left , vect ) ; vect . Add ( ptr . data ) ; storeInorder ( ptr . right , vect ) ; } public static void pairSumUtil ( List < int > vect1 , List < int > vect2 , int sum ) { int left = 0 ; int right = vect2 . Count - 1 ; while ( left < vect1 . Count && right >= 0 ) { if ( vect1 [ left ] + vect2 [ right ] == sum ) { Console . Write ( " ( " + vect1 [ left ] + " , ▁ " + vect2 [ right ] + " ) , ▁ " ) ; left ++ ; right -- ; } else if ( vect1 [ left ] + vect2 [ right ] < sum ) { left ++ ; } else { right -- ; } } } public static void pairSum ( Node root1 , Node root2 , int sum ) { List < int > vect1 = new List < int > ( ) , vect2 = new List < int > ( ) ; storeInorder ( root1 , vect1 ) ; storeInorder ( root2 , vect2 ) ; pairSumUtil ( vect1 , vect2 , sum ) ; } public static void Main ( string [ ] args ) { Node root1 = null ; root1 = insert ( root1 , 8 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 6 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 14 ) ; root1 = insert ( root1 , 13 ) ; Node root2 = null ; root2 = insert ( root2 , 5 ) ; root2 = insert ( root2 , 18 ) ; root2 = insert ( root2 , 2 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 3 ) ; root2 = insert ( root2 , 4 ) ; int sum = 10 ; pairSum ( root1 , root2 , sum ) ; } }


"Find parent of given node in a Binary Tree with given postorder traversal | C # implementation to find the parent of the given node K in a binary tree whose post - order traversal is N natural numbers ; Function to find the parent of the given node ; Condition to check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Finding the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver Code"
Solution in C#:

using System ; class GFG { static int findParent ( int height , int node ) { int start = 1 ; int end = ( int ) Math . Pow ( 2 , height ) - 1 ; if ( end == node ) return - 1 ; while ( node >= 1 ) { end = end - 1 ; int mid = start + ( end - start ) / 2 ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } return - 1 ; } public static void Main ( String [ ] args ) { int height = 4 ; int node = 6 ; int k = findParent ( height , node ) ; Console . Write ( k ) ; } }


"Find perimeter of shapes formed with 1 s in binary matrix | C # program to find perimeter of area coverede by 1 in 2D matrix consisits of 0 ' s ▁ and ▁ 1' s ; Find the number of covered side for mat [ i ] [ j ] . ; UP ; LEFT ; DOWN ; RIGHT ; Returns sum of perimeter of shapes formed with 1 s ; Traversing the matrix and finding ones to calculate their contribution . ; Driver code"
Solution in C#:

public class GFG { public const int R = 3 ; public const int C = 5 ; public static int numofneighbour ( int [ ] [ ] mat , int i , int j ) { int count = 0 ; if ( i > 0 && mat [ i - 1 ] [ j ] == 1 ) { count ++ ; } if ( j > 0 && mat [ i ] [ j - 1 ] == 1 ) { count ++ ; } if ( i < R - 1 && mat [ i + 1 ] [ j ] == 1 ) { count ++ ; } if ( j < C - 1 && mat [ i ] [ j + 1 ] == 1 ) { count ++ ; } return count ; } public static int findperimeter ( int [ ] [ ] mat ) { int perimeter = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { perimeter += ( 4 - numofneighbour ( mat , i , j ) ) ; } } } return perimeter ; } public static void Main ( string [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 0 , 1 , 0 , 0 , 0 } , new int [ ] { 1 , 1 , 1 , 0 , 0 } , new int [ ] { 1 , 0 , 0 , 0 , 0 } } ; Console . WriteLine ( findperimeter ( mat ) ) ; } }


"Find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | Function to generate the permutation of the first N natural numbers having sum of absolute difference between element and indices as minimum ; Initialize array arr [ ] from 1 to N ; Swap alternate positions ; Check N is greater than 1 and N is odd ; Swapping last two positions ; Print the permutation ; Driver code"
Solution in C#:

using System ; class GFG { static void findPermutation ( int N ) { int [ ] arr = new int [ N ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } for ( int i = 1 ; i < N ; i += 2 ) { temp = arr [ i ] ; arr [ i ] = arr [ i - 1 ] ; arr [ i - 1 ] = temp ; } if ( N % 2 == 1 && N > 1 ) { temp = arr [ N - 1 ] ; arr [ N - 1 ] = arr [ N - 2 ] ; arr [ N - 2 ] = temp ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int N = 7 ; findPermutation ( N ) ; } }


"Find permutation of first N natural numbers that satisfies the given condition | C # implementation of the approach ; Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code"
Solution in C#:

using System ; class GFG { static void Permutation ( int n , int k ) { int [ ] p = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) p [ i ] = i ; for ( int i = 1 ; i < n - k ; i ++ ) p [ i + 1 ] = i ; p [ 1 ] = n - k ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( p [ i ] + " ▁ " ) ; } static public void Main ( ) { int n = 5 , k = 2 ; Permutation ( n , k ) ; } }


"Find permutation of n which is divisible by 3 but not divisible by 6 | C # program to find permutation of n which is divisible by 3 but not divisible by 6 ; Function to find the permutation ; length of integer ; if integer is even ; return odd integer ; rotate integer ; return - 1 in case no required permutation exists ; Driver Code"
Solution in C#:

using System ; class GFG { static int findPermutation ( int n ) { int len = ( int ) Math . Ceiling ( Math . Log10 ( n ) ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( n % 2 != 0 ) { return n ; } else { n = ( n / 10 ) + ( n % 10 ) * ( int ) Math . Pow ( 10 , len - i - 1 ) ; continue ; } } return - 1 ; } public static void Main ( ) { int n = 132 ; Console . WriteLine ( findPermutation ( n ) ) ; } }


"Find permutation of numbers upto N with a specific sum in a specific range | C # program for the above approach ; Function to check if sum is possible with remaining numbers ; Stores the minimum sum possible with x numbers ; Stores the maximum sum possible with x numbers ; If S lies in the range [ minSum , maxSum ] ; Function to find the resultant permutation ; Stores the count of numbers in the given segment ; If the sum is not possible with numbers in the segment ; Output - 1 ; Stores the numbers present within the given segment ; Iterate over the numbers from 1 to N ; If ( S - i ) is a positive non - zero sum and if it is possible to obtain ( S - i ) remaining numbers ; Update sum S ; Update required numbers in the segement ; Push i in vector v ; If sum has been obtained ; Break from the loop ; If sum is not obtained ; Output - 1 ; Stores the numbers which are not present in given segment ; Loop to check the numbers not present in the segment ; Pointer to check if i is present in vector v or not ; If i is not present in v ; Push i in vector v1 ; Point to the first elements of v1 and v respectively ; Print the required permutation ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool possible ( int x , int S , int N ) { int minSum = ( x * ( x + 1 ) ) / 2 ; int maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) / 2 ; if ( S < minSum S > maxSum ) { return false ; } return true ; } public static void findPermutation ( int N , int L , int R , int S ) { int x = R - L + 1 ; if ( ! possible ( x , S , N ) ) { Console . WriteLine ( - 1 ) ; return ; } else { List < int > v = new List < int > ( ) ; for ( int i = N ; i >= 1 ; -- i ) { if ( ( S - i ) >= 0 && possible ( x - 1 , S - i , i - 1 ) ) { S = S - i ; x -- ; v . Add ( i ) ; } if ( S == 0 ) { break ; } } if ( S != 0 ) { Console . WriteLine ( - 1 ) ; return ; } List < int > v1 = new List < int > ( ) ; for ( int i = 1 ; i <= N ; ++ i ) { bool it = v . Contains ( i ) ; if ( ! it ) { v1 . Add ( i ) ; } } int j = 0 , f = 0 ; for ( int i = 1 ; i < L ; ++ i ) { Console . Write ( v1 [ j ] + " ▁ " ) ; j ++ ; } for ( int i = L ; i <= R ; ++ i ) { Console . Write ( v [ f ] + " ▁ " ) ; f ++ ; } for ( int i = R + 1 ; i <= N ; ++ i ) { Console . Write ( v1 [ j ] + " ▁ " ) ; j ++ ; } } return ; } public static void Main ( ) { int N = 6 , L = 3 , R = 5 , S = 8 ; findPermutation ( N , L , R , S ) ; } }


"Find permutation with maximum remainder Sum | C # implementation of the approach ; Function to find the permutation ; Put n at the first index 1 ; Put all the numbers from 2 to n sequentially ; Driver code ; Display the permutation"
Solution in C#:

using System ; class GFG { static int [ ] Findpermutation ( int n ) { int [ ] a = new int [ n + 1 ] ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( v [ i ] + " ▁ " ) ; } }


"Find points at a given distance on a line of given slope | C # program to find the points on a line of slope M at distance L ; Class to represent a co - ordinate point ; Function to print pair of points at distance ' l ' and having a slope ' m ' from the source ; m is the slope of line , and the required Point lies distance l away from the source Point ; Slope is 0 ; If slope is infinite ; Print the first Point ; Print the second Point ; Driver code"
Solution in C#:

using System ; class GFG { public class Point { public float x , y ; public Point ( ) { x = y = 0 ; } public Point ( float a , float b ) { x = a ; y = b ; } } ; static void printPoints ( Point source , float l , int m ) { Point a = new Point ( ) ; Point b = new Point ( ) ; if ( m == 0 ) { a . x = source . x + l ; a . y = source . y ; b . x = source . x - l ; b . y = source . y ; } else if ( Double . IsInfinity ( m ) ) { a . x = source . x ; a . y = source . y + l ; b . x = source . x ; b . y = source . y - l ; } else { float dx = ( float ) ( l / Math . Sqrt ( 1 + ( m * m ) ) ) ; float dy = m * dx ; a . x = source . x + dx ; a . y = source . y + dy ; b . x = source . x - dx ; b . y = source . y - dy ; } Console . WriteLine ( a . x + " , ▁ " + a . y ) ; Console . WriteLine ( b . x + " , ▁ " + b . y ) ; } public static void Main ( String [ ] args ) { Point p = new Point ( 2 , 1 ) , q = new Point ( 1 , 0 ) ; printPoints ( p , ( float ) Math . Sqrt ( 2 ) , 1 ) ; Console . WriteLine ( ) ; printPoints ( q , 5 , 0 ) ; } }


"Find position i to split Array such that prefix sum till i | C # program for the above approach ; Function to check if there is an element forming G . P . series having common ratio k ; If size of array is less than three then return - 1 ; Initialize the variables ; Calculate total sum of array ; Calculate Middle element of G . P . series ; Iterate over the range ; Store the first element of G . P . series in the variable temp ; Return position of middle element of the G . P . series if the first element is in G . P . of common ratio k ; Else return 0 ; if middle element is not found in arr [ ] ; Driver Code ; Given array"
Solution in C#:

using System ; class GFG { static int checkArray ( int [ ] arr , int N , int k ) { if ( N < 3 ) return - 1 ; int i , Sum = 0 , temp = 0 ; for ( i = 0 ; i < N ; i ++ ) Sum += arr [ i ] ; int R = ( k * k + k + 1 ) ; if ( Sum % R != 0 ) return 0 ; int Mid = k * ( Sum / R ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { temp += arr [ i - 1 ] ; if ( arr [ i ] == Mid ) { if ( temp == Mid / k ) return i + 1 ; else return 0 ; } } return 0 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( checkArray ( arr , N , K ) ) ; } }


"Find position of an element in a sorted array of infinite numbers | C # program to demonstrate working of an algorithm that finds an element in an array of infinite size ; Simple binary search algorithm ; Method takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can  assume size to be infinite in this  function.  NOTE THAT THIS FUNCTION ASSUMES  arr[] TO BE OF INFINITE SIZE  THEREFORE, THERE IS NO INDEX OUT  OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , thus use binary search between them ; Driver method to test the above function"
Solution in C#:

using System ; class GFG { static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static int findPos ( int [ ] arr , int key ) { int l = 0 , h = 1 ; int val = arr [ 0 ] ; while ( val < key ) { l = h ; h = 2 * h ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 } ; int ans = findPos ( arr , 10 ) ; if ( ans == - 1 ) Console . Write ( " Element ▁ not ▁ found " ) ; else Console . Write ( " Element ▁ found ▁ at ▁ " + " index ▁ " + ans ) ; } }


"Find position of given term in a series formed with only digits 4 and 7 allowed | C # program for the above approach ; Function to find the position of the number N ; To store the position of N ; Iterate through all digit of N ; If current digit is 7 ; If current digit is 4 ; Print the final position ; Driver Code ; Given number of the series ; Function Call"
Solution in C#:

using System ; class GFG { static void findPosition ( int n ) { int i = 0 ; int pos = 0 ; while ( n > 0 ) { if ( n % 10 == 7 ) { pos = pos + ( int ) Math . Pow ( 2 , i + 1 ) ; } else { pos = pos + ( int ) Math . Pow ( 2 , i ) ; } i ++ ; n = n / 10 ; } Console . Write ( pos ) ; } public static void Main ( ) { int N = 777 ; findPosition ( N ) ; } }


"Find position of left most dis | C # to find the leftmost position of first dis - similar bit ; Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver Code"
Solution in C#:

using System ; class GFG { static int bitPos ( int n1 , int n2 ) { if ( n1 == n2 ) return 0 ; int bitCount1 = ( int ) Math . Floor ( Math . Log ( n1 ) / Math . Log ( 2 ) ) + 1 ; int bitCount2 = ( int ) Math . Floor ( Math . Log ( n2 ) / Math . Log ( 2 ) ) + 1 ; int bitDiff = Math . Abs ( bitCount1 - bitCount2 ) ; int maxBitCount = Math . Max ( bitCount1 , bitCount2 ) ; if ( bitCount1 > bitCount2 ) { n2 = n2 * ( int ) Math . Pow ( 2 , bitDiff ) ; } else { n1 = n1 * ( int ) Math . Pow ( 2 , bitDiff ) ; } int xorValue = n1 ^ n2 ; int bitCountXorValue = ( int ) Math . Floor ( Math . Log ( xorValue ) / Math . Log ( 2 ) ) + 1 ; int disSimilarBitPosition = maxBitCount - bitCountXorValue + 1 ; return disSimilarBitPosition ; } public static void Main ( ) { int n1 = 53 , n2 = 55 ; Console . Write ( bitPos ( n1 , n2 ) ) ; } }


"Find position of non | C # implementation to find count of placing non - attacking rooks on the N x N chessboard ; Function to find the count of placing non - attacking rooks on the N x N chessboard ; Count of the Non - attacking rooks ; Printing lexographically smallest configuration ; Driver Code ; Function call"
Solution in C#:

using System ; class P2Placerooks { static void findCountRooks ( int [ ] row , int [ ] col , int n , int k ) { int res = n - k ; Console . WriteLine ( res + " ▁ " ) ; int ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( ri < k && row [ ri ] == 1 ) { ri ++ ; } while ( ci < k && col [ ci ] == 1 ) { ci ++ ; } Console . WriteLine ( ( ri + 1 ) + " ▁ " + ( ci + 1 ) + " ▁ " ) ; ri ++ ; ci ++ ; } } public static void Main ( String [ ] args ) { int n = 4 ; int k = 2 ; int [ ] row = { 1 , 2 } ; int [ ] col = { 4 , 2 } ; findCountRooks ( row , col , n , k ) ; } }


"Find position of the only set bit | C # program to find position of only set bit in a given number ; A utility function to check whether n is a power of 2 or not . goo . gl / 17 Arj See http : ; Returns position of the only set bit in ' n ' ; Iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; Unset current bit and set the next bit in ' i ' ; increment position ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPowerOfTwo ( int n ) { return ( n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ) ? true : false ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; int i = 1 , pos = 1 ; while ( ( i & n ) == 0 ) { i = i << 1 ; ++ pos ; } return pos ; } static void Main ( ) { int n = 16 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }


"Find position of the only set bit | C # program to find position of only set bit in a given number ; A utility function to check whether n is power of 2 or not ; Returns position of the only set bit in ' n ' ; One by one move the only set bit to right till it reaches end ; increment count of shifts ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPowerOfTwo ( int n ) { return n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; int count = 0 ; while ( n > 0 ) { n = n >> 1 ; ++ count ; } return count ; } static void Main ( ) { int n = 0 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }


"Find postorder traversal of BST from preorder traversal | C # program for finding postorder traversal of BST from preorder traversal ; Function to find postorder traversal from preorder traversal . ; If entire preorder array is traversed then return as no more element is left to be added to post order array . ; If array element does not lie in range specified , then it is not part of current subtree . ; Store current value , to be printed later , after printing left and right subtrees . Increment preIndex to find left and right subtrees , and pass this updated value to recursive calls . ; All elements with value between minval and val lie in left subtree . ; All elements with value between val and maxval lie in right subtree . ; Function to find postorder traversal . ; To store index of element to be traversed next in preorder array . This is passed by reference to utility function . ; Driver code ; Calling function"
Solution in C#:

using System ; class GFG { public class INT { public int data ; public INT ( int d ) { data = d ; } } public static void findPostOrderUtil ( int [ ] pre , int n , int minval , int maxval , INT preIndex ) { if ( preIndex . data == n ) { return ; } if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } int val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; Console . Write ( val + " ▁ " ) ; } public static void findPostOrder ( int [ ] pre , int n ) { INT preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , int . MinValue , int . MaxValue , preIndex ) ; } public static void Main ( string [ ] args ) { int [ ] pre = new int [ ] { 40 , 30 , 35 , 80 , 100 } ; int n = pre . Length ; findPostOrder ( pre , n ) ; } }


"Find postorder traversal of BST from preorder traversal | Run loop from 1 to length of pre ; Print from pivot length - 1 to zero ; Print from end to pivot length ; Driver Code"
Solution in C#:

using System ; class GFG { public void getPostOrderBST ( int [ ] pre ) { int pivotPoint = 0 ; for ( int i = 1 ; i < pre . Length ; i ++ ) { if ( pre [ 0 ] <= pre [ i ] ) { pivotPoint = i ; break ; } } for ( int i = pivotPoint - 1 ; i > 0 ; i -- ) { Console . Write ( pre [ i ] + " ▁ " ) ; } for ( int i = pre . Length - 1 ; i >= pivotPoint ; i -- ) { Console . Write ( pre [ i ] + " ▁ " ) ; } Console . Write ( pre [ 0 ] ) ; }


"Find prime factors of Array elements whose sum of exponents is divisible by K | C # program for the above approach ; To store the smallest prime factor till 10 ^ 5 ; Function to compute smallest prime factor array ; Initialize the spf array first element ; Marking smallest prime factor for every number to be itself ; Separately marking smallest prime factor for every even number as 2 ; Checking if i is prime ; Marking SPF for all numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function that finds minimum operation ; Create a spf [ ] array ; Map created to store the unique prime numbers ; To store the result ; To store every unique prime number ; Erase 1 as a key because it is not a prime number ; First prime number ; Frequency is divisible by K then insert primeNum in the result [ ] ; Print the elements if it exists ; Driver code ; Given array [ ] arr ; Given K ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] spf = new int [ 10001 ] ; static void spf_array ( int [ ] spf ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static void frequent_prime ( int [ ] arr , int N , int K ) { spf_array ( spf ) ; SortedDictionary < int , int > Hmap = new SortedDictionary < int , int > ( ) ; List < int > result = new List < int > ( ) ; int i = 0 ; for ( i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; while ( x != 1 ) { if ( Hmap . ContainsKey ( spf [ x ] ) ) Hmap [ spf [ x ] ] = spf [ x ] + 1 ; else Hmap . Add ( spf [ x ] , 1 ) ; x = x / spf [ x ] ; } } Hmap . Remove ( 1 ) ; foreach ( KeyValuePair < int , int > x in Hmap ) { int primeNum = x . Key ; int frequency = x . Value ; if ( frequency % K == 0 ) { result . Add ( primeNum ) ; } } if ( result . Count > 0 ) { foreach ( int it in result ) { Console . Write ( it + " ▁ " ) ; } } else { Console . Write ( " { } " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 } ; int K = 1 ; int N = arr . Length ; frequent_prime ( arr , N , K ) ; } }


"Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | C # implementation of the above approach ; Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; Print the coefficient of 2 in the prime factorization ; Print the coefficients of other primes ; Driver Code ; Input ; Function calling"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void primeFactorization ( int N ) { int [ ] sieve = new int [ N / 2 + 1 ] ; for ( int i = 0 ; i <= N / 2 ; i ++ ) { sieve [ i ] = 0 ; } for ( int i = 2 ; i <= N / 2 ; i ++ ) { if ( sieve [ i ] == 0 ) { for ( int j = i * i ; j <= N / 2 ; j += i ) { sieve [ j ] = 1 ; } } } List < int > prime = new List < int > ( ) ; for ( int i = 3 ; i <= N / 2 ; i ++ ) if ( sieve [ i ] == 0 ) prime . Add ( i ) ; int x = prime . Count ; Console . WriteLine ( "2 - > " + x ) ; foreach ( int i in prime ) Console . WriteLine ( i + " - > 1" ) ; } public static void Main ( String [ ] args ) { int N = 18 ; primeFactorization ( N ) ; } }


"Find prime number K in an array such that ( A [ i ] % K ) is maximum | C # implementation of the approach ; Function to return the required prime number from the array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code"
Solution in C#:

using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int getPrime ( int [ ] arr , int n ) { int max_val = arr . Max ( ) ; List < Boolean > prime = new List < Boolean > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . Insert ( i , true ) ; prime . Insert ( 1 , false ) ; prime . Insert ( 2 , false ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . Insert ( i , false ) ; } } int maximum = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { maximum = Math . Max ( maximum , arr [ i ] ) ; } } return maximum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = arr . Length ; Console . WriteLine ( getPrime ( arr , n ) ) ; } }


"Find prime numbers in the first half and second half of an array | C # program to print the prime numbers in the first half and second half of an array ; Function to check if a number is prime or not ; Function to find whether elements are prime or not ; Traverse in the given range ; Check if a number is prime or not ; Function to print the prime numbers in the first half and second half of an array ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static Boolean prime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void prime_range ( int start , int end , int [ ] a ) { for ( int i = start ; i < end ; i ++ ) { if ( prime ( a [ i ] ) ) Console . Write ( a [ i ] + " ▁ " ) ; } } static void Print ( int [ ] arr , int n ) { Console . Write ( " Prime ▁ numbers ▁ in ▁ the ▁ first ▁ half ▁ are ▁ " ) ; prime_range ( 0 , n / 2 , arr ) ; Console . WriteLine ( ) ; Console . Write ( " Prime ▁ numbers ▁ in ▁ the ▁ second ▁ half ▁ are ▁ " ) ; prime_range ( n / 2 , n , arr ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , 10 , 15 , 17 , 21 , 23 } ; int n = arr . Length ; Print ( arr , n ) ; } }


"Find probability of selecting element from kth column after N iterations | C # implementation of the above approach ; Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code"
Solution in C#:

using System ; class GFG { static int n = 4 ; static int m = 4 ; static float calcProbability ( int [ , ] M , int k ) { float [ , ] dp = new float [ m , n ] ; float [ ] sum = new float [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { dp [ 0 , j ] = M [ 0 , j ] ; sum [ 0 ] = sum [ 0 ] + dp [ 0 , j ] ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i , j ] += dp [ i - 1 , j ] / sum [ i - 1 ] + M [ i , j ] ; sum [ i ] += dp [ i , j ] ; } } return dp [ n - 1 , k - 1 ] / sum [ n - 1 ] ; } public static void Main ( ) { int [ , ] M = { { 1 , 1 , 0 , 3 } , { 2 , 3 , 2 , 3 } , { 9 , 3 , 0 , 2 } , { 2 , 3 , 2 , 2 } } ; int k = 3 ; Console . Write ( calcProbability ( M , k ) ) ; } }


"Find probability that a player wins when probabilities of hitting the target are given | C # mplementation of the approach ; Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point"
Solution in C#:

using System ; class GFG { static double find_probability ( double p , double q , double r , double s ) { double t = ( 1 - p / q ) * ( 1 - r / s ) ; double ans = ( p / q ) / ( 1 - t ) ; return ans ; } public static void Main ( ) { double p = 1 , q = 2 , r = 1 , s = 2 ; Console . WriteLine ( find_probability ( p , q , r , s ) ) ; } }


"Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | C # program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n , r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver code"
Solution in C#:

 using System ; class GFG { static int m = 4 ; static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void nCr ( int n , long p , int [ , ] f ) { for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i , j ] = 0 ; else if ( j == 0 j == i ) f [ i , j ] = 1 ; else f [ i , j ] = ( f [ i - 1 , j ] + f [ i - 1 , j - 1 ] ) % ( int ) p ; } } } static void operations ( int [ ] arr , int n , int [ , ] f ) { long p = 1000000007 ; nCr ( n , p - 1 , f ) ; Array . Sort ( arr ) ; long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { long x = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 , m - j ] * f [ i , j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 7 , 9 , 3 } ; int n = arr . Length ; int [ , ] f = new int [ n + 1 , m + 1 ] ; operations ( arr , n , f ) ; } }


"Find profession in a special family | C # program to find profession of a person at given level and position ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent  is a Doctor, this node  will be a Doctor if it  is at odd position and an  engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver code"
Solution in C#:

using System ; class GFG { static char findProffesion ( int level , int pos ) { if ( level == 1 ) return ' e ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' d ' ) return ( pos % 2 > 0 ) ? ' d ' : ' e ' ; return ( pos % 2 > 0 ) ? ' e ' : ' d ' ; } public static void Main ( ) { int level = 4 , pos = 2 ; if ( findProffesion ( level , pos ) == ' e ' ) Console . WriteLine ( " Engineer " ) ; else Console . WriteLine ( " Doctor " ) ; } }


"Find profession in a special family | c # program to find profession of a person at given level and position . ; Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver code"
Solution in C#:

using System ; public class GFG { public static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } public static char findProffesion ( int level , int pos ) { int c = countSetBits ( pos - 1 ) ; return ( c % 2 != 0 ) ? ' d ' : ' e ' ; } public static void Main ( string [ ] args ) { int level = 3 , pos = 4 ; string prof = ( findProffesion ( level , pos ) == ' e ' ) ? " Engineer " : " Doctor " ; Console . Write ( prof ) ; } }


"Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | C # program for the above approach ; Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver Code"
Solution in C#:

using System ; class GFG { static void findRange ( int [ ] arr , int N , int K ) { int L = Int32 . MinValue ; int R = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { int l = ( int ) Math . Ceiling ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; int r = ( int ) Math . Ceiling ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = Math . Max ( L , l ) ; R = Math . Min ( R , r ) ; } Console . WriteLine ( L + " ▁ " + R ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 9 , 11 } ; int K = 10 ; int N = arr . Length ; findRange ( arr , N , K ) ; } }


"Find relative complement of two sorted arrays | C # program to find all those elements of arr1 [ ] that are not present in arr2 [ ] ; If current element in arr2 [ ] is greater , then arr1 [ i ] can 't be  present in arr2[j..m-1] ; Skipping smaller elements of arr2 [ ] ; Equal elements found ( skipping in both arrays ) ; Printing remaining elements of arr1 [ ] ; Driver code"
Solution in C#:

using System ; namespace Complement { public class GFG { static void relativeComplement ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { Console . Write ( arr1 [ i ] + " ▁ " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ] ) { i ++ ; j ++ ; } } while ( i < n ) Console . Write ( arr1 [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr1 = { 3 , 6 , 10 , 12 , 15 } ; int [ ] arr2 = { 1 , 3 , 5 , 10 , 16 } ; int n = arr1 . Length ; int m = arr2 . Length ; relativeComplement ( arr1 , arr2 , n , m ) ; } } }


"Find relative rank of each element in array | C # program to implement the above approach ; Function to find relative rank for each element in the array A [ ] ; Create Rank Array ; Stack to store numbers in non - decreasing order from right ; Push last element in stack ; Iterate from second last element to first element ; If current element is less than the top of stack and push A [ i ] in stack ; Rank is stack size - 1 for current element ; Pop elements from stack till current element is greater than the top ; Push current element in Stack ; Rank is stack size - 1 ; Print rank of all elements ; Driver Code ; Given array A [ ] ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void findRank ( int [ ] A , int N ) { int [ ] rank = new int [ N ] ; Stack < int > s = new Stack < int > ( ) ; s . Push ( A [ N - 1 ] ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] < s . Peek ( ) ) { s . Push ( A [ i ] ) ; rank [ i ] = s . Count ( ) - 1 ; } else { while ( s . Count ( ) != 0 && A [ i ] >= s . Peek ( ) ) { s . Pop ( ) ; } s . Push ( A [ i ] ) ; rank [ i ] = s . Count ( ) - 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( rank [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] A = new int [ ] { 1 , 2 , 3 , 5 , 4 } ; int N = A . Length ; findRank ( A , N ) ; } }


"Find remainder when a number A raised to N factorial is divided by P | C # program for the above approach ; Function to calculate factorial ; Calculating factorial ; Returning factorial ; Function to calculate resultant remainder ; Function call to calculate factorial of n ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; public class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } static int remainder ( int n , int a , int p ) { int len = fact ( n ) ; int ans = 1 ; for ( int i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } public static void Main ( string [ ] args ) { int A = 2 , N = 1 , P = 2 ; Console . WriteLine ( remainder ( N , A , P ) ) ; } }


"Find remainder when a number A raised to N factorial is divided by P | package whatever do not write package name here ; Function to calculate ( A ^ N ! ) % P in O ( log y ) ; Initialize result ; Update x if it is more than or Equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now ; Returning modular power ; Function to calculate resultant remainder ; Initializing ans to store final remainder ; Calculating remainder ; Returning resultant remainder ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static long remainder ( long n , long a , long p ) { long ans = a % p ; for ( long i = 1 ; i <= n ; i ++ ) ans = power ( ans , i , p ) ; return ans ; } public static void Main ( String [ ] args ) { long A = 2 , N = 1 , P = 2 ; Console . Write ( remainder ( N , A , P ) ) ; } }


"Find right sibling of a binary tree with parent pointers | C # program to print right sibling of a node ; A Binary Tree Node ; Constructor ; Method to find right sibling ; GET Parent pointer whose right child is not a parent or itself of this node . There might be case when parent has no right child , but , current node is left child of the parent ( second condition is for that ) . ; Move to the required child , where right sibling can be present ; find right sibling in the given subtree ( from current node ) , when level will be 0 ; Iterate through subtree ; if no child are there , we cannot have right sibling in this path ; This is the case when we reach 9 node in the tree , where we need to again recursively find the right sibling ; Driver Program to test above functions ; passing 10"
Solution in C#:

using System ; public class Right_Sibling { public class Node { public int data ; public Node left , right , parent ; public Node ( int data , Node parent ) { this . data = data ; left = null ; right = null ; this . parent = parent ; } } public static Node findRightSibling ( Node node , int level ) { if ( node == null node . parent == null ) { return null ; } while ( node . parent . right == node || ( node . parent . right == null && node . parent . left == node ) ) { if ( node . parent == null node . parent . parent == null ) { return null ; } node = node . parent ; level -- ; } node = node . parent . right ; while ( level < 0 ) { if ( node . left != null ) { node = node . left ; } else if ( node . right != null ) { node = node . right ; } else { break ; } level ++ ; } if ( level == 0 ) { return node ; } return findRightSibling ( node , level ) ; } public static void Main ( string [ ] args ) { Node root = new Node ( 1 , null ) ; root . left = new Node ( 2 , root ) ; root . right = new Node ( 3 , root ) ; root . left . left = new Node ( 4 , root . left ) ; root . left . right = new Node ( 6 , root . left ) ; root . left . left . left = new Node ( 7 , root . left . left ) ; root . left . left . left . left = new Node ( 10 , root . left . left . left ) ; root . left . right . right = new Node ( 9 , root . left . right ) ; root . right . right = new Node ( 5 , root . right ) ; root . right . right . right = new Node ( 8 , root . right . right ) ; root . right . right . right . right = new Node ( 12 , root . right . right . right ) ; Console . WriteLine ( findRightSibling ( root . left . left . left . left , 0 ) . data ) ; } }


"Find root of a number using Newton 's method | C # implementation of the approach ; Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code"
Solution in C#:

using System ; class GFG { static double squareRoot ( double n , double l ) { double x = n ; double root ; int count = 0 ; while ( true ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( Math . Abs ( root - x ) < l ) break ; x = root ; } return root ; } public static void Main ( ) { double n = 327 ; double l = 0.00001 ; Console . WriteLine ( squareRoot ( n , l ) ) ; } }


"Find root of the tree where children id sum for every node is given | C # Find root of tree where children sum for every node id is given . ; Every node appears once as an id , and every node except for the root appears once in a sum . So if we subtract all the sums from all the ids , we 're left  with the root id. ; Driver code"
Solution in C#:

 using System ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findRoot ( pair [ ] arr , int n ) { int root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { root += ( arr [ i ] . first - arr [ i ] . second ) ; } return root ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 1 , 5 ) , new pair ( 2 , 0 ) , new pair ( 3 , 0 ) , new pair ( 4 , 0 ) , new pair ( 5 , 5 ) , new pair ( 6 , 5 ) } ; int n = arr . Length ; Console . Write ( " { 0 }
 " , findRoot ( arr , n ) ) ; } }


"Find row number of a binary matrix having maximum number of 1 s | C # program to find row with maximum 1 in row sorted binary matrix ; function for finding row with maximum 1 ; find left most position of 1 in a row find 1 st zero in a row ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 4 ; static void findMax ( int [ , ] arr ) { int row = 0 , i , j ; for ( i = 0 , j = N - 1 ; i < N ; i ++ ) { while ( arr [ i , j ] == 1 && j >= 0 ) { row = i ; j -- ; } } Console . Write ( " Row ▁ number ▁ = ▁ " + ( row + 1 ) ) ; Console . Write ( " , ▁ MaxCount ▁ = ▁ " + ( N - 1 - j ) ) ; } public static void Main ( ) { int [ , ] arr = { { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 1 } } ; findMax ( arr ) ; } }


"Find shortest unique prefix for every word in a given list | Set 2 ( Using Sorting ) | C # program to print shortest unique prefixes for every word . ; create an array to store the results ; sort the array of strings ; compare the first string with its only right neighbor ; Store the unique prefix of a [ 1 ] from its left neighbor ; compute common prefix of a [ i ] unique from its right neighbor ; compare the new prefix with previous prefix ; store the prefix of a [ i + 1 ] unique from its left neighbour ; compute the unique prefix for the last string in sorted array ; Driver code"
Solution in C#:

using System ; class GFG { public String [ ] uniquePrefix ( String [ ] a ) { int size = a . Length ; String [ ] res = new String [ size ] ; Array . Sort ( a ) ; int j = 0 ; while ( j < Math . Min ( a [ 0 ] . Length - 1 , a [ 1 ] . Length - 1 ) ) { if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) j ++ ; else break ; } int ind = 0 ; res [ ind ++ ] = a [ 0 ] . Substring ( 0 , j + 1 ) ; String temp_prefix = a [ 1 ] . Substring ( 0 , j + 1 ) ; for ( int i = 1 ; i < size - 1 ; i ++ ) { j = 0 ; while ( j < Math . Min ( a [ i ] . Length - 1 , a [ i + 1 ] . Length - 1 ) ) { if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) j ++ ; else break ; } String new_prefix = a [ i ] . Substring ( 0 , j + 1 ) ; if ( temp_prefix . Length > new_prefix . Length ) res [ ind ++ ] = temp_prefix ; else res [ ind ++ ] = new_prefix ; temp_prefix = a [ i + 1 ] . Substring ( 0 , j + 1 ) ; } j = 0 ; String sec_last = a [ size - 2 ] ; String last = a [ size - 1 ] ; while ( j < Math . Min ( sec_last . Length - 1 , last . Length - 1 ) ) { if ( sec_last [ j ] == last [ j ] ) j ++ ; else break ; } res [ ind ] = last . Substring ( 0 , j + 1 ) ; return res ; } public static void Main ( String [ ] args ) { GFG gfg = new GFG ( ) ; String [ ] input = { " zebra " , " dog " , " duck " , " dove " } ; String [ ] output = gfg . uniquePrefix ( input ) ; Console . WriteLine ( " The ▁ shortest ▁ unique ▁ prefixes " + " ▁ in ▁ sorted ▁ order ▁ are ▁ : " ) ; for ( int i = 0 ; i < output . Length ; i ++ ) Console . Write ( output [ i ] + " ▁ " ) ; } }


"Find side of Square which makes minimal area to fit two identical rectangles inside it | C # program of the above approach ; If ' a ' and ' b ' same then double ' a ' or ' b ' and return ( 2 * a ) or ( 2 * b ) ; Check if a != b ; If a > b ; Double the smaller value that is ' b ' and store it to ' newB ' ; Find the difference of ' newB ▁ and ▁ ' a ' ; If ' newB ' < a ; Then add the difference of ' newB ' and ' a ' to the ' b ' to make ' b ' and ' a ' as same ; Return side of the square a or b ; If ' newB ' > a then then add the difference of ' newB ' and ' a ' to the ' a ' to make ' a ' and ' newB ' as same ; Return side of the square a or newB ; If a < b ; Double the smaller value that is ' a ' and store it to ' newA ' ; Find the difference of ' newA ▁ and ▁ ' b ' ; If ' newA ' < b ; Then add the difference of ' newA ' and ' b ' to the ' a ' to make ' a ' and ' b ' as same ; Return side of the square a or b ; If ' newA ' > b then then add the difference of ' newA ' and ' b ' to the ' b ' to make ' b ' and ' newA ' as same ; Return side of the square b or newA ; Driver code ; Size of rectangle"
Solution in C#:

using System ; class GFG { public static int minimalSquareSide ( int a , int b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { int newB = b + b ; int diff = Math . Abs ( newB - a ) ; if ( newB < a ) { b = newB + diff ; if ( a == b ) return a ; return 0 ; } else { a = a + diff ; if ( a == newB ) return a ; return 0 ; } } else { int newA = a + a ; int diff = Math . Abs ( newA - b ) ; if ( newA < b ) { a = diff + newA ; if ( a == b ) return a ; return 0 ; } else { b = b + diff ; if ( b == newA ) return b ; return 0 ; } } } return 0 ; } public static void Main ( String [ ] args ) { int H , W ; H = 3 ; W = 1 ; Console . WriteLine ( minimalSquareSide ( H , W ) ) ; } }


"Find size of the largest ' + ' formed by all ones in a binary matrix | C # program to find the size of the largest ' + ' formed by all 1 's in given binary matrix ; size of binary square matrix ; Function to find the size of the largest ' + ' formed by all 1 's in given binary matrix ; left [ j ] [ j ] , right [ i ] [ j ] , top [ i ] [ j ] and bottom [ i ] [ j ] store maximum number of consecutive 1 's present to the left,  right, top and bottom of mat[i][j]  including cell(i, j) respectively ; initialize above four matrix ; initialize first row of top ; initialize last row of bottom ; initialize first column of left ; initialize last column of right ; fill all cells of above four matrix ; calculate left matrix ( filled left to right ) ; calculate top matrix ; calculate new value of j to calculate value of bottom ( i , j ) and right ( i , j ) ; calculate bottom matrix ; calculate right matrix ; revert back to old j ; n stores length of longest + found so far ; compute longest + ; find minimum of left ( i , j ) , right ( i , j ) , top ( i , j ) , bottom ( i , j ) ; largest + would be formed by a cell that has maximum value ; 4 directions of length n - 1 and 1 for middle cell ; matrix contains all 0 's ; Driver function to test above functions"
Solution in C#:


"Find smallest and largest element from square matrix diagonals | C # program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of square matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix"
Solution in C#:

using System ; public class GFG { static void diagonalsMinMax ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; if ( n == 0 ) return ; int principalMin = mat [ 0 , 0 ] , principalMax = mat [ 0 , 0 ] ; int secondaryMin = mat [ n - 1 , 0 ] , secondaryMax = mat [ n - 1 , 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { if ( i == j ) { if ( mat [ i , j ] < principalMin ) { principalMin = mat [ i , j ] ; } if ( mat [ i , j ] > principalMax ) { principalMax = mat [ i , j ] ; } } if ( ( i + j ) == ( n - 1 ) ) { if ( mat [ i , j ] < secondaryMin ) { secondaryMin = mat [ i , j ] ; } if ( mat [ i , j ] > secondaryMax ) { secondaryMax = mat [ i , j ] ; } } } } Console . WriteLine ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; Console . WriteLine ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } static void Main ( ) { int [ , ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }


"Find smallest and largest element from square matrix diagonals | C # program to find smallest and largest elements of both diagonals ; Function to find smallest and largest element from principal and secondary diagonal ; take length of square matrix ; declare and initialize variables with appropriate value ; take new smallest value ; take new largest value ; Condition for secondary diagonal is mat [ n - 1 - i ] [ i ] take new smallest value ; take new largest value ; Driver code ; Declare and initialize 5 X5 matrix"
Solution in C#:

using System ; public class GFG { static void diagonalsMinMax ( int [ , ] mat ) { int n = mat . GetLength ( 0 ) ; if ( n == 0 ) return ; int principalMin = mat [ 0 , 0 ] , principalMax = mat [ 0 , 0 ] ; int secondaryMin = mat [ n - 1 , 0 ] , secondaryMax = mat [ n - 1 , 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( mat [ i , i ] < principalMin ) { principalMin = mat [ i , i ] ; } if ( mat [ i , i ] > principalMax ) { principalMax = mat [ i , i ] ; } if ( mat [ n - 1 - i , i ] < secondaryMin ) { secondaryMin = mat [ n - 1 - i , i ] ; } if ( mat [ n - 1 - i , i ] > secondaryMax ) { secondaryMax = mat [ n - 1 - i , i ] ; } } Console . WriteLine ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; Console . WriteLine ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; Console . WriteLine ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } public static void Main ( ) { int [ , ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }


"Find smallest number K such that K % p = 0 and q % K = 0 | C # implementation of the approach ; Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code"
Solution in C#:

using System ; class GFG { static int getMinVal ( int p , int q ) { if ( q % p == 0 ) return p ; return - 1 ; } public static void Main ( ) { int p = 24 , q = 48 ; Console . WriteLine ( getMinVal ( p , q ) ) ; } }


"Find smallest number formed by inverting digits of given number N | C # program for the above approach ; Function to invert the digits of integer N to form minimum possible number ; Initialize the array ; Iterate till the number N exists ; Last digit of the number N ; Checking if the digit is smaller than 9 - digit ; Store the smaller digit in the array ; Reduce the number each time ; Check if the digit starts with 0 or not ; Print the answer ; Driver Code ; Given Number ; Function Call"
Solution in C#:

using System ; class GFG { static void number ( int num ) { int [ ] a = new int [ 20 ] ; int r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { Console . Write ( "9" ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) Console . Write ( a [ j ] ) ; } public static void Main ( string [ ] args ) { int num = 4545 ; number ( num ) ; } }


"Find smallest number n such that n XOR n + 1 equals to given k . | C # to find n such that XOR of n and n + 1 is equals to given n ; function to return the required n ; if k is of form 2 ^ i - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return 1 ; } public static void Main ( ) { int k = 31 ; Console . WriteLine ( xorCalc ( k ) ) ; } }


"Find smallest number with given digits and sum of digits | C # program for the above approach ; Function to print the minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and Q ; If P and Q are both zero or if Q is zero and N is not divisible by P then there is no possible integer which satisfies the given conditions ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver code ; Function Call"
Solution in C#:

using System ; public class GFG { static void printMinint ( int P , int Q , int N ) { if ( Q > P ) { int temp ; temp = P ; P = Q ; Q = temp ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } int count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += N / P ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( int i = 0 ; i < count_Q ; i ++ ) Console . Write ( Q ) ; for ( int i = 0 ; i < count_P ; i ++ ) Console . Write ( P ) ; } else { Console . WriteLine ( " Not ▁ Possible " ) ; } } public static void Main ( String [ ] args ) { int N = 32 ; int P = 7 ; int Q = 4 ; printMinint ( P , Q , N ) ; } }


"Find smallest number with given number of digits and sum of digits under given constraints | C # implementation of the approach ; Function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; To store the readonly number ; To store the value that is evenly distributed among all the digits ; To store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1 ; In the last rem digits one is added to the value obtained by equal distribution ; Driver function"
Solution in C#:

using System ; class GFG { static String findNumber ( int s , int d ) { String num = " " ; int val = s / d ; int rem = s % d ; int i ; for ( i = 1 ; i <= d - rem ; i ++ ) { num = num + String . Join ( " " , val ) ; } if ( rem > 0 ) { val ++ ; for ( i = d - rem + 1 ; i <= d ; i ++ ) { num = num + String . Join ( " " , val ) ; } } return num ; } public static void Main ( String [ ] args ) { int s = 25 , d = 4 ; Console . Write ( findNumber ( s , d ) ) ; } }


"Find smallest perfect square number A such that N + A is also a perfect square number | C # code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static long SmallestPerfectSquare ( long N ) { long X = ( long ) 1e9 ; long ans ; for ( int i = 1 ; i < Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { long a = i ; long b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = Math . Min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = - 1 ; return ans ; } public static void Main ( string [ ] args ) { long N = 3 ; Console . WriteLine ( SmallestPerfectSquare ( N ) ) ; } }


"Find smallest positive number Y such that Bitwise AND of X and Y is Zero | C # program to find smallest number Y for a given value of X such that X AND Y is zero ; Method to find smallest number Y for a given value of X such that X AND Y is zero ; Convert the number into its binary form ; Case 1 : If all bits are ones , then return the next number ; Case 2 : find the first 0 - bit index and return the Y ; To store the count of set bits ; Driver code"
Solution in C#:

using System ; class GFG { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Convert . ToString ( A_num , 2 ) ; long B = 1 ; int len = A_binary . Length ; int no_ones = bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary [ len - i - 1 ] ; if ( ch == '0' ) { B = ( long ) Math . Pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; Console . WriteLine ( X ) ; } }


"Find smallest value of K such that bitwise AND of numbers in range [ N , N | C # program for the above approach ; Function is to find the largest no which gives the sequence n & ( n - 1 ) & ( n - 2 ) & ... . . & ( n - k ) = 0. ; Since , we need the largest no , we start from n itself , till 0 ; Driver code"
Solution in C#:

using System ; class GFG { public static int findSmallestNumK ( int n ) { int cummAnd = n ; int i = n - 1 ; while ( cummAnd != 0 ) { cummAnd = cummAnd & i ; if ( cummAnd == 0 ) { return i ; } i -- ; } return - 1 ; } static void Main ( ) { int N = 17 ; int lastNum = findSmallestNumK ( N ) ; int K = lastNum == - 1 ? lastNum : N - lastNum ; Console . WriteLine ( K ) ; } }


"Find smallest values of x and y such that ax | C # program to find the smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; To find GCD using Eculcid 's algorithm ; Prints smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; Find LCM ; Driver code"
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 void findSmallest ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; Console . Write ( " x ▁ = ▁ " + lcm / a + " y = " } public static void Main ( ) { int a = 25 , b = 35 ; findSmallest ( a , b ) ; } }


"Find sub | C # implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ , ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call"
Solution in C#:

using System ; class GfG { static int N = 4 ; static int getSum ( int r1 , int r2 , int c1 , int c2 , int [ , ] dp ) { return dp [ r2 , c2 ] - dp [ r2 , c1 ] - dp [ r1 , c2 ] + dp [ r1 , c1 ] ; } static bool sumFound ( int K , int S , int [ , ] grid ) { int [ , ] dp = new int [ N + 1 , N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i + 1 , j + 1 ] = dp [ i + 1 , j ] + dp [ i , j + 1 ] - dp [ i , j ] + grid [ i , j ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int K = 2 ; int S = 14 ; if ( sumFound ( K , S , grid ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Find sub | C # program to find substring with given power ; Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; If curr_power is equal to target power we found a substring starting from index 0 and ending at index i . ; If curr_power - power already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findSubstring ( String str , int power ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int curr_power = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { curr_power = curr_power + ( str [ i ] - ' a ' + 1 ) ; if ( curr_power == power ) { Console . WriteLine ( " Substring ▁ from ▁ index ▁ 0" + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } if ( map . ContainsKey ( curr_power - power ) ) { Console . WriteLine ( " Substring ▁ from ▁ index ▁ " + ( map [ curr_power - power ] + 1 ) + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } if ( ! map . ContainsKey ( curr_power ) ) map . Add ( curr_power , i ) ; else map [ curr_power ] = i ; } Console . WriteLine ( " No ▁ substring ▁ with ▁ " + " given ▁ power ▁ exists " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int power = 36 ; findSubstring ( str , power ) ; } }


"Find subarray of Length K with Maximum Peak | C # implementation to Find subarray of Length K with Maximum Peak ; Function to find the subarray ; Make prefix array to store the prefix sum of peak count ; Count peak for previous index ; Check if this element is a peak ; Increment the count ; Check if number of peak in the sub array whose l = i is greater or not ; Print the result ; Driver code"
Solution in C#:

 using System ; class GFG { static void findSubArray ( int [ ] a , int n , int k ) { int [ ] pref = new int [ n ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i < n - 1 ; ++ i ) { pref [ i ] = pref [ i - 1 ] ; if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) { pref [ i ] ++ ; } } int peak = 0 ; int left = 0 ; for ( int i = 0 ; i + k - 1 < n ; ++ i ) { if ( pref [ i + k - 2 ] - pref [ i ] > peak ) { peak = pref [ i + k - 2 ] - pref [ i ] ; left = i ; } } Console . Write ( " Left ▁ = ▁ " + ( left + 1 ) + "
 " ) ; Console . Write ( " Right ▁ = ▁ " + ( left + k ) + "
 " ) ; Console . Write ( " Peak ▁ = ▁ " + peak + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 2 , 1 } ; int n = arr . Length ; int k = 3 ; findSubArray ( arr , n , k ) ; } }


"Find subarray with given sum | Set 1 ( Nonnegative Numbers ) | An efficient C # program to print subarray with sum as given sum ; Returns true if the there is a subarray of arr [ ] with sum equal to ' sum ' otherwise returns false . Also , prints the result ; Initialize curr_sum as value of first element and starting point as 0 ; Pick a starting point ; If curr_sum exceeds the sum , then remove the starting elements ; If curr_sum becomes equal to sum , then return true ; Add this element to curr_sum ; If we reach here , then no subarray ; Driver code"
Solution in C#:

using System ; class GFG { int subArraySum ( int [ ] arr , int n , int sum ) { int curr_sum = arr [ 0 ] , start = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { while ( curr_sum > sum && start < i - 1 ) { curr_sum = curr_sum - arr [ start ] ; start ++ ; } if ( curr_sum == sum ) { int p = i - 1 ; Console . WriteLine ( " Sum ▁ found ▁ between ▁ " + " indexes ▁ " + start + " ▁ and ▁ " + p ) ; return 1 ; } if ( i < n ) curr_sum = curr_sum + arr [ i ] ; } Console . WriteLine ( " No ▁ subarray ▁ found " ) ; return 0 ; } public static void Main ( ) { GFG arraysum = new GFG ( ) ; int [ ] arr = new int [ ] { 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 } ; int n = arr . Length ; int sum = 23 ; arraysum . subArraySum ( arr , n , sum ) ; } }


"Find subarray with given sum | Set 1 ( Nonnegative Numbers ) | C # code to Find subarray with given sum ; Returns true if the there is a subarray of arr [ ] with sum equal to ' sum ' otherwise returns false . Also , prints the result ; Pick a starting point ; try all subarrays starting with ' i ' ; Driver Code"
Solution in C#:

using System ; class GFG { int subArraySum ( int [ ] arr , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { int p = j - 1 ; Console . Write ( " Sum ▁ found ▁ between ▁ " + " indexes ▁ " + i + " ▁ and ▁ " + p ) ; return 1 ; } if ( curr_sum > sum j == n ) break ; curr_sum = curr_sum + arr [ j ] ; } } Console . Write ( " No ▁ subarray ▁ found " ) ; return 0 ; } public static void Main ( ) { GFG arraysum = new GFG ( ) ; int [ ] arr = { 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 } ; int n = arr . Length ; int sum = 23 ; arraysum . subArraySum ( arr , n , sum ) ; } }


"Find subarray with given sum | Set 2 ( Handles Negative Numbers ) | C # program to print subarray with sum as given sum ; Function to print subarray with sum as given sum ; cur_sum to keep track of cummulative sum till that point ; check whether cur_sum - sum = 0 , if 0 it means the sub array is starting from index 0 - so stop ; if hashMap already has the value , means we already have subarray with the sum - so stop ; if value is not present then add to hashmap ; if end is - 1 : means we have reached end without the sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void subArraySum ( int [ ] arr , int n , int sum ) { int cur_sum = 0 ; int start = 0 ; int end = - 1 ; Dictionary < int , int > hashMap = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { cur_sum = cur_sum + arr [ i ] ; if ( cur_sum - sum == 0 ) { start = 0 ; end = i ; break ; } if ( hashMap . ContainsKey ( cur_sum - sum ) ) { start = hashMap [ cur_sum - sum ] + 1 ; end = i ; break ; } hashMap [ cur_sum ] = i ; } if ( end == - 1 ) { Console . WriteLine ( " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " ) ; } else { Console . WriteLine ( " Sum ▁ found ▁ between ▁ indexes ▁ " + start + " ▁ to ▁ " + end ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 10 , 2 , - 2 , - 20 , 10 } ; int n = arr . Length ; int sum = - 10 ; subArraySum ( arr , n , sum ) ; } }


"Find subfactorial of a number | / C # program for the above approach ; Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static double subfactorial ( int N ) { double res = 0 , fact = 1 ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = fact * i ; if ( count % 2 == 0 ) res = res - ( 1 / fact ) ; else res = res + ( 1 / fact ) ; count ++ ; } return fact * ( 1 + res ) ; } public static void Main ( ) { int N = 4 ; Console . Write ( subfactorial ( N ) ) ; } }


"Find subsequences with maximum Bitwise AND and Bitwise OR | C # implementation of the above approach ; Function to find the maximum sum ; Maximum AND is maximum element ; Maximum OR is bitwise OR of all . ; Driver code"
Solution in C#:

using System ; using System . Linq ; public class GFG { static void maxSum ( int [ ] a , int n ) { int maxAnd = a . Max ( ) ; int maxOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxOR |= a [ i ] ; } Console . Write ( ( maxAnd + maxOR ) ) ; } public static void Main ( ) { int n = 4 ; int [ ] a = { 3 , 5 , 6 , 1 } ; maxSum ( a , n ) ; } }


"Find substrings that contain all vowels | C # program to find all substring that contain all vowels ; Returns true if x is vowel . ; Function to check whether a character is vowel or not ; To store vowels Outer loop picks starting character and inner loop picks ending character . ; If current character is not vowel , then no more result substrings possible starting from str [ i ] . ; If vowel , then we insert it in hash ; If all vowels are present in current substring ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } public static void findSubstring ( string str ) { HashSet < char > hash = new HashSet < char > ( ) ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( str [ j ] ) == false ) { break ; } hash . Add ( str [ j ] ) ; if ( hash . Count == 5 ) { Console . Write ( str . Substring ( i , ( j + 1 ) - i ) + " ▁ " ) ; } } hash . Clear ( ) ; } } public static void Main ( string [ ] args ) { string str = " aeoibsddaeiouudb " ; findSubstring ( str ) ; } }


"Find substrings that contain all vowels | c # program to find all substring that contain all vowels ; Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Function to FindSubstrings of string ; To store vowels ; If current character is vowel then insert into hash , ; If all vowels are present in current substring ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } public static void findSubstring ( string str ) { HashSet < char > hash = new HashSet < char > ( ) ; int start = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( isVowel ( str [ i ] ) == true ) { hash . Add ( str [ i ] ) ; if ( hash . Count == 5 ) { Console . Write ( str . Substring ( start , ( i + 1 ) - start ) + " ▁ " ) ; } } else { start = i + 1 ; hash . Clear ( ) ; } } } public static void Main ( string [ ] args ) { string str = " aeoibsddaeiouudb " ; findSubstring ( str ) ; } }


"Find sum in range L to R in given sequence of integers | C # program to find the sum in given range L to R ; Function to find the sum within the given range ; Generating array from given sequence ; Calculate the desired sum ; return the sum ; Driver code ; Initialise the range"
Solution in C#:

using System ; using System . Collections ; class GFG { public static int findSum ( int L , int R ) { ArrayList arr = new ArrayList ( ) ; int i = 0 ; int x = 2 ; while ( i <= R ) { arr . Add ( i + x ) ; if ( i + 1 <= R ) arr . Add ( i + 1 + x ) ; x *= - 1 ; i += 2 ; } int sum = 0 ; for ( i = L ; i <= R ; ++ i ) sum += ( int ) arr [ i ] ; return sum ; } public static void Main ( string [ ] args ) { int L = 0 , R = 5 ; Console . Write ( findSum ( L , R ) ) ; } }


"Find sum of N | C # program to find sum in Nth group ; calculate sum of Nth group ; Driver code"
Solution in C#:

using System ; class gfg { public static double nth_group ( int n ) { return n * ( 2 * Math . Pow ( n , 2 ) + 1 ) ; } public static int Main ( ) { int N = 5 ; Console . WriteLine ( nth_group ( N ) ) ; return 0 ; } }


"Find sum of a number and its maximum prime factor | C # program to find sum of n and it 's largest prime factor ; Function to return the sum of n and it 's largest prime factor ; Initialise maxPrime to - 1. ; n must be odd at this point , thus skip the even numbers and iterate only odd numbers ; This condition is to handle the case when n is a prime number greater than 2 ; finally return the sum . ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxPrimeFactors ( int n ) { int num = n ; int maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n /= 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) { maxPrime = n ; } int sum = maxPrime + num ; return sum ; } static void Main ( ) { int n = 19 ; Console . WriteLine ( maxPrimeFactors ( n ) ) ; } }


"Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | An efficient C # program to compute sum for given array of cell indexes ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Compute sum of all elements , sum of every row and sum every column ; Compute the desired sum for all given cell indexes ; Driver Code"
Solution in C#:

using System ; class GFG { static int R = 3 ; static int C = 3 ; public class Cell { public int r ; public int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int [ , ] mat , Cell [ ] arr , int n ) { int sum = 0 ; int [ ] row = new int [ R ] ; int [ ] col = new int [ C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { sum += mat [ i , j ] ; col [ j ] += mat [ i , j ] ; row [ i ] += mat [ i , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { int ro = arr [ i ] . r , co = arr [ i ] . c ; Console . WriteLine ( sum - row [ ro ] - col [ co ] + mat [ ro , co ] ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell [ ] arr = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . Length ; printSums ( mat , arr , n ) ; } }


"Find sum of all elements in a matrix except the elements in row and / or column of given cell ? | C # implementation of the approach ; A structure to represent a cell index ; r is row , varies from 0 to R - 1 ; c is column , varies from 0 to C - 1 ; Constructor ; A simple solution to find sums for a given array of cell indexes ; Iterate through all cell indexes ; Compute sum for current cell index ; Driver code"
Solution in C#:

using System ; class GFG { static int R = 3 ; static int C = 3 ; public class Cell { public int r ; public int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int [ , ] mat , Cell [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 , r = arr [ i ] . r , c = arr [ i ] . c ; for ( int j = 0 ; j < R ; j ++ ) { for ( int k = 0 ; k < C ; k ++ ) { if ( j != r && k != c ) { sum += mat [ j , k ] ; } } } Console . WriteLine ( sum ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell [ ] arr = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . Length ; printSums ( mat , arr , n ) ; } }


"Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree Node has key , pointer to left and right children ; A utility function to check if a given node is leaf or not ; This function returns sum of all left leaves in a given binary tree ; Initialize result ; Update result if root is not NULL ; If left of root is NULL , then add key of left child ; Else recur for left child of root ; Recur for right child of root and update res ; return result ; Driver program"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual bool isLeaf ( Node node ) { if ( node == null ) { return false ; } if ( node . left == null && node . right == null ) { return true ; } return false ; } public virtual int leftLeavesSum ( Node node ) { int res = 0 ; if ( node != null ) { if ( isLeaf ( node . left ) ) { res += node . left . data ; } else { res += leftLeavesSum ( node . left ) ; } res += leftLeavesSum ( node . right ) ; } return res ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; Console . WriteLine ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }


"Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree Node has key , pointer to left and right children ; Passing sum as accumulator and implementing pass by reference of sum variable ; Pass in a sum variable as an accumulator ; Check whether this node is a leaf node and is left . ; Pass true for left and false for right ; A wrapper over above recursive function ; use the above recursive function to evaluate sum ; Driver program ; Let us construct the Binary Tree shown in the above figure"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Sum { public int sum = 0 ; } public class BinaryTree { public Node root ; public virtual void leftLeavesSumRec ( Node node , bool isleft , Sum summ ) { if ( node == null ) { return ; } if ( node . left == null && node . right == null && isleft ) { summ . sum = summ . sum + node . data ; } leftLeavesSumRec ( node . left , true , summ ) ; leftLeavesSumRec ( node . right , false , summ ) ; } public virtual int leftLeavesSum ( Node node ) { Sum suum = new Sum ( ) ; leftLeavesSumRec ( node , false , suum ) ; return suum . sum ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; Console . WriteLine ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }


"Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree node ; A constructor to create a new Node ; Return the sum of left leaf nodes ; Using a stack_ for Depth - First Traversal of the tree ; sum holds the sum of all the left leaves ; Check if currentNode 's left  child is a leaf node ; if currentNode is a leaf , add its data to the sum ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key_ ) { this . key = key_ ; this . left = null ; this . right = null ; } } ; static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Stack < Node > stack_ = new Stack < Node > ( ) ; stack_ . Push ( root ) ; int sum = 0 ; while ( stack_ . Count > 0 ) { Node currentNode = stack_ . Peek ( ) ; stack_ . Pop ( ) ; if ( currentNode . left != null ) { stack_ . Push ( currentNode . left ) ; if ( currentNode . left . left == null && currentNode . left . right == null ) { sum = sum + currentNode . left . key ; } } if ( currentNode . right != null ) stack_ . Push ( currentNode . right ) ; } return sum ; } public static void Main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; Console . Write ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + "
 " ) ; } }


"Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree node ; constructor to create a new Node ; Return the sum of left leaf nodes ; A queue of pairs to do bfs traversal and keep track if the node is a left or right child if bool value is true then it is a left child . ; do bfs traversal ; if temp is a leaf node and left child of its parent ; if it is not leaf then push its children nodes into queue ; bool value is true here because it is left child of its parent ; bool value is false here because it is right child of its parent ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key_ ) { key = key_ ; left = null ; right = null ; } } ; public class pair { public Node first ; public bool second ; public pair ( Node first , bool second ) { this . first = first ; this . second = second ; } } static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Queue < pair > q = new Queue < pair > ( ) ; q . Enqueue ( new pair ( root , false ) ) ; int sum = 0 ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) . first ; bool is_left_child = q . Peek ( ) . second ; q . Dequeue ( ) ; if ( is_left_child ) sum = sum + temp . key ; if ( temp . left != null && temp . right != null && is_left_child ) sum = sum - temp . key ; if ( temp . left != null ) { q . Enqueue ( new pair ( temp . left , true ) ) ; } if ( temp . right != null ) { q . Enqueue ( new pair ( temp . right , false ) ) ; } } return sum ; } public static void Main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; Console . Write ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + "
 " ) ; } }


"Find sum of all nodes of the given perfect binary tree | C # code to find sum of all nodes of the given perfect binary tree ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static double sumNodes ( int l ) { double leafNodeCount = Math . Pow ( 2 , l - 1 ) ; double sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; double sum = sumLastLevel * l ; return sum ; } public static void Main ( ) { int l = 3 ; Console . Write ( sumNodes ( l ) ) ; } }


"Find sum of all nodes of the given perfect binary tree | C # program to implement the above approach ; function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; initialize ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static int sumNodes ( int l ) { int leafNodeCount = ( int ) Math . Pow ( 2 , l - 1 ) ; List < List < int > > vec = new List < List < int > > ( ) ; for ( int i = 1 ; i <= l ; i ++ ) { vec . Add ( new List < int > ( ) ) ; } for ( int i = 1 ; i <= leafNodeCount ; i ++ ) { vec [ l - 1 ] . Add ( i ) ; } for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . Count - 1 ) { vec [ i ] . Add ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) ; k += 2 ; } } int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < vec [ i ] . Count ; j ++ ) { sum += vec [ i ] [ j ] ; } } return sum ; } public static void Main ( string [ ] args ) { int l = 3 ; Console . WriteLine ( sumNodes ( l ) ) ; } }


"Find sum of all unique elements in the array for K queries | C # implementation to find the sum of all unique elements of the array after Q queries ; Function to find the sum of unique elements after Q Query ; Updating the array after processing each query ; Making it to 0 - indexing ; Iterating over the array to get the readonly array ; Variable to store the sum ; Hash to maintain perviously occured elements ; Loop to find the maximum sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int uniqueSum ( int [ ] A , int [ , ] R , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int ans = 0 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . Contains ( A [ i ] ) ) ans += A [ i ] ; s . Add ( A [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 0 , 0 , 0 , 0 , 0 } ; int [ , ] R = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } , { 3 , 3 } } ; int N = A . Length ; int M = R . GetLength ( 0 ) ; Console . Write ( uniqueSum ( A , R , N , M ) ) ; } }