"Sorting all array elements except one | C # program to sort all elements except element at index k . ; Move k - th element to end ; Sort all elements except last ; Store last element ( originally k - th ) ; Move all elements from k - th to one position ahead . ; Restore k - th element ; Driver code"
Solution in C#:

using System ; public class GFG { static int sortExceptK ( int [ ] arr , int k , int n ) { int temp = arr [ k ] ; arr [ k ] = arr [ n - 1 ] ; arr [ n - 1 ] = temp ; Array . Sort ( arr , 0 , n - 1 ) ; int last = arr [ n - 1 ] ; for ( int i = n - 1 ; i > k ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ k ] = last ; return 0 ; } public static void Main ( ) { int [ ] a = { 10 , 4 , 11 , 7 , 6 , 20 } ; int k = 2 ; int n = a . Length ; sortExceptK ( a , k , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }


"Sorting array elements with set bits equal to K | C # program for sorting array elements with set bits equal to K ; Represents node of a doubly linked list ; Function to sort elements with set bits equal to k ; initialise two vectors ; first vector contains indices of required element ; second vector contains required elements ; sorting the elements in second vector ; replacing the elements with k set bits with the sorted elements ; printing the new sorted array elements ; Utility function ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { static void sortWithSetbits ( int [ ] arr , int n , int k ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( bitCount ( arr [ i ] ) == k ) { v1 . Add ( i ) ; v2 . Add ( arr [ i ] ) ; } } v2 . Sort ( ) ; for ( int i = 0 ; i < v1 . Count ; i ++ ) { arr [ v1 [ i ] ] = v2 [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int [ ] arr = { 14 , 255 , 1 , 7 , 13 } ; int n = arr . Length ; int k = 3 ; sortWithSetbits ( arr , n , k ) ; } }


"Sorting array except elements in a subarray | C # program to sort all elements except given subarray . ; Sort whole array a [ ] except elements in range a [ l . . r ] ; Copy all those element that need to be sorted to an auxiliary array b [ ] ; sort the array b ; Copy sorted elements back to a [ ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void sortExceptUandL ( int [ ] a , int l , int u , int n ) { int [ ] b = new int [ n - ( u - l + 1 ) ] ; for ( int i = 0 ; i < l ; i ++ ) b [ i ] = a [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) b [ l + ( i - ( u + 1 ) ) ] = a [ i ] ; Array . Sort < int > ( b , 0 , n - ( u - l + 1 ) ) ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = b [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) a [ i ] = b [ l + ( i - ( u + 1 ) ) ] ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 3 , 12 , 14 , 9 } ; int n = a . Length ; int l = 2 , u = 4 ; sortExceptUandL ( a , l , u , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }


"Sorting array using Stacks | C # program to sort an array using stack ; This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and push it to the input stack ; push temp in tempory of stack ; Push array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Stack < int > sortStack ( Stack < int > input ) { Stack < int > tmpStack = new Stack < int > ( ) ; while ( input . Count != 0 ) { int tmp = input . Peek ( ) ; input . Pop ( ) ; while ( tmpStack . Count != 0 && tmpStack . Peek ( ) < tmp ) { input . Push ( tmpStack . Peek ( ) ) ; tmpStack . Pop ( ) ; } tmpStack . Push ( tmp ) ; } return tmpStack ; } static void sortArrayUsingStacks ( int [ ] arr , int n ) { Stack < int > input = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) input . Push ( arr [ i ] ) ; Stack < int > tmpStack = sortStack ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack . Peek ( ) ; tmpStack . Pop ( ) ; } } static void Main ( ) { int [ ] arr = new int [ ] { 10 , 5 , 15 , 45 } ; int n = arr . Length ; sortArrayUsingStacks ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Sorting array with conditional swapping | C # program to Check if it is possible to sort the array in ascending order ; Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code"
Solution in C#:

using System ; class GFG { static string possibleToSort ( int [ ] arr , int n , string str ) { int max_element = - 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { max_element = Math . Max ( max_element , arr [ i ] ) ; if ( str [ i ] == '0' ) { if ( max_element > i + 1 ) return " No " ; } } return " Yes " ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 6 } ; int n = arr . Length ; string str = "01110" ; Console . WriteLine ( possibleToSort ( arr , n , str ) ) ; } }


"Sorting array with reverse around middle | C # Program to answer queries on sum of sum of odd number digits of all the factors of a number ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; Driver code"
Solution in C#:

using System ; class GFG { static bool ifPossible ( int [ ] arr , int n ) { int [ ] cp = new int [ n ] ; Array . Copy ( arr , cp , n ) ; Array . Sort ( cp ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == cp [ i ] ) && ! ( arr [ n - 1 - i ] == cp [ i ] ) ) return false ; } return true ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 } ; int n = arr . Length ; if ( ifPossible ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Sorting objects using In | C # implementation of the approach ; Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Driver code ; Let 's represent objects as strings"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int partition ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { int j = l - 1 ; String temp ; int last_element = hash [ objects [ r ] ] ; for ( int i = l ; i < r ; i ++ ) { if ( hash [ objects [ i ] ] <= last_element ) { j ++ ; temp = objects [ i ] ; objects [ i ] = objects [ j ] ; objects [ j ] = temp ; } } j ++ ; temp = objects [ r ] ; objects [ r ] = objects [ j ] ; objects [ j ] = temp ; return j ; } static void quicksort ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { if ( l < r ) { int mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } static void sortObj ( List < String > objects ) { Dictionary < String , int > hash = new Dictionary < String , int > ( ) ; hash . Add ( " blue " , 1 ) ; hash . Add ( " red " , 2 ) ; hash . Add ( " yellow " , 3 ) ; quicksort ( objects , 0 , objects . Count - 1 , hash ) ; for ( int i = 0 ; i < objects . Count ; i ++ ) Console . Write ( objects [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > objects = new List < String > { " red " , " blue " , " red " , " yellow " , " blue " } ; sortObj ( objects ) ; } }


"Sorting without comparison of elements | C # program to sort an array without comparison operator . ; Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code"
Solution in C#:

using System ; class GFG { static void sortArr ( int [ ] arr , int n , int min , int max ) { int m = max - min + 1 ; int [ ] c = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ arr [ i ] - min ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < c [ i ] ; j ++ ) { Console . Write ( ( i + min ) + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 10 , 10 , 1 , 4 , 4 , 100 , 0 } ; int min = 0 , max = 100 ; int n = arr . Length ; sortArr ( arr , n , min , max ) ; } }


"Source to destination in 2 | C # implementation of the approach ; Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int Sx , int Sy , int Dx , int Dy , int x , int y ) { if ( Math . Abs ( Sx - Dx ) % x == 0 && Math . Abs ( Sy - Dy ) % y == 0 && ( Math . Abs ( Sx - Dx ) / x ) % 2 == ( Math . Abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } static void Main ( ) { int Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; int x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Space efficient iterative method to Fibonacci number | C # code to find nth fibonacci ; get second MSB ; consectutively set all the bits ; returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; take 2D array to store number 's ; run loop till MSB > 0 ; To return fibonacci number ; Driver Code ; Given n"
Solution in C#:


"Space optimization using bit manipulations | C # code to for marking multiples ; index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5"
Solution in C#:

using System ; class GFG { static bool checkbit ( int [ ] array , int index ) { int val = array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; if ( val == 0 ) return false ; return true ; } static void setbit ( int [ ] array , int index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } public static void Main ( String [ ] args ) { int a = 2 , b = 10 ; int size = Math . Abs ( b - a ) ; size = ( int ) Math . Ceiling ( ( double ) size / 32 ) ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; Console . WriteLine ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) Console . Write ( i + " ▁ " ) ; } }


"Space optimization using bit manipulations | C # program to mark numbers as multiple of 2 or 5 ; Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1"
Solution in C#:

