"Find Sum of all unique sub | C # for finding sum of all unique subarray sum ; function for finding grandSum ; Go through all subarrays , compute sums and count occurrences of sums . ; Print all those sums that appear once . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int res = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( m . ContainsKey ( sum ) ) { m [ sum ] = m [ sum ] + 1 ; } else { m . Add ( sum , 1 ) ; } } } foreach ( KeyValuePair < int , int > x in m ) if ( x . Value == 1 ) res += x . Key ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( findSubarraySum ( arr , n ) ) ; } }


"Find Surpasser Count of each element in array | Naive C # program to find surpasser count of each element in array ; Function to find surpasser count of each element in array ; stores surpasser count for element arr [ i ] ; Function to print an array ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static void findSurpasser ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; Console . Write ( count + " ▁ " ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 2 , 7 , 5 , 3 , 0 , 8 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; Console . WriteLine ( " Surpasser ▁ Count ▁ of " + " ▁ array ▁ is ▁ " ) ; findSurpasser ( arr , n ) ; } }


"Find Tangent at a given point on the curve | C # program for find Tangent on a curve at given point ; function for find Tangent ; differentiate given equation ; check that point on the curve or not ; if differentiate is negative ; differentiate is positive ; differentiate is zero ; Driver code ; declare variable ; call function findTangent"
Solution in C#:

 using System ; class GFG { static void findTangent ( int A , int x , int y ) { int dif = A - x * 2 ; if ( y == ( 2 * x - x * x ) ) { if ( dif < 0 ) Console . Write ( " y ▁ = ▁ " + dif + " x " + ( x * dif + y ) + "
 " ) ; else if ( dif > 0 ) Console . Write ( " y ▁ = ▁ " + dif + " x + " + - x * dif + y + "
 " ) ; else Console . Write ( " Not ▁ possible " + "
 " ) ; } } public static void Main ( ) { int A = 2 , x = 2 , y = 0 ; findTangent ( A , x , y ) ; } }


"Find Two Missing Numbers | Set 2 ( XOR based solution ) | Program to find 2 Missing Numbers ; Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver program ; Range of numbers is 2 plus size of array"
Solution in C#:

using System ; class GFG { static void findTwoMissingNumbers ( int [ ] arr , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n - 2 ; i ++ ) XOR ^= arr [ i ] ; for ( int i = 1 ; i <= n ; i ++ ) XOR ^= i ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i & set_bit_no ) > 0 ) x = x ^ i ; else y = y ^ i ; } Console . WriteLine ( " Two ▁ Missing ▁ Numbers ▁ are ▁ " ) ; Console . WriteLine ( x + " ▁ " + y ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = 2 + arr . Length ; findTwoMissingNumbers ( arr , n ) ; } }


"Find Union and Intersection of two unsorted arrays | A C # program to print union and intersection of two unsorted arrays ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static void printUnion ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { if ( m > n ) { int [ ] tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Array . Sort ( arr1 ) ; for ( int i = 0 ; i < m ; i ++ ) Console . Write ( arr1 [ i ] + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } } static void printIntersection ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { if ( m > n ) { int [ ] tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Array . Sort ( arr1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } } static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static public void Main ( ) { int [ ] arr1 = { 7 , 1 , 5 , 2 , 3 , 6 } ; int [ ] arr2 = { 3 , 8 , 6 , 20 , 7 } ; int m = arr1 . Length ; int n = arr2 . Length ; Console . WriteLine ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; printUnion ( arr1 , arr2 , m , n ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; printIntersection ( arr1 , arr2 , m , n ) ; } }


"Find Union and Intersection of two unsorted arrays | C # code to find intersection when elements may not be distinct ; Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call"
Solution in C#:

using System ; class GFG { static void intersection ( int [ ] a , int [ ] b , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { Console . Write ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void Main ( ) { int [ ] a = { 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 } ; int [ ] b = { 3 , 3 , 5 } ; int n = a . Length ; int m = b . Length ; Array . Sort ( a ) ; Array . Sort ( b ) ; intersection ( a , b , n , m ) ; } }


"Find Union and Intersection of two unsorted arrays | C # program to find union and intersection using Hashing ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Driver Code ; Function call"
Solution in C#:

 using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static void printUnion ( int [ ] arr1 , int [ ] arr2 ) { HashSet < int > hs = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr1 . Length ; i ++ ) hs . Add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . Length ; i ++ ) hs . Add ( arr2 [ i ] ) ; Console . WriteLine ( string . Join ( " , ▁ " , hs ) ) ; } static void printIntersection ( int [ ] arr1 , int [ ] arr2 ) { HashSet < int > hs = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr1 . Length ; i ++ ) hs . Add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . Length ; i ++ ) if ( hs . Contains ( arr2 [ i ] ) ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr1 = { 7 , 1 , 5 , 2 , 3 , 6 } ; int [ ] arr2 = { 3 , 8 , 6 , 20 , 7 } ; Console . WriteLine ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printUnion ( arr1 , arr2 ) ; Console . WriteLine ( "
 Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printIntersection ( arr1 , arr2 ) ; } }


"Find Union and Intersection of two unsorted arrays | C # program to find union and intersection using similar Hashing Technique without using any predefined Java Collections Time Complexity best case & avg case = O ( m + n ) Worst case = O ( nlogn ) package com . arrays . math ; ; Prints intersection of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; Iterate first array ; Iterate second array ; Prints union of arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] ; placeZeroes function ; Function to itreate array ; placeValue function ; Hashing collision happened increment position and do recursive call ; Driver code"
Solution in C#:

using System ; class UnsortedIntersectionUnion { public void findPosition ( int [ ] a , int [ ] b ) { int v = ( a . Length + b . Length ) ; int [ ] ans = new int [ v ] ; int zero1 = 0 ; int zero2 = 0 ; Console . Write ( " Intersection ▁ : ▁ " ) ; for ( int i = 0 ; i < a . Length ; i ++ ) zero1 = iterateArray ( a , v , ans , i ) ; for ( int j = 0 ; j < b . Length ; j ++ ) zero2 = iterateArray ( b , v , ans , j ) ; int zero = zero1 + zero2 ; placeZeros ( v , ans , zero ) ; printUnion ( v , ans , zero ) ; } private void printUnion ( int v , int [ ] ans , int zero ) { int zero1 = 0 ; Console . Write ( " Union : " for ( int i = 0 ; < ; i ++ ) { if ( ( zero == 0 && ans [ i ] == 0 ) || ( ans [ i ] == 0 && zero1 > 0 ) ) continue ; if ( ans [ i ] == 0 ) zero1 ++ ; . Write ( ans [ i ] + " , " ) ; } } private void ( int v , int [ ] ans , int zero ) { if ( zero == 2 ) { Console . WriteLine ( "0" ) ; int [ ] d = { 0 } ; placeValue ( d , ans , 0 , 0 , v ) ; } if ( zero == 1 ) { int [ ] d = { 0 } ; placeValue ( d , ans , 0 , 0 , v ) ; } } private int ( int [ ] a , int v , int [ ] ans , int i ) { if ( a [ i ] != 0 ) { int p = a [ i ] % v ; placeValue ( a , ans , i , p , v ) ; } else return 1 ; return 0 ; } private void ( int [ ] a , int [ ] ans , int i , int p , int v ) { p = p % v ; if ( ans [ p ] == 0 ) ans [ p ] = a [ i ] ; else { if ( ans [ p ] == a [ i ] ) Console . Write ( a [ i ] + " , " ) ; else { p = p + 1 ; ( a , ans , i , p , v ) ; } } } public static void ( ) { int [ ] a = { 7 , 1 , 5 , 2 , 3 , 6 } ; int [ ] b = { 3 , 8 , 6 , 20 , 7 } ; UnsortedIntersectionUnion uiu = new UnsortedIntersectionUnion ( ) ; uiu . findPosition ( a , b ) ; } }


"Find Unique ID and Domain Name of a Website from a string | C # program for the above approach ; Function to check if a character is alphabet or not ; Function to check if a character is a numeric or not ; Function to find ID and Domain name from a given String ; Stores ID and the domain names ; Stores the words of String S ; Stores the temporary word ; Traverse the String S ; If the current character is space ; Push the curr in words ; Update the curr ; Otherwise ; If curr is not empty ; If length of ss is 10 ; Traverse the String ss ; If j is in the range [ 5 , 9 ) ; If current character is not numeric ; Mark flag 1 ; Otherwise ; If current character is not alphabet ; Mark flag 1 ; If flag is false ; Assign ss to ID ; If subString formed by the first 3 character is " www " and last 3 character is " moc " ; Update the domain name ; Print ID and Domain ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static bool ischar ( char x ) { if ( ( x >= ' A ' && x <= ' Z ' ) || ( x >= ' a ' && x <= ' z ' ) ) { return true ; } return false ; } static bool isnum ( char x ) { if ( x >= '0' && x <= '9' ) return true ; return false ; } static void findIdandDoMain ( String S , int N ) { String ID = " " , Domain = " " ; List < String > words = new List < String > ( ) ; String curr = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ▁ ' ) { words . Add ( curr ) ; curr = " " ; } else { if ( S [ i ] == ' . ' ) { if ( i + 1 == N || ( i + 1 < N && S [ i + 1 ] == ' ▁ ' ) ) continue ; } curr += S [ i ] ; } } if ( curr . Length > 0 ) words . Add ( curr ) ; foreach ( String ss in words ) { if ( ss . Length == 10 ) { bool flag = false ; for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j >= 5 && j < 9 ) { if ( isnum ( ss [ j ] ) == false ) flag = true ; } else { if ( ischar ( ss [ j ] ) == false ) flag = true ; } } if ( ! flag ) { ID = ss ; } } if ( ss . Length > 2 && ss . Substring ( 0 , 3 ) . Equals ( " www " ) && ss . Substring ( ss . Length - 3 ) . Equals ( " com " ) ) { Domain = ss . Substring ( 4 , ss . Length - 4 ) ; } } Console . Write ( " ID ▁ = ▁ " + ID + "
 " ) ; Console . Write ( " Domain ▁ = ▁ " + Domain ) ; } public static void Main ( String [ ] args ) { String S = " We ▁ thank ▁ ABCDE1234F ▁ for ▁ visiting ▁ " + " us ▁ and ▁ buying ▁ products ▁ item ▁ AMZrr @ ! k . ▁ " + " For ▁ more ▁ offers , ▁ visit ▁ us ▁ at ▁ www . amazon . com " ; int N = S . Length ; findIdandDoMain ( S , N ) ; } }


"Find Unique pair in an array with pairs of numbers | C # program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code"
Solution in C#:

using System ; class GFG { static void findUniquePair ( int [ ] arr , int n ) { int XOR = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) XOR = XOR ^ arr [ i ] ; int set_bit_no = XOR & ~ ( XOR - 1 ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) > 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } Console . WriteLine ( " The ▁ unique ▁ pair ▁ is ▁ ( " + x + " , ▁ " + y + " ) " ) ; } public static void Main ( ) { int [ ] a = { 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 } ; int n = a . Length ; findUniquePair ( a , n ) ; } }


