"Find minimum number of currency notes and values that sum to given amount | C # program to accept an amount and count number of notes ; function to count and print currency notes ; count notes using Greedy approach ; Print notes ; Driver function"
Solution in C#:

using System ; public class GfG { public static void countCurrency ( int amount ) { int [ ] notes = new int [ ] { 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 } ; int [ ] noteCounter = new int [ 9 ] ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( amount >= notes [ i ] ) { noteCounter [ i ] = amount / notes [ i ] ; amount = amount - noteCounter [ i ] * notes [ i ] ; } } Console . WriteLine ( " Currency ▁ Count ▁ - > " ) ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( noteCounter [ i ] != 0 ) { Console . WriteLine ( notes [ i ] + " ▁ : ▁ " + noteCounter [ i ] ) ; } } } public static void Main ( ) { int amount = 868 ; countCurrency ( amount ) ; } }


"Find minimum number of merge operations to make an array palindrome | C # program to find number of operations to make an array palindrome ; Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinOps ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 4 , 5 , 9 , 1 } ; Console . Write ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + findMinOps ( arr , arr . Length ) ) ; } }


"Find minimum number of steps to reach the end of String | C # implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valiINT_MAXd move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code"
Solution in C#:

using System ; class GFG { static int INT_MAX = int . MaxValue ; static int minSteps ( string str , int n , int k ) { if ( str [ n - 1 ] == '0' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str [ i + k ] == '1' ) steps = Math . Min ( steps , dp [ i + k ] ) ; if ( str [ i + 1 ] == '1' ) steps = Math . Min ( steps , dp [ i + 1 ] ) ; if ( str [ i + 2 ] == '1' ) steps = Math . Min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return - 1 ; return dp [ 0 ] ; } public static void Main ( ) { string str = "101000011" ; int n = str . Length ; int k = 5 ; Console . WriteLine ( minSteps ( str , n , k ) ) ; } }


"Find minimum number to be divided to make a number a perfect square | C # program to find minimum number which divide n to make it a perfect square . ; Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 == 1 ) ans *= 2 ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 == 1 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } public static void Main ( ) { int n = 72 ; Console . WriteLine ( findMinNumber ( n ) ) ; } }


"Find minimum operations needed to make an Array beautiful | C # implementation of above approach ; Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver program"
Solution in C#:

class GFG { static int minOperations ( int [ ] A , int n ) { if ( ( n & 1 ) > 0 ) return - 1 ; int zeros = 0 , consZeros = 0 , ones = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] == 0 ) zeros ++ ; else ones ++ ; if ( i + 1 < n ) { if ( A [ i ] == 0 && A [ i + 1 ] == 0 ) consZeros ++ ; } } if ( A [ 0 ] == A [ n - 1 ] && A [ 0 ] == 0 ) consZeros ++ ; if ( zeros == ones ) return consZeros ; else return - 1 ; } static void Main ( ) { int [ ] A = new int [ ] { 1 , 1 , 0 , 0 } ; int n = A . Length ; System . Console . WriteLine ( minOperations ( A , n ) ) ; } }


"Find minimum positive integer x such that a ( x ^ 2 ) + b ( x ) + c >= k | C # implementation of the approach ; Function to return the minimum positive integer satisfying the given equation ; Binary search to find the value of x ; Return the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int MinimumX ( int a , int b , int c , int k ) { int x = int . MaxValue ; if ( k <= c ) return 0 ; int h = k - c ; int l = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . Min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } public static void Main ( ) { int a = 3 , b = 2 , c = 4 , k = 15 ; Console . Write ( MinimumX ( a , b , c , k ) ) ; } }


"Find minimum possible size of array with given rules for removing elements | C # program to find size of minimum possible array after removing elements according to given rules ; dp [ i , j ] denotes the minimum number of elements left in the subarray arr [ i . . j ] . ; If already evaluated ; If size of array is less than 3 ; Initialize result as the case when first element is separated ( not removed using given rules ) ; Now consider all cases when first element forms a triplet and removed . Check for all possible triplets ( low , i , j ) ; Check if this triplet follows the given rules of removal . And elements between ' low ' and ' i ' , and between ' i ' and ' j ' can be recursively removed . ; Insert value in table and return result ; This function mainly initializes dp table and calls recursive function minSizeRec ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 1000 ; static int [ , ] dp = new int [ MAX , MAX ] ; static int minSizeRec ( int [ ] arr , int low , int high , int k ) { if ( dp [ low , high ] != - 1 ) { return dp [ low , high ] ; } if ( ( high - low + 1 ) < 3 ) { return high - low + 1 ; } int res = 1 + minSizeRec ( arr , low + 1 , high , k ) ; for ( int i = low + 1 ; i <= high - 1 ; i ++ ) { for ( int j = i + 1 ; j <= high ; j ++ ) { if ( arr [ i ] == ( arr [ low ] + k ) && arr [ j ] == ( arr [ low ] + 2 * k ) && minSizeRec ( arr , low + 1 , i - 1 , k ) == 0 && minSizeRec ( arr , i + 1 , j - 1 , k ) == 0 ) { res = Math . Min ( res , minSizeRec ( arr , j + 1 , high , k ) ) ; } } } return ( dp [ low , high ] = res ) ; } static int minSize ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } return minSizeRec ( arr , 0 , n - 1 , k ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 4 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( minSize ( arr , n , k ) ) ; } }


"Find minimum radius such that atleast k point lie inside the circle | C # program to find minimum radius such that atleast k point lie inside the circle ; Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driven Program"
Solution in C#:

using System ; class GFG { static int minRadius ( int k , int [ ] x , int [ ] y , int n ) { int [ ] dis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Array . Sort ( dis ) ; return dis [ k - 1 ] ; } public static void Main ( ) { int k = 3 ; int [ ] x = { 1 , - 1 , 1 } ; int [ ] y = { 1 , - 1 , - 1 } ; int n = x . Length ; Console . WriteLine ( minRadius ( k , x , y , n ) ) ; } }


"Find minimum shift for longest common prefix | C # program to find longest common prefix after rotation of second string . ; function for KMP search ; p [ 1 ] = 0 ; ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and len ; print result ; Driver code"
Solution in C#:

using System ; class GFG { static void KMP ( int m , int n , String str2 , String str1 ) { int pos = 0 , len = 0 ; int [ ] p = new int [ m + 1 ] ; int k = 0 ; char [ ] ch1 = str1 . ToCharArray ( ) ; char [ ] ch2 = str2 . ToCharArray ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && ch1 [ k ] != ch1 [ i - 1 ] ) k = p [ k ] ; if ( ch1 [ k ] == ch1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( int j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && j < n && ch1 [ j ] != ch2 [ i ] ) j = p [ j ] ; if ( j < n && ch1 [ j ] == ch2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } Console . WriteLine ( " Shift ▁ = ▁ " + pos ) ; Console . WriteLine ( " Prefix ▁ = ▁ " + str1 . Substring ( 0 , len ) ) ; } public static void Main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " forgeeksgeeks " ; int n = str1 . Length ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ; } }


"Find minimum speed to finish all Jobs | C # program to find minimum speed to finish all jobs ; Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver program ; Print required maxLenwer"
Solution in C#:

using System ; using System . Linq ; class GFG { static bool isPossible ( int [ ] A , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } static int minJobSpeed ( int [ ] A , int n , int H ) { if ( H < n ) return - 1 ; int max = A . Max ( ) ; int lo = 1 , hi = max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } public static void Main ( ) { int [ ] A = { 3 , 6 , 7 , 11 } ; int H = 8 ; int n = A . Length ; Console . WriteLine ( minJobSpeed ( A , n , H ) ) ; } }


"Find minimum sum of factors of number | C # program to find minimum sum of product of number ; To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code"
Solution in C#:

using System ; public class GFG { static int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } public static void Main ( ) { int num = 12 ; Console . Write ( findMinSum ( num ) ) ; } }


"Find minimum sum such that one of every three consecutive elements is taken | A Dynamic Programming based C # program to find minimum possible sum of elements of array such that an element out of every three consecutive is picked . ; A utility function to find minimum of 3 elements ; Returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; Create a DP table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; When there are less than or equal to 3 elements ; Iterate through all other elements ; Driver code"
Solution in C#:

using System ; class GFG { static int minimum ( int a , int b , int c ) { return Math . Min ( Math . Min ( a , b ) , c ) ; } static int findMinSum ( int [ ] arr , int n ) { int [ ] sum = new int [ n ] ; sum [ 0 ] = arr [ 0 ] ; sum [ 1 ] = arr [ 1 ] ; sum [ 2 ] = arr [ 2 ] ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = arr [ i ] + minimum ( sum [ i - 3 ] , sum [ i - 2 ] , sum [ i - 1 ] ) ; return minimum ( sum [ n - 1 ] , sum [ n - 2 ] , sum [ n - 3 ] ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 20 , 2 , 10 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Min ▁ Sum ▁ is ▁ " + findMinSum ( arr , n ) ) ; } }


"Find minimum time to finish all jobs with given constraints | C # program to find minimum time to finish all jobs with given number of assignees ; Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time ' time ' ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Initialize answer ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; Update answer ; Driver program"
Solution in C#:

using System ; class GFG { static int getMax ( int [ ] arr , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } static bool isPossible ( int time , int K , int [ ] job , int n ) { int cnt = 1 ; int curr_time = 0 ; for ( int i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } static int findMinTime ( int K , int T , int [ ] job , int n ) { int end = 0 , start = 0 ; for ( int i = 0 ; i < n ; ++ i ) end += job [ i ] ; int ans = end ; int job_max = getMax ( job , n ) ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . Min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } public static void Main ( ) { int [ ] job = { 10 , 7 , 8 , 12 , 6 , 8 } ; int n = job . Length ; int k = 4 , T = 5 ; Console . WriteLine ( findMinTime ( k , T , job , n ) ) ; } }


"Find minimum value of the expression by choosing K elements from given array | C # program to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Function to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int minimumValue ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 21 , 5 , 3 , 8 } ; int k = 3 ; int n = arr . Length ; Console . Write ( minimumValue ( arr , n , k ) ) ; } }


"Find minimum value to assign all array elements so that array product becomes greater | C # program to find minimum value that can be assigned to along elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val double val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code"
Solution in C#:

using System ; public class GFG { static long findMinValue ( long [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . Log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . Log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } static public void Main ( ) { long [ ] arr = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . Length ; Console . WriteLine ( findMinValue ( arr , n ) ) ; } }


"Find minimum x such that ( x % k ) * ( x / k ) == n | C # Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver Code to test above function"
Solution in C#:

using System ; public class GFG { static int minimumX ( int n , int k ) { int ans = int . MaxValue ; for ( int rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = Math . Min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } static public void Main ( ) { int n = 4 , k = 6 ; Console . WriteLine ( minimumX ( n , k ) ) ; n = 5 ; k = 5 ; Console . WriteLine ( minimumX ( n , k ) ) ; } }


"Find minimum x such that ( x % k ) * ( x / k ) == n | Set | C # Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code to test above function"
Solution in C#:

using System ; class solution { static int minimumX ( int n , int k ) { int mini = int . MaxValue ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int fir = i ; int sec = n / i ; int num1 = fir * k + sec ; int res = ( num1 / k ) * ( num1 % k ) ; if ( res == n ) mini = Math . Min ( num1 , mini ) ; int num2 = sec * k + fir ; res = ( num2 / k ) * ( num2 % k ) ; if ( res == n ) mini = Math . Min ( num2 , mini ) ; } } return mini ; } public static void Main ( ) { int n = 4 , k = 6 ; Console . WriteLine ( minimumX ( n , k ) ) ; n = 5 ; k = 5 ; Console . WriteLine ( minimumX ( n , k ) ) ; } }


"Find mirror image of a point in 2 | C # code to find mirror image ; function which finds coordinates of mirror image . This function return a pair of double ; Driver code"
Solution in C#:

using System ; class GFG { class pair { public double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static pair mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return new pair ( x , y ) ; } public static void Main ( String [ ] args ) { double a = - 1.0 ; double b = 1.0 ; double c = 0.0 ; double x1 = 1.0 ; double y1 = 0.0 ; pair image = mirrorImage ( a , b , c , x1 , y1 ) ; Console . Write ( " Image ▁ of ▁ point ▁ ( " + x1 + " , ▁ " + y1 + " ) ▁ " ) ; Console . Write ( " by ▁ mirror ▁ ( " + a + " ) x ▁ + ▁ ( " + b + " ) y ▁ + ▁ ( " + c + " ) ▁ = ▁ 0 , ▁ is ▁ : " ) ; Console . WriteLine ( " ( " + image . first + " , ▁ " + image . second + " ) " ) ; } }


"Find mirror of a given node in Binary tree | C # program to find the mirror Node in Binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; create new Node and initialize it ; recursive function to find mirror of Node ; if any of the Node is none then Node itself and decendent have no mirror , so return none , no need to further explore ! ; if left Node is target Node , then return right 's key (that is mirror) and vice   versa  ; first recur external Nodes ; if no mirror found , recur internal Nodes ; interface for mirror search ; Driver code ; target Node whose mirror have to be searched"
Solution in C#:

using System ; class GfG { class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node n = new Node ( ) ; n . key = key ; n . left = null ; n . right = null ; return n ; } static int findMirrorRec ( int target , Node left , Node right ) { if ( left == null right == null ) return 0 ; if ( left . key == target ) return right . key ; if ( right . key == target ) return left . key ; int mirror_val = findMirrorRec ( target , left . left , right . right ) ; if ( mirror_val != 0 ) return mirror_val ; return findMirrorRec ( target , left . right , right . left ) ; } static int findMirror ( Node root , int target ) { if ( root == null ) return 0 ; if ( root . key == target ) return target ; return findMirrorRec ( target , root . left , root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . right = newNode ( 7 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . left = newNode ( 8 ) ; root . right . left . right = newNode ( 9 ) ; int target = root . left . left . key ; int mirror = findMirror ( root , target ) ; if ( mirror != 0 ) Console . WriteLine ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ Node ▁ " + mirror ) ; else Console . WriteLine ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ null ▁ " ) ; } }


"Find missing element in a sorted array of consecutive numbers | C # implementation of the approach ; Function to return the missing element ; Check if middle element is consistent ; No inconsistency till middle elements When missing element is just after the middle element ; Move right ; Inconsistency found When missing element is just before the middle element ; Move left ; No missing element found ; Driver code"
Solution in C#:

using System ; class GFG { public static int findMissing ( int [ ] arr , int n ) { int l = 0 , h = n - 1 ; int mid ; while ( h > l ) { mid = l + ( h - l ) / 2 ; if ( arr [ mid ] - mid == arr [ 0 ] ) { if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) return arr [ mid ] + 1 ; else { l = mid + 1 ; } } else { if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) return arr [ mid ] - 1 ; else { h = mid - 1 ; } } } return - 1 ; } public static void Main ( ) { int [ ] arr = { - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 } ; int n = arr . Length ; Console . WriteLine ( findMissing ( arr , n ) ) ; } }


"Find missing number in another array which is shuffled copy | C # implementation to find the missing number in shuffled array ; Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code"
Solution in C#:

using System ; class GFG { static int missingNumber ( int [ ] arr1 , int [ ] arr2 , int n ) { int mnum = 0 ; for ( int i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } public static void Main ( ) { int [ ] arr1 = { 4 , 8 , 1 , 3 , 7 } ; int [ ] arr2 = { 7 , 4 , 3 , 1 } ; int n = arr1 . Length ; Console . Write ( " Missing ▁ number ▁ = ▁ " + missingNumber ( arr1 , arr2 , n ) ) ; } }


"Find modular node in a linked list | C # program to find modular node in a linked list ; A Linkedlist node ; Function to find modular node in the linked list ; Corner cases ; Traverse the given list ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; public Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void Main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; Console . Write ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) Console . WriteLine ( answer . data ) ; else Console . WriteLine ( " null " ) ; } }


"Find multiple of x closest to or a ^ b ( a raised to power b ) | C # Program to find closest multiple of x to a ^ b ; function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; Answer is either ( ans * x ) or ( ans + 1 ) * x ; Printing nearest answer ; Driver Program"
Solution in C#:

using System ; public class GFG { static void multiple ( int a , int b , int x ) { if ( b < 0 ) { if ( a == 1 && x == 1 ) Console . WriteLine ( "1" ) ; else Console . WriteLine ( "0" ) ; } int mul = ( int ) Math . Pow ( a , b ) ; int ans = mul / x ; int ans1 = x * ans ; int ans2 = x * ( ans + 1 ) ; Console . WriteLine ( ( ( mul - ans1 ) <= ( ans2 - mul ) ) ? ans1 : ans2 ) ; } static public void Main ( ) { int a = 349 , b = 1 , x = 4 ; multiple ( a , b , x ) ; } }


"Find n positive integers that satisfy the given equations | C # implementation of the approach ; Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static void findIntegers ( int n , int x , int y ) { ArrayList ans = new ArrayList ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans . Add ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { Console . Write ( " - 1" ) ; return ; } ans . Add ( y - ( n - 1 ) ) ; int store = 0 ; for ( int i = 0 ; i < n ; i ++ ) store += ( int ) ans [ i ] * ( int ) ans [ i ] ; if ( store < x ) { Console . Write ( " - 1" ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( int ) ans [ i ] + " ▁ " ) ; } static void Main ( ) { int n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ; } }


"Find n | C # program for nth nodes of inorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a binary tree , print its nth nodes of inorder ; first recur on left child ; when count = n then print element ; now recur on right child ; Driver Code"
Solution in C#:

using System ; class GFG { public static int count = 0 ; public class Node { public int data ; public Node left ; public Node right ; } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void NthInorder ( Node node , int n ) { if ( node == null ) { return ; } if ( count <= n ) { NthInorder ( node . left , n ) ; count ++ ; if ( count == n ) { Console . Write ( " { 0 : D } ▁ " , node . data ) ; } NthInorder ( node . right , n ) ; } } public static void Main ( string [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; int n = 4 ; NthInorder ( root , n ) ; } }


"Find n | C # program to calculate nth term of a series ; Function for calualtion ; For summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code"
Solution in C#:

using System ; class GFG { static int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( seriesFunc ( n ) ) ; n = 13 ; Console . WriteLine ( seriesFunc ( 13 ) ) ; } }


"Find n | C # program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } public static int primorial ( int n ) { int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } public static int findNextPrime ( int n ) { int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } public static int fortunateNumber ( int n ) { int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( fortunateNumber ( n ) ) ; } }


"Find n | C # program to find n - th element in the series 9 , 33 , 73 , 128. . ; Returns n - th element of the series ; driver function"
Solution in C#:

using System ; class GFG { static int series ( int n ) { return ( 8 * n * n ) + 1 ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( series ( n ) ) ; } }


"Find n | C # program to find n - th node of Postorder Traversal of Binary Tree ; A binary tree node structure ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; Driver code ; prints n - th node found"
Solution in C#:

using System ; public class NthNodePostOrder { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } static int flag = 0 ; static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) Console . Write ( root . data ) ; } } public static void Main ( String [ ] args ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } }


"Find n | C # program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver Code"
Solution in C#:

using System ; class GFG { static string findNthNo ( int n ) { string res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } char [ ] arr = res . ToCharArray ( ) ; Array . Reverse ( arr ) ; return new string ( arr ) ; } public static void Main ( ) { int n = 13 ; Console . Write ( findNthNo ( n ) ) ; } }


"Find n | C # program to find n - th number in a series made of digits 4 and 7 ; Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int printNthElement ( int n ) { int [ ] arr = new int [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } public static void Main ( ) { int n = 6 ; Console . Write ( printNthElement ( n ) ) ; } }


"Find n | C # program to find n - th term of series 1 , 3 , 6 , 10 , 15 , 21. . . ; Function to find the nth term of series ; Loop to add numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int term ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i ; return ans ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( term ( n ) ) ; } }


"Find n | C # program to find nth term ; utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver code"
Solution in C#:

using System ; class GFG { static int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } public static void Main ( ) { int n = 5 ; Console . WriteLine ( findTerm ( n ) ) ; } }


"Find n | C # program to find the n - th term in series 1 3 6 10 . . . ; Function to find nth term ; Driver Code"
Solution in C#:

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


"Find n | C # program to find the nth element of Stern 's Diatomic Series ; function to find nth stern ' diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver Code"
Solution in C#:

using System ; class GFG { static int findSDSFunc ( int n ) { int [ ] DP = new int [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } static public void Main ( ) { int n = 15 ; Console . WriteLine ( findSDSFunc ( n ) ) ; } }


"Find n | C # program to find the nth term of the series 1 2 2 3 3 3 ... ; function to solve the quadratic equation ; calculating the Nth term ; driver code to check the above function"
Solution in C#:

using System ; class Series { static int term ( int n ) { int x = ( ( ( 1 ) + ( int ) Math . Sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( term ( n ) ) ; } }


"Find n | C # program to find the value at n - th place in the given sequence ; Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code"
Solution in C#:

using System ; class GFG { static int findNumber ( int n ) { int x = ( int ) Math . Floor ( ( - 1 + Math . Sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; int Base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - Base + 1 ; } public static void Main ( ) { int n = 55 ; Console . WriteLine ( findNumber ( n ) ) ; } }


"Find n | C # program to find the value at n - th place in the given sequence ; Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code"
Solution in C#:

using System ; class GFG { static int findNumber ( int n ) { n -- ; int i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } public static void Main ( ) { int n = 3 ; Console . WriteLine ( findNumber ( n ) ) ; } }


"Find n | C # program to print n - th permutation ; utility for calculating factorial ; function for nth permutation ; length of given string ; Count frequencies of all characters ; out string for output string ; iterate till sum equals n ; We update both n and sum in this loop . ; check for characters present in freq [ ] ; Remove character ; calculate sum after fixing a particular char ; if sum > n fix that char as present char and update sum and required nth after fixing char at that position ; if sum < n , add character back ; if sum == n means this char will provide its greatest permutation as nth permutation ; append string termination character and print result ; Driver program to test above method"
Solution in C#:

using System ; public class GFG { static int MAX_CHAR = 26 ; static int MAX_FACT = 20 ; static long [ ] fact = new long [ MAX_FACT ] ; static void precomputeFactorirals ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static void nPermute ( String str , int n ) { precomputeFactorirals ( ) ; int len = str . Length ; int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; string ou = " " ; int sum = 10 ; int k = 0 ; while ( sum >= n ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; int xsum = ( int ) fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= ( int ) ( fact [ freq [ j ] ] ) ; sum += xsum ; if ( sum >= n ) { ou += ( char ) ( i + ' a ' ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { ou += ( char ) ( i + ' a ' ) ; freq [ i ++ ] -- ; } Console . Write ( ou ) ; } public static void Main ( ) { int n = 2 ; String str = " geeksquiz " ; nPermute ( str , n ) ; } }


"Find next Smaller of next Greater in an array | C # Program to find Right smaller element of next greater element ; function find Next greater element ; create empty stack ; Traverse all array elements in reverse order order == ' G ' we compute next greater elements of every element order == ' S ' we compute right smaller element of every element ; Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S ) ; store the next greater element of current element ; If all elements in S were smaller than arr [ i ] ; Push this element ; Function to find Right smaller element of next greater element ; stores indexes of next greater elements ; stores indexes of right smaller elements ; Find next greater element Here G indicate next greater element ; Find right smaller element using same function nextGreater ( ) Here S indicate right smaller elements ; If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void nextGreater ( int [ ] arr , int [ ] next , char order ) { Stack < int > stack = new Stack < int > ( ) ; for ( int i = arr . Length - 1 ; i >= 0 ; i -- ) { while ( stack . Count != 0 && ( ( order == ' G ' ) ? arr [ stack . Peek ( ) ] <= arr [ i ] : arr [ stack . Peek ( ) ] >= arr [ i ] ) ) stack . Pop ( ) ; if ( stack . Count != 0 ) next [ i ] = stack . Peek ( ) ; else next [ i ] = - 1 ; stack . Push ( i ) ; } } public static void nextSmallerOfNextGreater ( int [ ] arr ) { int [ ] NG = new int [ arr . Length ] ; int [ ] RS = new int [ arr . Length ] ; nextGreater ( arr , NG , ' G ' ) ; nextGreater ( arr , RS , ' S ' ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( NG [ i ] != - 1 && RS [ NG [ i ] ] != - 1 ) Console . Write ( arr [ RS [ NG [ i ] ] ] + " ▁ " ) ; else Console . Write ( " - 1 ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 5 , 1 , 9 , 2 , 5 , 1 , 7 } ; nextSmallerOfNextGreater ( arr ) ; } }


"Find next palindrome prime | C # program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code"
Solution in C#:

using System ; using System . Text ; using System . Collections ; class Geeks { static bool isPrime ( int num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( int i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } static int primePalindrome ( int N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( int x = 1 ; x < 100000 ; ++ x ) { string s = x . ToString ( ) ; char [ ] buffer = s . ToCharArray ( ) ; Array . Reverse ( buffer ) ; int y = Int32 . Parse ( s + new string ( buffer ) . Substring ( 1 ) ) ; if ( y >= N && isPrime ( y ) == true ) return y ; } return - 1 ; } public static void Main ( ) { Console . WriteLine ( primePalindrome ( 112 ) ) ; } }


"Find next right node of a given key | Set 2 | C # program to find next right of a given key using preorder traversal ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find next node for given node in same level in a binary tree by using pre - order traversal ; return null if tree is empty ; if desired node is found , set value_level to current level ; if value_level is already set , then current node is the next right node ; recurse for left subtree by increasing level by 1 ; if node is found in left subtree , return it ; recurse for right subtree by increasing level by 1 ; Function to find next node of given node in the same level in given binary tree ; A utility function to test above functions ; Driver main ; Let us create binary tree given in the above example"
Solution in C#:

using System ; class GfG { public class V { public int value_level = 0 ; } public class Node { public Node left , right ; public int key ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static Node nextRightNode ( Node root , int k , int level , V value ) { if ( root == null ) return null ; if ( root . key == k ) { value . value_level = level ; return null ; } else if ( value . value_level != 0 ) { if ( level == value . value_level ) return root ; } Node leftNode = nextRightNode ( root . left , k , level + 1 , value ) ; if ( leftNode != null ) return leftNode ; return nextRightNode ( root . right , k , level + 1 , value ) ; } static Node nextRightNodeUtil ( Node root , int k ) { V v = new V ( ) ; return nextRightNode ( root , k , 1 , v ) ; } static void test ( Node root , int k ) { Node nr = nextRightNodeUtil ( root , k ) ; if ( nr != null ) Console . WriteLine ( " Next ▁ Right ▁ of ▁ " + k + " ▁ is ▁ " + nr . key ) ; else Console . WriteLine ( " No ▁ next ▁ right ▁ node " + " ▁ found ▁ for ▁ " + k ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 6 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 4 ) ; test ( root , 10 ) ; test ( root , 2 ) ; test ( root , 6 ) ; test ( root , 5 ) ; test ( root , 8 ) ; test ( root , 4 ) ; } }


"Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U | C # program for the above approach ; To store the time ; Function to perform DFS to store times , distance and parent of each node ; Update the distance of node u ; Update parent of node u ; Increment time timeT ; Discovery time of node u ; Traverse the adjacency list of current node and recursively call DFS for each vertex ; If current node Adj [ u , i ] is unvisited ; Update the finishing time ; Function to add edges between nodes u and v ; Function to find the node U such that path from root to U contains nodes in V [ ] ; Initialise vis , dis , parent , preTime , and postTime ; Store Adjacency List ; Create adjacency List ; Perform DFS Traversal ; Stores the distance of deepest vertex ' u ' ; Update the deepest node by traversing the qu [ ] ; Find deepest vertex ; Replace each vertex with it 's  corresponding parent except  the root vertex ; Checks if the ancestor with respect to deepest vertex u ; Update ans ; Print the result ; Driver Code ; Total vertices ; Given set of vertices ; Given edges ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int timeT = 0 ; static void dfs ( int u , int p , int dis , int [ ] vis , int [ ] distance , int [ ] parent , int [ ] preTime , int [ ] postTime , List < List < int > > Adj ) { distance [ u ] = dis ; parent [ u ] = p ; vis [ u ] = 1 ; timeT ++ ; preTime [ u ] = timeT ; for ( int i = 0 ; i < Adj [ u ] . Count ; i ++ ) { if ( vis [ Adj [ u ] [ i ] ] == 0 ) { dfs ( Adj [ u ] [ i ] , u , dis + 1 , vis , distance , parent , preTime , postTime , Adj ) ; } } timeT ++ ; postTime [ u ] = timeT ; } static void addEdge ( List < List < int > > Adj , int u , int v ) { Adj [ u ] . Add ( v ) ; Adj [ v ] . Add ( u ) ; } static void findNodeU ( int N , int V , int [ ] Vertices , int [ , ] Edges ) { int [ ] vis = new int [ N + 1 ] ; int [ ] distance = new int [ N + 1 ] ; int [ ] parent = new int [ N + 1 ] ; int [ ] preTime = new int [ N + 1 ] ; int [ ] postTime = new int [ N + 1 ] ; List < List < int > > Adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . Add ( new List < int > ( ) ) ; int u = 0 , v ; for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdge ( Adj , Edges [ i , 0 ] , Edges [ i , 1 ] ) ; } dfs ( 1 , 0 , 0 , vis , distance , parent , preTime , postTime , Adj ) ; int maximumDistance = 0 ; maximumDistance = 0 ; for ( int k = 0 ; k < V ; k ++ ) { if ( maximumDistance < distance [ Vertices [ k ] ] ) { maximumDistance = distance [ Vertices [ k ] ] ; u = Vertices [ k ] ; } if ( parent [ Vertices [ k ] ] != 0 ) { Vertices [ k ] = parent [ Vertices [ k ] ] ; } } bool ans = true ; bool flag ; for ( int k = 0 ; k < V ; k ++ ) { if ( preTime [ Vertices [ k ] ] <= preTime [ u ] && postTime [ Vertices [ k ] ] >= postTime [ u ] ) flag = true ; else flag = false ; ans = ans & flag ; } if ( ans ) Console . WriteLine ( u ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int N = 10 ; int V = 5 ; int [ ] Vertices = { 4 , 3 , 8 , 9 , 10 } ; int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 5...


"Find non | C # program for the above approach ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int INF64 = ( int ) 1e8 ; static int N = 200 * 1000 + 13 ; static int n ; static int [ ] arr = new int [ N ] ; static int [ ] brr = new int [ N ] ; static void brute ( int ind , int l , int r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < ( int ) n ; i ++ ) Console . Write ( brr [ i ] + " ▁ " ) ; System . Environment . Exit ( 0 ) ; } for ( int i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } public static void Main ( ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; } }


"Find nth Fibonacci number using Golden ratio | C # program to find n - th Fibonacci number ; Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; Driver code"
Solution in C#:

using System ; class GFG { static double PHI = 1.6180339 ; static int [ ] f = { 0 , 1 , 1 , 2 , 3 , 5 } ; static int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 ; int fn = 5 ; while ( t < n ) { fn = ( int ) Math . Round ( fn * PHI ) ; t ++ ; } return fn ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( n + " th ▁ Fibonacci " + " ▁ Number ▁ = ▁ " + fib ( n ) ) ; } }


"Find nth Hermite number | C # program to find nth Hermite number ; Function to return nth Hermite number ; Base condition ; Driver Code ; Print nth Hermite number"
Solution in C#:

using System ; class GFG { static int getHermiteNumber ( int n ) { if ( n == 0 ) return 1 ; else if ( n == 1 ) return 1 ; else return - 2 * ( n - 1 ) * getHermiteNumber ( n - 2 ) ; } public static void Main ( ) { int n = 6 ; Console . WriteLine ( getHermiteNumber ( n ) ) ; } }


"Find nth Hermite number | C # program to find nth Hermite number ; Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number"
Solution in C#:

using System ; class GFG { static int doubleFactorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } static int hermiteNumber ( int n ) { if ( n % 2 == 1 ) return 0 ; else { int number = ( int ) ( Math . Pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * - 1 ; return number ; } } public static void Main ( ) { int n = 6 ; Console . WriteLine ( hermiteNumber ( n ) ) ; } }


"Find nth Magic Number | C # program to find nth magic number ; Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit or n = n / 2 ; Driver Code"
Solution in C#:

using System ; public class GFG { static int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n != 0 ) { pow = pow * 5 ; if ( ( int ) ( n & 1 ) == 1 ) answer += pow ; n >>= 1 ; } return answer ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( " nth ▁ magic " + " ▁ number ▁ is ▁ " + nthMagicNo ( n ) ) ; } }


"Find nth term of a given recurrence relation | C # program to find nth term of a given recurrence relation ; function to return required value ; Get the answer ; Return the answer ; Driver code ; Get the value of n ; function call to print result"
Solution in C#:

using System ; class GFG { static int sum ( int n ) { int ans = ( n * ( n - 1 ) ) / 2 ; return ans ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum ( n ) ) ; } }


"Find nth term of the Dragon Curve Sequence | C # code to find nth term of the Dragon Curve Sequence ; function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Driver Code ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output"
Solution in C#:

using System ; class GFG { static String Dragon_Curve_Sequence ( int n ) { String s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { String temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . Length ; j ++ ) { temp += s [ j ] ; if ( prev == '0' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } public static void Main ( ) { int n = 4 ; String s = Dragon_Curve_Sequence ( n ) ; Console . WriteLine ( s ) ; } }


"Find nth term of the series 5 2 13 41 | C # program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; to store the nth term of series ; if n is even number ; if n is odd number ; return nth term ; Driver code"
Solution in C#:

using System ; class GFG { static long nthTermOfTheSeries ( int n ) { long nthTerm ; if ( n % 2 == 0 ) nthTerm = ( long ) Math . Pow ( n - 1 , 2 ) + n ; else nthTerm = ( long ) Math . Pow ( n + 1 , 2 ) + n ; return nthTerm ; } public static void Main ( ) { int n ; n = 8 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 12 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 102 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 999 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; n = 9999 ; Console . WriteLine ( nthTermOfTheSeries ( n ) ) ; } }


"Find number formed by K times alternatively reducing X and adding Y to 0 | C # program for the above approach ; Function to find the value obtained after alternatively reducing X and adding Y to 0 total K number of times ; Stores the final result after adding only Y to 0 ; Stores the final number after reducing only X from 0 ; Return the result obtained ; Driver Code"
Solution in C#:

using System ; class GFG { static int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = - 1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } public static void Main ( String [ ] args ) { int X = 2 , Y = 5 , K = 3 ; Console . WriteLine ( positionAfterKJumps ( X , Y , K ) ) ; } }


"Find number from given list for which value of the function is closest to A | C # program to find number from given list for which value of the function is closest to A ; Function to find number from given list for which value of the function is closest to A ; Stores the final index ; Declaring a variable to store the minimum absolute difference ; Finding F ( n ) ; Updating the index of the answer if new absolute difference is less than tmp ; Driver code"
Solution in C#:

using System ; class GFG { static int leastValue ( int P , int A , int N , int [ ] a ) { int ans = - 1 ; float tmp = float . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { float t = ( float ) ( P - a [ i ] * 0.006 ) ; if ( Math . Abs ( t - A ) < tmp ) { tmp = Math . Abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } public static void Main ( String [ ] args ) { int N = 2 , P = 12 , A = 2005 ; int [ ] a = { 1000 , 2000 } ; Console . WriteLine ( leastValue ( P , A , N , a ) ) ; } }


"Find number from its divisors | C # implementation of the approach ; Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of a number ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findX ( int [ ] a , int n ) { Array . Sort ( a ) ; int x = a [ 0 ] * a [ n - 1 ] ; List < int > vec = new List < int > ( ) ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { vec . Add ( i ) ; if ( ( x / i ) != i ) vec . Add ( x / i ) ; } } vec . Sort ( ) ; if ( vec . Count != n ) { return - 1 ; } else { int i = 0 ; foreach ( int it in vec ) { if ( a [ i ++ ] != it ) return - 1 ; } } return x ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 5 , 4 , 10 } ; int n = a . Length ; Console . Write ( findX ( a , n ) ) ; } }


"Find number of days between two given dates | C # program two find number of days between two given dates ; A date has day ' d ' , month ' m ' and year ' y ' ; To store number of days in all months from January to Dec . ; This function counts number of leap years before the given date ; Check if the current year needs to be considered for the count of leap years or not ; An year is a leap year if it is a multiple of 4 , multiple of 400 and not a multiple of 100. ; This function returns number of days between two given dates ; initialize count using years and day ; Add days for months in given date ; Since every leap year is of 366 days , Add a day for every leap year ; SIMILARLY , COUNT TOTAL NUMBER OF DAYS BEFORE ' dt2' ; return difference between two counts ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public class Date { public int d , m , y ; public Date ( int d , int m , int y ) { this . d = d ; this . m = m ; this . y = y ; } } ; static int [ ] monthDays = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; static int countLeapYears ( Date d ) { int years = d . y ; if ( d . m <= 2 ) { years -- ; } return years / 4 - years / 100 + years / 400 ; } static int getDifference ( Date dt1 , Date dt2 ) { int n1 = dt1 . y * 365 + dt1 . d ; for ( int i = 0 ; i < dt1 . m - 1 ; i ++ ) { n1 += monthDays [ i ] ; } n1 += countLeapYears ( dt1 ) ; int n2 = dt2 . y * 365 + dt2 . d ; for ( int i = 0 ; i < dt2 . m - 1 ; i ++ ) { n2 += monthDays [ i ] ; } n2 += countLeapYears ( dt2 ) ; return ( n2 - n1 ) ; } public static void Main ( String [ ] args ) { Date dt1 = new Date ( 1 , 2 , 2000 ) ; Date dt2 = new Date ( 1 , 2 , 2004 ) ; Console . WriteLine ( " Difference ▁ between ▁ two ▁ dates ▁ is ▁ " + getDifference ( dt1 , dt2 ) ) ; } }


"Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | C # implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Driver code ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values of a subtree ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; Add edges"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] m ; static int [ , ] a ; static int [ ] x ; static List < int > [ ] g ; static int ans = 0 ; static void dfs ( int u , int p ) { foreach ( int v in g [ u ] ) { if ( v != p ) { dfs ( v , u ) ; for ( int i = 0 ; i < 22 ; i ++ ) a [ u , i ] += a [ v , i ] ; } } int pp = 0 ; for ( int i = 0 ; i < 22 ; i ++ ) { if ( ! ( ( a [ u , i ] > 0 && x [ i ] - a [ u , i ] > 0 ) || ( a [ u , i ] == 0 && x [ i ] == 0 ) ) ) { pp = 1 ; break ; } } if ( pp == 0 ) ans ++ ; } public static void Main ( String [ ] args ) { int n = 4 ; g = new List < int > [ n + 1 ] ; m = new int [ n + 1 ] ; m [ 1 ] = 1 ; m [ 2 ] = 3 ; m [ 3 ] = 2 ; m [ 4 ] = 3 ; a = new int [ n + 1 , 22 ] ; x = new int [ 22 ] ; for ( int i = 1 ; i <= n ; i ++ ) { g [ i ] = new List < int > ( ) ; int y = m [ i ] ; int k = 0 ; while ( y != 0 ) { int p = y % 2 ; if ( p == 1 ) { x [ k ] ++ ; a [ i , k ] ++ ; } y = y / 2 ; k ++ ; } } g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; g [ 1 ] . Add ( 3 ) ; g [ 3 ] . Add ( 1 ) ; g [ 1 ] . Add ( 4 ) ; g [ 4 ] . Add ( 1 ) ; dfs ( 1 , 0 ) ; Console . WriteLine ( ans ) ; } }


"Find number of endless points | C # program to find count of endless points ; Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage in row or column after this point , increment result . ; Driver code"
Solution in C#:

using System ; public class GFG { static int countEndless ( bool [ , ] input , int n ) { bool [ , ] row = new bool [ n , n ] ; bool [ , ] col = new bool [ n , n ] ; for ( int j = 0 ; j < n ; j ++ ) { bool isEndless = true ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i , j ] == false ) isEndless = false ; col [ i , j ] = isEndless ; } } for ( int i = 0 ; i < n ; i ++ ) { bool isEndless = true ; for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( input [ i , j ] == false ) isEndless = false ; row [ i , j ] = isEndless ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) if ( row [ i , j ] && col [ i , j ] ) ans ++ ; return ans ; } public static void Main ( ) { bool [ , ] input = { { true , false , true , true } , { false , true , true , true } , { true , true , true , true } , { false , true , true , false } } ; int n = 4 ; Console . Write ( countEndless ( input , n ) ) ; } }


"Find number of factors of N when location of its two factors whose product is N is given | C # program to implement the above problem ; Function to find the number of factors ; print the number of factors ; Driver code ; initialize the factors position"
Solution in C#:

using System ; class GFG { static void findFactors ( int a , int b ) { int c ; c = a + b - 1 ; Console . Write ( c ) ; } public static void Main ( String [ ] args ) { int a , b ; a = 13 ; b = 36 ; findFactors ( a , b ) ; } }


"Find number of magical pairs of string of length L | C # implementation of the approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; Y must be even now y = y >> 1 ; y = y / 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } public static void Main ( ) { int L = 2 ; int P = ( int ) Math . Pow ( 10 , 9 ) ; int ans = power ( 325 , L , P ) ; Console . WriteLine ( ans ) ; } }


"Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | C # program to finds the number of pairs ( x , y ) from [ ] X and [ ] Y such that x ^ y > y ^ x ; Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver code"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] X , int [ ] Y , int m , int n ) { int [ ] suffix = new int [ 1005 ] ; long total_pairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( int i = ( int ) 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return ( int ) total_pairs ; } public static void Main ( String [ ] args ) { int [ ] X = { 10 , 19 , 18 } ; int [ ] Y = { 11 , 15 , 9 } ; int m = X . Length ; int n = Y . Length ; Console . Write ( countPairs ( X , Y , m , n ) ) ; } }


"Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x | C # program to finds number of pairs ( x , y ) in an array such that x ^ y > y ^ x ; Function to return count of pairs with x as one element of the pair . It mainly looks for all values in Y [ ] where x ^ Y [ i ] > Y [ i ] ^ x ; If x is 0 , then there cannot be any value in Y such that x ^ Y [ i ] > Y [ i ] ^ x ; If x is 1 , then the number of pais is equal to number of zeroes in Y [ ] ; Find number of elements in Y [ ] with values greater than x getting upperbound of x with binary search ; If we have reached here , then x must be greater than 1 , increase number of pairs for y = 0 and y = 1 ; Decrease number of pairs for x = 2 and ( y = 4 or y = 3 ) ; Increase number of pairs for x = 3 and y = 2 ; Function to that returns count of pairs ( x , y ) such that x belongs to X [ ] , y belongs to Y [ ] and x ^ y > y ^ x ; To store counts of 0 , 1 , 2 , 3 and 4 in array Y ; Sort Y [ ] so that we can do binary search in it ; Initialize result ; Take every element of X and count pairs with it ; Driver method"
Solution in C#:

using System ; class GFG { static int count ( int x , int [ ] Y , int n , int [ ] NoOfY ) { if ( x == 0 ) return 0 ; if ( x == 1 ) return NoOfY [ 0 ] ; int idx = Array . BinarySearch ( Y , x ) ; int ans ; if ( idx < 0 ) { idx = Math . Abs ( idx + 1 ) ; ans = Y . Length - idx ; } else { while ( idx < n && Y [ idx ] == x ) { idx ++ ; } ans = Y . Length - idx ; } ans += ( NoOfY [ 0 ] + NoOfY [ 1 ] ) ; if ( x == 2 ) ans -= ( NoOfY [ 3 ] + NoOfY [ 4 ] ) ; if ( x == 3 ) ans += NoOfY [ 2 ] ; return ans ; } static int countPairs ( int [ ] X , int [ ] Y , int m , int n ) { int [ ] NoOfY = new int [ 5 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( Y [ i ] < 5 ) NoOfY [ Y [ i ] ] ++ ; Array . Sort ( Y ) ; int total_pairs = 0 ; for ( int i = 0 ; i < m ; i ++ ) total_pairs += count ( X [ i ] , Y , n , NoOfY ) ; return total_pairs ; } public static void Main ( ) { int [ ] X = { 2 , 1 , 6 } ; int [ ] Y = { 1 , 5 } ; Console . Write ( " Total ▁ pairs ▁ = ▁ " + countPairs ( X , Y , X . Length , Y . Length ) ) ; } }


"Find number of pairs ( x , y ) in an array such that x ^ y > y ^ x |"
Solution in C#:

public static int countPairsBruteForce ( int [ ] X , int [ ] Y , int m , int n ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( Math . Pow ( X [ i ] , Y [ j ] ) > Math . Pow ( Y [ j ] , X [ i ] ) ) ans ++ ; return ans ; }


"Find number of solutions of a linear equation of n variables | A Dynamic programming based C # program to find number of non - negative solutions for a given linear equation ; Returns counr of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver code"
Solution in C#:

using System ; class GFG { static int countSol ( int [ ] coeff , int n , int rhs ) { int [ ] dp = new int [ rhs + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; } public static void Main ( ) { int [ ] coeff = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . Length ; Console . Write ( countSol ( coeff , n , rhs ) ) ; } }


"Find number of solutions of a linear equation of n variables | A naive recursive C # program to find number of non - negative solutions for a given linear equation ; Recursive function that returns count of solutions for given RHS value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code"
Solution in C#:

using System ; class GFG { static int countSol ( int [ ] coeff , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } public static void Main ( ) { int [ ] coeff = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . Length ; Console . Write ( countSol ( coeff , 0 , n - 1 , rhs ) ) ; } }


"Find number of square of area Z which can be built in a matrix having blocked regions | C # implementation of the approach ; Function to calculate the number of square areas of size K * K ; Row array and column array to store the lengths of differences between consecutive rows / columns ; Fill the conrow vector ; Fill the concol vector ; To store the required answer ; Every pair of row size and column size would result in an unblocked region ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int subgrids ( int N , int Z , int [ ] row , int [ ] col , int r , int d ) { List < int > conrow = new List < int > ( ) ; List < int > concol = new List < int > ( ) ; int K = ( int ) Math . Sqrt ( Z ) ; conrow . Add ( row [ 0 ] - 0 - 1 ) ; conrow . Add ( N + 1 - row [ r - 1 ] - 1 ) ; for ( int i = 1 ; i < r ; i ++ ) { conrow . Add ( row [ i ] - row [ i - 1 ] - 1 ) ; } concol . Add ( col [ 0 ] - 0 - 1 ) ; concol . Add ( N + 1 - col [ d - 1 ] - 1 ) ; for ( int i = 1 ; i < d ; i ++ ) { concol . Add ( col [ i ] - col [ i - 1 ] - 1 ) ; } int row_size = conrow . Count ; int col_size = concol . Count ; int answer = 0 ; for ( int i = 0 ; i < row_size ; i ++ ) { for ( int j = 0 ; j < col_size ; j ++ ) { int total = ( concol [ j ] / K ) * ( conrow [ i ] / K ) ; answer += ( total ) ; } } return answer ; } public static void Main ( String [ ] args ) { int N = 8 , Z = 4 ; int [ ] row = { 4 , 6 } ; int [ ] col = { 3 , 8 } ; int r = row . Length ; int d = col . Length ; Console . Write ( subgrids ( N , Z , row , col , r , d ) ) ; } }


"Find number of subarrays with XOR value a power of 2 | C # Program to count number of subarrays with Bitwise - XOR as power of 2 ; Function to find number of subarrays ; Hash Map to store prefix XOR values ; When no element is selected ; Check for all the powers of 2 , till a MAX value ; Insert Current prefixxor in Hash Map ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 10 ; static int findSubarray ( int [ ] array , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , 1 ) ; int answer = 0 ; int preXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int value = 1 ; preXor ^= array [ i ] ; for ( int j = 1 ; j <= MAX ; j ++ ) { int Y = value ^ preXor ; if ( mp . ContainsKey ( Y ) ) { answer += mp [ Y ] ; } value *= 2 ; } if ( mp . ContainsKey ( preXor ) ) { mp [ preXor ] = mp [ preXor ] + 1 ; } else { mp . Add ( preXor , 1 ) ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] array = { 2 , 6 , 7 , 5 , 8 } ; int n = array . Length ; Console . WriteLine ( findSubarray ( array , n ) ) ; } }


"Find number of subarrays with even sum | C # program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver code"
Solution in C#:

using System ; class GFG { static int countEvenSum ( int [ ] arr , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " ) ; Console . WriteLine ( countEvenSum ( arr , n ) ) ; } }


"Find number of subarrays with even sum | C # program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single even element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even or odd count ; Use handshake lemma to count even subarrays ( Note that an even cam be formed by two even or two odd ) ; Driver code"
Solution in C#:

using System ; class GFG { static int countEvenSum ( int [ ] arr , int n ) { int [ ] temp = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }


"Find number of subarrays with even sum | C # program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; result may be large enough not to fit in int ; ; to keep track of subarrays with even sum starting from index i ; if a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomeseven when a [ i ] gets added to it . ; if a [ i ] is even then all subarrays starting from index i + 1 which was even remainseven and one extra a [ i ] even subarray gets added to it . ; Driver Code"
Solution in C#:

using System ; public class GFG { public static long countEvenSum ( int [ ] a , int n ) { long res = 0 ; int s = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] % 2 == 1 ) { s = n - i - 1 - s ; } else { s = s + 1 ; } res = res + s ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . Length ; Console . WriteLine ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }


"Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | C # program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code"
Solution in C#:

using System ; public class GFG { static int count ( string s , int k ) { int n = s . Length ; int d = 0 , i ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s [ i ] ; if ( d % k == 0 ) count += 1 ; for ( i = k ; i < n ; i ++ ) { int prev = s [ i - k ] ; d -= prev ; d += s [ i ] ; if ( d % k == 0 ) count += 1 ; } return count ; } public static void Main ( ) { string s = " bcgabc " ; int k = 3 ; int ans = count ( s , k ) ; Console . Write ( ans ) ; } }


"Find number of times a string occurs as a subsequence in given string | A Dynamic Programming based C # program to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; Iterative DP function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; Create a table to store results of sub - problems ; If first string is empty ; If second string is empty ; Fill lookup [ ] [ ] in bottom up manner ; If last characters are same , we have two options - 1. consider last characters of both strings in solution 2. ignore last character of first string ; If last character are different , ignore last character of first string ; Driver Code"
Solution in C#:

using System ; class GFG { static int count ( String a , String b ) { int m = a . Length ; int n = b . Length ; int [ , ] lookup = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) lookup [ 0 , i ] = 0 ; for ( int i = 0 ; i <= m ; ++ i ) lookup [ i , 0 ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( a [ i - 1 ] == b [ j - 1 ] ) lookup [ i , j ] = lookup [ i - 1 , j - 1 ] + lookup [ i - 1 , j ] ; else lookup [ i , j ] = lookup [ i - 1 , j ] ; } } return lookup [ m , n ] ; } public static void Main ( ) { String a = " GeeksforGeeks " ; String b = " Gks " ; Console . WriteLine ( count ( a , b ) ) ; } }


"Find number of times a string occurs as a subsequence in given string | A Naive recursive C # program to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; Recursive function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; If both first and second string is empty , or if second string is empty , return 1 ; If only first string is empty and second string is not empty , return 0 ; If last characters are same Recur for remaining strings by 1. considering last characters of both strings 2. ignoring last character of first string ; If last characters are different , ignore last char of first string and recur for remaining string ; Driver code"
Solution in C#:

using System ; class GFG { static int count ( string a , string b , int m , int n ) { if ( ( m == 0 && n == 0 ) n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a [ m - 1 ] == b [ n - 1 ] ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; } public static void Main ( ) { string a = " GeeksforGeeks " ; string b = " Gks " ; Console . Write ( count ( a , b , a . Length , b . Length ) ) ; } }


"Find number of transformation to make two Matrix Equal | C # program to find number of countOpsation to make two matrix equals ; Update matrix A [ ] [ ] so that only A [ ] [ ] has to be countOpsed ; Check necessary condition for condition for existence of full countOpsation ; If countOpsation is possible calculate total countOpsation ; Driver code"
Solution in C#:

using System ; class GFG { static int countOps ( int [ , ] A , int [ , ] B , int m , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) A [ i , j ] -= B [ i , j ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j < m ; j ++ ) if ( A [ i , j ] - A [ i , 0 ] - A [ 0 , j ] + A [ 0 , 0 ] != 0 ) return - 1 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += Math . Abs ( A [ i , 0 ] ) ; for ( int j = 0 ; j < m ; j ++ ) result += Math . Abs ( A [ 0 , j ] - A [ 0 , 0 ] ) ; return ( result ) ; } public static void Main ( ) { int [ , ] A = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int [ , ] B = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Console . Write ( countOps ( A , B , 3 , 3 ) ) ; } }


"Find numbers a and b that satisfy the given conditions | C # implementation of the above approach ; Function to print the required numbers ; Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; Special case if n = 1 we get a = 0 so ( a * b ) < n ; If no pair satisfies the conditions ; Driver code"
Solution in C#:

using System ; class GFG { static void find ( int n ) { int b = n ; int a = b * ( n - 1 ) ; if ( a * b > n && a / b < n ) { Console . Write ( " a ▁ = ▁ " + a + " , ▁ b ▁ = ▁ " + b ) ; } else Console . WriteLine ( - 1 ) ; } public static void Main ( ) { int n = 10 ; find ( n ) ; } }


"Find numbers in range [ L , R ] that are coprime with given Array elements | C # program for the above approach ; Function to find all the elements in the range [ L , R ] which are co prime with all array elements ; Store all the divisors of array element in S ; Find the divisors ; Stores all possible required number satisfying the given criteria ; Insert all element [ L , R ] ; Traverse the set ; Remove the multiples of ele ; Print the resultant numbers ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void elementsCoprimeWithArr ( int [ ] A , int N , int L , int R ) { HashSet < int > S = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = A [ i ] ; for ( int j = 1 ; j <= Math . Sqrt ( curr_ele ) + 1 ; j ++ ) { if ( curr_ele % j == 0 ) { S . Add ( j ) ; S . Add ( curr_ele / j ) ; } } } HashSet < int > store = new HashSet < int > ( ) ; for ( int i = L ; i <= R ; i ++ ) store . Add ( i ) ; S . Remove ( 1 ) ; foreach ( int it in S ) { int ele = it ; int index = 1 ; while ( index * ele <= R ) { store . Remove ( index * ele ) ; index ++ ; } } foreach ( int i in store ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 } ; int L = 1 , R = 10 ; int N = arr . Length ; elementsCoprimeWithArr ( arr , N , L , R ) ; } }


"Find numbers of balancing positions in string | C # program to find number of balancing points in string ; method to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 256 ; static int countBalance ( string s ) { int [ ] rightVisited = new int [ MAX_CHAR ] ; int [ ] leftVisited = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) rightVisited [ s [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftVisited [ s [ i ] ] ++ ; rightVisited [ s [ i ] ] -- ; int j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } public static void Main ( String [ ] args ) { string str = " abaababa " ; Console . WriteLine ( countBalance ( str ) ) ; } }


"Find numbers that divide X and Y to produce the same remainder | C # program to find numbers that divide X and Y to produce the same remainder ; Function to find the required number as M ; Finding the maximum value among X and Y ; Loop to iterate through maximum value among X and Y . ; If the condition satisfies , then print the value of M ; Driver code"
Solution in C#:

using System ; class GFG { static void printModulus ( int X , int Y ) { int n = Math . Max ( X , Y ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( X % i == Y % i ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int X , Y ; X = 10 ; Y = 20 ; printModulus ( X , Y ) ; } }


"Find numbers that divide X and Y to produce the same remainder | C # program to find numbers that divide X and Y to produce the same remainder ; Function to print all the possible values of M such that X % M = Y % M ; Finding the absolute difference of X and Y ; Iterating from 1 ; Loop to print all the factors of D ; If i is a factor of d , then print i ; If d / i is a factor of d , then print d / i ; Driver code"
Solution in C#:

using System ; public class GFG { static void printModulus ( int X , int Y ) { int d = Math . Abs ( X - Y ) ; int i = 1 ; while ( i * i <= d ) { if ( d % i == 0 ) { Console . Write ( i + " ▁ " ) ; if ( d / i != i ) Console . Write ( d / i + " ▁ " ) ; } i ++ ; } } public static void Main ( String [ ] args ) { int X = 10 ; int Y = 26 ; printModulus ( X , Y ) ; } }


"Find numbers which are multiples of first array and factors of second array | C # implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; 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 int lcm ( int x , int y ) { int temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } static void findNumbers ( int [ ] a , int n , int [ ] b , int m ) { int lcmA = 1 , gcdB = 0 ; for ( int i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { Console . Write ( " - 1" ) ; return ; } int num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) Console . Write ( num + " ▁ " ) ; num += lcmA ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 2 , 4 } ; int [ ] b = { 16 , 32 , 64 } ; int n = a . Length ; int m = b . Length ; findNumbers ( a , n , b , m ) ; } }


"Find numbers with K odd divisors in a given range | C # program to count numbers with k odd divisors in a range . ; Utility function to check if number is perfect square or not ; Utility Function to return count of divisors of a number ; Note that this loop runs till square root ; If divisors are equal , count it only once ; Otherwise print both ; Function to calculate all divisors having exactly k divisors between a and b ; Initialize result ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPerfect ( int n ) { int s = ( int ) ( Math . Sqrt ( n ) ) ; return ( s * s == n ) ; } static int divisorsCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count += 1 ; else count += 2 ; } } return count ; } static int kDivisors ( int a , int b , int k ) { int count = 0 ; for ( int i = a ; i <= b ; i ++ ) { if ( isPerfect ( i ) ) if ( divisorsCount ( i ) == k ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int a = 21 , b = 149 , k = 333 ; Console . Write ( kDivisors ( a , b , k ) ) ; } }


"Find one extra character in a string | C # program to find extra character in one string ; Determine String with extra character . ; Add character codes of both the strings ; Add last character code of large String . ; Minus the character code of smaller string from the character code of large string . The result will be the extra character code . ; Driver code"
Solution in C#:

using System ; class GFG { private static char findExtraCharacter ( String s1 , String s2 ) { String smallStr ; String largeStr ; if ( s1 . Length > s2 . Length ) { smallStr = s2 ; largeStr = s1 ; } else { smallStr = s1 ; largeStr = s2 ; } int smallStrCodeTotal = 0 ; int largeStrCodeTotal = 0 ; int i = 0 ; for ( ; i < smallStr . Length ; i ++ ) { smallStrCodeTotal += smallStr [ i ] ; largeStrCodeTotal += largeStr [ i ] ; } largeStrCodeTotal += largeStr [ i ] ; int intChar = largeStrCodeTotal - smallStrCodeTotal ; return ( char ) intChar ; } public static void Main ( String [ ] args ) { String s1 = " abcd " ; String s2 = " cbdae " ; char extraChar = findExtraCharacter ( s1 , s2 ) ; Console . WriteLine ( " Extra ▁ character : ▁ " + extraChar ) ; } }


"Find one extra character in a string | C # program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; Driver Code ; given string"
Solution in C#:

using System ; class GFG { static char findExtraCharcter ( string strA , string strB ) { int res = 0 , i ; for ( i = 0 ; i < strA . Length ; i ++ ) { res ^= strA [ i ] ; } for ( i = 0 ; i < strB . Length ; i ++ ) { res ^= strB [ i ] ; } return ( ( char ) ( res ) ) ; } public static void Main ( ) { string strA = " abcd " ; string strB = " cbdad " ; Console . WriteLine ( findExtraCharcter ( strA , strB ) ) ; } }


"Find one extra character in a string | C # program to find extra character in one string ; store string values in map ; store second string in map with frequency ; store first string in map with frequency ; if the frequency is 1 then this character is which is added extra ; Driver code ; given string ; find Extra Character"
Solution in C#:

using System ; class GFG { static char findExtraCharcter ( char [ ] strA , char [ ] strB ) { int [ ] m1 = new int [ 256 ] ; for ( int i = 0 ; i < strB . Length ; i ++ ) m1 [ strB [ i ] ] ++ ; for ( int i = 0 ; i < strA . Length ; i ++ ) m1 [ strA [ i ] ] -- ; for ( int i = 0 ; i < m1 . Length ; i ++ ) { if ( m1 [ i ] == 1 ) return ( char ) i ; } return char . MinValue ; } public static void Main ( String [ ] args ) { String strA = " abcd " ; String strB = " cbdad " ; Console . WriteLine ( findExtraCharcter ( strA . ToCharArray ( ) , strB . ToCharArray ( ) ) ) ; } }


"Find optimal weights which can be used to weigh all the weights in the range [ 1 , X ] | C # implementation of the approach ; Function to find the optimal weights ; Number of weights required ; Finding the value of required powers of 3 ; Optimal Weights are powers of 3 ; Driver code"
Solution in C#:

using System ; class GFG { static void findWeights ( int X ) { int sum = 0 ; int power = 0 ; int number = 3 ; while ( sum < X ) { sum = number - 1 ; sum /= 2 ; power ++ ; number *= 3 ; } int ans = 1 ; for ( int i = 1 ; i <= power ; i ++ ) { Console . Write ( ans + " ▁ " ) ; ans = ans * 3 ; } } static public void Main ( ) { int X = 2 ; findWeights ( X ) ; } }


"Find original numbers from gcd ( ) every pair | C # implementation for above approach ; Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; reverse array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void findNumbers ( int [ ] arr , int n ) { Array . Sort ( arr ) ; reverse ( arr ) ; int [ ] freq = new int [ arr [ 0 ] + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int size = ( int ) Math . Sqrt ( n ) ; int [ ] brr = new int [ size ] ; int x , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] ] > 0 && l < size ) { brr [ l ] = arr [ i ] ; freq [ brr [ l ] ] -- ; l ++ ; for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { x = __gcd ( arr [ i ] , brr [ j ] ) ; freq [ x ] -= 2 ; } } } } printArr ( brr , size ) ; } public static void reverse ( int [ ] input ) { int last = input . Length - 1 ; int middle = input . Length / 2 ; for ( int i = 0 ; i <= middle ; i ++ ) { int temp = input [ i ] ; input [ i ] = input [ last - i ] ; input [ last - i ] = temp ; } } static int __gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 } ; int n = arr . Length ; findNumbers ( arr , n ) ; } }


"Find original sequence from Array containing the sequence merged many times in order | C # program for the above approach ; Function that returns the restored permutation ; List to store the result ; Map to mark the elements which are taken in result ; Check if the element is coming first time ; Push in result vector ; Mark it in the map ; Return the answer ; Function to print the result ; Driver Code ; Given Array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > restore ( int [ ] arr , int N ) { List < int > result = new List < int > ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) && mp [ arr [ i ] ] == 0 ) { result . Add ( arr [ i ] ) ; if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } else mp . Add ( arr [ i ] , 0 ) ; } return result ; } static void print_result ( List < int > result ) { for ( int i = 0 ; i < result . Count ; i ++ ) Console . Write ( result [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 } ; int N = arr . Length ; print_result ( restore ( arr , N ) ) ; } }


"Find original sequence from Array containing the sequence merged many times in order | C # program for the above approach ; Function that returns the restored permutation ; List to store the result ; Set to insert unique elements ; Check if the element is coming first time ; Push in result vector ; Function to print the result ; Driver Code ; Given Array ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > restore ( int [ ] arr , int N ) { List < int > result = new List < int > ( ) ; int count1 = 1 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { s . Add ( arr [ i ] ) ; if ( s . Count == count1 ) { result . Add ( arr [ i ] ) ; count1 ++ ; } } return result ; } static void print_result ( List < int > result ) { for ( int i = 0 ; i < result . Count ; i ++ ) Console . Write ( result [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 13 , 1 , 24 , 13 , 24 , 2 , 2 } ; int N = arr . Length ; print_result ( restore ( arr , N ) ) ; } }


"Find other two sides and angles of a right angle triangle | C # program to print all sides and angles of right angle triangle given one side ; Function to find angle A Angle in front of side a ; applied cosine rule ; convert into degrees ; Function to find angle B Angle in front of side b ; applied cosine rule ; convert into degrees and return ; Function to print all angles of the right angled triangle ; for calculate angle A ; for calculate angle B ; Function to find other two sides of the right angled triangle ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Print angles of the triangle ; Driver Code"
Solution in C#:

using System ; class GFG { static double PI = 3.1415926535 ; static double findAnglesA ( double a , double b , double c ) { double A = Math . Acos ( ( b * b + c * c - a * a ) / ( 2 * b * c ) ) ; return A * 180 / PI ; } static double findAnglesB ( double a , double b , double c ) { double B = Math . Acos ( ( a * a + c * c - b * b ) / ( 2 * a * c ) ) ; return B * 180 / PI ; } static void printAngles ( int a , int b , int c ) { double x = ( double ) a ; double y = ( double ) b ; double z = ( double ) c ; double A = findAnglesA ( x , y , z ) ; double B = findAnglesB ( x , y , z ) ; Console . WriteLine ( " Angles ▁ are ▁ A ▁ = ▁ " + A + " , ▁ B ▁ = ▁ " + B + " , ▁ C ▁ = ▁ " + 90 ) ; } static void printOtherSides ( int n ) { int b = 0 , c = 0 ; if ( ( n & 1 ) > 0 ) { if ( n == 1 ) Console . WriteLine ( - 1 ) ; else { b = ( n * n - 1 ) / 2 ; c = ( n * n + 1 ) / 2 ; Console . WriteLine ( " Side ▁ b ▁ = ▁ " + b + " , ▁ Side ▁ c ▁ = ▁ " + c ) ; } } else { if ( n == 2 ) Console . WriteLine ( - 1 ) ; else { b = n * n / 4 - 1 ; c = n * n / 4 + 1 ; Console . WriteLine ( " Side ▁ b ▁ = ▁ " + b + " , ▁ Side ▁ c ▁ = ▁ " + c ) ; } } printAngles ( n , b , c ) ; } public static void Main ( ) { int a = 12 ; printOtherSides ( a ) ; } }


"Find other two sides of a right angle triangle | C # program to print other two sides of right angle triangle given one side ; Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver code"
Solution in C#:

using System ; class GFG { static void printOtherSides ( int n ) { if ( n % 2 != 0 ) { if ( n == 1 ) Console . WriteLine ( " - 1" ) ; else { int b = ( n * n - 1 ) / 2 ; int c = ( n * n + 1 ) / 2 ; Console . Write ( " b ▁ = ▁ " + b + " , ▁ c ▁ = ▁ " + c ) ; } } else { if ( n == 2 ) Console . Write ( " - 1" ) ; else { int b = n * n / 4 - 1 ; int c = n * n / 4 + 1 ; Console . Write ( " b ▁ = ▁ " + b + " , ▁ c ▁ = ▁ " + c ) ; } } } public static void Main ( ) { int a = 3 ; printOtherSides ( a ) ; } }


"Find out the correct position of the ball after shuffling | C # implementation of the above approach ; Function to generate the index of the glass containing the ball ; Change the index ; Change the index ; Print the index ; Driver Code ; Storing all the shuffle operation"
Solution in C#:

using System ; class GFG { static int M = 3 ; static int N = 2 ; static void getIndex ( int n , int [ , ] shuffle ) { for ( int i = 0 ; i < 3 ; i ++ ) { if ( shuffle [ i , 0 ] == n ) n = shuffle [ i , 1 ] ; else if ( shuffle [ i , 1 ] == n ) n = shuffle [ i , 0 ] ; } Console . WriteLine ( n ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ , ] shuffle = { { 3 , 1 } , { 2 , 1 } , { 1 , 2 } } ; getIndex ( n , shuffle ) ; } }


"Find out the prime numbers in the form of A + nB or B + nA | C # implementation of the approach ; Utility function to check whether two numbers is co - prime or not ; Utility function to check whether a number is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; finding the Prime numbers ; Checking whether given numbers are co - prime or not ; To store the N primes ; If ' possible ' is true ; Printing n numbers of prime ; checking the form of a + nb ; Checking the form of b + na ; If ' possible ' is false return - 1 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static bool coprime ( int a , int b ) { if ( __gcd ( a , b ) == 1 ) return true ; else return false ; } static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n == 2 n == 3 ) return true ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void findNumbers ( int a , int b , int n ) { bool possible = true ; if ( ! coprime ( a , b ) ) possible = false ; int c1 = 1 ; int c2 = 1 ; int num1 , num2 ; HashSet < int > st = new HashSet < int > ( ) ; if ( possible ) { while ( st . Count != n ) { num1 = a + ( c1 * b ) ; if ( isPrime ( num1 ) ) { st . Add ( num1 ) ; } c1 ++ ; num2 = b + ( c2 * a ) ; if ( isPrime ( num2 ) ) { st . Add ( num2 ) ; } c2 ++ ; } foreach ( int i in st ) Console . Write ( i + " ▁ " ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int a = 3 ; int b = 5 ; int n = 4 ; findNumbers ( a , b , n ) ; } }


"Find pair with greatest product in array | C # program to find a pair with product in given array . ; Function to find greatest number that us ; Driver code"
Solution in C#:

using System ; class GFG { static int findGreatest ( int [ ] arr , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . Max ( result , arr [ i ] ) ; return result ; } static public void Main ( ) { int [ ] arr = { 30 , 10 , 9 , 3 , 35 } ; int n = arr . Length ; Console . WriteLine ( findGreatest ( arr , n ) ) ; } }