using System ; class GFG { static public void Main ( ) { int a = 2 , b = 10 ; int size = Math . Abs ( b - a ) + 1 ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; Console . WriteLine ( " MULTIPLES ▁ of ▁ 2" + " ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) Console . Write ( i + " ▁ " ) ; } }


"Spanning Tree With Maximum Degree ( Using Kruskal 's Algorithm) | C # implementation of the approach ; par and rank will store the parent and rank of particular node in the Union Find Algorithm ; Find function of Union Find Algorithm ; Union function of Union Find Algorithm ; Function to find the required spanning tree ; Initialising parent of a node by itself ; Variable to store the node with maximum degree ; Finding the node with maximum degree ; Union of all edges incident on vertex with maximum degree ; Carrying out normal Kruskal Algorithm ; Driver code ; Number of nodes ; Number of edges ; ArrayList to store the graph ; Array to store the degree of each node in the graph ; Add edges and update degrees"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] par ; static int [ ] rank ; static int find ( int x ) { if ( par [ x ] != x ) par [ x ] = find ( par [ x ] ) ; return par [ x ] ; } static void union ( int u , int v ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) return ; if ( rank [ x ] > rank [ y ] ) par [ y ] = x ; else if ( rank [ x ] < rank [ y ] ) par [ x ] = y ; else { par [ x ] = y ; rank [ y ] ++ ; } } static void findSpanningTree ( int [ ] deg , int n , int m , List < int > [ ] g ) { par = new int [ n + 1 ] ; rank = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) par [ i ] = i ; int max = 1 ; for ( int i = 2 ; i <= n ; i ++ ) if ( deg [ i ] > deg [ max ] ) max = i ; foreach ( int v in g [ max ] ) { Console . WriteLine ( max + " ▁ " + v ) ; union ( max , v ) ; } for ( int u = 1 ; u <= n ; u ++ ) { foreach ( int v in g [ u ] ) { int x = find ( u ) ; int y = find ( v ) ; if ( x == y ) continue ; union ( x , y ) ; Console . WriteLine ( u + " ▁ " + v ) ; } } } public static void Main ( String [ ] args ) { int n = 5 ; int m = 5 ; List < int > [ ] g = new List < int > [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) g [ i ] = new List < int > ( ) ; int [ ] deg = new int [ n + 1 ] ; g [ 1 ] . Add ( 2 ) ; g [ 2 ] . Add ( 1 ) ; deg [ 1 ] ++ ; deg [ 2 ] ++ ; g [ 1 ] . Add ( 5 ) ; g [ 5 ] . Add ( 1 ) ; deg [ 1 ] ++ ; deg [ 5 ] ++ ; g [ 2 ] . Add ( 3 ) ; g [ 3 ] . Add ( 2 ) ; deg [ 2 ] ++ ; deg [ 3 ] ++ ; g [ 5 ] . Add ( 3 ) ; g [ 3 ] . Add ( 5 ) ; deg [ 3 ] ++ ; deg [ 5 ] ++ ; g [ 3 ] . Add ( 4 ) ; g [ 4 ] . Add ( 3 ) ; deg [ 3 ] ++ ; deg [ 4 ] ++ ; findSpanningTree ( deg , n , m , g ) ; } }


"Sparse Search | C # program to implement binary search in a sparse array . ; Binary Search in an array with blanks ; Modified Part ; Search for both side for a non empty string ; No non - empty string on both sides ; Normal Binary Search ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int binarySearch ( string [ ] arr , int low , int high , string x ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] == " " ) { int left = mid - 1 ; int right = mid + 1 ; while ( true ) { if ( left < low && right > high ) return - 1 ; if ( left >= low && arr [ left ] != " " ) { mid = left ; break ; } else if ( right <= high && arr [ right ] != " " ) { mid = right ; break ; } left -- ; right ++ ; } } if ( arr [ mid ] == x ) return mid ; else if ( x . CompareTo ( arr [ mid ] ) < 0 ) return binarySearch ( arr , low , mid - 1 , x ) ; else return binarySearch ( arr , mid + 1 , high , x ) ; } static int sparseSearch ( string [ ] arr , string x , int n ) { return binarySearch ( arr , 0 , n - 1 , x ) ; } public static void Main ( string [ ] args ) { string [ ] arr = { " for " , " geeks " , " " , " " , " " , " " , " ide " , " practice " , " " , " " , " " , " quiz " } ; string x = " geeks " ; int n = x . Length ; int index = sparseSearch ( arr , x , n ) ; if ( index != - 1 ) Console . Write ( x + " ▁ found ▁ at ▁ index ▁ " + index ) ; else Console . Write ( x + " ▁ not ▁ found " ) ; } }


"Special prime numbers | C # program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver function"
Solution in C#:

using System ; using System . Collections ; class GFG { static ArrayList primes = new ArrayList ( ) ; static void SieveofEratosthenes ( int n ) { bool [ ] visited = new bool [ n * n + 2 ] ; for ( int i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( int j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . Add ( i ) ; } } static bool specialPrimeNumbers ( int n , int k ) { SieveofEratosthenes ( n ) ; int count = 0 ; for ( int i = 0 ; i < primes . Count ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( ( int ) primes [ j ] + ( int ) primes [ j + 1 ] + 1 == ( int ) primes [ i ] ) { count ++ ; break ; } } if ( count == k ) return true ; } return false ; } public static void Main ( ) { int n = 27 , k = 2 ; if ( specialPrimeNumbers ( n , k ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Special two digit numbers in a Binary Search Tree | A binary tree node ; Function to create a new node ; If the tree is empty , return a new , single node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to find if number is special or not ; Check if number is two digit or not ; Function to count number of special two digit number ; Driver code ; Function call"
Solution in C#:

using System ; public class Node { public int info ; public Node left , right ; public Node ( int d ) { info = d ; left = right = null ; } } public class BinaryTree { public static Node head ; public static int count ; public Node insert ( Node node , int info ) { if ( node == null ) { return ( new Node ( info ) ) ; } else { if ( info <= node . info ) { node . left = insert ( node . left , info ) ; } else { node . right = insert ( node . right , info ) ; } } return node ; } static int check ( int num ) { int sum = 0 , i = num , sum_of_digits , prod_of_digits ; if ( num < 10 num > 99 ) { return 0 ; } else { sum_of_digits = ( i % 10 ) + ( i / 10 ) ; prod_of_digits = ( i % 10 ) * ( i / 10 ) ; sum = sum_of_digits + prod_of_digits ; } if ( sum == num ) { return 1 ; } else { return 0 ; } } static void countSpecialDigit ( Node rt ) { int x ; if ( rt == null ) { return ; } else { x = check ( rt . info ) ; if ( x == 1 ) { count = count + 1 ; } countSpecialDigit ( rt . left ) ; countSpecialDigit ( rt . right ) ; } } static public void Main ( ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 50 ) ; tree . insert ( root , 29 ) ; tree . insert ( root , 59 ) ; tree . insert ( root , 19 ) ; tree . insert ( root , 53 ) ; tree . insert ( root , 556 ) ; tree . insert ( root , 56 ) ; tree . insert ( root , 94 ) ; tree . insert ( root , 13 ) ; countSpecialDigit ( root ) ; Console . WriteLine ( count ) ; } }


"Sphenic Number | C # program to check whether a number is a Sphenic number or not ; Create a global array of size 10001 ; ; This functions finds all primes smaller than ' limit ' . Using simple sieve of eratosthenes . ; Initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; One by one traverse all numbers so that their multiples can be marked as composite . ; If p is not changed , then it is a prime ; Update all multiples of p ; To store the 8 divisors ; To count the number of divisor ; Finally check if there re 8 divisor and all the numbers are distinct prime no return 1 else return 0 ) ; ; Driver code"
Solution in C#:

using System ; class GFG { static bool [ ] arr = new bool [ 1001 ] ; static void simpleSieve ( ) { for ( int i = 0 ; i < 1001 ; i ++ ) arr [ i ] = true ; for ( int p = 2 ; p * p < 1001 ; p ++ ) { if ( arr [ p ] ) { for ( int i = p * 2 ; i < 1001 ; i = i + p ) arr [ i ] = false ; } } } static int find_sphene ( int N ) { int [ ] arr1 = new int [ 8 ] ; int count = 0 ; int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( N % i == 0 && count < 8 ) { count ++ ; arr1 [ j ++ ] = i ; } } if ( count == 8 && ( arr [ arr1 [ 1 ] ] && arr [ arr1 [ 2 ] ] && arr [ arr1 [ 3 ] ] ) ) return 1 ; return 0 ; } public static void Main ( String [ ] args ) { int n = 60 ; simpleSieve ( ) ; int ans = find_sphene ( n ) ; if ( ans == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " NO " ) ; } }


"Split Array into min number of subsets with difference between each pair greater than 1 | C # program for the above approach ; Function to split the array into minimum number of subsets with difference strictly > 1 ; Sort the array ; Traverse through the sorted array ; Check the pairs of elements with difference 1 ; If we find even a single pair with difference equal to 1 , then 2 partitions else only 1 partition ; Print the count of partitions ; Driver Code ; Given array ; Size of the array ; Function call"
Solution in C#:

using System ; class GFG { static void split ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } Console . Write ( count ) ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 2 , 4 , 6 } ; int n = arr . Length ; split ( arr , n ) ; } }


"Split N ^ 2 numbers into N groups of equal sum | Java implementation of the above approach ; Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; Driver code"
Solution in C#:

using System ; class GFG { static void printGroups ( int n ) { int x = 1 ; int y = n * n ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n / 2 ; j ++ ) { Console . Write ( " { ▁ " + x + " , ▁ " + y + " } ▁ " ) ; x ++ ; y -- ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int n = 4 ; printGroups ( n ) ; } }


"Split N as the sum of K numbers satisfying the given conditions | C # program to implement the above approach ; List to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static List < int > primes = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] prime = new bool [ 10005 ] ; for ( int i = 0 ; i < 10005 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . Add ( p ) ; } static void generate ( int n , int k ) { List < int > prod = new List < int > ( ) ; SieveOfEratosthenes ( ) ; int l = primes . Count ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i + 1 ; j < l ; j ++ ) { if ( primes [ i ] * primes [ j ] > 0 ) prod . Add ( primes [ i ] * primes [ j ] ) ; } } prod . Sort ( ) ; int sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) sum += prod [ i ] ; if ( sum > n ) Console . Write ( " - 1" ) ; else { for ( int i = 0 ; i < k - 1 ; i ++ ) { Console . Write ( prod [ i ] + " , ▁ " ) ; } Console . Write ( n - sum + "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 52 , k = 5 ; generate ( n , k ) ; } }