"Find X and Y intercepts of a line passing through the given points | C # implementation of the approach ; Function to find the X and Y intercepts of the line passing through the given points ; if line is parallel to y axis ; x - intercept will be p [ 0 ] ; y - intercept will be infinity ; if line is parallel to x axis ; x - intercept will be infinity ; y - intercept will be p [ 1 ] ; Slope of the line ; y = mx + c in where c is unknown Use any of the given point to find c ; For finding the x - intercept put y = 0 ; For finding the y - intercept put x = 0 ; Driver code"
Solution in C#:

using System ; class GFG { static void getXandYintercept ( int [ ] P , int [ ] Q ) { int a = P [ 1 ] - Q [ 1 ] ; int b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { Console . WriteLine ( P [ 0 ] ) ; Console . WriteLine ( " infinity " ) ; return ; } if ( a == 0 ) { Console . WriteLine ( " infinity " ) ; Console . WriteLine ( P [ 1 ] ) ; return ; } double m = a / ( b * 1.0 ) ; int x = P [ 0 ] ; int y = P [ 1 ] ; double c = y - m * x ; y = 0 ; double r = ( y - c ) / ( m * 1.0 ) ; Console . WriteLine ( r ) ; x = 0 ; y = ( int ) ( m * x + c ) ; Console . WriteLine ( c ) ; } public static void Main ( ) { int [ ] p1 = { 5 , 2 } ; int [ ] p2 = { 2 , 7 } ; getXandYintercept ( p1 , p2 ) ; } }


"Find XOR of all elements in an Array | C # program to find the XOR of all elements in the array ; Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int xorOfArray ( int [ ] arr , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . Length ; Console . WriteLine ( xorOfArray ( arr , n ) ) ; } }


"Find XOR of numbers from the range [ L , R ] | C # implementation of the approach ; Function to return the XOR of elements from the range [ 1 , n ] ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to return the XOR of elements from the range [ l , r ] ; Driver code"
Solution in C#:

using System ; class GFG { static int findXOR ( int n ) { int mod = n % 4 ; if ( mod == 0 ) return n ; else if ( mod == 1 ) return 1 ; else if ( mod == 2 ) return n + 1 ; else if ( mod == 3 ) return 0 ; return 0 ; } static int findXOR ( int l , int r ) { return ( findXOR ( l - 1 ) ^ findXOR ( r ) ) ; } public static void Main ( ) { int l = 4 , r = 8 ; Console . WriteLine ( findXOR ( l , r ) ) ; } }


"Find XOR of two number without using XOR operator | C # program to find XOR without using ^ ; Returns XOR of x and y ; Driver Code"
Solution in C#:

using System ; class GFG { static int myXOR ( int x , int y ) { return ( x y ) & ( ~ x ~ y ) ; } static public void Main ( ) { int x = 3 , y = 5 ; Console . WriteLine ( " XOR ▁ is ▁ " + ( myXOR ( x , y ) ) ) ; } }


"Find XOR of two number without using XOR operator | C # program to find XOR without using ^ ; Returns XOR of x and y ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int myXOR ( int x , int y ) { return ( x & ( ~ y ) ) | ( ( ~ x ) & y ) ; } public static void Main ( ) { int x = 3 , y = 5 ; Console . WriteLine ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }


"Find XOR of two number without using XOR operator | C # program to find XOR without using ^ ; Returns XOR of x and y ; Initialize result ; Assuming 32 - bit int ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver Code"
Solution in C#:

using System ; class GFG { static int myXOR ( int x , int y ) { int res = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { int b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int xoredBit = ( ( b1 & b2 ) != 0 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } public static void Main ( String [ ] args ) { int x = 3 , y = 5 ; Console . WriteLine ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }


"Find XOR sum of Bitwise AND of all pairs from given two Arrays | C # algorithm for the above approach ; Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; variable to store anshu ; when there has been no AND of pairs before this ; Driver code ; Input ] ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int XorSum ( int [ ] A , int [ ] B , int N , int M ) { int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( ans == - 1 ) ans = ( A [ i ] & B [ j ] ) ; else ans ^= ( A [ i ] & B [ j ] ) ; } } return ans ; } public static void Main ( ) { int [ ] A = { 3 , 5 } ; int [ ] B = { 2 , 3 } ; int N = A . Length ; int M = B . Length ; Console . Write ( XorSum ( A , B , N , M ) ) ; } }


"Find XOR sum of Bitwise AND of all pairs from given two Arrays | C # program for the above approach ; Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; Variable to store xor sums of first array and second array respectively . ; Xor sum of first array ; Xor sum of second array ; Required answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; class GFG { static int XorSum ( int [ ] A , int [ ] B , int N , int M ) { int ans1 = 0 , ans2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) ans1 = ans1 ^ A [ i ] ; for ( int i = 0 ; i < M ; i ++ ) ans2 = ans2 ^ B [ i ] ; return ( ans1 & ans2 ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 } ; int [ ] B = { 2 , 3 } ; int N = A . Length ; int M = B . Length ; Console . Write ( XorSum ( A , B , N , M ) ) ; } }


"Find a K | C # program to implement the above approach ; Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Driver Code ; Given array ; Size of the array ; Given K ; Function Call"
Solution in C#:

 using System ; class GFG { static bool isSubarrayExistUtil ( int [ ] arr , int K , int N ) { int totalXOR = 0 ; int SubarrayXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) totalXOR ^= arr [ i ] ; for ( int i = 0 ; i < K ; i ++ ) SubarrayXOR ^= arr [ i ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; for ( int i = K ; i < N ; i ++ ) { SubarrayXOR ^= arr [ i ] ; SubarrayXOR ^= arr [ i - 1 ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; } return false ; } static void isSubarrayExist ( int [ ] arr , int K , int N ) { if ( isSubarrayExistUtil ( arr , K , N ) ) Console . Write ( " YES
 " ) ; else Console . Write ( " NO
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 } ; int N = arr . Length ; int K = 5 ; isSubarrayExist ( arr , K , N ) ; } }


"Find a N | C # program to find N digit number such that it is not divisible by any of its digits ; Function that print the answer ; if n == 1 then it is not possible ; loop to n - 1 times ; print 4 as last digit of the number ; Driver code ; Function call"
Solution in C#:

 using System ; class GFG { static void findTheNumber ( int n ) { if ( n == 1 ) { Console . Write ( " Impossible " + "
 " ) ; return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { Console . Write ( "5" ) ; } Console . Write ( "4" ) ; } public static void Main ( String [ ] args ) { int n = 12 ; findTheNumber ( n ) ; } }


"Find a Square Matrix such that sum of elements in every row and column is K | C # implementation of the approach ; Function to print the required matrix ; Print k for the left diagonal elements ; Print 0 for the rest ; Driver code"
Solution in C#:

 using System ; class GFG { static void printMatrix ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) Console . Write ( k + " ▁ " ) ; else Console . Write ( "0 ▁ " ) ; } Console . Write ( "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 3 , k = 7 ; printMatrix ( n , k ) ; } }


"Find a Symmetric matrix of order N that contain integers from 0 to N | C # implementation of the approach ; Function to generate the required matrix ; Form cyclic array of elements 1 to n - 1 ; Store initial array into final array ; Fill the last row and column with 0 's ; Swap 0 and the number present at the current indexed row ; Also make changes in the last row with the number we swapped ; Print the final array ; Driver code"
Solution in C#:

using System ; class GFG { static void solve ( long n ) { long [ , ] initial_array = new long [ ( int ) n - 1 , ( int ) n - 1 ] ; long [ , ] final_array = new long [ ( int ) n , ( int ) n ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 , ( int ) i ] = i + 1 ; for ( long i = 1 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) initial_array [ ( int ) i , ( int ) j ] = initial_array [ ( int ) i - 1 , ( int ) ( ( int ) j + 1 ) % ( ( int ) n - 1 ) ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) final_array [ ( int ) i , ( int ) j ] = initial_array [ ( int ) i , ( int ) j ] ; for ( long i = 0 ; i < n ; ++ i ) final_array [ ( int ) i , ( int ) n - 1 ] = final_array [ ( int ) n - 1 , ( int ) i ] = 0 ; for ( long i = 0 ; i < n ; ++ i ) { long t0 = final_array [ ( int ) i , ( int ) i ] ; long t1 = final_array [ ( int ) i , ( int ) n - 1 ] ; long s = final_array [ ( int ) i , ( int ) i ] ; final_array [ ( int ) i , ( int ) i ] = final_array [ ( int ) i , ( int ) n - 1 ] ; final_array [ ( int ) i , ( int ) n - 1 ] = s ; final_array [ ( int ) n - 1 , ( int ) i ] = t0 ; } for ( long i = 0 ; i < n ; ++ i ) { for ( long j = 0 ; j < n ; ++ j ) Console . Write ( final_array [ ( int ) i , ( int ) j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { long n = 5 ; solve ( n ) ; } }


"Find a common element in all rows of a given row | A C # program to find a common element in all rows of a row wise sorted array ; Specify number of rows and columns ; Returns common element in all rows of mat [ M ] [ N ] . If there is no common element , then - 1 is returned ; An array to store indexes of current last column ; To store index of row whose current last element is minimum ; Initialize current last element of all rows ; Initialize min_row as first row ; Keep finding min_row in current last column , till either all elements of last column become same or we hit first column . ; Find minimum in current last column ; eq_count is count of elements equal to minimum in current last column . ; Traverse current last column elements again to update it ; Decrease last column index of a row whose value is more than minimum . ; Reduce last column index by 1 ; If equal count becomes M , return the value ; Driver code"
Solution in C#:

using System ; class GFG { static int M = 4 ; static int N = 5 ; static int findCommon ( int [ , ] mat ) { int [ ] column = new int [ M ] ; int min_row ; int i ; for ( i = 0 ; i < M ; i ++ ) column [ i ] = N - 1 ; min_row = 0 ; while ( column [ min_row ] >= 0 ) { for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i , column [ i ] ] < mat [ min_row , column [ min_row ] ] ) min_row = i ; } int eq_count = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i , column [ i ] ] > mat [ min_row , column [ min_row ] ] ) { if ( column [ i ] == 0 ) return - 1 ; column [ i ] -= 1 ; } else eq_count ++ ; } if ( eq_count == M ) return mat [ min_row , column [ min_row ] ] ; } return - 1 ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) Console . Write ( " No ▁ common ▁ element " ) ; else Console . Write ( " Common ▁ element ▁ is ▁ " + result ) ; } }


"Find a common element in all rows of a given row | C # implementation of the approach ; Specify number of rows and columns ; Returns common element in all rows of mat [ M , N ] . If there is no common element , then - 1 is returned ; A hash map to store count of elements ; Increment the count of first element of the row ; Starting from the second element of the current row ; If current element is different from the previous element i . e . it is appearing for the first time in the current row ; Find element having count equal to number of rows ; No such element found ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int M = 4 ; static int N = 5 ; static int findCommon ( int [ , ] mat ) { Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; int i , j ; for ( i = 0 ; i < M ; i ++ ) { if ( cnt . ContainsKey ( mat [ i , 0 ] ) ) { cnt [ mat [ i , 0 ] ] = cnt [ mat [ i , 0 ] ] + 1 ; } else { cnt . Add ( mat [ i , 0 ] , 1 ) ; } for ( j = 1 ; j < N ; j ++ ) { if ( mat [ i , j ] != mat [ i , j - 1 ] ) if ( cnt . ContainsKey ( mat [ i , j ] ) ) { cnt [ mat [ i , j ] ] = cnt [ mat [ i , j ] ] + 1 ; } else { cnt . Add ( mat [ i , j ] , 1 ) ; } } } foreach ( KeyValuePair < int , int > ele in cnt ) { if ( ele . Value == M ) return ele . Key ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) Console . WriteLine ( " No ▁ common ▁ element " ) ; else Console . WriteLine ( " Common ▁ element ▁ is ▁ " + result ) ; } }


"Find a distinct pair ( x , y ) in given range such that x divides y | C # implementation of the approach ; Driver code"
Solution in C#:

using System ; class GFG { static void findpair ( int l , int r ) { int c = 0 ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = i + 1 ; j <= r ; j ++ ) { if ( j % i == 0 && j != i ) { Console . Write ( i + " , ▁ " + j ) ; c = 1 ; break ; } } if ( c == 1 ) break ; } } static void Main ( ) { int l = 1 , r = 10 ; findpair ( l , r ) ; } }


"Find a distinct pair ( x , y ) in given range such that x divides y | C # implementation of the approach ; Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code"
Solution in C#:

using System ; class GFG { static void findpair ( int l , int r ) { int ans1 = l ; int ans2 = 2 * l ; Console . WriteLine ( ans1 + " , ▁ " + ans2 ) ; } public static void Main ( ) { int l = 1 , r = 10 ; findpair ( l , r ) ; } }


"Find a number M < N such that difference between their XOR and AND is maximum | C # program for the above approach ; Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Driver Code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static int findM ( int N ) { int M = 0 ; int MSB = ( int ) Math . Log ( N ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) M += ( 1 << i ) ; } return M ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( findM ( N ) ) ; } }


"Find a number M < N such that difference between their XOR and AND is maximum | C # program for the above approach ; Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Driver Code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static int getMaxDifference ( int N ) { int M = - 1 ; int maxDiff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( getMaxDifference ( N ) ) ; } }


"Find a number containing N | C # implementation of above approach ; Function to return the String generated by appending "10" n - 1 times ; Initialising String as empty ; Function to return the decimal equivalent of the given binary String ; Initializing base value to 1 i . e 2 ^ 0 ; Function that calls the constructString and binarytodecimal and returns the answer ; Driver code"
Solution in C#:

using System ; class GFG { static String constructString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base_t = 1 ; int len = num . Length ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value = dec_value + base_t ; base_t = base_t * 2 ; } return dec_value ; } static int findNumber ( int n ) { String s = constructString ( n - 1 ) ; int num = binaryToDecimal ( s ) ; return num ; } static public void Main ( ) { int n = 4 ; Console . Write ( findNumber ( n ) ) ; } }


"Find a number containing N | C # implementation of the approach ; Function to compute number using our deduced formula ; Initialize num to n - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int findNumber ( int n ) { int num = n - 1 ; num = 2 * ( int ) Math . Pow ( 4 , num ) ; num = ( int ) Math . Floor ( num / 3.0 ) ; return num ; } static public void Main ( ) { int n = 5 ; Console . Write ( findNumber ( n ) ) ; } }


"Find a number in minimum steps | C # program to Find a number in minimum steps ; Steps sequence ; Current sum ; Sign of the number ; Basic steps required to get sum >= required value . ; If we have reached ahead to destination . ; If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum . ; If the current time instance is even and sum is odd than it takes 2 more steps and few negations in previous elements to reach there . ; Driver Program"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static List < int > find ( int n ) { List < int > ans = new List < int > ( ) ; int sum = 0 ; int i = 1 ; int sign = ( n >= 0 ? 1 : - 1 ) ; n = Math . Abs ( n ) ; for ( ; sum < n ; ) { ans . Add ( sign * i ) ; sum += i ; i ++ ; } if ( sum > sign * n ) { if ( i % 2 != 0 ) { sum -= n ; if ( sum % 2 != 0 ) { ans . Add ( sign * i ) ; sum += i ; i ++ ; } int a = ans [ ( ( sum / 2 ) - 1 ) ] ; ans . RemoveAt ( ( sum / 2 ) - 1 ) ; ans . Insert ( ( ( sum / 2 ) - 1 ) , a * ( - 1 ) ) ; } else { sum -= n ; if ( sum % 2 != 0 ) { sum -- ; ans . Add ( sign * i ) ; ans . Add ( sign * - 1 * ( i + 1 ) ) ; } ans . Insert ( ( sum / 2 ) - 1 , ans [ ( sum / 2 ) - 1 ] * - 1 ) ; } } return ans ; } public static void Main ( String [ ] args ) { int n = 20 ; if ( n == 0 ) Console . Write ( " Minimum ▁ number ▁ of ▁ Steps : ▁ 0
 Step ▁ sequence :
 0" ) ; else { List < int > a = find ( n ) ; Console . Write ( " Minimum ▁ number ▁ of ▁ Steps : ▁ " + a . Count + "
 Step ▁ sequence :
 " ) ; foreach ( int i in a ) Console . Write ( i + " ▁ " ) ; } } }


"Find a number in minimum steps | C # program to find a number in minimum steps ; To represent data of a node in tree ; Prints level of node n ; Create a queue and insert root ; Do level order traversal ; Remove a node from queue ; To astatic void infinite loop ; Check if dequeued number is same as n ; Insert children of dequeued node to queue ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static readonly int InF = 99999 ; public class number { public int no ; public int level ; public number ( ) { } public number ( int n , int l ) { this . no = n ; this . level = l ; } } ; static void findnthnumber ( int n ) { Queue < number > q = new Queue < number > ( ) ; number r = new number ( 0 , 1 ) ; q . Enqueue ( r ) ; while ( q . Count != 0 ) { number temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . no >= InF temp . no <= - InF ) break ; if ( temp . no == n ) { Console . Write ( " Found ▁ number ▁ n ▁ at ▁ level ▁ " + ( temp . level - 1 ) ) ; break ; } q . Enqueue ( new number ( temp . no + temp . level , temp . level + 1 ) ) ; q . Enqueue ( new number ( temp . no - temp . level , temp . level + 1 ) ) ; } } public static void Main ( String [ ] args ) { findnthnumber ( 13 ) ; } }


"Find a number that divides maximum array elements | C # program to find a number that divides maximum array elements ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; Function to find a number that divides maximum array elements ; precalculating Smallest Prime Factor ; Hash to store frequency of each divisors ; Traverse the array and get spf of each element ; calling getFactorization function ; Returns Set view ; Driver program"
Solution in C#:

using System ; using System . Collections . Generic ; class Solution { static readonly int MAXN = 100001 ; static int [ ] spf = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static List < int > getFactorization ( int x ) { List < int > ret = new List < int > ( ) ; while ( x != 1 ) { int temp = spf [ x ] ; ret . Add ( temp ) ; while ( x % temp == 0 ) x = x / temp ; } return ret ; } static int maxElement ( int [ ] A , int n ) { sieve ( ) ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int j = 0 ; j < n ; ++ j ) { List < int > p = getFactorization ( A [ j ] ) ; for ( int i = 0 ; i < p . Count ; i ++ ) if ( m . ContainsKey ( p [ i ] ) ) m [ p [ i ] ] = m [ p [ i ] ] + 1 ; else m . Add ( p [ i ] , 1 ) ; } int cnt = 0 , ans = 10000000 ; foreach ( KeyValuePair < int , int > me in m ) { if ( me . Value >= cnt ) { cnt = me . Value ; if ( ans > me . Key ) ans = me . Key ; else ans = ans ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 5 , 10 } ; int n = A . Length ; Console . Write ( maxElement ( A , n ) ) ; } }


"Find a number which give minimum sum when XOR with every number of array of integers | C # implementation of above approach ; Function to find an integer X such that the sum of all the array elements after getting XORed with X is minimum ; Finding Maximum element of array ; Find Maximum number of bits required in the binary representation of maximum number so log2 is calculated ; Running loop from p times which is the number of bits required to represent all the elements of the array ; If the bits in same position are set then count ; If count becomes greater than half of size of array then we need to make that bit '0' by setting X bit to '1' ; Again using shift operation to calculate the required number ; Calculate minimized sum ; Print solution ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { public static void findX ( int [ ] a , int n ) { int itr = a . Max ( ) ; int p = ( int ) Math . Log ( itr , 2 ) + 1 ; int x = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) { x += 1 << i ; } } long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( x ^ a [ i ] ) ; Console . Write ( " X ▁ = ▁ " + x + " , ▁ Sum ▁ = ▁ " + sum ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; findX ( a , n ) ; } }


"Find a number x such that sum of x and its digits is equal to given n . | C # program to find x such that x + digSum ( x ) is equal to n . ; utility function for digit sum ; function for finding x ; iterate from 1 to n . For every no . check if its digit sum with it is equal to n . ; if no such i found return - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int digSum ( int n ) { int sum = 0 , rem = 0 ; while ( n > 0 ) { rem = n % 10 ; sum += rem ; n /= 10 ; } return sum ; } static int findX ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return - 1 ; } public static void Main ( ) { int n = 43 ; Console . Write ( " x ▁ = ▁ " + findX ( n ) ) ; } }


"Find a pair ( n , r ) in an integer array such that value of nPr is maximum | C # implementation of the approach ; Function to print the pair ( n , r ) such that nPr is maximum possible ; There should be atleast 2 elements ; Findex the largest 2 elements ; Driver code"
Solution in C#:

using System ; class GFG { static void findPair ( int [ ] arr , int n ) { if ( n < 2 ) { Console . Write ( " - 1" ) ; return ; } int i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } Console . WriteLine ( " n ▁ = ▁ " + first + " ▁ and ▁ r ▁ = ▁ " + second ) ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . Length ; findPair ( arr , n ) ; } }


"Find a pair from the given array with maximum nCr value | C # implementation of the approach ; Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printMaxValPair ( List < long > v , int n ) { v . Sort ( ) ; long N = v [ ( int ) n - 1 ] ; if ( N % 2 == 1 ) { long first_maxima = N / 2 ; long second_maxima = first_maxima + 1 ; long ans1 = ( long ) 3e18 , ans2 = ( long ) 3e18 ; long from_left = - 1 , from_right = - 1 ; long from = - 1 ; for ( long i = 0 ; i < n ; ++ i ) { if ( v [ ( int ) i ] > first_maxima ) { from = i ; break ; } else { long diff = first_maxima - v [ ( int ) i ] ; if ( diff < ans1 ) { ans1 = diff ; from_left = v [ ( int ) i ] ; } } } from_right = v [ ( int ) from ] ; long diff1 = first_maxima - from_left ; long diff2 = from_right - second_maxima ; if ( diff1 < diff2 ) Console . WriteLine ( N + " ▁ " + from_left ) ; else Console . WriteLine ( N + " ▁ " + from_right ) ; } else { long maxima = N / 2 ; long ans1 = ( long ) 3e18 ; long R = - 1 ; for ( long i = 0 ; i < n - 1 ; ++ i ) { long diff = Math . Abs ( v [ ( int ) i ] - maxima ) ; if ( diff < ans1 ) { ans1 = diff ; R = v [ ( int ) i ] ; } } Console . WriteLine ( N + " ▁ " + R ) ; } } public static void Main ( String [ ] args ) { long [ ] arr = { 1 , 1 , 2 , 3 , 6 , 1 } ; List < long > v = new List < long > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) v . Add ( arr [ i ] ) ; int n = v . Count ; printMaxValPair ( v , n ) ; } }


"Find a pair in Array with second largest product | C # program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Driver Code ; Given array ; Function call"
Solution in C#:

using System ; class GFG { static void maxProduct ( int [ ] arr , int N ) { if ( N < 3 ) { return ; } int a = arr [ 0 ] , b = arr [ 1 ] ; int c = 0 , d = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a ; d = b ; a = arr [ i ] ; b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] ; d = arr [ j ] ; } Console . WriteLine ( c + " ▁ " + d ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . Length ; maxProduct ( arr , N ) ; } }


"Find a pair in Array with second largest product | C # program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Sort the array ; Initialize smallest element of the array ; Initialize largest element of the array ; Print second largest product pair ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; class GFG { static void maxProduct ( int [ ] arr , int N ) { if ( N < 3 ) { return ; } Array . Sort ( arr ) ; int smallest1 = arr [ 0 ] ; int smallest3 = arr [ 2 ] ; int largest1 = arr [ N - 1 ] ; int largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { Console . Write ( smallest1 + " ▁ " + smallest3 ) ; } else { Console . Write ( largest1 + " ▁ " + largest3 ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . Length ; maxProduct ( arr , N ) ; } }


"Find a pair of overlapping intervals from a given Set | C # program to implement the above approach ; Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R are equal ; Print the index of interval ; Stores r - value of current interval ; If T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Driver code ; Given l - value of segments ; Given r - value of segments ; Given size ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findOverlapSegement ( int N , int [ ] a , int [ ] b ) { List < Tuple < Tuple < int , int > , int > > tup = new List < Tuple < Tuple < int , int > , int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x , y ; x = a [ i ] ; y = b [ i ] ; tup . Add ( new Tuple < Tuple < int , int > , int > ( new Tuple < int , int > ( x , y ) , i ) ) ; } tup . Sort ( ) ; int curr = tup [ 0 ] . Item1 . Item2 ; int currPos = tup [ 0 ] . Item2 ; for ( int i = 1 ; i < N ; i ++ ) { int Q = tup [ i - 1 ] . Item1 . Item1 ; int R = tup [ i ] . Item1 . Item1 ; if ( Q == R ) { if ( tup [ i - 1 ] . Item1 . Item2 < tup [ i ] . Item1 . Item2 ) Console . Write ( tup [ i - 1 ] . Item2 + " ▁ " + tup [ i ] . Item2 ) ; else Console . Write ( tup [ i ] . Item2 + " ▁ " + tup [ i - 1 ] . Item2 ) ; return ; } int T = tup [ i ] . Item1 . Item2 ; if ( T <= curr ) { Console . Write ( tup [ i ] . Item2 + " ▁ " + currPos ) ; return ; } else { curr = T ; currPos = tup [ i ] . Item2 ; } } Console . Write ( " - 1 ▁ - 1" ) ; } static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 2 , 2 } ; int [ ] b = { 5 , 10 , 10 , 2 , 15 } ; int N = a . Length ; findOverlapSegement ( N , a , b ) ; } }


"Find a pair with given sum in BST | C # program to find a pair with given sum using hashing ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static Node NewNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return NewNode ( key ) ; if ( key < root . data ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static bool findpairUtil ( Node root , int sum , HashSet < int > set ) { if ( root == null ) return false ; if ( findpairUtil ( root . left , sum , set ) ) return true ; if ( set . Contains ( sum - root . data ) ) { Console . WriteLine ( " Pair ▁ is ▁ found ▁ ( " + ( sum - root . data ) + " , ▁ " + root . data + " ) " ) ; return true ; } else set . Add ( root . data ) ; return findpairUtil ( root . right , sum , set ) ; } static void findPair ( Node root , int sum ) { HashSet < int > set = new HashSet < int > ( ) ; if ( ! findpairUtil ( root , sum , set ) ) Console . Write ( " Pairs ▁ do ▁ not ▁ exit " + "
 " ) ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 15 ) ; root = insert ( root , 10 ) ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 12 ) ; root = insert ( root , 16 ) ; root = insert ( root , 25 ) ; root = insert ( root , 10 ) ; int sum = 33 ; findPair ( root , sum ) ; } }


"Find a pair with given sum in a Balanced BST | C # code to find a pair with given sum in a Balanced BST ; A binary tree node ; Root of BST ; Constructor ; Inorder traversal of the tree ; Utility function for inorder traversal of the tree ; This method mainly calls insertRec ( ) ; A recursive function to insert a new key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Method that adds values of given BST into ArrayList and hence returns the ArrayList ; Base Case ; method that checks if there is a pair present ; This list a1 is passed as an argument in treeToList method which is later on filled by the values of BST ; a2 list contains all the values of BST returned by treeToList method ; Starting index of a2 ; Ending index of a2 ; Target Found ! ; decrements end ; increments start ; Driver code ; 15 / \ 10 20 / \ / \ 8 12 16 25"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinarySearchTree { Node root ; BinarySearchTree ( ) { root = null ; } void inorder ( ) { inorderUtil ( this . root ) ; } void inorderUtil ( Node node ) { if ( node == null ) return ; inorderUtil ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inorderUtil ( node . right ) ; } void insert ( int key ) { root = insertRec ( root , key ) ; } Node insertRec ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( data < root . data ) root . left = insertRec ( root . left , data ) ; else if ( data > root . data ) root . right = insertRec ( root . right , data ) ; return root ; } List < int > treeToList ( Node node , List < int > list ) { if ( node == null ) return list ; treeToList ( node . left , list ) ; list . Add ( node . data ) ; treeToList ( node . right , list ) ; return list ; } bool isPairPresent ( Node node , int target ) { List < int > a1 = new List < int > ( ) ; List < int > a2 = treeToList ( node , a1 ) ; int start = 0 ; int end = a2 . Count - 1 ; while ( start < end ) { if ( a2 [ start ] + a2 [ end ] == target ) { Console . WriteLine ( " Pair ▁ Found : ▁ " + a2 [ start ] + " ▁ + ▁ " + a2 [ end ] + " ▁ " + " = ▁ " + target ) ; return true ; } if ( a2 [ start ] + a2 [ end ] > target ) { end -- ; } if ( a2 [ start ] + a2 [ end ] < target ) { start ++ ; } } Console . WriteLine ( " No ▁ such ▁ values ▁ are ▁ found ! " ) ; return false ; } public static void Main ( String [ ] args ) { BinarySearchTree tree = new BinarySearchTree ( ) ; tree . insert ( 15 ) ; tree . insert ( 10 ) ; tree . insert ( 20 ) ; tree . insert ( 8 ) ; tree . insert ( 12 ) ; tree . insert ( 16 ) ; tree . insert ( 25 ) ; tree . isPairPresent ( tree . root , 33 ) ; } }


"Find a pair with given sum in a Balanced BST | In a balanced binary search tree isPairPresent two element which sums to a given value time O ( n ) space O ( logn ) ; A BST node ; Stack type ; A utility function to create a stack of given size ; BASIC OPERATIONS OF STACK ; Returns true if a pair with target sum exists in BST , otherwise false ; Create two stacks . s1 is used for normal inorder traversal and s2 is used for reverse inorder traversal ; Note the sizes of stacks is MAX_SIZE , we can find the tree size and fix stack size as O ( Logn ) for balanced trees like AVL and Red Black tree . We have used MAX_SIZE to keep the code simple done1 , val1 and curr1 are used for normal inorder traversal using s1 done2 , val2 and curr2 are used for reverse inorder traversal using s2 ; The loop will break when we either find a pair or one of the two traversals is complete ; Find next node in normal Inorder traversal . See following post www . geeksforgeeks . org / inorder - tree - traversal - without - recursion / https : ; Find next node in REVERSE Inorder traversal . The only difference between above and below loop is , in below loop right subtree is traversed before left subtree ; If we find a pair , then print the pair and return . The first condition makes sure that two same values are not added ; If sum of current values is smaller , then move to next node in normal inorder traversal ; If sum of current values is greater , then move to next node in reverse inorder traversal ; If any of the inorder traversals is over , then there is no pair so return false ; A utility function to create BST node ; Driver program to test above functions ; 15 / \ 10 20 / \ / \ 8 12 16 25"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static readonly int MAX_SIZE = 100 ; public class node { public int val ; public node left , right ; } ; public class Stack { public int size ; public int top ; public node [ ] array ; } ; static Stack createStack ( int size ) { Stack stack = new Stack ( ) ; stack . size = size ; stack . top = - 1 ; stack . array = new node [ stack . size ] ; return stack ; } static int isFull ( Stack stack ) { return ( stack . top - 1 == stack . size ) ? 1 : 0 ; } static int isEmpty ( Stack stack ) { return stack . top == - 1 ? 1 : 0 ; } static void push ( Stack stack , node node ) { if ( isFull ( stack ) == 1 ) return ; stack . array [ ++ stack . top ] = node ; } static node pop ( Stack stack ) { if ( isEmpty ( stack ) == 1 ) return null ; return stack . array [ stack . top -- ] ; } static bool isPairPresent ( node root , int target ) { Stack s1 = createStack ( MAX_SIZE ) ; Stack s2 = createStack ( MAX_SIZE ) ; bool done1 = false , done2 = false ; int val1 = 0 , val2 = 0 ; node curr1 = root , curr2 = root ; while ( true ) { while ( done1 == false ) { if ( curr1 != null ) { push ( s1 , curr1 ) ; curr1 = curr1 . left ; } else { if ( isEmpty ( s1 ) == 1 ) done1 = true ; else { curr1 = pop ( s1 ) ; val1 = curr1 . val ; curr1 = curr1 . right ; done1 = true ; } } } while ( done2 == false ) { if ( curr2 != null ) { push ( s2 , curr2 ) ; curr2 = curr2 . right ; } else { if ( isEmpty ( s2 ) == 1 ) done2 = true ; else { curr2 = pop ( s2 ) ; val2 = curr2 . val ; curr2 = curr2 . left ; done2 = true ; } } } if ( ( val1 != val2 ) && ( val1 + val2 ) == target ) { Console . Write ( " Pair ▁ Found : ▁ " + val1 + " + ▁ " + val2 + " ▁ = ▁ " + target + "
 " ) ; return true ; } else if ( ( val1 + val2 ) < target ) done1 = false ; else if ( ( val1 + val2 ) > target ) done2 = false ; if ( val1 >= val2 ) return false ; } } static node NewNode ( int val ) { node tmp = new node ( ) ; tmp . val = val ; tmp . right = tmp . left = null ; return tmp ; } public static void Main (...


"Find a pair with sum N having minimum absolute difference | C # program to implement the above approach ; Function to find the value of X and Y having minimum value of Math . abs ( X - Y ) ; If N is an odd number ; If N is an even number ; Driver Code"
Solution in C#:

using System ; class GFG { static void findXandYwithminABSX_Y ( int N ) { if ( N % 2 == 1 ) { Console . Write ( ( N / 2 ) + " ▁ " + ( N / 2 + 1 ) ) ; } else { Console . Write ( ( N / 2 - 1 ) + " ▁ " + ( N / 2 + 1 ) ) ; } } public static void Main ( ) { int N = 12 ; findXandYwithminABSX_Y ( N ) ; } }


"Find a palindromic string B such that given String A is a subsequense of B | C # program to find a palindromic string B such that given String A is a subsequense of B ; Function to check if a string is palindrome ; Reversing a string ; check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; else return B ; Swap values of left and right ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkPalindrome ( String s ) { String x = reverse ( s ) ; return x . Equals ( s ) ; } static String findStringB ( String A ) { String B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String A = " ab " ; Console . WriteLine ( findStringB ( A ) ) ; } }


"Find a partition point in array to maximize its xor sum | C # program to find partition point in array to maximize xor sum ; Function to calculate Prefix Xor array ; Calculating prefix xor ; Function to find partition point in array to maximize xor sum ; To store prefix xor ; Compute the prefix xor ; To store sum and index ; Calculate the maximum sum that can be obtained splitting the array at some index i ; PrefixXor [ i ] = Xor of all arr elements till i ' th ▁ index ▁ PrefixXor [ n - 1 ] ▁ ▁ ^ ▁ PrefixXor [ i ] ▁ = ▁ Xor ▁ of ▁ all ▁ elements ▁ ▁ from ▁ i + 1' th index to n - 1 'th index ; Return the index ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static void ComputePrefixXor ( int [ ] arr , int [ ] PrefixXor , int n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } static int Xor_Sum ( int [ ] arr , int n ) { int [ ] PrefixXor = new int [ n ] ; ComputePrefixXor ( arr , PrefixXor , n ) ; int sum = 0 , index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . Length ; Console . WriteLine ( Xor_Sum ( arr , n ) ) ; } }


"Find a partition point in array to maximize its xor sum | C # program to find partition point in array to maximize xor sum ; Function to find partition point in array to maximize xor sum ; Traverse through the array ; Calculate xor of elements left of index i including ith element ; Calculate xor of the elements right of index i ; Keep the maximum possible xor sum ; Return the 1 based index of the array ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static int Xor_Sum ( int [ ] arr , int n ) { int sum = 0 , index = - 1 ; int left_xor = 0 , right_xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . Length ; Console . WriteLine ( Xor_Sum ( arr , n ) ) ; } }


"Find a peak element | A C # program to find a peak element ; Find the peak element in the array ; First or last element is peak element ; Check for every other element ; Check if the neighbors are smaller ; Driver Code"
Solution in C#:

using System ; public class GFG { static int findPeak ( int [ ] arr , int n ) { if ( n == 1 ) return 0 ; if ( arr [ 0 ] >= arr [ 1 ] ) return 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) return n - 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i - 1 ] && arr [ i ] >= arr [ i + 1 ] ) return i ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 20 , 4 , 1 , 0 } ; int n = arr . Length ; Console . Write ( " Index ▁ of ▁ a ▁ peak ▁ point ▁ is ▁ " + findPeak ( arr , n ) ) ; } }


"Find a peak element | A C # program to find a peak element element using divide and conquer ; A binary search based function that returns index of a peak element ; Find index of middle element ; Compare middle element with its neighbours ( if neighbours exist ) ; If middle element is not peak and its left neighbor is greater than it , then left half must have a peak element ; If middle element is not peak and its right neighbor is greater than it , then right half must have a peak element ; A wrapper over recursive function findPeakUtil ( ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int findPeakUtil ( int [ ] arr , int low , int high , int n ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == 0 arr [ mid - 1 ] <= arr [ mid ] ) && ( mid == n - 1 arr [ mid + 1 ] <= arr [ mid ] ) ) return mid ; else if ( mid > 0 && arr [ mid - 1 ] > arr [ mid ] ) return findPeakUtil ( arr , low , ( mid - 1 ) , n ) ; else return findPeakUtil ( arr , ( mid + 1 ) , high , n ) ; } static int findPeak ( int [ ] arr , int n ) { return findPeakUtil ( arr , 0 , n - 1 , n ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 20 , 4 , 1 , 0 } ; int n = arr . Length ; Console . WriteLine ( " Index ▁ of ▁ a ▁ peak ▁ " + " point ▁ is ▁ " + findPeak ( arr , n ) ) ; } }


"Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | C # program to find the required permutation of first 2 * N natural numbers ; Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver code"
Solution in C#:

using System ; class GFG { static void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) Console . Write ( y + " ▁ " + x + " ▁ " ) ; else Console . Write ( x + " ▁ " + y + " ▁ " ) ; } } public static void Main ( ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; } }


"Find a point such that sum of the Manhattan distances is minimized | C # implementation of above approach ; Function to print the required points which minimizes the sum of Manhattan distances ; Sorting points in all k dimension ; Output the required k points ; Driver code ; function call to print required points"
Solution in C#:

using System ; class GFG { static void minDistance ( int n , int k , int [ ] [ ] point ) { for ( int i = 0 ; i < k ; i ++ ) Array . Sort ( point [ i ] ) ; for ( int i = 0 ; i < k ; i ++ ) System . Console . Write ( point [ i ] [ ( int ) Math . Ceiling ( ( double ) ( n / 2 ) - 1 ) ] + " ▁ " ) ; } public static void Main ( ) { int n = 4 ; int k = 4 ; int [ ] [ ] point = new int [ ] [ ] { new int [ ] { 1 , 5 , 2 , 4 } , new int [ ] { 6 , 2 , 0 , 6 } , new int [ ] { 9 , 5 , 1 , 3 } , new int [ ] { 6 , 7 , 5 , 9 } } ; minDistance ( n , k , point ) ; } }


"Find a point whose sum of distances from all given points on a line is K | C # program for the above approach ; Function to find the sum of distances of all points from a given point ; Stores sum of distances ; Traverse the array ; Return the sum ; Function to find such a point having sum of distances of all other points from this point equal to K ; If N is odd keep left as arr [ n / 2 ] else keep left as arr [ n / 2 - 1 ] + 1 ; ; Keep right as arr [ N - 1 ] ; Perform binary search in the right half ; Calculate the mid index of the range ; If temp is equal to K ; Print the value of mid ; If the value of K < temp ; Update right to mid - 1 ; If the value of K > temp ; Update left to mid + 1 ; Update the value of left ; Update the value of right ; Perform binary search on the left half ; Calculate the mid index of the range ; If temp is equal to K ; Print mid ; if K > temp ; Update right to mid - 1 ; If K < temp ; Update left to mid + 1 ; If no such point found ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findSum ( int [ ] arr , int N , int pt ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += Math . Abs ( arr [ i ] - pt ) ; } return sum ; } public static void findPoint ( int [ ] arr , int N , int K ) { int left ; if ( N % 2 != 0 ) { left = arr [ N / 2 ] ; } else { left = arr [ N / 2 - 1 ] + 1 ; } int right = arr [ N - 1 ] ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { Console . WriteLine ( mid ) ; return ; } else if ( K < temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } left = arr [ 0 ] ; right = arr [ N / 2 ] - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { Console . WriteLine ( mid ) ; return ; } else if ( K > temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } Console . WriteLine ( " - 1" ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 7 , 11 } ; int K = 18 ; int N = arr . Length ; findPoint ( arr , N , K ) ; } }


"Find a range of composite numbers of given length | C # program to find a range of composite numbers of given length ; method to find factorial of given number ; to print range of length n having all composite integers ; Driver method"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static void printRange ( int n ) { int a = factorial ( n + 2 ) + 2 ; int b = a + n - 1 ; Console . WriteLine ( " [ " + a + " , ▁ " + b + " ] " ) ; } public static void Main ( ) { int n = 3 ; printRange ( n ) ; } }


"Find a rotation with maximum hamming distance | C # program to Find another array such that the hamming distance from the original array is maximum ; Return the maximum hamming distance of a rotation ; arr [ ] to brr [ ] two times so that we can traverse through all rotations . ; We know hamming distance with 0 rotation would be 0. ; We try other rotations one by one and compute Hamming distance of every rotation ; We can never get more than n . ; driver code"
Solution in C#:

using System ; class GFG { static int maxHamming ( int [ ] arr , int n ) { int [ ] brr = new int [ 2 * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; int maxHam = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int currHam = 0 ; for ( int j = i , k = 0 ; j < ( i + n ) ; j ++ , k ++ ) if ( brr [ j ] != arr [ k ] ) currHam ++ ; if ( currHam == n ) return n ; maxHam = Math . Max ( maxHam , currHam ) ; } return maxHam ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 8 } ; int n = arr . Length ; Console . Write ( maxHamming ( arr , n ) ) ; } }


"Find a sorted subsequence of size 3 in linear time | C # Program for above approach ; Function to find the triplet ; If number of elements < 3 then no triplets are possible ; track best sequence length ( not current sequence length ) ; min number in array ; least max number in best sequence i . e . track arr [ j ] ( e . g . in array { 1 , 5 , 3 } our best sequence would be { 1 , 3 } with arr [ j ] = 3 ) ; save arr [ i ] ; Iterate from 1 to nums . size ( ) ; this condition is only hit when current sequence size is 2 ; update best sequence max number to a smaller value ( i . e . we 've found a smaller value for arr[j]) ; store best sequence start value i . e . arr [ i ] ; Increase best sequence length & save next number in our triplet ; We 've found our arr[k]!  Print the output ; No triplet found ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static void find3Numbers ( int [ ] nums ) { if ( nums . Length < 3 ) { Console . Write ( " No ▁ such ▁ triplet ▁ found " ) ; return ; } int seq = 1 ; int min_num = nums [ 0 ] ; int max_seq = Int32 . MinValue ; int store_min = min_num ; for ( int i = 1 ; i < nums . Length ; i ++ ) { if ( nums [ i ] == min_num ) continue ; else if ( nums [ i ] < min_num ) { min_num = nums [ i ] ; continue ; } else if ( nums [ i ] < max_seq ) { max_seq = nums [ i ] ; store_min = min_num ; } else if ( nums [ i ] > max_seq ) { seq ++ ; if ( seq == 3 ) { Console . WriteLine ( " Triplet : ▁ " + store_min + " , ▁ " + max_seq + " , ▁ " + nums [ i ] ) ; return ; } max_seq = nums [ i ] ; } } Console . Write ( " No ▁ such ▁ triplet ▁ found " ) ; } static void Main ( ) { int [ ] nums = { 1 , 2 , - 1 , 7 , 5 } ; find3Numbers ( nums ) ; } }


"Find a string in lexicographic order which is in between given two strings | C # program to find the string in lexicographic order which is in between given two strings ; Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to ' a ' ; Driver Code ; If not equal , print the resultant string"
Solution in C#:

using System ; public class GFG { static String lexNext ( String str , int n ) { char [ ] s = str . ToCharArray ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' z ' ) { s [ i ] ++ ; return new String ( s ) ; } s [ i ] = ' a ' ; } return null ; } static public void Main ( ) { String S = " abcdeg " , T = " abcfgh " ; int n = S . Length ; String res = lexNext ( S , n ) ; if ( res != T ) { Console . Write ( res ) ; } else { Console . Write ( " - 1" ) ; } } }


"Find a string such that every character is lexicographically greater than its immediate next character | C # program to print a String in reverse alphabetical order upto given number ; Function that prints the required String ; Find modulus with 26 ; Print extra characters required ; Print the given reverse String countOfStr times ; Driver Code ; Initialize a String in reverse order"
Solution in C#:

using System ; public class GFG { static String printString ( int n , String str ) { String str2 = " " ; int extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( int i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) { str2 += str [ i ] ; } } int countOfStr = n / 26 ; for ( int i = 1 ; i <= countOfStr ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { str2 += str [ j ] ; } } return str2 ; } public static void Main ( ) { int n = 30 ; String str = " zyxwvutsrqponmlkjihgfedcba " ; Console . Write ( printString ( n , str ) ) ; } }


"Find a subarray of size K whose sum is a perfect square | C # program for the above approach ; Function to check if a given number is a perfect square or not ; Find square root of n ; Check if the square root is an integer or not ; Function to print the subarray whose sum is a perfect square ; Sum of first K elements ; If the first k elements have a sum as perfect square ; Iterate through the array ; If sum is perfect square ; If subarray not found ; Driver Code ; Given array ; Given subarray size K ; Function Call"
Solution in C#:

using System ; class GFG { class pair { public int first , second ; } static bool isPerfectSquare ( int n ) { double sr = Math . Sqrt ( n ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static void SubarrayHavingPerfectSquare ( int [ ] arr , int k ) { pair ans = new pair ( ) ; int sum = 0 , i ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } bool found = false ; if ( isPerfectSquare ( sum ) ) { ans . first = 0 ; ans . second = i - 1 ; } else { for ( int j = i ; j < arr . Length ; j ++ ) { sum = sum + arr [ j ] - arr [ j - k ] ; if ( isPerfectSquare ( sum ) ) { found = true ; ans . first = j - k + 1 ; ans . second = j ; } } for ( int k1 = ans . first ; k1 <= ans . second ; k1 ++ ) { Console . Write ( arr [ k1 ] + " ▁ " ) ; } } if ( found == false ) { Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 20 , 34 , 51 , 10 , 99 , 87 , 23 , 45 } ; int K = 3 ; SubarrayHavingPerfectSquare ( arr , K ) ; } }


"Find a subarray whose sum is divisible by size of the array | C # implementation of above approach ; Function to find a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Generating all sub - arrays ; If the sum of the sub - array [ i : j ] is a multiple of N ; If the function reaches here it means there are no subarrays with sum as a multiple of N ; Driver code"
Solution in C#:

using System ; class GFG { static void CheckSubarray ( int [ ] arr , int N ) { int [ ] presum = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 1 ; i <= N ; i += 1 ) { for ( int j = i ; j <= N ; j += 1 ) { if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) { Console . Write ( ( i - 1 ) + " ▁ " + ( j - 1 ) ) ; return ; } } } Console . Write ( - 1 ) ; } public static void Main ( ) { int [ ] arr = { 7 , 5 , 3 , 7 } ; int N = arr . Length ; CheckSubarray ( arr , N ) ; } }


"Find a subarray whose sum is divisible by size of the array | C # implementation of the approach ; Function to check is there exists a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Modulo class vector ; Storing the index value in the modulo class vector ; If there exists a sub - array with starting index equal to zero ; In this class , there are more than two presums % N . Hence difference of any two subarrays would be a multiple of N ; 0 based indexing ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void CheckSubarray ( int [ ] arr , int N ) { int [ ] presum = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } List < int > [ ] moduloclass = new List < int > [ N ] ; for ( int i = 0 ; i < N ; i += 1 ) { moduloclass [ i ] = new List < int > ( ) ; } for ( int i = 1 ; i <= N ; i += 1 ) { moduloclass [ presum [ i ] % N ] . Add ( i - 1 ) ; } if ( moduloclass [ 0 ] . Count > 0 ) { Console . Write ( 0 + " ▁ " + moduloclass [ 0 ] [ 0 ] ) ; return ; } for ( int i = 1 ; i < N ; i += 1 ) { if ( moduloclass [ i ] . Count >= 2 ) { Console . Write ( moduloclass [ i ] [ 0 ] + 1 + " ▁ " + moduloclass [ i ] [ 1 ] ) ; return ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 3 , 5 , 2 } ; int N = arr . Length ; CheckSubarray ( arr , N ) ; } }


"Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | C # program to implement the above approach ; Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver Code"
Solution in C#:

using System ; public class GFG { static void print_triplet ( int [ ] arr , int n ) { for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { Console . Write ( i - 1 + " ▁ " + i + " ▁ " + ( i + 1 ) ) ; return ; } } Console . Write ( - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 5 , 2 , 1 , 6 } ; int N = arr . Length ; print_triplet ( arr , N ) ; } }


"Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | C # program to implement the above approach ; Function to find a triplet that satisfy the conditions ; Traverse the given array ; Stores current element ; Stores element just before the current element ; Stores element just after the current element ; Check the given conditions ; Print a triplet ; If no triplet found ; Driver Code"
Solution in C#:

using System ; class GFG { static void FindTrip ( int [ ] arr , int N ) { for ( int i = 1 ; i < N - 1 ; i ++ ) { int p = arr [ i - 1 ] ; int q = arr [ i ] ; int r = arr [ i + 1 ] ; if ( p < q && q > r ) { Console . WriteLine ( ( i - 1 ) + " ▁ " + ( i ) + " ▁ " + ( i + 1 ) ) ; return ; } } Console . WriteLine ( - 1 ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 4 , 3 } ; int N = arr . Length ; FindTrip ( arr , N ) ; } }


"Find a triplet in an array whose sum is closest to a given number | C # implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int solution ( List < int > arr , int x ) { arr . Sort ( ) ; int closestSum = int . MaxValue ; for ( int i = 0 ; i < arr . Count - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . Count - 1 ; while ( ptr1 < ptr2 ) { int sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; if ( Math . Abs ( x - sum ) < Math . Abs ( x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return closestSum ; } public static void Main ( String [ ] args ) { int [ ] ar = { - 1 , 2 , 1 , - 4 } ; List < int > arr = new List < int > ( ar ) ; int x = 1 ; Console . WriteLine ( solution ( arr , x ) ) ; } }


"Find a triplet in an array whose sum is closest to a given number | C # implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; Update the closestSum ; Return the closest sum found ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int solution ( ArrayList arr , int x ) { int closestSum = int . MaxValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) { for ( int j = i + 1 ; j < arr . Count ; j ++ ) { for ( int k = j + 1 ; k < arr . Count ; k ++ ) { if ( Math . Abs ( x - closestSum ) > Math . Abs ( x - ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ) ) { closestSum = ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ; } } } } return closestSum ; } public static void Main ( string [ ] args ) { ArrayList arr = new ArrayList ( ) { - 1 , 2 , 1 , - 4 } ; int x = 1 ; Console . Write ( solution ( arr , x ) ) ; } }


"Find a triplet that sum to a given value | C # program to find a triplet ; returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Sort the elements ; Now fix the first element one by one and find the other two elements ; To find the other two elements , start two index variables from two corners of the array and move them toward each other index of the first element ; in the remaining elements index of the last element ; A [ i ] + A [ l ] + A [ r ] > sum ; If we reach here , then no triplet was found ; partition function ; Partitioning index ; Driver Code"
Solution in C#:

using System ; class GFG { bool find3Numbers ( int [ ] A , int arr_size , int sum ) { int l , r ; quickSort ( A , 0 , arr_size - 1 ) ; for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { l = i + 1 ; r = arr_size - 1 ; while ( l < r ) { if ( A [ i ] + A [ l ] + A [ r ] == sum ) { Console . Write ( " Triplet ▁ is ▁ " + A [ i ] + " , ▁ " + A [ l ] + " , ▁ " + A [ r ] ) ; return true ; } else if ( A [ i ] + A [ l ] + A [ r ] < sum ) l ++ ; else r -- ; } } return false ; } int partition ( int [ ] A , int si , int ei ) { int x = A [ ei ] ; int i = ( si - 1 ) ; int j ; for ( j = si ; j <= ei - 1 ; j ++ ) { if ( A [ j ] <= x ) { i ++ ; int temp = A [ i ] ; A [ i ] = A [ j ] ; A [ j ] = temp ; } } int temp1 = A [ i + 1 ] ; A [ i + 1 ] = A [ ei ] ; A [ ei ] = temp1 ; return ( i + 1 ) ; } if ( si < ei ) { pi = partition ( A , si , ei ) ; quickSort ( A , si , pi - 1 ) ; quickSort ( A , pi + 1 , ei ) ; } } static void ( ) { = new ( ) ; int [ ] = new int [ ] { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = A . Length ; triplet . find3Numbers ( A , arr_size , sum ) ; } }


"Find a triplet that sum to a given value | C # program to find a triplet that sum to a given value ; returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; If we reach here , then no triplet was found ; Driver Code"
Solution in C#:

using System ; class GFG { static bool find3Numbers ( int [ ] A , int arr_size , int sum ) { for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { for ( int j = i + 1 ; j < arr_size - 1 ; j ++ ) { for ( int k = j + 1 ; k < arr_size ; k ++ ) { if ( A [ i ] + A [ j ] + A [ k ] == sum ) { Console . WriteLine ( " Triplet ▁ is ▁ " + A [ i ] + " , ▁ " + A [ j ] + " , ▁ " + A [ k ] ) ; return true ; } } } } return false ; } static public void Main ( ) { int [ ] A = { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = A . Length ; find3Numbers ( A , arr_size , sum ) ; } }


"Find a triplet that sum to a given value | C # program to find a triplet using Hashing ; returns true if there is triplet with sum equal to ' sum ' present in A [ ] . Also , prints the triplet ; Fix the first element as A [ i ] ; Find pair in subarray A [ i + 1. . n - 1 ] with sum equal to sum - A [ i ] ; If we reach here , then no triplet was found ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static bool find3Numbers ( int [ ] A , int arr_size , int sum ) { for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { HashSet < int > s = new HashSet < int > ( ) ; int curr_sum = sum - A [ i ] ; for ( int j = i + 1 ; j < arr_size ; j ++ ) { if ( s . Contains ( curr_sum - A [ j ] ) ) { Console . Write ( " Triplet ▁ is ▁ { 0 } , ▁ { 1 } , ▁ { 2 } " , A [ i ] , A [ j ] , curr_sum - A [ j ] ) ; return true ; } s . Add ( A [ j ] ) ; } } return false ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = A . Length ; find3Numbers ( A , arr_size , sum ) ; } }


"Find a valid parenthesis sequence of length K from a given valid parenthesis sequence | C # program for the above approach ; Function to find the subsequence of length K forming valid sequence ; Stores the resultant String ; Check whether character at index i is visited or not ; Traverse the String ; Push index of open bracket ; Pop and mark visited ; Increment count by 2 ; Append the characters and create the resultant String ; Return the resultant String ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String findString ( String s , int k ) { int n = s . Length ; String ans = " ▁ " ; Stack < int > st = new Stack < int > ( ) ; bool [ ] vis = new bool [ n ] ; List < bool > vis ( n , false ) ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ( ' ) { st . Push ( i ) ; } if ( count < k && s [ i ] == ' ) ' ) { vis [ st . Peek ( ) ] = true ; st . Pop ( ) ; vis [ i ] = true ; count += 2 ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == true ) { ans += s [ i ] ; } } return ans ; } public static void Main ( String [ ] args ) { String s = " ( ) ( ) ( ) " ; int K = 2 ; Console . Write ( findString ( s , K ) ) ; } }


"Find a value whose XOR with given number is maximum | C # implementation of the above approach ; Function To Calculate Answer ; Find number of bits in the given integer ; XOR the given integer with poe ( 2 , number_of_bits - 1 and print the result ; Driver Code"
Solution in C#:

using System ; class GFG { static int calculate ( int X ) { int number_of_bits = 8 ; return ( ( 1 << number_of_bits ) - 1 ) ^ X ; } public static void Main ( ) { int X = 4 ; Console . WriteLine ( " Required ▁ Number ▁ is ▁ : ▁ " + calculate ( X ) ) ; } }


"Find a way to fill matrix with 1 ' s ▁ and ▁ 0' s in blank positions | C # implementation of the approach ; Function to generate and print the required matrix ; Replace the ' . ' ; Toggle number ; For each row , change the starting number ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 2 ; static int M = 2 ; static void Matrix ( char [ , ] a ) { char ch = '1' ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i , j ] == ' . ' ) a [ i , j ] = ch ; ch = ( ch == '1' ) ? '0' : '1' ; Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; if ( i % 2 == 0 ) ch = '0' ; else ch = '1' ; } } public static void Main ( String [ ] args ) { char [ , ] a = { { ' . ' , ' _ ' } , { ' _ ' , ' . ' } } ; Matrix ( a ) ; } }


"Find all Autobiographical Numbers with given number of digits | C # implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . Pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code"
Solution in C#:

using System ; class autobio { public static bool isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = num . ToString ( ) ; for ( i = 0 ; i < autoStr . Length ; i ++ ) { index = Int32 . Parse ( autoStr [ i ] + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . Length ; j ++ ) { number = Int32 . Parse ( autoStr [ j ] + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . Pow ( 10.0 , n - 1 ) ; high = Math . Pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; Console . Write ( i + " , ▁ " ) ; } if ( flag == 0 ) Console . WriteLine ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void Main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }


"Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | C # program to find the factors of large perfect square number in O ( Math . Sqrt ( Math . Sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the Math . Sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 , 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Drivers Code"
Solution in C#:

using System ; class GFG { static int MAX = 100000 ; static void findFactors ( int N ) { int temp = ( int ) Math . Sqrt ( N ) ; int [ ] factor = new int [ MAX ] ; for ( int l = 0 ; l < MAX ; l ++ ) factor [ l ] = 1 ; int i , j , k ; int len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp /= 2 ; } for ( j = 3 ; j < Math . Sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp /= j ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } int [ , ] M = new int [ len1 , MAX ] ; int tpc = 0 , tpr = 0 ; M [ 0 , 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr , 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr , tpc ] = M [ tpr , tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i , j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i , j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { Console . Write ( arr2 [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 900 ; findFactors ( N ) ; } }


"Find all angles of a given triangle | C # Code to find all three angles of a triangle given coordinate of all three vertices ;  ; returns square of distance b / w two points ; Square of lengths be a2 , b2 , c2 ; length of sides be a , b , c ; From Cosine law ; Converting to degree ; printing all the angles ; Driver Code"
Solution in C#:

using System ; class GFG { class Point { public int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } static int lengthSquare ( Point p1 , Point p2 ) { int xDiff = p1 . x - p2 . x ; int yDiff = p1 . y - p2 . y ; return xDiff * xDiff + yDiff * yDiff ; } static void printAngle ( Point A , Point B , Point C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = ( float ) Math . Sqrt ( a2 ) ; float b = ( float ) Math . Sqrt ( b2 ) ; float c = ( float ) Math . Sqrt ( c2 ) ; float alpha = ( float ) Math . Acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = ( float ) Math . Acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = ( float ) Math . Acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = ( float ) ( alpha * 180 / Math . PI ) ; betta = ( float ) ( betta * 180 / Math . PI ) ; gamma = ( float ) ( gamma * 180 / Math . PI ) ; Console . WriteLine ( " alpha ▁ : ▁ " + alpha ) ; Console . WriteLine ( " betta ▁ : ▁ " + betta ) ; Console . WriteLine ( " gamma ▁ : ▁ " + gamma ) ; } public static void Main ( String [ ] args ) { Point A = new Point ( 0 , 0 ) ; Point B = new Point ( 0 , 1 ) ; Point C = new Point ( 1 , 0 ) ; printAngle ( A , B , C ) ; } }


"Find all angles of a triangle in 3D | C # program for finding all angles of a triangle ; function for finding the angle ; Driver code"
Solution in C#:

using System ; class GFG { static double angle_triangle ( int x1 , int x2 , int x3 , int y1 , int y2 , int y3 , int z1 , int z2 , int z3 ) { int num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) ; double den = Math . Sqrt ( Math . Pow ( ( x2 - x1 ) , 2 ) + Math . Pow ( ( y2 - y1 ) , 2 ) + Math . Pow ( ( z2 - z1 ) , 2 ) ) * Math . Sqrt ( Math . Pow ( ( x3 - x1 ) , 2 ) + Math . Pow ( ( y3 - y1 ) , 2 ) + Math . Pow ( ( z3 - z1 ) , 2 ) ) ; double angle = Math . Acos ( num / den ) * ( 180.0 / 3.141592653589793238463 ) ; return angle ; } public static void Main ( ) { int x1 = - 1 , y1 = 3 , z1 = 2 ; int x2 = 2 , y2 = 3 , z2 = 5 ; int x3 = 3 , y3 = 5 , z3 = - 2 ; double angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) ; double angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) ; double angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) ; Console . WriteLine ( " Angles ▁ are ▁ : " ) ; Console . WriteLine ( " angle ▁ A ▁ = ▁ " + angle_A + " ▁ degree " ) ; Console . WriteLine ( " angle ▁ B ▁ = ▁ " + angle_B + " ▁ degree " ) ; Console . WriteLine ( " angle ▁ C ▁ = ▁ " + angle_C + " ▁ degree " ) ; } }


"Find all array elements occurring more than âŒŠN / 3 âŒ ‹ times | C # program to find if any element appears more than n / 3 ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code"
Solution in C#:

using System ; class GFG { static void findMajority ( int [ ] arr , int n ) { int count1 = 0 , count2 = 0 ; int flag = 0 ; int first = Int32 . MinValue ; int second = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) { Console . Write ( first + " ▁ " ) ; flag = 1 ; } if ( count2 > n / 3 ) { Console . Write ( second + " ▁ " ) ; flag = 1 ; } if ( flag == 0 ) Console . Write ( " No ▁ Majority ▁ Element " ) ; } static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 1 , 3 , 2 , 1 , 1 } ; int n = arr . Length ; findMajority ( arr , n ) ; } }


"Find all combinations that add upto given number | C # program to find out all combinations of positive numbers that add upto given number ; arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] . It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver code"
Solution in C#:

using System ; class GFG { static void findCombinationsUtil ( int [ ] arr , int index , int num , int reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( int i = 0 ; i < index ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return ; } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } static void findCombinations ( int n ) { int [ ] arr = new int [ n ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } static public void Main ( ) { int n = 5 ; findCombinations ( n ) ; } }


"Find all distinct palindromic sub | C # program to find all distinct palindrome sub - strings of a given string ; Function to print all distinct palindrome sub - strings of s ; table for storing results ( 2 rows for odd - and even - length palindromes ; Find all sub - string palindromes from the given input string insert ' guards ' to iterate easily over s ; int rp = 0 ; length of ' palindrome ▁ radius ' ; Attempt to expand palindrome centered at i ; Incrementing the length of palindromic radius as and when we find vaid palindrome ; Assigning the found palindromic length to odd / even length array ; remove ' guards ' ; Put all obtained palindromes in a hash map to find only distinct palindromess ; printing all distinct palindromes from hash map ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void palindromeSubStrs ( string s ) { Dictionary < string , int > m = new Dictionary < string , int > ( ) ; int n = s . Length ; int [ , ] R = new int [ 2 , n + 1 ] ; s = " @ " + s + " # " ; for ( int j = 0 ; j <= 1 ; j ++ ) { R [ j , 0 ] = 0 ; int i = 1 ; while ( i <= n ) { while ( s [ i - rp - 1 ] == s [ i + j + rp ] ) rp ++ ; R [ j , i ] = rp ; int k = 1 ; while ( ( R [ j , i - k ] != rp - k ) && k < rp ) { R [ j , i + k ] = Math . Min ( R [ j , i - k ] , rp - k ) ; k ++ ; } rp = Math . Max ( rp - k , 0 ) ; i += k ; } } s = s . Substring ( 1 ) ; if ( ! m . ContainsKey ( s . Substring ( 0 , 1 ) ) ) m . Add ( s . Substring ( 0 , 1 ) , 1 ) ; else m [ s . Substring ( 0 , 1 ) ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= 1 ; j ++ ) for ( int rp = R [ j , i ] ; rp > 0 ; rp -- ) { if ( ! m . ContainsKey ( s . Substring ( i - rp - 1 , 2 * rp + j ) ) ) m . Add ( s . Substring ( i - rp - 1 , 2 * rp + j ) , 1 ) ; else m [ s . Substring ( i - rp - 1 , 2 * rp + j ) ] ++ ; } if ( ! m . ContainsKey ( s . Substring ( i , 1 ) ) ) m . Add ( s . Substring ( i , 1 ) , 1 ) ; else m [ s . Substring ( i , 1 ) ] ++ ; } Console . WriteLine ( " Below ▁ are ▁ " + ( m . Count ) ) ; foreach ( KeyValuePair < string , int > ii in m ) Console . WriteLine ( ii . Key ) ; } public static void Main ( string [ ] args ) { palindromeSubStrs ( " abaaa " ) ; } }


"Find all distinct subset ( or subsequence ) sums of an array | C # program to print distinct subset sums of a given array . ; Uses Dynamic Programming to find distinct subset sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with sum equal to j . ; There is always a subset with 0 sum ; Fill dp [ ] [ ] in bottom up manner ; Sums that were achievable without current array element ; Print last row elements ; Driver code"
Solution in C#:

using System ; class GFG { static void printDistSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; bool [ , ] dp = new bool [ n + 1 , sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i , arr [ i - 1 ] ] = true ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 , j ] == true ) { dp [ i , j ] = true ; dp [ i , j + arr [ i - 1 ] ] = true ; } } } for ( int j = 0 ; j <= sum ; j ++ ) if ( dp [ n , j ] == true ) Console . Write ( j + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistSum ( arr , n ) ; } }


"Find all distinct subset ( or subsequence ) sums of an array | C # program to print distinct subset sums of a given array . ; sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void distSumRec ( int [ ] arr , int n , int sum , int currindex , HashSet < int > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . Add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; foreach ( int i in s ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistSum ( arr , n ) ; } }