"Split N into two integers whose addition to A and B makes them equal | C # program to implement the above approach ; Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver code"
Solution in C#:

using System ; class GFG { static void findPair ( int A , int B , int N ) { int X , Y ; X = N - B + A ; if ( X % 2 != 0 ) { Console . Write ( " - 1" ) ; } else { X = X / 2 ; Y = N - X ; Console . Write ( X + " ▁ " + Y ) ; } } public static void Main ( string [ ] args ) { int A = 1 ; int B = 3 ; int N = 4 ; findPair ( A , B , N ) ; } }


"Split N natural numbers into two sets having GCD of their sums greater than 1 | C # program to split N natural numbers into two sets having GCD of their sums greater than 1 ; Function to create and print the two sets ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code"
Solution in C#:

 using System ; class GFG { static void createSets ( long n ) { if ( n <= 2 ) { Console . Write ( " - 1" ) ; return ; } else { long x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; Console . Write ( x + "
 " ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; Console . Write ( i + " ▁ " ) ; } } return ; } public static void Main ( String [ ] args ) { long N = 7 ; createSets ( N ) ; } }


"Split N natural numbers into two sets having GCD of their sums greater than 1 | C # program to split N natural numbers into two sets having GCD of their sums greater than 1 ; Function to create and print the two sets ; No such split possible for N <= 2 ; Print the first set consisting of even elements ; Print the second set consisting of odd ones ; Driver Code"
Solution in C#:

 using System ; class GFG { static void createSets ( int N ) { if ( N <= 2 ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 2 ; i <= N ; i += 2 ) Console . Write ( i + " ▁ " ) ; Console . Write ( "
 " ) ; for ( int i = 1 ; i <= N ; i += 2 ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 6 ; createSets ( N ) ; } }


"Split N powers of 2 into two subsets such that their difference of sum is minimum | C # program to find the minimum difference possible by splitting all powers of 2 up to N into two sets of equal size ; Store the largest ; Form two separate groups ; Initialize the sum for two groups as 0 ; Insert 2 ^ n in the first group ; Calculate the sum of least n / 2 - 1 elements added to the first set ; Sum of remaining n / 2 - 1 elements ; Min difference between the two groups ; Driver Code"
Solution in C#:

using System ; class GFG { public static void MinDiff ( int n ) { int val = ( int ) Math . Pow ( 2 , n ) ; int sep = n / 2 ; int grp1 = 0 ; int grp2 = 0 ; grp1 = grp1 + val ; for ( int i = 1 ; i < sep ; i ++ ) grp1 = grp1 + ( int ) Math . Pow ( 2 , i ) ; for ( int i = sep ; i < n ; i ++ ) grp2 = grp2 + ( int ) Math . Pow ( 2 , i ) ; Console . Write ( Math . Abs ( grp1 - grp2 ) ) ; } public static void Main ( ) { int n = 4 ; MinDiff ( n ) ; } }


"Split a Numeric String into Fibonacci Sequence | C # program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver Code ; Given String ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static bool splitIntoFibonacciHelper ( int pos , string S , ArrayList seq ) { if ( pos == S . Length && ( seq . Count >= 3 ) ) { return true ; } long num = 0 ; for ( int i = pos ; i < S . Length ; i ++ ) { num = num * 10 + ( S [ i ] - '0' ) ; if ( num > Int64 . MaxValue ) break ; if ( S [ pos ] == '0' && i > pos ) break ; if ( seq . Count > 2 && ( num > ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) break ; if ( seq . Count < 2 || ( num == ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) { seq . Add ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . Remove ( seq . Count - 1 ) ; } } return false ; } static void splitIntoFibonacci ( string S ) { ArrayList seq = new ArrayList ( ) ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . Count >= 3 ) { for ( int i = 0 ; i < seq . Count ; i ++ ) Console . Write ( seq [ i ] + " ▁ " ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( string [ ] args ) { string S = "11235813" ; splitIntoFibonacci ( S ) ; } }


"Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1 | C # program to split a given String into K segments such that the sum of product of occurence of characters in them is minimized ; Function to return the minimum sum of products of occurences of 0 and 1 in each segments ; Store the length of the String ; Not possible to generate subsets greater than the length of String ; If the number of subsets equals the length ; Precompute the sum of products for all index ; Calculate the minimum sum of products for K subsets ; Driver code"
Solution in C#:

using System ; class GFG { static int minSumProd ( string S , int K ) { int len = S . Length ; if ( K > len ) return - 1 ; if ( K == len ) return 0 ; int [ ] dp = new int [ len ] ; int count_zero = 0 , count_one = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( S [ j ] == '0' ) { count_zero ++ ; } else { count_one ++ ; } dp [ j ] = count_zero * count_one ; } for ( int i = 1 ; i < K ; i ++ ) { for ( int j = len - 1 ; j >= i ; j -- ) { count_zero = 0 ; count_one = 0 ; dp [ j ] = Int32 . MaxValue ; for ( int k = j ; k >= i ; k -- ) { if ( S [ k ] == '0' ) { count_zero ++ ; } else { count_one ++ ; } dp [ j ] = Math . Min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; } } } return dp [ len - 1 ] ; } public static void Main ( string [ ] args ) { string S = "1011000110110100" ; int K = 5 ; Console . Write ( minSumProd ( S , K ) ) ; } }


"Split a given array into K subarrays minimizing the difference between their maximum and minimum | C # program for the above approach ; Function to find the subarray ; Add the difference to vectors ; Sort vector to find minimum k ; Initialize result ; Adding first k - 1 values ; Return the minimized sum ; Driver Code ; Given array [ ] arr ; Given K ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int find ( int [ ] a , int n , int k ) { List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < n ; ++ i ) { v . Add ( a [ i - 1 ] - a [ i ] ) ; } v . Sort ( ) ; int res = a [ n - 1 ] - a [ 0 ] ; for ( int i = 0 ; i < k - 1 ; ++ i ) { res += v [ i ] ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 15 , 16 , 23 , 42 } ; int N = arr . Length ; int K = 3 ; Console . Write ( find ( arr , N , K ) + "
 " ) ; } }


"Split a number as sum of K numbers which are not divisible by K | C # program to split a number as sum of K numbers which are not divisible by K ; Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code"
Solution in C#:

 using System ; class GFG { static void printKParts ( int N , int K ) { if ( N % K == 0 ) { for ( int i = 1 ; i < K ; i ++ ) Console . Write ( "1 , ▁ " ) ; Console . Write ( N - ( K - 1 ) + "
 " ) ; } else { if ( K == 2 ) { Console . Write ( " Not ▁ Possible " + "
 " ) ; return ; } for ( int i = 1 ; i < K - 1 ; i ++ ) Console . Write ( 1 + " , ▁ " ) ; Console . Write ( 2 + " , ▁ " + ( N - K ) + "
 " ) ; } } public static void Main ( String [ ] args ) { int N = 18 , K = 5 ; printKParts ( N , K ) ; } }


"Split a number into 3 parts such that none of the parts is divisible by 3 | C # program to split a number into three parts such than none of them is divisible by 3. ; Print x = 1 , y = 1 and z = N - 2 ; Otherwise , print x = 1 , y = 2 and z = N - 3 ; Driver code"
Solution in C#:

using System ; public class GFG { static void printThreeParts ( int N ) { if ( N % 3 == 0 ) Console . WriteLine ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " + ( N - 2 ) ) ; else Console . WriteLine ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " + ( N - 3 ) ) ; } static public void Main ( ) { int N = 10 ; printThreeParts ( N ) ; } }


"Split a string in equal parts such that all parts are palindromes | C # implementation of the approach ; Function to return the frequency array for the given string ; Function to return the required count ; Add frequencies of the even appearing characters ; Count of the characters that appeared odd number of times ; If there are no characters with odd frequency ; If there are no characters with even frequency ; Only a single character with odd frequency ; More than 1 character with odd frequency string isn 't a palindrome ; All odd appearing characters can also contribute to the even length palindrome if one character is removed from the frequency leaving it as even ; If k palindromes are possible where k is the number of characters with odd frequency ; Current character can no longer be an element in a string other than the mid character ; If current character has odd frequency > 1 take two characters which can be used in any of the parts ; Update the frequency ; If not possible , then every character of the string will act as a separate palindrome ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] getFrequencies ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } return freq ; } static int countMinParts ( String str ) { int n = str . Length ; int [ ] freq = getFrequencies ( str ) ; List < int > oddFreq = new List < int > ( ) ; List < int > evenFreq = new List < int > ( ) ; int i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . Add ( freq [ i ] ) ; else oddFreq . Add ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . Count ; i ++ ) { sumEven += evenFreq [ i ] ; } if ( oddFreq . Count == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . Count == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . Count ) { if ( ( sumEven / 2 ) % oddFreq . Count == 0 ) return oddFreq . Count ; if ( oddFreq [ i ] == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq . Insert ( i , oddFreq [ i ] - 2 ) ; } return n ; } public static void Main ( String [ ] args ) { String s = " noonpeep " ; Console . WriteLine ( countMinParts ( s ) ) ; } }


"Split a string into maximum number of unique substrings | C # program for the above approach ; Function to find the maximum count of unique substrings by splitting a string into maximum number of unique substrings ; Utility function to find maximum count of unique substrings by splitting the string ; Stores maximum count of unique substring by splitting the string into substrings ; Iterate over the characters of the string ; Stores prefix substring ; Check if the current substring already exists ; Insert tmp into set ; Recursively call for remaining characters of string ; Remove from the set ; Return answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public int maxUniqueSplit ( String S ) { return maxUnique ( S , new HashSet < String > ( ) ) ; } public int maxUnique ( String S , HashSet < String > set ) { int max = 0 ; for ( int i = 1 ; i <= S . Length ; i ++ ) { String tmp = S . Substring ( 0 , i ) ; if ( ! set . Contains ( tmp ) ) { set . Add ( tmp ) ; max = Math . Max ( max , maxUnique ( S . Substring ( i ) , set ) + 1 ) ; set . Remove ( tmp ) ; } } return max ; } } public class GFG { public static void Main ( String [ ] args ) { Solution st = new Solution ( ) ; String S = " ababccc " ; int res = st . maxUniqueSplit ( S ) ; Console . WriteLine ( res ) ; } }


"Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | C # program for the above approach ; Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Driver Code ; Given array and K ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int make_cuts ( int [ ] arr , int n , int K ) { int ans = 0 ; List < int > poss = new List < int > ( ) ; int ce = 0 ; int co = 0 ; for ( int x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . Add ( Math . Abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } poss . Sort ( ) ; foreach ( int x in poss ) { if ( K >= x ) { ans ++ ; K -= x ; } else break ; } return ans ; } public static void Main ( String [ ] args ) { int N = 6 ; int K = 4 ; int [ ] arr = { 1 , 2 , 5 , 10 , 15 , 20 } ; Console . Write ( make_cuts ( arr , N , K ) ) ; } }


"Split an array into equal length subsequences consisting of equal elements only | C # program for the above approach ; Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of each array element ; Store the GCD of frequencies of all array elements ; Traverse the map ; Update GCD ; If the GCD is greater than 1 , print YES otherwise print NO ; Driver Code ; Given array ; Store the size of the array"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void splitArray ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) == true ) mp [ arr [ i ] ] += 1 ; else mp [ arr [ i ] ] = 1 ; } int G = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { G = gcd ( G , i . Value ) ; } if ( G > 1 ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int n = arr . Length ; splitArray ( arr , n ) ; } }


"Split an array into minimum number of non | C # program for the above approach ; Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , [ ] arr ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; If previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minimumSubarrays ( int [ ] arr , int n ) { char current = ' N ' ; int answer = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( current == ' N ' ) { if ( arr [ i ] < arr [ i - 1 ] ) { current = ' D ' ; } else if ( arr [ i ] == arr [ i - 1 ] ) { current = ' N ' ; } else { current = ' I ' ; } } else if ( current == ' I ' ) { if ( arr [ i ] >= arr [ i - 1 ] ) { current = ' I ' ; } else { current = ' N ' ; answer += 1 ; } } else { if ( arr [ i ] <= arr [ i - 1 ] ) { current = ' D ' ; } else { current = ' N ' ; answer += 1 ; } } } Console . Write ( answer ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 9 , 5 , 4 , 6 , 8 } ; int n = arr . Length ; minimumSubarrays ( arr , n ) ; } }


"Split array into K non | C # program for the above approach ; Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int maximumSum ( int [ ] arr , int [ ] S , int N , int K ) { int ans = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ N - 1 - i ] ; arr [ N - 1 - i ] = temp ; } for ( int i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; Array . Sort ( S ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -- ; } int counter = K - 1 ; for ( int i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 13 , 7 , 17 } ; int [ ] S = { 1 , 3 } ; int N = arr . Length ; int K = S . Length ; Console . WriteLine ( maximumSum ( arr , S , N , K ) ) ; } }


"Split array into K subsets to maximize sum of their second largest elements | C # program for the above approach ; Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Drive Code ; Given array [ ] arr ; Size of array ; Function Call"
Solution in C#:

using System ; class GFG { static void splitArray ( int [ ] arr , int n , int K ) { Array . Sort ( arr ) ; int i = n - 1 ; int result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } Console . Write ( result ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 5 , 1 , 3 } ; int N = arr . Length ; int K = 2 ; splitArray ( arr , N , K ) ; } }


"Split array into K | C # implementation for above approach ; Function to find the minimum sum of 2 nd smallest elements of each subset ; Sort the array ; Stores minimum sum of second elements of each subset ; Traverse first K 2 nd smallest elements ; Update their sum ; Print the sum ; Driver Code ; Given Array ; Given length of array ; Given subset lengths"
Solution in C#:

using System ; class GFG { public static void findMinimum ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 1 ; i < 2 * ( N / K ) ; i += 2 ) { ans += arr [ i ] ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 } ; int N = arr . Length ; int K = 5 ; findMinimum ( arr , N , K ) ; } }


"Split array into equal length subsets with maximum sum of Kth largest element of each subset | C # program to implement the above approach ; Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver code"
Solution in C#:

using System ; class GFG { static int maximumKthLargestsumPart ( int [ ] arr , int N , int M , int K ) { int maxSum = 0 ; if ( N % M != 0 ) return - 1 ; int sz = ( N / M ) ; if ( K > sz ) return - 1 ; Array . Sort ( arr ) ; Array . Reverse ( arr ) ; for ( int i = 1 ; i <= M ; i ++ ) { maxSum += arr [ i * K - 1 ] ; } return maxSum ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int M = 2 ; int K = 1 ; int N = arr . Length ; Console . WriteLine ( maximumKthLargestsumPart ( arr , N , M , K ) ) ; } }


"Split array into minimum number of subarrays having GCD of its first and last element exceeding 1 | C # program for the above approach ; Function to find the minimum number of subarrays ; Right pointer ; Left pointer ; Count of subarrays ; Find GCD ( left , right ) ; Found a valid large subarray between arr [ left , right ] ; Searched the arr [ 0. . right ] and found no subarray having size > 1 and having gcd ( left , right ) > 1 ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int minSubarrays ( int [ ] arr , int n ) { int right = n - 1 ; int left = 0 ; int subarrays = 0 ; while ( right >= 0 ) { for ( left = 0 ; left <= right ; left += 1 ) { if ( __gcd ( arr [ left ] , arr [ right ] ) > 1 ) { subarrays += 1 ; right = left - 1 ; break ; } if ( left == right && __gcd ( arr [ left ] , arr [ right ] ) == 1 ) { return 0 ; } } } return subarrays ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( ) { int N = 6 ; int [ ] arr = { 2 , 3 , 4 , 4 , 4 , 3 } ; Console . Write ( minSubarrays ( arr , N ) ) ; } }


"Split array into minimum number of subsets having maximum pair sum at most K | C # program to implement the above approach ; Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code"
Solution in C#:

using System ; class GFG { static int cntMinSub ( int [ ] arr , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; Array . Sort ( arr ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = arr . Length ; int K = 26 ; Console . Write ( cntMinSub ( arr , N , K ) ) ; } }


"Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | C # program to implement the above approach ; Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; If N is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int MinimumNoOfWays ( int [ ] arr , int n ) { int mini_no_of_ways ; if ( n % 2 == 0 ) { mini_no_of_ways = n / 2 ; } else { mini_no_of_ways = n / 2 + 1 ; } return mini_no_of_ways ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 4 , 2 , 1 , 5 } ; int N = arr . Length ; Console . WriteLine ( MinimumNoOfWays ( arr , N ) ) ; } }


"Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray | C # program for the above approach ; Function to find the minimum cost of splitting the array into subarrays ; Get the maximum element ; [ ] dp will store the minimum cost upto index i ; Initialize the result array ; Initialise the first element ; Create the frequency array ; Update the frequency ; Counting the cost of the duplicate element ; Minimum cost of operation from 0 to j ; Total cost of the array ; Driver Code ; Given cost K ; Function Call"
Solution in C#:

using System ; using System . Linq ; class GFG { static long findMinCost ( int [ ] a , int k , int n ) { int max_ele = a . Max ( ) ; long [ ] dp = new long [ n + 1 ] ; for ( int i = 1 ; i <= n ; ++ i ) dp [ i ] = int . MaxValue ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int [ ] freq = new int [ max_ele + 1 ] ; for ( int j = i ; j < n ; ++ j ) { freq [ a [ j ] ] ++ ; int cost = 0 ; for ( int x = 0 ; x <= max_ele ; ++ x ) { cost += ( freq [ x ] == 1 ) ? 0 : freq [ x ] ; } dp [ j + 1 ] = Math . Min ( dp [ i ] + cost + k , dp [ j + 1 ] ) ; } } return dp [ n ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 , 1 } ; int K = 2 ; int n = arr . Length ; Console . Write ( findMinCost ( arr , K , n ) ) ; } }


"Split array into subarrays such that sum of difference between their maximums and minimums is maximum | C # program for the above approach ; Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Base Case ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver Code"
Solution in C#:

using System ; class GFG { static int getValue ( int [ ] arr , int N ) { int [ ] dp = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { int min = arr [ i ] ; int max = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { min = Math . Min ( arr [ j ] , min ) ; max = Math . Max ( arr [ j ] , max ) ; dp [ i ] = Math . Max ( dp [ i ] , max - min + ( ( j >= 1 ) ? dp [ j - 1 ] : 0 ) ) ; } } return dp [ N - 1 ] ; } static public void Main ( ) { int [ ] arr = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . Length ; Console . Write ( getValue ( arr , N ) ) ; } }


"Split array into two equal length subsets such that all repetitions of a number lies in a single subset | C # program for the above approach ; Function to create the frequency array of the given array arr [ ] ; Hashmap to store the frequencies ; Store freq for each element ; Get the total frequencies ; Store frequencies in subset [ ] array ; Return frequency array ; Function to check is sum N / 2 can be formed using some subset ; dp [ i ] [ j ] store the answer to form sum j using 1 st i elements ; Initialize dp [ ] [ ] with true ; Fill the subset table in the bottom up manner ; If current element is less than j ; Update current state ; Return the result ; Function to check if the given array can be split into required sets ; Store frequencies of arr [ ] ; If size of arr [ ] is odd then print " Yes " ; Check if answer is true or not ; Print the result ; Driver code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int [ ] findSubsets ( int [ ] arr ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( M . ContainsKey ( arr [ i ] ) ) { M [ arr [ i ] ] ++ ; } else { M [ arr [ i ] ] = 1 ; } } int [ ] subsets = new int [ M . Count ] ; int I = 0 ; foreach ( KeyValuePair < int , int > playerEntry in M ) { subsets [ I ] = playerEntry . Value ; I ++ ; } return subsets ; } static bool subsetSum ( int [ ] subsets , int target ) { bool [ , ] dp = new bool [ subsets . Length + 1 , target + 1 ] ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= subsets . Length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] ; if ( j >= subsets [ i - 1 ] ) { dp [ i , j ] |= dp [ i - 1 , j - subsets [ i - 1 ] ] ; } } } return dp [ subsets . Length , target ] ; } static void divideInto2Subset ( int [ ] arr ) { int [ ] subsets = findSubsets ( arr ) ; if ( ( arr . Length ) % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } bool isPossible = subsetSum ( subsets , arr . Length / 2 ) ; if ( isPossible ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; divideInto2Subset ( arr ) ; } }


"Split array into two subarrays such that difference of their maximum is minimum | C # Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinDif ( int [ ] arr , int N ) { Array . Sort ( arr ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } public static void Main ( ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int N = arr . Length ; Console . Write ( findMinDif ( arr , N ) ) ; } }


"Split array into two subarrays such that difference of their sum is minimum | C # Program for above approach ; Function to return minimum difference between sum of two subarrays ; To store total sum of array ; Calculate the total sum of the array ; Stores the prefix sum ; Stores the minimum difference ; Traverse the given array ; To store minimum difference ; Update minDiff ; Return minDiff ; Driver Code ; Given array ; Length of the array"
Solution in C#:

using System ; class GFG { static int minDiffSubArray ( int [ ] arr , int n ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) total_sum += arr [ i ] ; int prefix_sum = 0 ; int minDiff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix_sum += arr [ i ] ; int diff = Math . Abs ( ( total_sum - prefix_sum ) - prefix_sum ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( minDiffSubArray ( arr , n ) ) ; } }


"Split array into two subarrays such that difference of their sum is minimum | C # program for the above approach ; Function to return minimum difference between two subarray sums ; To store prefix sums ; Generate prefix sum array ; To store suffix sums ; Generate suffix sum array ; Stores the minimum difference ; Traverse the given array ; Calculate the difference ; Update minDiff ; Return minDiff ; Driver Code ; Given array ; Length of the array"
Solution in C#:

using System ; class GFG { static int minDiffSubArray ( int [ ] arr , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int minDiff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int diff = Math . Abs ( prefix_sum [ i ] - suffix_sum [ i + 1 ] ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( minDiffSubArray ( arr , n ) ) ; } }


"Split array into two subsequences having minimum count of pairs with sum equal to X | C # program for the above approach ; Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int N , int X ) { List < int > A = new List < int > ( ) , B = new List < int > ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . Add ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . Add ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . Add ( arr [ i ] ) ; } else { B . Add ( arr [ i ] ) ; } c ++ ; } } Console . Write ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . Count ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . Count ; i ++ ) { Console . Write ( B [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = arr . Length ; solve ( arr , N , X ) ; } }


"Split first N natural numbers into two subsequences with non | C # program for the above approach ; Function to split 1 to N into two subsequences with non - coprime sums ; Driver code"
Solution in C#:

using System ; class GFG { public static void printSubsequence ( int N ) { Console . Write ( " { ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { Console . Write ( i + " , ▁ " ) ; } Console . WriteLine ( N - 1 + " ▁ } " ) ; Console . Write ( " { ▁ " + N + " ▁ } " ) ; } public static void Main ( string [ ] args ) { int N = 8 ; printSubsequence ( N ) ; } }


"Split given arrays into subarrays to maximize the sum of maximum and minimum in each subarrays | C # program for the above approach ; Function to find the maximum sum of differences of subarrays by splitting array into non - overlapping subarrays ; Stores the answer for prefix and initialize with zero ; Assume i - th index as right endpoint ; Choose the current value as the maximum and minimum ; Find the left endpoint and update the array dp [ ] ; Return answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int maxDiffSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int maxVal = arr [ i ] , minVal = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { minVal = Math . Min ( minVal , arr [ j ] ) ; maxVal = Math . Max ( maxVal , arr [ j ] ) ; if ( j - 1 >= 0 ) dp [ i ] = Math . Max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) ; else dp [ i ] = Math . Max ( dp [ i ] , maxVal - minVal ) ; } } return dp [ n - 1 ] ; } public static void Main ( ) { int [ ] arr = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . Length ; Console . WriteLine ( maxDiffSum ( arr , N ) ) ; } }


"Split given isosceles triangle of height H into N equal parts | C # Code for above approach ; Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Driver code ; Given N ; Given H ; Function call"
Solution in C#:

using System ; class GFG { static void findPoint ( int n , int h ) { for ( int i = 1 ; i < n ; i ++ ) Console . Write ( " { 0 : F2 } ▁ " , Math . Sqrt ( i / ( n * 1.0 ) ) * h ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int h = 2 ; findPoint ( n , h ) ; } }


"Split n into maximum composite numbers | C # program to split a number into maximum number of composite numbers . ; function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; Driver program"
Solution in C#:

using System ; class GFG { static int count ( int n ) { if ( n < 4 ) return - 1 ; int rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return - 1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return - 1 ; return ( n - 15 ) / 4 + 2 ; } return 0 ; } public static void Main ( ) { int n = 90 ; Console . WriteLine ( count ( n ) ) ; n = 143 ; Console . WriteLine ( count ( n ) ) ; } }


"Split numeric , alphabetic and special symbols from a String | C # program to split an alphanumeric string using stringbuffer ; Driver code"
Solution in C#:

using System ; using System . Text ; class GFG { static void splitString ( string str ) { StringBuilder alpha = new StringBuilder ( ) ; StringBuilder num = new StringBuilder ( ) ; StringBuilder special = new StringBuilder ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( Char . IsDigit ( str [ i ] ) ) num . Append ( str [ i ] ) ; else if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) alpha . Append ( str [ i ] ) ; else special . Append ( str [ i ] ) ; } Console . WriteLine ( alpha ) ; Console . WriteLine ( num ) ; Console . WriteLine ( special ) ; } public static void Main ( ) { string str = " geeks01 $ $ for02geeks03 ! @ ! ! " ; splitString ( str ) ; } }


"Split the array and add the first part to the end | C # program to split array and move first part to end . ; Function to spilt array and move first part to end ; make a temporary array with double the size ; copy array element in to new array twice ; Driver code"
Solution in C#:

using System ; class GFG { public static void SplitAndAdd ( int [ ] A , int length , int rotation ) { int [ ] tmp = new int [ length * 2 ] ; Array . Copy ( A , 0 , tmp , 0 , length ) ; Array . Copy ( A , 0 , tmp , length , length ) ; for ( int i = rotation ; i < rotation + length ; i ++ ) { A [ i - rotation ] = tmp [ i ] ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int position = 2 ; SplitAndAdd ( arr , n , position ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Split the array and add the first part to the end | C # program to split array and move first part to end . ; Rotate array by 1. ; Driver code"
Solution in C#:

using System ; class GFG { public static void splitArr ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ 0 ] ; for ( int j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } public static void Main ( ) { int [ ] arr = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int position = 2 ; splitArr ( arr , 6 , position ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }


"Split the array and add the first part to the end | Set 2 | C # program to Split the array and add the first part to the end ; Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver code ; Function calling"
Solution in C#:

using System ; class GFG { static void rvereseArray ( int [ ] arr , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void splitArr ( int [ ] arr , int k , int n ) { rvereseArray ( arr , 0 , n - 1 ) ; rvereseArray ( arr , 0 , n - k - 1 ) ; rvereseArray ( arr , n - k , n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int k = 2 ; splitArr ( arr , k , n ) ; printArray ( arr , n ) ; } }


"Split the array elements into strictly increasing and decreasing sequence | C # program to implement the above approach ; Function to print both the arrays ; Store both arrays ; Used for hashing ; Iterate for every element ; Increase the Count ; If first occurrence ; If second occurrence ; If occurs more than 2 times ; Sort in increasing order ; Print the increasing array ; Sort ; Print the decreasing array ; Driver code"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void PrintBothArrays ( int [ ] a , int n ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mpp . ContainsKey ( a [ i ] ) ) mpp [ a [ i ] ] = mpp [ a [ i ] ] + 1 ; else mpp [ a [ i ] ] = 1 ; if ( mpp [ a [ i ] ] == 1 ) v1 . Add ( a [ i ] ) ; else if ( mpp [ a [ i ] ] == 2 ) v2 . Add ( a [ i ] ) ; else { Console . WriteLine ( " Not ▁ possible " ) ; return ; } } v1 . Sort ( ) ; Console . WriteLine ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v1 . Count ; i ++ ) Console . Write ( v1 [ i ] + " ▁ " ) ; v2 . Sort ( ) ; v2 . Reverse ( ) ; Console . WriteLine ( "
 Strictly ▁ decreasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v2 . Count ; i ++ ) Console . Write ( v2 [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 7 , 2 , 7 , 3 , 3 , 1 , 4 } ; int n = a . Length ; PrintBothArrays ( a , n ) ; } }


"Split the array into odd number of segments of odd lengths | C # to check whether given array is breakable or not ; Function to check ; Check the result by processing the first & last element and size ; Driver code"
Solution in C#:

using System ; class GFG { static int checkArray ( int [ ] arr , int n ) { return ( ( arr [ 0 ] % 2 ) > 0 && ( arr [ n - 1 ] % 2 ) > 0 && ( n % 2 ) > 0 ) ? 1 : 0 ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( checkArray ( arr , n ) ) ; } }


"Split the binary string into substrings with equal number of 0 s and 1 s | C # implementation of the above approach ; Function to return the count of maximum substrings str can be divided into ; To store the count of 0 s and 1 s ; To store the count of maximum substrings str can be divided into ; It is not possible to split the string ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSubStr ( String str , int n ) { int count0 = 0 , count1 = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { count0 ++ ; } else { count1 ++ ; } if ( count0 == count1 ) { cnt ++ ; } } if ( cnt == 0 ) { return - 1 ; } return cnt ; } public static void Main ( String [ ] args ) { String str = "0100110101" ; int n = str . Length ; Console . WriteLine ( maxSubStr ( str , n ) ) ; } }


"Split the fraction into sum of multiple fractions having numerator as 1 | C # program for the above approach ; Function to split the fraction into distinct unit fraction ; To store answer ; While numerator is positive ; Finding x = ceil ( d / n ) ; Add 1 / x to list of ans ; Update fraction ; Driver code ; Given Input ; Function Call ; Print Answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < string > FractionSplit ( long n , long d ) { List < string > UnitFactions = new List < string > ( ) ; while ( n > 0 ) { long x = ( d + n - 1 ) / n ; string s = "1 / " + x . ToString ( ) ; UnitFactions . Add ( s ) ; n = n * x - d ; d = d * x ; } return UnitFactions ; } public static void Main ( string [ ] args ) { long n = 13 , d = 18 ; List < string > res = FractionSplit ( n , d ) ; foreach ( string s in res ) Console . Write ( s + " , ▁ " ) ; } }


"Split the given array into K sub | C # implementation of the above approach ; Function to check if mid can be maximum sub - arrays sum ; If individual element is greater maximum possible sum ; Increase sum of current sub - array ; If the sum is greater than mid increase count ; Check condition ; Function to find maximum subarray sum which is minimum ; start = array [ i ] ; Max subarray sum , considering subarray of length 1 ; end += array [ i ] ; Max subarray sum , considering subarray of length n ; Answer stores possible maximum sub array sum ; If mid is possible solution Put answer = mid ; ; Driver Code"
Solution in C#:

using System ; class GFG { static Boolean check ( int mid , int [ ] array , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } static int solve ( int [ ] array , int n , int K ) { int start = 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( array [ i ] > start ) } int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] array = { 1 , 2 , 3 , 4 } ; int n = array . Length ; int K = 3 ; Console . WriteLine ( solve ( array , n , K ) ) ; } }


"Split the given string into Odds : Digit DP | C # program to split the given string into odds ; Function to check whether a string is an odd number or not ; A function to find the minimum number of segments the given string can be divided such that every segment is a odd number ; Declare a splitdp [ ] array and initialize to - 1 ; Build the DP table in a bottom - up manner ; Initially Check if the entire prefix is odd ; If the Given Prefix can be split into Odds then for the remaining string from i to j Check if Odd . If yes calculate the minimum split till j ; To check if the substring from i to j is a odd number or not ; If it is an odd number , then update the dp array ; Return the minimum number of splits for the entire string ; Driver code"
Solution in C#:

using System ; class GFG { static int checkOdd ( string number ) { int n = number . Length ; int num = number [ n - 1 ] - '0' ; return ( num & 1 ) ; } static int splitIntoOdds ( string number ) { int numLen = number . Length ; int [ ] splitDP = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . Substring ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . Substring ( i , j ) ) == 1 ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . Min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void Main ( string [ ] args ) { Console . WriteLine ( splitIntoOdds ( "123456789123456789123" ) ) ; } }


"Split the given string into Primes : Digit DP | C # implementation of the above approach ; Function to check whether a String is a prime number or not ; A recursive function to find the minimum number of segments the given String can be divided such that every segment is a prime ; If the number is null ; checkPrime function is called to check if the number is a prime or not . ; A very large number denoting maximum ; Consider a minimum of 6 and length since the primes are less than 10 ^ 6 ; Recursively call the function to check for the remaining String ; Evaluating minimum splits into Primes for the suffix ; Checks if no combination found ; Driver code"
Solution in C#:

 using System ; class GFG { static bool checkPrime ( String number ) { int num = Int32 . Parse ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { if ( number . Length == 0 ) return 0 ; if ( number . Length <= 6 && checkPrime ( number ) ) return 1 ; else { int numLen = number . Length ; int ans = 1000000 ; for ( int i = 1 ; i <= 6 && i <= numLen ; i ++ ) { if ( checkPrime ( number . Substring ( 0 , i ) ) ) { int val = splitIntoPrimes ( number . Substring ( i ) ) ; if ( val != - 1 ) { ans = Math . Min ( ans , 1 + val ) ; } } } if ( ans == 1000000 ) return - 1 ; return ans ; } } public static void Main ( String [ ] args ) { Console . Write ( splitIntoPrimes ( "13499315" ) + "
 " ) ; Console . Write ( splitIntoPrimes ( "43" ) + "
 " ) ; } }


"Split the given string into Primes : Digit DP | C # implementation of the above approach ; Function to precompute all the primes upto 1000000 and store it in a set using Sieve of Eratosthenes ; Converting int to String ; A function to find the minimum number of segments the given String can be divided such that every segment is a prime ; Declare a splitdp [ ] array and initialize to - 1 ; Call sieve function to store primes in primes array ; Build the DP table in a bottom - up manner ; If the prefix is prime then the prefix will be found in the prime set ; If the given prefix can be split into primes , then for the remaining String from i to j check if prime . If yes calculate the minimum split till j ; To check if the subString from i to j is a prime number or not ; If it is a prime , then update the dp array ; Return the minimum number of splits for the entire String ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void getPrimesFromSeive ( HashSet < String > primes ) { bool [ ] prime = new bool [ 1000001 ] ; for ( int i = 0 ; i < 1000001 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * i ; j <= 1000000 ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= 1000000 ; i ++ ) { if ( prime [ i ] == true ) primes . Add ( String . Join ( " " , i ) ) ; } } static int splitIntoPrimes ( String number ) { int numLen = number . Length ; int [ ] splitDP = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; HashSet < String > primes = new HashSet < String > ( ) ; getPrimesFromSeive ( primes ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && ( primes . Contains ( number . Substring ( 0 , i ) ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( primes . Contains ( number . Substring ( i , j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . Min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void Main ( String [ ] args ) { Console . Write ( splitIntoPrimes ( "13499315" ) + "
 " ) ; Console . Write ( splitIntoPrimes ( "43" ) + "
 " ) ; } }


"Split the number N by maximizing the count of subparts divisible by K | C # program to split the number N by maximizing the count of subparts divisible by K ; Function to count the subparts ; Total subStr till now ; If it can be divided , then this subString is one of the possible answer ; Convert String to long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver code"
Solution in C#:

 using System ; class GFG { static int count ( String N , int X , String subStr , int index , int n ) { if ( index == n ) return 0 ; String a = subStr + N [ index ] ; int b = 0 ; if ( long . Parse ( a ) % X == 0 ) b = 1 ; int m1 = count ( N , X , a , index + 1 , n ) ; int m2 = b + count ( N , X , " " , index + 1 , n ) ; return Math . Max ( m1 , m2 ) ; } public static void Main ( String [ ] args ) { String N = "00001242" ; int K = 3 ; int l = N . Length ; Console . Write ( count ( N , K , " " , 0 , l ) + "
 " ) ; } }


"Split the number into N parts such that difference between the smallest and the largest part is minimum | C # implementation of the approach ; Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code"
Solution in C#:

using System ; public class GFG { static void split ( int x , int n ) { if ( x < n ) Console . WriteLine ( " - 1 ▁ " ) ; else if ( x % n == 0 ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( x / n ) + " ▁ " ) ; } else { int zp = n - ( x % n ) ; int pp = x / n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i >= zp ) Console . Write ( ( pp + 1 ) + " ▁ " ) ; else Console . Write ( pp + " ▁ " ) ; } } } static public void Main ( ) { int x = 5 ; int n = 3 ; split ( x , n ) ; } }


"Split the string into substrings using delimiter | C # implementation to split String into substrings on the basis of delimiter ; function to split String into subStrings on the basis of delimiter and return the subStrings after split ; to count the number of split Strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty String , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted Strings ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < String > splitStrings ( String str , char dl ) { String word = " " ; int num = 0 ; str = str + dl ; int l = str . Length ; List < String > substr_list = new List < String > ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) { word = word + str [ i ] ; } else { if ( ( int ) word . Length != 0 ) { substr_list . Add ( word ) ; } word = " " ; } } return substr_list ; } public static void Main ( ) { String str = " geeks ; for ; geeks " ; char dl = ' ; ' ; List < String > res = splitStrings ( str , dl ) ; foreach ( String x in res ) { Console . WriteLine ( x ) ; } } }


"Splitting a Numeric String | C # program to split a numeric string in an Increasing sequence if possible ; Function accepts a string and checks if string can be split . ; if there is only 1 number in the string then it is not possible to split it ; storing the substring from 0 to i + 1 to form initial number of the increasing sequence ; convert string to integer and add 1 and again convert back to string s2 ; if s2 is not a substring of number than not possible ; if s2 is the next substring of the numeric string ; Incearse num2 by 1 i . e the next number to be looked for ; check if string is fully traversed then break ; If next string doesnot occurs in a given numeric string then it is not possible ; if the string was fully traversed and conditions were satisfied ; if conditions failed to hold ; Driver Code ; Call the split function for splitting the string"
Solution in C#:

using System ; class GFG { static void split ( string str ) { int len = str . Length ; if ( len == 1 ) { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } string s1 = " " , s2 = " " ; long num1 , num2 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int flag = 0 ; s1 = str . Substring ( 0 , i + 1 ) ; num1 = Convert . ToInt64 ( ( s1 ) ) ; num2 = num1 + 1 ; s2 = num2 . ToString ( ) ; int k = i + 1 ; while ( flag == 0 ) { int l = s2 . Length ; if ( k + l > len ) { flag = 1 ; break ; } if ( ( str . Substring ( k , l ) . Equals ( s2 ) ) ) { flag = 0 ; num2 ++ ; k = k + l ; if ( k == len ) break ; s2 = num2 . ToString ( ) ; l = s2 . Length ; if ( k + 1 > len ) { flag = 1 ; break ; } } else flag = 1 ; } if ( flag == 0 ) { Console . WriteLine ( " Possible " + " ▁ " + s1 ) ; break ; } else if ( flag == 1 && i > len / 2 - 1 ) { Console . WriteLine ( " Not ▁ Possible " ) ; break ; } } } static void Main ( ) { string str = "99100" ; split ( str ) ; } }


"Splitting starting N nodes into new Circular Linked List while preserving the old nodes | C # implementation of the approach ; Function to add a node to the empty list ; If not empty ; Creating a node dynamically ; Assigning the data ; Creating the link ; Function to add a node to the beginning of the list ; If list is empty ; Create node ; Assign data ; Function to traverse and print the list ; If list is empty ; Pointing to the first Node of the list ; Traversing the list ; Function to find the length of the CircularLinkedList ; Stores the length ; List is empty ; Iterator Node to traverse the List ; Return the length of the list ; Function to split the first k nodes into a new CircularLinkedList and the remaining nodes stay in the original CircularLinkedList ; Empty Node for reference ; Check if the list is empty If yes , then return null ; NewLast will contain the last node of the new split list itr to iterate the node till the required node ; Update NewLast to the required node and link the last to the start of rest of the list ; Return the last node of the required list ; Driver code ; Create a new list for the starting k nodes ; Append the new last node into the new list ; Print the new lists"
Solution in C#:

using System ; public class CircularLinkedList { public Node last ; public class Node { public int data ; public Node next ; } ; public Node addToEmpty ( int data ) { if ( this . last != null ) return this . last ; Node temp = new Node ( ) ; temp . data = data ; this . last = temp ; this . last . next = this . last ; return last ; } public Node addBegin ( int data ) { if ( last == null ) return addToEmpty ( data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = this . last . next ; this . last . next = temp ; return this . last ; } public void traverse ( ) { Node p ; if ( this . last == null ) { Console . WriteLine ( " List ▁ is ▁ empty . " ) ; return ; } p = this . last . next ; do { Console . Write ( p . data + " ▁ " ) ; p = p . next ; } while ( p != this . last . next ) ; Console . WriteLine ( " " ) ; } public int length ( ) { int x = 0 ; if ( this . last == null ) return x ; Node itr = this . last . next ; while ( itr . next != this . last . next ) { x ++ ; itr = itr . next ; } return ( x + 1 ) ; } public Node split ( int k ) { Node pass = new Node ( ) ; if ( this . last == null ) return this . last ; Node newLast , itr = this . last ; for ( int i = 0 ; i < k ; i ++ ) { itr = itr . next ; } newLast = itr ; pass . next = itr . next ; newLast . next = this . last . next ; this . last . next = pass . next ; return newLast ; } public static void Main ( String [ ] args ) { CircularLinkedList clist = new CircularLinkedList ( ) ; clist . last = null ; clist . addToEmpty ( 12 ) ; clist . addBegin ( 10 ) ; clist . addBegin ( 8 ) ; clist . addBegin ( 6 ) ; clist . addBegin ( 4 ) ; clist . addBegin ( 2 ) ; Console . WriteLine ( " Original ▁ list : " ) ; clist . traverse ( ) ; int k = 4 ; CircularLinkedList clist2 = new CircularLinkedList ( ) ; clist2 . last = clist . split ( k ) ; Console . WriteLine ( " The ▁ new ▁ lists ▁ are : " ) ; clist2 . traverse ( ) ; clist . traverse ( ) ; } }


"Spt function or Smallest Parts Function of a given number | C # implementation to find the Spt Function to given number ; Variable to store spt function of a number ; Find the value of frequency of minimum element ; Function to add value of frequency of minimum element among all representations of n ; Find the value of frequency of minimum element ; Calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; If sum becomes n , consider this representation ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int spt = 0 ; static int count ( List < int > arr , int min_i ) { int count = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) if ( min_i == arr [ i ] ) count ++ ; return count ; } static void printList ( List < int > arr ) { int min_i = int . MaxValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) min_i = Math . Min ( min_i , arr [ i ] ) ; int freq = count ( arr , min_i ) ; spt += freq ; } static void findWays ( List < int > arr , int i , int n ) { if ( n == 0 ) printList ( arr ) ; for ( int j = i ; j <= n ; j ++ ) { arr . Add ( j ) ; findWays ( arr , j , n - j ) ; arr . RemoveAt ( arr . Count - 1 ) ; } } static void spt_function ( int n ) { List < int > arr = new List < int > ( ) ; findWays ( arr , 1 , n ) ; Console . Write ( spt ) ; } public static void Main ( String [ ] args ) { int N = 4 ; spt_function ( N ) ; } }


"Sqrt ( or Square Root ) Decomposition Technique | Set 1 ( Introduction ) | C # program to demonstrate working of Square Root Decomposition . ; original array ; decomposed array ; block size ; Time Complexity : O ( 1 ) ; Time Complexity : O ( sqrt ( n ) ) ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Fills values in input [ ] ; initiating block pointer ; calculating size of block ; building the decomposed array ; entering next block incementing block pointer ; Driver code ; We have used separate array for input because the purpose of this code is to explain SQRT decomposition in competitive programming where we have multiple inputs ."
Solution in C#:

using System ; class GFG { static int MAXN = 10000 ; static int SQRSIZE = 100 ; static int [ ] arr = new int [ MAXN ] ; static int [ ] block = new int [ SQRSIZE ] ; static int blk_sz ; static void update ( int idx , int val ) { int blockNumber = idx / blk_sz ; block [ blockNumber ] += val - arr [ idx ] ; arr [ idx ] = val ; } static int query ( int l , int r ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { sum += arr [ l ] ; l ++ ; } while ( l + blk_sz <= r ) { sum += block [ l / blk_sz ] ; l += blk_sz ; } while ( l <= r ) { sum += arr [ l ] ; l ++ ; } return sum ; } static void preprocess ( int [ ] input , int n ) { int blk_idx = - 1 ; blk_sz = ( int ) Math . Sqrt ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = input [ i ] ; if ( i % blk_sz == 0 ) { blk_idx ++ ; } block [ blk_idx ] += arr [ i ] ; } } public static void Main ( String [ ] args ) { int [ ] input = { 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 } ; int n = input . Length ; preprocess ( input , n ) ; Console . WriteLine ( " query ( 3 , ▁ 8 ) ▁ : ▁ " + query ( 3 , 8 ) ) ; Console . WriteLine ( " query ( 1 , ▁ 6 ) ▁ : ▁ " + query ( 1 , 6 ) ) ; update ( 8 , 0 ) ; Console . WriteLine ( " query ( 8 , ▁ 8 ) ▁ : ▁ " + query ( 8 , 8 ) ) ; } }


"Square Free Number | C # Program to print all prime factors ; Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver program"
Solution in C#:

using System ; class GFG { static bool isSquareFree ( int n ) { if ( n % 2 == 0 ) n = n / 2 ; if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { n = n / i ; if ( n % i == 0 ) return false ; } } return true ; } public static void Main ( ) { int n = 10 ; if ( isSquareFree ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Square of large number represented as String | C # program to multiply two numbers represented as strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; generate the result string ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static String multiply ( String num1 , String num2 ) { int n1 = num1 . Length ; int n2 = num2 . Length ; if ( n1 == 0 n2 == 0 ) return "0" ; int [ ] result = new int [ n1 + n2 ] ; int i_n1 = 0 ; int i_n2 = 0 ; int i = 0 ; for ( i = n1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n_1 = num1 [ i ] - '0' ; i_n2 = 0 ; for ( int j = n2 - 1 ; j >= 0 ; j -- ) { int n_2 = num2 [ j ] - '0' ; int sum = n_1 * n_2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } i = result . Length - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == - 1 ) return "0" ; String s = " " ; while ( i >= 0 ) s += ( result [ i -- ] ) . ToString ( ) ; return s ; } public static void Main ( String [ ] args ) { String str1 = "454545454545454545" ; Console . WriteLine ( multiply ( str1 , str1 ) ) ; } }


"Square root of a number by Repeated Subtraction method | C # implementation of the above approach ; Function to return the square root of the given number ; Subtract n - th odd number ; Return the result ; Driver code"
Solution in C#:

using System ; class GFG { public static int SquareRoot ( int num ) { int count = 0 ; for ( int n = 1 ; n <= num ; n += 2 ) { num = num - n ; count += 1 ; if ( num == 0 ) break ; } return count ; } public static void Main ( ) { int N = 81 ; Console . Write ( SquareRoot ( N ) ) ; } }


"Square root of a number using log | C # program to demonstrate finding square root of a number using sqrt ( )"
Solution in C#:

using System ; class GFG { public static void Main ( ) { double n = 12 ; Console . Write ( Math . Sqrt ( n ) ) ; } }


"Square root of a number without using sqrt ( ) function | C # implementation of the approach ; Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code"
Solution in C#:

using System ; class GFG { static double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( Math . Abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } static void findSqrt ( double n ) { double i = 1 ; Boolean found = false ; while ( ! found ) { if ( i * i == n ) { Console . WriteLine ( i ) ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; Console . Write ( " { 0 : F5 } " , res ) ; found = true ; } i ++ ; } } public static void Main ( String [ ] args ) { double n = 3 ; findSqrt ( n ) ; } }


"Square root of an integer | A C # program to find floor ( sqrt ( x ) ) ; Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver Code"
Solution in C#:

using System ; class GFG { static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } static public void Main ( ) { int x = 11 ; Console . WriteLine ( floorSqrt ( x ) ) ; } }


"Square root of an integer | A C # program to find floor ( sqrt ( x ) ; Base Cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code"
Solution in C#:

using System ; class GFG { public static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int start = 1 , end = x , ans = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid * mid == x ) return mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } else end = mid - 1 ; } return ans ; } static public void Main ( ) { int x = 11 ; Console . WriteLine ( floorSqrt ( x ) ) ; } }


"Squared triangular number ( Sum of cubes ) | C # program to check if a given number is sum of cubes of natural numbers . ; Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code"
Solution in C#:

using System ; class GFG { public static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; } static public void Main ( string [ ] args ) { int s = 9 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }


"Squared triangular number ( Sum of cubes ) | C # program to check if a given number is sum of cubes of natural numbers . ; Returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integerroot exists ) . Else returns - 1. ; Considering the equation n * ( n + 1 ) / 2 = num . The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int isTriangular ( int num ) { if ( num < 0 ) return 0 ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return - 1 ; double root1 = ( - b + Math . Sqrt ( d ) ) / ( 2 * a ) ; double root2 = ( - b - Math . Sqrt ( d ) ) / ( 2 * a ) ; if ( ( int ) ( root1 ) > 0 && ( int ) ( Math . Floor ( root1 ) ) == ( int ) ( root1 ) ) return ( int ) ( root1 ) ; if ( ( int ) ( root2 ) > 0 && ( int ) ( Math . Floor ( root2 ) ) == ( int ) ( root2 ) ) return ( int ) ( root2 ) ; return - 1 ; } static int isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; if ( ( sr - Math . Floor ( sr ) ) == 0 ) return ( int ) ( Math . Floor ( sr ) ) ; else return - 1 ; } static int findS ( int s ) { int sr = isPerfectSquare ( s ) ; if ( sr == - 1 ) return - 1 ; return isTriangular ( sr ) ; } public static void Main ( ) { int s = 9 ; int n = findS ( s ) ; if ( n == - 1 ) Console . Write ( " - 1" ) ; else Console . Write ( n ) ; } }


"Squareroot ( n ) | C # program to find sqrt ( n ) 'th node  of a linked list  ; Linked list node ; Function to get the sqrt ( n ) th node of a linked list ; Traverse the list ; check if j = sqrt ( i ) ; for first node ; increment j if j = sqrt ( i ) ; return node 's data  ; function to add a new node at the beginning of the list ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver code ; Start with the empty list"
Solution in C#:

using System ; public class GfG { class Node { public int data ; public Node next ; } static Node head = null ; static int printsqrtn ( Node head ) { Node sqrtn = null ; int i = 1 , j = 1 ; while ( head != null ) { if ( i == j * j ) { if ( sqrtn == null ) sqrtn = head ; else sqrtn = sqrtn . next ; j ++ ; } i ++ ; head = head . next ; } return sqrtn . data ; } static void print ( Node head ) { while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; head = head . next ; } Console . WriteLine ( ) ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } public static void Main ( String [ ] args ) { push ( 40 ) ; push ( 30 ) ; push ( 20 ) ; push ( 10 ) ; Console . Write ( " Given ▁ linked ▁ list ▁ is : " ) ; print ( head ) ; Console . Write ( " sqrt ( n ) th ▁ node ▁ is ▁ " + printsqrtn ( head ) ) ; } }


"Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void print ( List < int > a , int n ) { for ( int i = 0 ; i <= n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void sort ( List < int > a , int n ) { for ( int i = n ; i >= 0 ; i -- ) for ( int j = n ; j > n - i ; j -- ) if ( a [ j ] > a [ j - 1 ] ) { int tempswap = a [ j ] ; a [ j ] = a [ j - 1 ] ; a [ j - 1 ] = tempswap ; } print ( a , n ) ; } static void Main ( ) { int n = 6 ; List < int > a = new List < int > ( ) ; a . Add ( 2 ) ; a . Add ( 4 ) ; a . Add ( 3 ) ; a . Add ( 2 ) ; a . Add ( 4 ) ; a . Add ( 5 ) ; a . Add ( 3 ) ; sort ( a , n ) ; } }


"Stable sort for descending order | C # program to demonstrate descending order ; Driver code"
Solution in C#:

 using System ; class GFG { static void reverse ( int [ ] a ) { int i , k , n = a . Length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . Length ; Array . Sort ( arr ) ; reverse ( arr ) ; Console . WriteLine ( " Array ▁ after ▁ sorting ▁ : ▁
 " ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }


"Stein 's Algorithm for finding GCD | Iterative C # program to implement Stein 's Algorithm ; Function to implement Stein 's  Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; Finding K , where K is the greatest power of 2 that divides both a and b ; Dividing a by 2 until a becomes odd ; From here on , ' a ' is always odd ; If b is even , remove all factor of 2 in b ; Now a and b are both odd . Swap if necessary so a <= b , then set b = b - a ( which is even ) . ; restore common factors of 2 ; Driver code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a b ) & 1 ) == 0 ; ++ k ) { a >>= 1 ; b >>= 1 ; } while ( ( a & 1 ) == 0 ) a >>= 1 ; do { while ( ( b & 1 ) == 0 ) b >>= 1 ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } b = ( b - a ) ; } while ( b != 0 ) ; return a << k ; } public static void Main ( ) { int a = 34 , b = 17 ; Console . Write ( " Gcd ▁ of ▁ given ▁ " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }