"Minimize length by removing subsequences forming valid parenthesis from a given string | C # program for the above approach ; Function to find the minimum count of remaining characters left into the string by removing the valid subsequences ; Length of the string ; Stores opening parenthesis ' ( ' of the given string ; Stores square parenthesis ' [ ' of the given string ; Stores count of opening parenthesis ' ( ' in valid subsequences ; Stores count of opening parenthesis ' [ ' in valid subsequences ; Iterate over each characters of S ; If current character is ' [ ' ; insert into stack ; If i is equal to ' ] ' ; If stack is not empty and top element of stack is ' [ ' ; Remove top element from stack ; Update squareCount ; If current character is ' ( ' ; Insert into stack ; If i is equal to ' ) ' ; If stack is not empty and top element of stack is ' ( ' ; Remove top element from stack ; Update roundCount ; Print the minimum number of remaining characters left into S ; Driver code ; input string ; function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void deleteSubseq ( String s ) { int N = s . Length ; Stack < char > roundStk = new Stack < char > ( ) ; Stack < char > squareStk = new Stack < char > ( ) ; int roundCount = 0 ; int squareCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' [ ' ) { squareStk . Push ( s [ i ] ) ; } else if ( s [ i ] == ' ] ' ) { if ( squareStk . Count != 0 && squareStk . Peek ( ) == ' [ ' ) { squareStk . Pop ( ) ; squareCount += 1 ; } } else if ( s [ i ] == ' ( ' ) { roundStk . Push ( s [ i ] ) ; } else { if ( roundStk . Count != 0 && squareStk . Peek ( ) == ' ( ' ) { squareStk . Pop ( ) ; roundCount += 1 ; } } } Console . WriteLine ( N - ( 2 * squareCount + 2 * roundCount ) ) ; } public static void Main ( String [ ] args ) { String s = " [ ] ] ) ( [ " ; deleteSubseq ( s ) ; } }


"Minimize length of Substrings containing at least one common Character | C # program to implement the above approach ; Function to check and return if subStrings of length mid has a common character a ; Length of the String ; Initialise the first occurrence of character a ; Check that distance b / w the current and previous occurrence of character a is less than or equal to mid ; If distance exceeds mid ; Function to check for all the alphabets , if subStrings of length mid have a character common ; Check for all 26 alphabets ; Check that char i + a is common in all the subStrings of length mid ; If no characters is common ; Function to calculate and return the minm length of subStrings ; Initialise low and high ; Perform binary search ; Update mid ; Check if one common character is present in the length of the mid ; Returns the minimum length that contain one common character ; Function to check if all characters are distinct ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static bool check ( char [ ] str , int mid , char a ) { int n = str . Length ; int previous = - 1 , i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] == a ) { if ( i - previous > mid ) { return false ; } previous = i ; } } if ( i - previous > mid ) return false ; else return true ; } static bool possible ( char [ ] str , int mid ) { for ( int i = 0 ; i < 26 ; ++ i ) { if ( check ( str , mid , ( char ) ( i + ' a ' ) ) ) return true ; } return false ; } static int findMinLength ( char [ ] str ) { int low = 1 , high = str . Length ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( possible ( str , mid ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } static bool ifAllDistinct ( String str ) { HashSet < char > s = new HashSet < char > ( ) ; foreach ( char c in str . ToCharArray ( ) ) { s . Add ( c ) ; } return s . Count == str . Length ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( ifAllDistinct ( str ) ) Console . Write ( - 1 + "
 " ) ; else Console . Write ( findMinLength ( str . ToCharArray ( ) ) ) ; } }


"Minimize length of a given string by removing subsequences forming valid parenthesis | C # program of the above approach ; Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Driver code ; Given string ; Size of the string ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void removeValidBracketSequences ( string str , int N ) { List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; List < int > C = new List < int > ( ) ; bool [ ] vis = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ( ' ) { A . Add ( i ) ; } else if ( str [ i ] == ' { ' ) { B . Add ( i ) ; } else if ( str [ i ] == ' [ ' ) { C . Add ( i ) ; } else if ( str [ i ] == ' ) ' && ( A . Count > 0 ) ) { vis [ A [ A . Count - 1 ] ] = false ; A . RemoveAt ( A . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' } ' && ( B . Count > 0 ) ) { vis [ B [ B . Count - 1 ] ] = false ; B . RemoveAt ( B . Count - 1 ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ] ' && ( C . Count > 0 ) ) { vis [ C [ C . Count - 1 ] ] = false ; C . RemoveAt ( C . Count - 1 ) ; vis [ i ] = false ; } } for ( int i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) Console . Write ( str [ i ] ) ; } } static void Main ( ) { string str = " ( ( ) { ( ) ( { } ) " ; int N = str . Length ; removeValidBracketSequences ( str , N ) ; } }


"Minimize length of a string by removing occurrences of another string from it as a substring | C # program for the above approach ; Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Print the readonly answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required"
Solution in C#:

using System ; class GFG { static void minLength ( String S , String T , int N , int M ) { String temp = " " ; int subtract = 0 ; for ( int i = 0 ; i < N ; ++ i ) { temp += S [ i ] ; if ( temp . Length >= M ) { if ( T . Equals ( temp . Substring ( temp . Length - M , M ) ) ) { subtract += M ; int cnt = 0 ; while ( cnt != M ) { temp = temp . Substring ( 0 , temp . Length - 1 ) ; ++ cnt ; } } } } Console . WriteLine ( ( N - subtract ) ) ; } public static void Main ( String [ ] args ) { String S = " aabcbcbd " , T = " abc " ; int N = S . Length ; int M = T . Length ; minLength ( S , T , N , M ) ; } }


"Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | C # program for the above approach ; Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int minLength ( String S ) { Stack < char > st = new Stack < char > ( ) ; foreach ( char ch in S . ToCharArray ( ) ) { if ( st . Count == 0 ) st . Push ( ch ) ; else { char top = st . Peek ( ) ; if ( Math . Abs ( ch - top ) == 1 ) st . Pop ( ) ; else { st . Push ( ch ) ; } } } return st . Count ; } public static void Main ( String [ ] args ) { String S = "12213" ; Console . WriteLine ( minLength ( S ) ) ; } }


"Minimize length of a string by removing suffixes and prefixes of same characters | C # program for the above approach ; Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code"
Solution in C#:

using System ; class GFG { static int minLength ( string s ) { int i = 0 , j = s . Length - 1 ; for ( ; i < j && s [ i ] == s [ j ] ; ) { char d = s [ i ] ; while ( i <= j && s [ i ] == d ) i ++ ; while ( i <= j && s [ j ] == d ) j -- ; } return j - i + 1 ; } public static void Main ( string [ ] args ) { string S = " aacbcca " ; Console . WriteLine ( minLength ( S ) ) ; } }


"Minimize length of an array by removing similar subarrays from both ends | C # program for the above approach ; Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Driver Code ; Input ; Function call to find the minimized length of the array"
Solution in C#:

using System ; public class GFG { static void findMinLength ( int [ ] arr , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } Console . WriteLine ( back - front + 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = arr . Length ; findMinLength ( arr , N ) ; } }


"Minimize length of prefix of string S containing all characters of another string T | C # program for the above approach ; Base Case - if T is empty , it matches 0 length prefix ; Convert strings to lower case for uniformity ; Update dictCount to the letter count of T ; If new character is found , initialize its entry , and increase nUnique ; Increase count of ch ; Iterate from 0 to N ; i - th character ; Skip if ch not in targetStr ; Decrease Count ; If the count of ch reaches 0 , we do not need more ch , and can decrease nUnique ; If nUnique reaches 0 , we have found required prefix ; Otherwise ; Driver code"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static int getPrefixLength ( string srcStr , string targetStr ) { if ( targetStr . Length == 0 ) return 0 ; srcStr = srcStr . ToLower ( ) ; targetStr = targetStr . ToLower ( ) ; Dictionary < char , int > dictCount = new Dictionary < char , int > ( ) ; int nUnique = 0 ; foreach ( var ch in targetStr ) { if ( dictCount . ContainsKey ( ch ) != true ) { nUnique += 1 ; dictCount [ ch ] = 0 ; } dictCount [ ch ] += 1 ; } for ( int i = 0 ; i < srcStr . Length ; i ++ ) { char ch = srcStr [ i ] ; if ( dictCount . ContainsKey ( ch ) != true ) continue ; dictCount [ ch ] -= 1 ; if ( dictCount [ ch ] == 0 ) nUnique -= 1 ; if ( nUnique == 0 ) return ( i + 1 ) ; } return - 1 ; } public static void Main ( ) { string S = " MarvoloGaunt " ; string T = " Tom " ; Console . Write ( getPrefixLength ( S , T ) ) ; } }


"Minimize length of string by replacing K pairs of distinct adjacent characters | C # program to implement the above approach ; Function to minimize the length of the String by replacing distinct pairs of adjacent characters ; Stores the length of the given String ; Stores the index of the given String ; Traverse the given String ; If two adjacent characters are distinct ; If all characters are equal ; If all characters are distinct ; Driver Code"
Solution in C#:

using System ; class GFG { static int MinLen ( String str , int K ) { int N = str . Length ; int i = 0 ; while ( i < N - 1 ) { if ( str [ i ] != str [ i + 1 ] ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . Max ( 1 , N - K ) ; } public static void Main ( String [ ] args ) { String str = " aabc " ; int K = 1 ; Console . Write ( MinLen ( str , K ) ) ; } }


"Minimize maximum array element by splitting array elements into powers of two at most K times | C # program for the above approach ; Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver code"
Solution in C#:

using System ; class GFG { static void minimumSize ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; if ( count ( arr , 0 ) == N ) Console . WriteLine ( 0 ) ; else if ( K >= N ) Console . WriteLine ( 1 ) ; else Console . WriteLine ( arr [ K ] ) ; } static void reverse ( int [ ] a ) { int i , t ; int n = a . Length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int count ( int [ ] a , int n ) { int freq = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = arr . Length ; minimumSize ( arr , N , K ) ; } }


"Minimize maximum array element possible by at most K splits on the given array | C # program for the above approach ; Function to check if all array elements can be reduced to at most mid by at most K splits ; Stores the number of splits required ; Traverse the array arr [ ] ; Update count ; If possible , return true . Otherwise return false ; Function to find the minimum possible value of maximum array element that can be obtained by at most K splits ; Set lower and upper limits ; Perform Binary Search ; Calculate mid ; Check if all array elements can be reduced to at most mid value by at most K splits ; Update the value of hi ; Otherwise ; Update the value of lo ; Return the minimized maximum element in the array ; Driver Code"
Solution in C#:

using System ; class GFG { static bool possible ( int [ ] A , int N , int mid , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += ( A [ i ] - 1 ) / mid ; } return count <= K ; } static int minimumMaximum ( int [ ] A , int N , int K ) { int lo = 1 ; Array . Sort ( A ) ; int hi = A [ N - 1 ] ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 2 } ; int K = 4 ; int N = arr . Length ; Console . WriteLine ( minimumMaximum ( arr , N , K ) ) ; } }


"Minimize maximum difference between adjacent elements possible by removing a single array element | C # program to implement the above approach ; Function to find maximum difference between adjacent array elements ; Store the maximum difference ; Traverse the array ; Update maximum difference ; Function to calculate the minimum of maximum difference between adjacent array elements possible by removing a single array element ; Stores the required minimum ; Stores the updated array ; Skip the i - th element ; Update MinValue ; Return MinValue ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxAdjacentDifference ( List < int > A ) { int diff = 0 ; for ( int i = 1 ; i < A . Count ; i ++ ) { diff = Math . Max ( diff , A [ i ] - A [ i - 1 ] ) ; } return diff ; } static int MinimumValue ( int [ ] arr , int N ) { int MinValue = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { List < int > new_arr = new List < int > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; new_arr . Add ( arr [ j ] ) ; } MinValue = Math . Min ( MinValue , maxAdjacentDifference ( new_arr ) ) ; } return MinValue ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 7 , 8 } ; int N = arr . Length ; Console . WriteLine ( MinimumValue ( arr , N ) ) ; } }


"Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 | Function to find the minimum number of increment and decrement of pairs required to make all array elements equal ; Stores the sum of the array ; If sum is not divisible by N ; Update sum ; Store the minimum number of operations ; Iterate while i is less than N ; Add absolute difference of current element with k to ans ; Increase i bye 1 ; Return the value in ans2 ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int find ( List < int > arr , int N ) { int Sum = 0 ; foreach ( int item in arr ) Sum += item ; if ( Sum % N == 1 ) return - 1 ; int k = Sum / N ; int ans = 0 ; int i = 0 ; while ( i < N ) { ans = ans + Math . Abs ( k - arr [ i ] ) ; i += 1 ; } return ans / 2 ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 5 , 4 , 1 , 10 } ; int N = arr . Count ; Console . Write ( find ( arr , N ) ) ; } }


"Minimize number of boxes by putting small box inside bigger one | C # implementation to minimize the number of the box by putting small box inside the bigger one ; Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; Check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { public static void minBox ( int [ ] arr , int n ) { int box = n ; Array . Sort ( arr ) ; int curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } Console . WriteLine ( box ) ; } public static void Main ( String [ ] args ) { int [ ] size = { 1 , 2 , 3 } ; int n = size . Length ; minBox ( size , n ) ; } }


"Minimize operations of removing 2 i | C # program to implement the above approach ; Function to find minimum count of steps required to remove all the array elements ; Stores minimum count of steps required to remove all the array elements ; Update N ; Traverse each bit of N ; If current bit is set ; Update cntStep ; Driver code"
Solution in C#:

using System ; class GFG { static int minimumStepReqArr ( int [ ] arr , int N ) { int cntStep = 0 ; N += 1 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( N & ( 1 << i ) ) != 0 ) { cntStep += 1 ; } } return cntStep ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . Length ; Console . WriteLine ( minimumStepReqArr ( arr , N ) ) ; } }


"Minimize operations required to obtain N | Function to find the minimum number of operations ; Storing the minimum operations to obtain all numbers up to N ; Initilal state ; Iterate for the remaining numbers ; If the number can be obtained by multiplication by 2 ; If the number can be obtained by multiplication by 3 ; Obtaining the number by adding 1 ; Return the minm operations to obtain n ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int n ) { int [ ] dp = new int [ n + 1 ] ; int x ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = int . MaxValue ; if ( i % 2 == 0 ) { x = dp [ i / 2 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { x = dp [ i / 3 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } public static void Main ( string [ ] args ) { int n = 15 ; Console . Write ( minOperations ( n ) ) ; } }


"Minimize operations to convert each node of N | C # program for the above approach ; Create adjacency list ; Function to add an edges in graph ; Function to perform the DFS of graph recursively from a given vertex u ; Check for the condition for the flipping of node 's initial value ; Traverse all the children of the current source node u ; Swap foo and foo1 signifies there is change of level ; Function to perform the DFSUtil ( ) for all the unvisited vertices ; Traverse the given set of nodes ; If the current node is unvisited ; Print the number of operations ; Function to count the number of flips required to change initial node values to final node value ; Add the given edges ; DFS Traversal ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int N = 3 ; static List < List < int > > adj = new List < List < int > > ( ) ; static List < bool > visited ; static int ans ; static void addEdges ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void DFSUtil ( int u , bool foo , bool foo1 , bool [ ] initial , bool [ ] finall ) { visited [ u ] = true ; if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == true ) { ans += 1 ; foo ^= true ; } for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( visited [ adj [ u ] [ i ] ] == false ) DFSUtil ( adj [ u ] [ i ] , foo1 , foo , initial , finall ) ; } } static void DFS ( int V , bool [ ] initial , bool [ ] finall ) { ans = 0 ; visited = new List < bool > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { visited . Add ( false ) ; } for ( int u = 1 ; u <= 1 ; u ++ ) { if ( visited [ u ] == false ) DFSUtil ( u , false , false , initial , finall ) ; } Console . Write ( ans ) ; } static void countOperations ( int N , bool [ ] initial , bool [ ] finall , int [ , ] Edges ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdges ( Edges [ i , 0 ] , Edges [ i , 1 ] ) ; } DFS ( N + 1 , initial , finall ) ; } static void Main ( ) { for ( int i = 0 ; i < N + 1 ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } } ; bool [ ] initial = { true , true , false } ; bool [ ] finall = { false , true , true } ; countOperations ( N , initial , finall , Edges ) ; } }


"Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | C # program for the above approach ; Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completely , increment the count of operations by 1 ; Return Answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperations ( int [ ] A , int [ ] B , int N ) { int cnt = 0 ; int i = 0 ; while ( i < N ) { int j = 0 ; while ( j < N && i < N ) { if ( B [ i ] == A [ j ] ) { i ++ ; } j ++ ; } cnt ++ ; } return cnt ; } public static void Main ( string [ ] args ) { int [ ] A = { 2 , 4 , 6 , 1 , 5 , 3 } ; int [ ] B = { 6 , 5 , 4 , 2 , 3 , 1 } ; int N = A . Length ; Console . WriteLine ( minOperations ( A , B , N ) ) ; } }


"Minimize operations to make all array elements | C # program for the above approach ; Function to find minimum the number of operations required to make all the array elements to - 1 ; Stores the array elements with their corresponding indices ; Push the array element and it 's index ; Sort the elements according to it 's first value ; Stores the minimum number of operations required ; Traverse until vp is not empty ; Stores the first value of vp ; Stores the second value of vp ; Update the minCnt ; Pop the back element from the vp until the first value is same as val and difference between indices is less than K ; Return the minCnt ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static int minOperations ( int [ ] arr , int N , int K ) { List < pair > vp = new List < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { vp . Add ( new pair ( arr [ i ] , i ) ) ; } vp . Sort ( ) ; int minCnt = 0 ; while ( vp . Count != 0 ) { int val , ind ; val = vp [ vp . Count - 1 ] . first ; ind = vp [ vp . Count - 1 ] . second ; minCnt ++ ; while ( vp . Count != 0 && vp [ vp . Count - 1 ] . first == val && ind - vp [ vp . Count - 1 ] . second + 1 <= K ) vp . RemoveAt ( vp . Count - 1 ) ; } return minCnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 18 , 11 , 18 , 11 , 18 } ; int K = 3 ; int N = arr . Length ; Console . Write ( minOperations ( arr , N , K ) ) ; } }


"Minimize positive product of two given numbers by at most N decrements | C # program to implement the above approach ; Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code"
Solution in C#:

using System ; class GFG { static int minProd ( int X , int Y , int N ) { if ( X <= Y ) { if ( N < X ) return ( X - N ) * Y ; else { return Math . Max ( Y - ( N - X + 1 ) , 1 ) ; } } if ( Y >= N ) return ( Y - N ) * X ; return Math . Max ( X - ( N - Y + 1 ) , 1 ) ; } public static void Main ( String [ ] args ) { int X = 47 , Y = 42 , N = 167 ; Console . WriteLine ( minProd ( X , Y , N ) ) ; } }


"Minimize prize count required such that smaller value gets less prize in an adjacent pair | C # implementation to find the minimum prizes required such that adjacent smaller elements gets less number of prizes ; Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to compute the smaller elements at the left ; Loop to find the smaller elements at the right ; Loop to find the minimum prizes required ; Driver Code"
Solution in C#:

 using System ; class GFG { static int minPrizes ( int [ ] arr , int n ) { int [ ] dpLeft = new int [ n ] ; dpLeft [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { dpLeft [ i ] = dpLeft [ i - 1 ] + 1 ; } else { dpLeft [ i ] = 1 ; } } int [ ] dpRight = new int [ n ] ; dpRight [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { dpRight [ i ] = dpRight [ i + 1 ] + 1 ; } else { dpRight [ i ] = 1 ; } } int totalPrizes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalPrizes += Math . Max ( dpLeft [ i ] , dpRight [ i ] ) ; } Console . Write ( totalPrizes + "
 " ) ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; minPrizes ( arr , n ) ; } }


"Minimize prize count required such that smaller value gets less prize in an adjacent pair | C # implementation to find the minimum prizes required such that adjacent smaller elements gets less number of prizes ; Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes ; Loop to iterate over every elements of the array ; Loop to find the consecutive smaller elements at left ; Loop to find the consecutive smaller elements at right ; Driver Code"
Solution in C#:

 using System ; class GFG { static int findMinPrizes ( int [ ] arr , int n ) { int totalPrizes = 0 , j , x , y ; for ( int i = 0 ; i < n ; i ++ ) { x = 1 ; j = i ; while ( j > 0 && arr [ j ] > arr [ j - 1 ] ) { x ++ ; j -- ; } j = i ; y = 1 ; while ( j < n - 1 && arr [ j ] > arr [ j + 1 ] ) { y ++ ; j ++ ; } totalPrizes += Math . Max ( x , y ) ; } Console . Write ( totalPrizes + "
 " ) ; return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; findMinPrizes ( arr , n ) ; } }


"Minimize product of first 2 ^ Kâ €“ 1 Natural Numbers by swapping bits for any pair any number of times | C # program for the above approach ; Function to find the minimum positive product after swapping bits at the similar position for any two numbers ; Stores the resultant number ; range / 2 numbers will be 1 and range / 2 numbers will be range - 1 ; Multiply with the final number ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int minimumPossibleProduct ( int K ) { int res = 1 ; int range = ( 1 << K ) - 1 ; for ( int i = 0 ; i < K ; i ++ ) { res *= ( range - 1 ) ; } res *= range ; return res ; } public static void Main ( string [ ] args ) { int K = 3 ; Console . WriteLine ( minimumPossibleProduct ( K ) ) ; } }


"Minimize product of first N | C # program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Driver Code ; Given Number N ; Function Call"
Solution in C#:

 using System ; class GFG { static int mod = ( int ) 1e9 + 7 ; static void minProduct ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= ( n - 4 ) / 2 ; i ++ ) { ans = ( int ) ( 1L * ans * ( n - 2 ) ) % mod ; } ans = ( int ) ( 1L * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; Console . Write ( ans + "
 " ) ; } public static void Main ( String [ ] args ) { int N = 8 ; minProduct ( N ) ; } }


"Minimize product of two scores possible by at most M reductions | C # program for the above approach ; Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; Case 2 - R2 reduces first ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static int minProductUtil ( int R1 , int B1 , int R2 , int B2 , int M ) { int x = Math . Min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { int y = Math . Min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } static int minProduct ( int R1 , int B1 , int R2 , int B2 , int M ) { int res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; int res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . Min ( res1 , res2 ) ; } public static void Main ( String [ ] args ) { int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; Console . Write ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ; } }


"Minimize remaining array element by removing pairs and replacing them by their absolute difference | C # program for above approach ; Function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i , sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver function"
Solution in C#:

using System ; public class GFG { static int smallestLeft ( int [ ] arr , int total , int sum , int i , int [ , ] dp ) { if ( i == 0 ) { return Math . Abs ( total - 2 * sum ) ; } if ( dp [ i , sum ] != - 1 ) return dp [ i , sum ] ; int X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) ; int Y = smallestLeft ( arr , total , sum , i - 1 , dp ) ; return dp [ i , sum ] = Math . Min ( X , Y ) ; } static void UtilSmallestElement ( int [ ] arr , int N ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += arr [ i ] ; } int [ , ] dp = new int [ N + 1 , total ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < total ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( smallestLeft ( arr , total , 0 , N , dp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 4 , 1 , 8 , 1 } ; int N = arr . Length ; UtilSmallestElement ( arr , N ) ; } }


"Minimize remaining array element by removing pairs and replacing them by their absolute difference | C # program to implement the above approach ; Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code"
Solution in C#:

using System ; class GFG { static int SmallestElementLeft ( int [ ] arr , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } int req = totalSum / 2 ; bool [ ] dp = new bool [ req + 1 ] ; for ( int i = 0 ; i < req + 1 ; i ++ ) { dp [ i ] = false ; } dp [ 0 ] = true ; int reach = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = req ; j - arr [ i ] >= 0 ; j -- ) { dp [ j ] = dp [ j ] || dp [ j - arr [ i ] ] ; if ( dp [ j ] ) { reach = Math . Max ( reach , j ) ; } } } return totalSum - ( 2 * reach ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 2 } ; int N = arr . Length ; Console . Write ( SmallestElementLeft ( arr , N ) ) ; } }


"Minimize remaining array element by removing pairs and replacing them with their average | C # program to implement the above approach ; Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static int findSmallestNumLeft ( int [ ] arr , int N ) { List < int > PQ = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . Add ( arr [ i ] ) ; } PQ . Sort ( ) ; PQ . Reverse ( ) ; while ( PQ . Count > 1 ) { int top1 = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; int top2 = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; PQ . Add ( ( top1 + top2 + 1 ) / 2 ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } return PQ [ 0 ] ; } public static void Main ( ) { int [ ] arr = { 30 , 16 , 40 } ; int N = arr . Length ; Console . Write ( findSmallestNumLeft ( arr , N ) ) ; } }


"Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum | C # program to implement the above approach ; Utility pair ; Function to print the smallest element left in the array and the pairs by given operation ; Stores array elements and return the minimum element of [ ] arr in O ( 1 ) ; Stores all the pairs that can be selected by the given operations ; Traverse the array [ ] arr ; Traverse pq while count of elements left in pq greater than 1 ; Stores top element of pq ; Pop top element of pq ; Stores top element of pq ; Pop top element of pq ; Insert ( X + Y + 1 ) / 2 in pq ; Insert the pair ( X , Y ) in pairsArr [ ] ; Print the element left in pq by performing the given operations ; Stores count of elements in pairsArr [ ] ; Print all the pairs that can be selected in given operations ; If i is the first index of pairsArr [ ] ; Print current pairs of pairsArr [ ] ; If i is not the last index of pairsArr [ ] ; If i is the last index of pairsArr [ ] ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void smallestNumberLeftInPQ ( int [ ] arr , int N ) { List < int > pq = new List < int > ( ) ; List < pair > pairsArr = new List < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . Add ( arr [ i ] ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count > 1 ) { int X = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; int Y = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; pq . Add ( ( X + Y + 1 ) / 2 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; pairsArr . Add ( new pair ( X , Y ) ) ; } Console . Write ( " { " + pq [ 0 ] + " } , ▁ " ) ; int sz = pairsArr . Count ; for ( int i = 0 ; i < sz ; i ++ ) { if ( i == 0 ) { Console . Write ( " { ▁ " ) ; } Console . Write ( " ( " + pairsArr [ i ] . first + " , ▁ " + pairsArr [ i ] . second + " ) " ) ; if ( i != sz - 1 ) { Console . Write ( " , ▁ " ) ; } if ( i == sz - 1 ) { Console . Write ( " ▁ } " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int N = arr . Length ; smallestNumberLeftInPQ ( arr , N ) ; } }


"Minimize remaining array sizes by removing equal pairs of first array elements | C # program for the above approach ; Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 [ ] ; Store the count of 0 s in L2 [ ] ; Traverse the array L1 [ ] ; If condition is true ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 [ ] ; If condition is true ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void countRemainingElements ( int [ ] L1 , int [ ] L2 , int n ) { int one = 0 ; int zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( L1 [ i ] == 1 ) one ++ ; else zero ++ ; } int ans = n ; for ( int i = 0 ; i < n ; i ++ ) { if ( L2 [ i ] == 1 ) { one -- ; if ( one < 0 ) { ans = i ; break ; } } else { zero -- ; if ( zero < 0 ) { ans = i ; break ; } } } Console . WriteLine ( n - ans ) ; } static public void Main ( ) { int [ ] L1 = { 1 , 1 , 0 , 0 } ; int [ ] L2 = { 0 , 0 , 0 , 1 } ; int N = L1 . Length ; countRemainingElements ( L1 , L2 , N ) ; } }


"Minimize removal of alternating subsequences to empty given Binary String | C # program for the above approach ; Function to find the minimum number of operations required to empty the string ; Initialize variables ; Traverse the string ; If current character is 0 ; Update maximum consecutive 0 s and 1 s ; Print the minimum operation ; Driver Code ; input string ; length of string ; Function Call"
Solution in C#:

using System ; class GFG { public static void minOpsToEmptyString ( string S , int N ) { int one = 0 , zero = 0 ; int x0 = 0 , x1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) { x0 ++ ; x1 = 0 ; } else { x1 ++ ; x0 = 0 ; } zero = Math . Max ( x0 , zero ) ; one = Math . Max ( x1 , one ) ; } Console . WriteLine ( Math . Max ( one , zero ) ) ; } static public void Main ( ) { int N = S . Length ; string S = "0100100111" ; int N = S . Length ; minOpsToEmptyString ( S , N ) ; } }


"Minimize removal of non | C # program for the above approach ; Function to find minimum count of steps required ot make String S an empty String ; Stores count of occurences ' ( ' ; Stores count of occurences ' ) ' ; Traverse the String , str ; If current character is ' ( ' ; Update count_1 ; Update count_2 ; If all the characters are same , then print - 1 ; If the count of occurence of ' ) ' and ' ( ' are same then print 0 ; If length of String is Odd ; Driver Code ; Given String ; Size of the String ; Function Call"
Solution in C#:

 using System ; class GFG { static void canReduceString ( String S , int N ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ( ' ) { count_1 ++ ; } else { count_2 ++ ; } } if ( count_1 == 0 count_2 == 0 ) { Console . Write ( " - 1" + "
 " ) ; } else if ( count_1 == count_2 ) { Console . Write ( "0" + "
 " ) ; } else if ( N % 2 != 0 ) { Console . Write ( " - 1" ) ; } else { Console . Write ( Math . Abs ( count_1 - count_2 ) / 2 ) ; } } public static void Main ( String [ ] args ) { String S = " ) ) ) ( ( ( " ; int N = S . Length ; canReduceString ( S , N ) ; } }


"Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | C # program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int minRemovals ( string str , int N ) { int ans = 0 ; bool X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { ans ++ ; while ( str [ i ] == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == '0' && str [ N - 1 ] == '0' ) { ans -- ; } return ans ; } public static void Main ( ) { string S = "11010001" ; int N = S . Length ; Console . Write ( minRemovals ( S , N ) ) ; } }


"Minimize removal or insertions required to make two strings equal | C # program for the above approach ; Function to find the longest substring in string A which is a subsequence in B ; If the indices are out of bounds ; If an already computed subproblem occurred ; Required answer if the all the characters of A and B are the same ; Required answer if there is no substring A which is a subsequence in B ; Required answer if the current character in B is skipped ; The answer for the subproblem is the maximum among the three ; Function to return the minimum strings operations required to make two strings equal ; Initialize the dp vector ; Driver Code"
Solution in C#:

using System ; class GFG { static int findLongestSubstring ( int posA , int posB , String A , String B , int canSkip , int n , int [ , , ] dp ) { if ( posA >= n posB >= n ) { return 0 ; } if ( dp [ posA , posB , canSkip ] != - 1 ) { return dp [ posA , posB , canSkip ] ; } int op1 = 0 ; int op2 = 0 ; int op3 = 0 ; if ( A [ posA ] == B [ posB ] ) { op1 = 1 + findLongestSubstring ( posA + 1 , posB + 1 , A , B , 0 , n , dp ) ; } if ( canSkip == 1 ) { op2 = findLongestSubstring ( posA + 1 , posB , A , B , canSkip , n , dp ) ; } op3 = findLongestSubstring ( posA , posB + 1 , A , B , canSkip , n , dp ) ; return dp [ posA , posB , canSkip ] = Math . Max ( op1 , Math . Max ( op2 , op3 ) ) ; } static void minOperations ( String A , String B , int N ) { int [ , , ] dp = new int [ N , N , 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . WriteLine ( N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ) ; } static public void Main ( ) { String A = " abab " ; String B = " baba " ; int N = A . Length ; minOperations ( A , B , N ) ; } }


"Minimize removals to remove another string as a subsequence of a given string | C # program for above approach ; Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Driver code ; Input ; Function call to get minimum number of character removals"
Solution in C#:

using System ; public class GFG { static void printMinimumRemovals ( string str , string X ) { int N = str . Length ; int M = X . Length ; int [ , ] dp = new int [ N , M ] ; for ( int j = 0 ; j < M ; j ++ ) { if ( str [ 0 ] == X [ j ] ) { dp [ 0 , j ] = 1 ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( str [ i ] == X [ j ] ) { dp [ i , j ] = dp [ i - 1 , j ] + 1 ; if ( j != 0 ) dp [ i , j ] = Math . Min ( dp [ i , j ] , dp [ i - 1 , j - 1 ] ) ; } else { dp [ i , j ] = dp [ i - 1 , j ] ; } } } Console . WriteLine ( dp [ N - 1 , M - 1 ] ) ; } public static void Main ( String [ ] args ) { string str = " btagd " ; string X = " bad " ; printMinimumRemovals ( str , X ) ; } }


"Minimize replacement of characters to its nearest alphabet to make a string palindromic | C # program for the above approach ; Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Driver code ; Given string ; Function call"
Solution in C#:

using System ; class GFG { public static int minOperations ( String s ) { int len = s . Length ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = Math . Max ( s [ i ] , s [ len - 1 - i ] ) - Math . Min ( s [ i ] , s [ len - 1 - i ] ) ; int D2 = 26 - D1 ; result += Math . Min ( D1 , D2 ) ; } return result ; } public static void Main ( String [ ] args ) { String s = " abccdb " ; Console . WriteLine ( minOperations ( s ) ) ; } }


"Minimize replacements by previous or next alphabet required to make all characters of a string the same | C # program for the above approach ; Function to find the minimum count of operations to make all characters of the String same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver code ; Given String str ; Function call"
Solution in C#:

using System ; class GFG { static int minCost ( string s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += Math . Min ( Math . Abs ( i - ( s [ j ] - ' a ' ) ) , 26 - Math . Abs ( i - ( s [ j ] - ' a ' ) ) ) ; } minValue = Math . Min ( minValue , cnt ) ; } return minValue ; } public static void Main ( ) { string str = " geeksforgeeks " ; int N = str . Length ; Console . WriteLine ( minCost ( str , N ) ) ; } }


"Minimize replacements or swapping of same indexed characters required to make two given strings palindromic | C # program to implement the above approach ; Function to find minimum operations to make both the strings palindromic ; Stores index of the left pointer ; Stores index of the right pointer ; Stores count of minimum operations to make both the strings palindromic ; If str1 [ i ] equal to str1 [ j ] and str2 [ i ] not equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] not equal to str1 [ j ] and str2 [ i ] equal to str2 [ j ] ; Update cntOp ; If str1 [ i ] is not equal to str1 [ j ] and str2 [ i ] is not equal to str2 [ j ] ; If str1 [ i ] is equal to str2 [ j ] and str2 [ i ] is equal to str1 [ j ] ; Update cntOp ; Update cntOp ; Update i and j ; Driver Code ; Stores length of str1"
Solution in C#:

using System ; class GFG { static int MincntBothPalin ( string str1 , string str2 , int N ) { int i = 0 ; int j = N - 1 ; int cntOp = 0 ; while ( i < j ) { if ( str1 [ i ] == str1 [ j ] && str2 [ i ] != str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] == str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == str2 [ j ] && str2 [ i ] == str1 [ j ] ) { cntOp += 1 ; } else { cntOp += 2 ; } } i += 1 ; j -= 1 ; } return cntOp ; } public static void Main ( ) { string str1 = " dbba " ; string str2 = " abcd " ; int N = str1 . Length ; Console . WriteLine ( MincntBothPalin ( str1 , str2 , N ) ) ; } }


"Minimize replacements to make every element in an array exceed every element in another given array | C # program for the above approach ; Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void MinTime ( int [ ] a , int [ ] b , int n , int m ) { int ans = 2147483647 ; int [ ] prefix_a = new int [ 10 ] ; int [ ] prefix_b = new int [ 10 ] ; Array . Clear ( prefix_a , 0 , prefix_a . Length ) ; Array . Clear ( prefix_b , 0 , prefix_b . Length ) ; for ( int i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ans = Math . Min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = Math . Min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] A = { 0 , 0 , 1 , 3 , 3 } ; int [ ] B = { 2 , 0 , 3 } ; int N = A . Length ; int M = B . Length ; MinTime ( A , B , N , M ) ; } }


"Minimize replacements with values up to K to make sum of two given arrays equal | C # program to implement the above approach ; Function to find minimum number of replacements required to make the sum of two arrays equal ; Stores the sum of a [ ] ; Stores the sum of b [ ] ; Calculate sum of the array a [ ] ; Calculate sum of the array b [ ] ; Stores the difference between a [ ] and b [ ] ; Left and Right pointer to traverse the array a [ ] ; Left and Right pointer to traverse the array b [ ] ; Stores the count of moves ; Sort the arrays in ascending order ; Iterate while diff != 0 and l1 <= r1 or l2 <= r2 ; If diff is greater than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; If diff is less than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; Increment count of res by one ; If diff is 0 , then return res ; Otherwise , return - 1 ; Driver Code"
Solution in C#:

using System ; public class GFG { public static int makeSumEqual ( int [ ] a , int [ ] b , int K , int M , int N ) { int sum1 = 0 ; int sum2 = 0 ; foreach ( int el in a ) sum1 += el ; foreach ( int el in b ) sum2 += el ; int diff = sum1 - sum2 ; int l1 = 0 , r1 = M - 1 ; int l2 = 0 , r2 = N - 1 ; int res = 0 ; Array . Sort ( a ) ; Array . Sort ( b ) ; while ( l1 <= r1 l2 <= r2 ) { if ( diff == 0 ) { break ; } if ( diff > 0 ) { if ( l2 <= r2 && l1 <= r1 ) { if ( K - b [ l2 ] < a [ r1 ] - 1 ) { int sub = Math . Min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . Min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else if ( l1 <= r1 ) { int sub = Math . Min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . Min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else { if ( l1 <= r1 && l2 <= r2 ) { if ( K - a [ l1 ] < b [ r2 ] - 1 ) { int sub = Math . Min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . Min ( K - a [ l1 ] , - 1 * diff ) ; diff += sub ; a [ l1 ] -= sub ; l1 ++ ; } } else if ( l2 <= r2 ) { int sub = Math . Min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . Min ( K - a [ l1 ] , diff ) ; diff += sub ; a [ l1 ] += sub ; l1 ++ ; } } res ++ ; } if ( diff == 0 ) return res ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 4 , 3 } ; int [ ] B = { 6 , 6 , 6 } ; int M = A . Length ; int N = B . Length ; int K = 6 ; Console . WriteLine ( makeSumEqual ( A , B , K , M , N ) ) ; } }


"Minimize splits to generate monotonous Substrings from given String | C # Program to implement the above approach ; Function to return readonly result ; Initialize variable to keep track of ongoing sequence ; If current sequence is neither increasing nor decreasing ; If prev char is greater ; If prev char is same ; Otherwise ; If current sequence is Non - Decreasing ; If prev char is smaller ; If prev char is same ; Update ongoing ; Otherwise ; Update ongoing ; Increase count ; If current sequence is Non - Increasing ; If prev char is greater , then update ongoing with D ; If prev char is equal , then update current with D ; Otherwise , update ongoing with N and increment count ; Return count + 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static int minReqSubstring ( String s , int n ) { char ongoing = ' N ' ; int count = 0 , l = s . Length ; for ( int i = 1 ; i < l ; i ++ ) { if ( ongoing == ' N ' ) { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' N ' ; } else { ongoing = ' I ' ; } } else if ( ongoing == ' I ' ) { if ( s [ i ] > s [ i - 1 ] ) { ongoing = ' I ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' I ' ; } else { ongoing = ' N ' ; count += 1 ; } } else { if ( s [ i ] < s [ i - 1 ] ) { ongoing = ' D ' ; } else if ( s [ i ] == s [ i - 1 ] ) { ongoing = ' D ' ; } else { ongoing = ' N ' ; count += 1 ; } } } return count + 1 ; } public static void Main ( String [ ] args ) { String S = " aeccdhba " ; int n = S . Length ; Console . Write ( minReqSubstring ( S , n ) ) ; } }


"Minimize steps defined by a string required to reach the destination from a given source | C # program for the above approach ; Function to find the minimum length of string required to reach from source to destination ; Size of the string ; Stores the index of the four directions E , W , N , S ; If destination reached ; Iterate over the string ; Move east ; Change x1 according to direction E ; Move west ; Change x1 according to direction W ; Move north ; Change y1 according to direction N ; Move south ; Change y1 according to direction S ; Store the max of all positions ; Print the minimum length of string required ; Otherwise , it is impossible ; Driver Code ; Given string ; Given source and destination ; Function call"
Solution in C#:

using System ; class GFG { static void minimum_length ( int x1 , int y1 , int x2 , int y2 , string str ) { int n = str . Length ; int pos1 , pos2 , pos3 , pos4 ; pos1 = - 1 ; pos2 = - 1 ; pos3 = - 1 ; pos4 = - 1 ; if ( x1 == x2 && y1 == y2 ) { Console . WriteLine ( "0" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( x2 > x1 ) { if ( str [ i ] == ' E ' ) { x1 = x1 + 1 ; if ( x1 == x2 ) { pos1 = i ; } } } if ( x2 < x1 ) { if ( str [ i ] == ' W ' ) { x1 = x1 - 1 ; if ( x1 == x2 ) { pos2 = i ; } } } if ( y2 > y1 ) { if ( str [ i ] == ' N ' ) { y1 = y1 + 1 ; if ( y1 == y2 ) { pos3 = i ; } } } if ( y2 < y1 ) { if ( str [ i ] == ' S ' ) { y1 = y1 - 1 ; if ( y1 == y2 ) { pos4 = i ; } } } } int z ; z = Math . Max ( pos1 , Math . Max ( Math . Max ( pos2 , pos3 ) , pos4 ) ) ; if ( x1 == x2 && y1 == y2 ) { Console . WriteLine ( z + 1 ) ; } else { Console . WriteLine ( " - 1" ) ; } } } public static void Main ( ) { string str = " SESNW " ; int x1 = 0 , x2 = 1 , y1 = 0 , y2 = 1 ; minimum_length ( x1 , y1 , x2 , y2 , str ) ; } }


"Minimize steps required to make all array elements same by adding 1 , 2 or 5 | C # program for the above approach ; Function to calculate the minimum number of steps ; count stores number of operations required to make all elements equal to minimum value ; Remark , the array should remain unchanged for further calculations with different minimum ; Storing the current value of arr [ i ] in val ; Finds how much extra amount is to be removed ; Subtract the maximum number of 5 and stores remaining ; Subtract the maximum number of 2 and stores remaining ; Restores the actual value of arr [ i ] ; Return the count ; Function to find the minimum number of steps to make array elements same ; Sort the array in descending order ; Stores the minimum array element ; Stores the operations required to make array elements equal to minimum ; Stores the operations required to make array elements equal to minimum - 1 ; Stores the operations required to make array elements equal to minimum - 2 ; Return minimum of the three counts ; Driver Code"
Solution in C#:

using System ; public class GFG { static int calculate_steps ( int [ ] arr , int n , int minimum ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += arr [ i ] / 5 ; arr [ i ] = arr [ i ] % 5 ; count += arr [ i ] / 2 ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] > 0 ) { count ++ ; } } arr [ i ] = val ; } return count ; } static int solve ( int [ ] arr , int n ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int minimum = arr [ n - 1 ] ; int count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . Min ( count1 , Math . Min ( count2 , count3 ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 6 } ; int N = arr . Length ; Console . Write ( solve ( arr , N ) ) ; } }


"Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M | C # program for the above approach ; Stores the prime factor of numbers ; Function to find GCD of a and b ; Base Case ; Otherwise , calculate GCD ; Function to precompute the prime numbers till 1000000 ; Initialize all the positions with their respective values ; Iterate over the range [ 2 , sqrt ( 10 ^ 6 ) ] ; If i is prime number ; Mark it as the factor ; Utility function to count the number of steps to make X and Y equal ; Initialise steps ; Iterate x is at most 1 ; Divide with the smallest prime factor ; If X and Y can 't be  made equal ; Return steps ; Function to find the minimum number of steps required to make X and Y equal ; Generate all the prime factors ; Calculate GCD of x and y ; Divide the numbers by their gcd ; If not possible , then return - 1 ; ; Return the resultant number of steps ; Driver code"
Solution in C#:

using System ; class GFG { static int [ ] primes = new int [ 1000006 ] ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void preprocess ( ) { for ( int i = 1 ; i <= 1000000 ; i ++ ) primes [ i ] = i ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( primes [ i ] == i ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { if ( primes [ j ] == j ) primes [ j ] = i ; } } } } static int Steps ( int x , int m ) { int steps = 0 ; bool flag = false ; while ( x > 1 ) { if ( primes [ x ] > m ) { flag = true ; break ; } x /= primes [ x ] ; steps ++ ; } if ( flag ) return - 1 ; return steps ; } static int minimumSteps ( int x , int y , int m ) { preprocess ( ) ; int g = gcd ( x , y ) ; x = x / g ; y = y / g ; int x_steps = Steps ( x , m ) ; int y_steps = Steps ( y , m ) ; if ( x_steps == - 1 y_steps == - 1 ) return - 1 ; return x_steps + y_steps ; } static void Main ( ) { int X = 160 ; int Y = 180 ; int M = 10 ; Console . Write ( minimumSteps ( X , Y , M ) ) ; } }


"Minimize steps required to move all 1 's in a matrix to a given index | C # program to calculate the minimum steps required to reach a given cell from all cells consisting of 1 's ; Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Driver code ; Given matrix ; Given position ; Function call"
Solution in C#:

 using System ; class GFG { static int findAns ( int [ , ] mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i , j ] == 1 ) { ans += Math . Abs ( x - i ) + Math . Abs ( y - j ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; Console . Write ( findAns ( mat , x , y , mat . GetLength ( 0 ) , mat . GetLength ( 1 ) ) + "
 " ) ; } }


"Minimize steps required to reach the value N | C # program for the above approach ; Function to find the minimum steps required to reach N by either moving i steps forward or 1 steps backward ; Stores the required count ; IF total moves required is less than double of N ; Update steps ; Steps required to reach N ; Update steps ; Driver code ; Given value of N"
Solution in C#:

using System ; class GFG { static void minimumsteps ( int N ) { int steps = 0 ; while ( steps * ( steps + 1 ) < 2 * N ) { steps ++ ; } if ( steps * ( steps + 1 ) / 2 == N + 1 ) { steps ++ ; } Console . WriteLine ( steps ) ; } static public void Main ( ) { int N = 18 ; minimumsteps ( N ) ; } }


"Minimize subarray increments / decrements required to reduce all array elements to 0 | C # program of the above approach ; Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum positive ; Return minOp ; Driver Code"
Solution in C#:

using System ; class GFG { static int minOperation ( int [ ] arr ) { int minOp = int . MinValue ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . Abs ( minNeg ) + maxPos ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 4 , 1 } ; Console . WriteLine ( minOperation ( arr ) ) ; } }


"Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | C # program for the above approach ; Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinMoves ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % N != 0 ) return - 1 ; int avg = sum / N ; int total = 0 ; int needCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { needCount += ( arr [ i ] - avg ) ; total = Math . Max ( Math . Max ( Math . Abs ( needCount ) , arr [ i ] - avg ) , total ) ; } return total ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 5 } ; int N = arr . Length ; Console . Write ( findMinMoves ( arr , N ) ) ; } }


"Minimize sum by dividing all elements of a subarray by K | C # implementation of the approach ; Function to return the maximum subarray sum ; Function to return the minimized sum of the array elements after performing the given operation ; Find maximum subarray sum ; Find total sum of the array ; Maximum subarray sum is already negative ; Choose the subarray whose sum is maximum and divide all elements by K ; Driver code"
Solution in C#:

using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static double minimizedSum ( int [ ] a , int n , int K ) { int sum = maxSubArraySum ( a , n ) ; double totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + ( double ) sum / ( double ) K ; return totalSum ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , - 2 , 3 } ; int n = a . Length ; int K = 2 ; Console . WriteLine ( minimizedSum ( a , n , K ) ) ; } }


"Minimize sum of K positive integers with given LCM | C # program to implement the above approach ; Function to find the prime power of X ; Stores prime powers of X ; Iterate over the range [ 2 , Math . Sqrt ( X ) ] ; If X is divisible by i ; Stores prime power ; Calculate prime power of X ; Update X ; Update p ; Insert prime powers into primePow [ ] ; If X exceeds 1 ; Function to calculate the sum of array elements ; Stores sum of array elements ; Traverse the array ; Update sum ; Function to partition array into K groups such that sum of elements of the K groups is minimum ; If count of prime powers is equal to pos ; Stores minimum sum of of each partition of [ ] arr into K groups ; Traverse the array [ ] arr ; Include primePow [ pos ] into i - th groups ; Update res ; Remove factors [ pos ] from i - th groups ; Utility function to calculate minimum sum of K positive integers whose LCM is X ; Stores all prime powers of X ; Stores count of prime powers ; Stores minimum sum of K positive integers whose LCM is X ; If n is less than or equal to k ; Traverse primePow [ ] array ; Update sum ; Update sum ; arr [ i ] : Stores element in i - th group by partitioning the primePow [ ] array ; Update sum ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static List < int > primePower ( int X ) { List < int > primePow = new List < int > ( ) ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { int p = 1 ; while ( X % i == 0 ) { X /= i ; p *= i ; } primePow . Add ( p ) ; } } if ( X > 1 ) { primePow . Add ( X ) ; } return primePow ; } static int getSum ( int [ ] ar ) { int sum = 0 ; foreach ( int i in ar ) { sum += i ; } return sum ; } static int getMinSum ( int pos , int [ ] arr , List < int > primePow ) { if ( pos == primePow . Count ) { return getSum ( arr ) ; } int res = int . MaxValue ; for ( int i = 0 ; i < arr . Length ; i ++ ) { arr [ i ] *= primePow [ pos ] ; res = Math . Min ( res , getMinSum ( pos + 1 , arr , primePow ) ) ; arr [ i ] /= primePow [ pos ] ; } return res ; } static int minimumSumWithGivenLCM ( int k , int x ) { List < int > primePow = primePower ( x ) ; int n = primePow . Count ; int sum = 0 ; if ( n <= k ) { foreach ( int i in primePow ) { sum += i ; } sum += k - n ; } else { int [ ] arr = new int [ k ] ; for ( int l = 0 ; l < arr . Length ; l ++ ) arr [ l ] = 1 ; sum = getMinSum ( 0 , arr , primePow ) ; } return sum ; } public static void Main ( String [ ] args ) { int k = 3 , x = 210 ; Console . Write ( minimumSumWithGivenLCM ( k , x ) ) ; } }


"Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | C # program for the above approach ; Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code"
Solution in C#:

using System ; class GFG { public static int minSumDifference ( int [ ] ar , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; int rem = sum % n ; return rem * ( n - rem ) ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 } ; int N = arr . Length ; Console . Write ( minSumDifference ( arr , N ) ) ; } }


"Minimize sum of adjacent difference with removal of one element from array | C # implementation of the approach ; Function to find the element ; Value variable for storing the total value ; Declaring maximum value as zero ; If array contains on element ; Storing the maximum value in temp variable ; Adding the adjacent difference modulus values of removed element . Removing adjacent difference modulus value after removing element ; Returning total value - maximum value ; Driver code"
Solution in C#:

using System ; class GFG { static int findMinRemoval ( int [ ] arr , int n ) { int temp , value = 0 ; int maximum = 0 ; if ( n == 1 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) + Math . Abs ( arr [ i ] - arr [ i - 1 ] ) - Math . Abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = Math . Max ( maximum , temp ) ; } return ( value - maximum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 3 , 2 , 10 } ; int n = arr . Length ; Console . WriteLine ( findMinRemoval ( arr , n ) ) ; } }


"Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | C # program for the above approach ; Function to find the minimum sum of the array such that Bitwise AND of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a row ; Traverse the range [ 0 , N - 1 ] ; If i not equal to j ; Update the value of res ; Increment the sum by res ; Return minimum possible sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int findMinSum ( int [ , ] mat , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i != j ) { res |= mat [ i , j ] ; } } sum += res ; } return sum ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { - 1 , 2 , 3 } , { 9 , - 1 , 7 } , { 4 , 5 , - 1 } } ; int N = mat . GetLength ( 0 ) ; Console . WriteLine ( findMinSum ( mat , N ) ) ; } }


"Minimize sum of given array by removing all occurrences of a single digit | Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver Code"
Solution in C#:

using System ; public class GFG { static int remove ( int N , int digit ) { String strN = N . ToString ( ) ; String ans = " " ; foreach ( char i in strN . ToCharArray ( ) ) { if ( ( i - '0' ) == digit ) { continue ; } ans += i ; } return Int32 . Parse ( ans ) ; } static void getMin ( int [ ] arr ) { int minSum = Int32 . MaxValue ; for ( int i = 0 ; i < 10 ; i ++ ) { int curSum = 0 ; foreach ( int num in arr ) curSum += remove ( num , i ) ; minSum = Math . Min ( minSum , curSum ) ; } Console . WriteLine ( minSum ) ; } static public void Main ( ) { int [ ] arr = { 34 , 23 , 85 , 93 } ; getMin ( arr ) ; } }


"Minimize sum of incompatibilities of K equal | C # program for the above approach ; Function to find the minimum incompatibility sum ; Stores the count of element ; Traverse the array ; If number i not occurs in Map ; Put the element in the Map ; Increment the count of i in the Hash Map ; If count of i in Map is greater than K then return - 1 ; Stores all total state ; Traverse over all the state ; If number of set bit is equal to N / K ; Stores the minimum value at a state ; Initialize the dp state with - 1 ; Call the recursive function ; Recursive function to find the minimum required value ; Base Case ; Stores the minimum value of the current state ; If dp [ state ] [ index ] is already calculated ; return dp [ state ] [ index ] ; Traverse over all the bits ; If count of set bit is N / K ; Store the new state after choosing N / K elements ; Stores the minimum and maximum of current subset ; Stores if the elements have been already selected or not ; Stores if it is possible to select N / K elements ; Traverse over the array ; If not chosen already for another subset ; If already chosen for another subset or current subset ; Mark the good false ; Mark the current number visited ; Mark the current position in mask newstate ; Update the maximum and minimum ; If good is true then Update the res ; Update the current sp state ; Return the res ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class Solution { int k ; int n ; int goal ; int [ , ] dp ; List < int > bits = new List < int > ( ) ; public int minimumIncompatibility ( int [ ] A , int k ) { this . n = A . Length ; this . k = k ; goal = n / k ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; foreach ( int i in A ) { if ( ! map . ContainsKey ( i ) ) map [ i ] = 0 ; map [ i ] ++ ; if ( map [ i ] > k ) return - 1 ; } int state = ( 1 << n ) - 1 ; for ( int i = 0 ; i <= state ; i ++ ) { if ( Convert . ToString ( i , 2 ) . Count ( c => c == '1' ) == goal ) bits . Add ( i ) ; } dp = new int [ 1 << n , k ] ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } return dfs ( A , state , 0 ) ; } public int dfs ( int [ ] A , int state , int index ) { if ( index >= k ) { return 0 ; } int res = 1000 ; if ( dp [ state , index ] != - 1 ) { return dp [ state , index ] ; } foreach ( int bit in bits ) { if ( Convert . ToString ( bit , 2 ) . Count ( c => c == '1' ) == goal ) { int newstate = state ; int mn = 100 , mx = - 1 ; bool [ ] visit = new bool [ n + 1 ] ; bool good = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( bit & ( 1 << j ) ) != 0 ) { if ( visit [ A [ j ] ] == true || ( state & ( 1 << j ) ) == 0 ) { good = false ; break ; } visit [ A [ j ] ] = true ; newstate = newstate ^ ( 1 << j ) ; mx = Math . Max ( mx , A [ j ] ) ; mn = Math . Min ( mn , A [ j ] ) ; } } if ( good ) { res = Math . Min ( res , mx - mn + dfs ( A , newstate , index + 1 ) ) ; } } } dp [ state , index ] = res ; return res ; } } class GFG { public static void Main ( ) { Solution st = new Solution ( ) ; int [ ] arr = { 1 , 2 , 1 , 4 } ; int K = 2 ; Console . Write ( st . minimumIncompatibility ( arr , K ) ) ; } }


"Minimize sum of product of same | C # Program to implement the above approach ; Function to print the arrays ; Function to reverse the subarray ; Function to calculate the minimum product of same - indexed elements of two given arrays ; Calculate initial product ; Traverse all odd length subarrays ; Remove the previous product ; Add the current product ; Check if current product is minimum or not ; Traverse all even length subarrays ; Remove the previous product ; Add to the current product ; Check if current product is minimum or not ; Update the pointers ; Reverse the subarray ; Print the subarray ; Driver Code"
Solution in C#:

using System ; public class GFG { static void print ( long [ ] a , long [ ] b ) { long minProd = 0 ; for ( int i = 0 ; i < a . Length ; ++ i ) { Console . Write ( a [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( int i = 0 ; i < b . Length ; ++ i ) { Console . Write ( b [ i ] + " ▁ " ) ; minProd += a [ i ] * b [ i ] ; } Console . WriteLine ( ) ; Console . WriteLine ( minProd ) ; } static void reverse ( int left , int right , long [ ] arr ) { while ( left < right ) { long temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ; ++ left ; -- right ; } } static void minimumProdArray ( long [ ] a , long [ ] b , int l ) { long total = 0 ; for ( int i = 0 ; i < a . Length ; ++ i ) { total += a [ i ] * b [ i ] ; } long min = Int32 . MaxValue ; int first = 0 ; int second = 0 ; for ( int i = 0 ; i < a . Length ; ++ i ) { int left = i - 1 ; int right = i + 1 ; long total1 = total ; while ( left >= 0 && right < a . Length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } for ( int i = 0 ; i < a . Length ; ++ i ) { int left = i ; int right = i + 1 ; long total1 = total ; while ( left >= 0 && right < a . Length ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } if ( min < total ) { reverse ( first , second , a ) ; print ( a , b ) ; } else { print ( a , b ) ; } } public static void Main ( string [ ] args ) { int n = 4 ; long [ ] a = { 2 , 3 , 1 , 5 } ; long [ ] b = { 8 , 2 , 4 , 3 } ; minimumProdArray ( a , b , n ) ; } }


"Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | C # program for the above approach ; Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int maximumCount ( int [ ] arr1 , int [ ] arr2 , int s1 , int s2 ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < s1 ; i ++ ) { sum1 += arr1 [ i ] ; } for ( int a = 0 ; a < s2 ; a ++ ) { sum2 += arr2 [ a ] ; } int len = 0 ; if ( s1 >= s2 ) { len = s2 ; } else { len = s1 ; } Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; int j = 0 , k = s2 - 1 , count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( sum1 <= sum2 ) { if ( arr2 [ k ] >= arr1 [ i ] ) { int dif1 = arr1 [ j ] , dif2 = arr2 [ k ] ; sum1 -= dif1 ; sum1 += dif2 ; sum2 -= dif2 ; sum2 += dif1 ; j ++ ; k -- ; count ++ ; } else { break ; } } else { break ; } } return count ; } static public void Main ( ) { int [ ] arr1 = new int [ ] { 1 , 3 , 2 , 4 } ; int [ ] arr2 = new int [ ] { 6 , 7 , 8 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . WriteLine ( maximumCount ( arr1 , arr2 , N , M ) ) ; } }


"Minimize swaps of adjacent characters to sort every possible rearrangement of given Binary String | C # program for the above approach ; Precalculate the values of power of 2 ; Function to calculate 2 ^ N % mod ; Function to find sum of inversions ; Initialise a list of 0 s and ? s ; Traverse the string in the reversed manner ; If the current character is 1 ; Effectively calculate a * b ^ ( b - 1 ) ; If the current character is 0 ; Increment count of zeroes ; Double count the zeroes ; Find b * 2 ^ ( b - 1 ) ; Increment count of questions ; Return the final count ; Driver code ; Given string S ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MOD = 1000000007 ; static List < int > MEM = new List < int > ( new int [ ] { 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 } ) ; static int mod_pow2 ( int n ) { while ( n >= MEM . Count ) MEM . Add ( ( MEM [ MEM . Count - 1 ] * 2 ) % MOD ) ; return MEM [ n ] ; } static int inversions ( char [ ] bstr ) { int total = 0 , zeros = 0 , questions = 0 ; Array . Reverse ( bstr ) ; foreach ( char x in bstr ) { int q ; if ( x == '1' ) { int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; } else if ( x == '0' ) { zeros += 1 ; } else { total *= 2 ; int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; questions += 1 ; } } return total ; } static void Main ( ) { char [ ] S = " ? 0 ? " . ToCharArray ( ) ; Console . WriteLine ( inversions ( S ) ) ; } }


"Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same | C # program for the above approach ; Function to check if S contains any pair of adjacent characters that are same ; Traverse the String S ; If current pair of adjacent characters are the same ; Return true ; Utility function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Check if the required String is formed already ; Traverse the String S ; Swap the characters at i and j position ; Swap for Backtracking Step ; Function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Stores the resultant minimum number of swaps required ; Function call to find the minimum swaps required ; Print the result ; Driver Code"
Solution in C#:

using System ; public class GFG { static int ansSwaps ; static bool check ( char [ ] S ) { for ( int i = 1 ; i < S . Length ; i ++ ) { if ( S [ i - 1 ] == S [ i ] ) { return false ; } } return true ; } static void minimumSwaps ( char [ ] S , int swaps , int idx ) { if ( check ( S ) ) { ansSwaps = Math . Min ( ansSwaps , swaps ) ; } for ( int i = idx ; i < S . Length ; i ++ ) { for ( int j = i + 1 ; j < S . Length ; j ++ ) { swap ( S , i , j ) ; minimumSwaps ( S , swaps + 1 , i + 1 ) ; S = swap ( S , i , j ) ; } } } static char [ ] swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void findMinimumSwaps ( char [ ] S ) { ansSwaps = int . MaxValue ; minimumSwaps ( S , 0 , 0 ) ; if ( ansSwaps == int . MaxValue ) Console . Write ( " - 1" ) ; else Console . Write ( ansSwaps ) ; } public static void Main ( String [ ] args ) { String S = " ABAACD " ; findMinimumSwaps ( S . ToCharArray ( ) ) ; } }


"Minimize swaps of same | C # program for the above approach ; Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call"
Solution in C#:

 using System ; public class GFG { static void countSwaps ( string S , string T ) { int [ ] value = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; int N = S . Length ; int sum1 = 0 ; int sum2 = 0 ; bool flag = false ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += value [ S [ i ] - ' a ' ] ; sum2 += value [ T [ i ] - ' a ' ] ; if ( ( value [ S [ i ] - ' a ' ] % 2 == 0 && value [ T [ i ] - ' a ' ] % 2 == 1 ) || ( value [ S [ i ] - ' a ' ] % 2 == 1 && value [ T [ i ] - ' a ' ] % 2 == 0 ) ) flag = false ; } if ( sum1 % 2 == 1 && sum2 % 2 == 1 ) Console . Write ( "0
 " ) ; else if ( sum1 % 2 == 0 && sum2 % 2 == 0 ) { if ( flag ) Console . Write ( "1" ) ; else Console . Write ( " - 1" ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { string S = " acd " ; string T = " dbf " ; countSwaps ( S , T ) ; } }


"Minimize swaps required to make all prime | C # program for the above approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Driver Code ; Pre - calculate prime [ ]"
Solution in C#:

using System ; class GFG { static bool [ ] prime = new bool [ ( int ) 1e4 + 2 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < ( int ) 1e4 + 2 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < ( int ) 1e4 + 2 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < ( int ) 1e4 + 2 ; i += p ) prime [ i ] = false ; } } } static int countMin ( int [ ] arr , int n ) { int cMinSwaps = 0 ; int cPrimeIndices = 0 ; int cPrimeNos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ i + 1 ] ) { cPrimeIndices ++ ; if ( prime [ arr [ i ] ] == false ) cMinSwaps ++ ; else cPrimeNos ++ ; } else if ( prime [ arr [ i ] ] ) { cPrimeNos ++ ; } } if ( cPrimeNos >= cPrimeIndices ) return cMinSwaps ; else return - 1 ; } public static void Main ( ) { SieveOfEratosthenes ( ) ; int n = 5 ; int [ ] arr = { 2 , 7 , 8 , 5 , 13 } ; Console . Write ( countMin ( arr , n ) ) ; } }


"Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | C # program for the above approach ; Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static int minimum_swaps ( int [ ] arr , int n ) { int count = 0 ; int max_el = arr . Max ( ) ; int min_el = arr . Min ( ) ; if ( min_el == max_el ) return 0 ; int index_max = - 1 ; int index_min = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max_el && index_max == - 1 ) { index_max = i ; } if ( arr [ i ] == min_el ) { index_min = i ; } } count += index_max ; count += ( n - 1 - index_min ) ; if ( index_min < index_max ) count -= 1 ; return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 6 , 5 } ; int N = arr . Length ; Console . WriteLine ( minimum_swaps ( arr , N ) ) ; } }


"Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C # Program to implement the above approach ; Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static int countSwaps ( int [ ] A , int n ) { Array . Sort ( A ) ; int ind = 1 , res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ind < n && A [ ind ] == A [ i ] ) ind ++ ; if ( ind < n && A [ ind ] > A [ i ] ) { res ++ ; ind ++ ; } if ( ind >= n ) break ; } return res ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 3 , 3 , 2 , 5 } ; Console . Write ( countSwaps ( A , 5 ) ) ; } }


"Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C # program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSwaps ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } max_frequency = Math . Max ( max_frequency , mp [ arr [ i ] ] ) ; } return n - max_frequency ; } public static void Main ( String [ ] args ) { int [ ] A = { 6 , 5 , 4 , 3 , 2 , 1 } ; Console . Write ( countSwaps ( A , 6 ) ) ; } }


"Minimize swaps required to place largest and smallest array elements at first and last array indices | C # program for the above approach ; Function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; Stores the smallest array element ; Stores the smallest array element ; Stores the last occurrence of the smallest array element ; Stores the first occurrence of the largest array element ; Traverse the array [ ] arr ; If a [ i ] is less than min_element ; Update min_element ; Update min_ind ; If a [ i ] is greater than max_element ; Update max_element ; Update max_ind ; If max_ind is equal to min_ind ; Return 0 ; If max_ind is greater than min_ind ; Otherwise ; Driver Code ; Input ; Print the result"
Solution in C#:

 using System ; public class GFG { static int minimumMoves ( int [ ] a , int n ) { int min_element = int . MaxValue ; int max_element = int . MinValue ; int min_ind = - 1 ; int max_ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= min_element ) { min_element = a [ i ] ; min_ind = i ; } if ( a [ i ] > max_element ) { max_element = a [ i ] ; max_ind = i ; } } if ( max_ind == min_ind ) { return 0 ; } else if ( max_ind > min_ind ) { return max_ind + ( n - min_ind - 2 ) ; } else { return max_ind + n - min_ind - 1 ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 35 , 46 , 17 , 23 } ; int N = arr . Length ; Console . Write ( minimumMoves ( arr , N ) + "
 " ) ; } }


"Minimize the absolute difference of sum of two subsets | C # program for Minimize the absolute difference of sum of two subsets ; function to print difference ; summation of n elements ; if divisible by 4 ; if remainder 1 or 2. In case of remainder 2 , we divide elements from 3 to n in groups of size 4 and put 1 in one group and 2 in group . This also makes difference 1. ; We put elements from 4 to n in groups of size 4. Remaining elements 1 , 2 and 3 can be divided as ( 1 , 2 ) and ( 3 ) . ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + ( ( s / 2 ) + 1 ) ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 1 ) ; } else { Console . WriteLine ( " First ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Second ▁ " + " subset ▁ sum ▁ = ▁ " + s / 2 ) ; Console . WriteLine ( " Difference " + " ▁ = ▁ " + 0 ) ; } } } public static void Main ( ) { int n = 6 ; subsetDifference ( n ) ; } }


"Minimize the cost of buying the Objects | C # implementation of the approach ; Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function"
Solution in C#:

using System ; class GFG { static int totalPay ( int totalItems , int priceOfOneItem , int N , int M ) { int freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; int amount = actual * priceOfOneItem ; return amount ; } public static void Main ( String [ ] args ) { int T = 12 , P = 8 ; int N = 2 , M = 1 ; Console . Write ( " Amount ▁ = ▁ " + totalPay ( T , P , N , M ) ) ; } }


"Minimize the cost of partitioning an array into K groups | C # implementation of above approach ; Function to return the minimum number of operations needed to partition the array in k contiguous groups such that all elements of a given group are identical ; n is the size of the array ; dp ( i , j ) represents the minimum cost for partitioning the array [ 0. . i ] into j groups ; Base case , cost is 0 for parititoning the array [ 0. .0 ] into 1 group ; Fill dp ( i , j ) and the answer will be stored at dp ( n - 1 , k ) ; The maximum groups that the segment 0. . i can be divided in is represented by maxGroups ; Initialize dp ( i , j ) to infinity ; Divide segment 0. . i in 1 group ; map and freqOfMode are together used to keep track of the frequency of the most occurring element in [ 0. . i ] ; Change all the elements in the range 0. . i to the most frequent element in this range ; If the jth group is the segment from it . . i , we change all the elements in this range to this range 's most occurring element ; Number of elements we need to change in the jth group i . e . the range it . . i ; For all the possible sizes of the jth group that end at the ith element we pick the size that gives us the minimum cost for dp ( i , j ) elementsToChange is the cost of making all the elements in the jth group identical and we make use of dp ( it - 1 , j - 1 ) to find the overall minimal cost ; Return the minimum cost for partitioning array [ 0. . n - 1 ] into k groups which is stored at dp ( n - 1 , k ) ; Driver code"
Solution in C#:

using System ; class GFG { static int getMinimumOps ( int [ ] ar , int k ) { int n = ar . Length ; int [ , ] dp = new int [ n , k + 1 ] ; dp [ 0 , 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int maxGroups = Math . Min ( k , i + 1 ) ; for ( int j = 1 ; j <= maxGroups ; j ++ ) { dp [ i , j ] = int . MaxValue ; if ( j == 1 ) { int [ ] freq = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = 0 ; it <= i ; it ++ ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; } dp [ i , 1 ] = ( i + 1 ) - freqOfMode ; } else { int [ ] freq = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = i ; it >= j - 1 ; it -- ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; int elementsToChange = i - it + 1 ; elementsToChange -= freqOfMode ; dp [ i , j ] = Math . Min ( dp [ it - 1 , j - 1 ] + elementsToChange , dp [ i , j ] ) ; } } } } return dp [ n - 1 , k ] ; } public static void Main ( String [ ] args ) { int k = 3 ; int [ ] ar = { 3 , 1 , 3 , 3 , 2 , 1 , 8 , 5 } ; Console . WriteLine ( getMinimumOps ( ar , k ) ) ; } }


"Minimize the cost of selecting two numbers whose product is X | C # implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; If i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to minimize the cost of finding two numbers for every number such that the product of those two is equal to X ; Pre - calculation ; If X == 1 , then there is no way to find N and M . Print - 1 ; Case 3 is always valid and cost for that is C + X C for choosing 1 and M = X / 1 ; Case 1 N is prime , first number cost is fixed N is max_prime number divides this number ; If X is prime then the maximum prime number is the number itself . For this case , M becomes 1 and this shouldn 't be considered. ; Find M for this case ; Add cost for the second number also ; Update min_cost , if the cost for prime is minimum ; Case 2 If N is composite For this find the minimum prime number that divides A [ i ] and consider this as M ; Find N for that number ; Check if this number is composite or not if N is prime then there is no way to find any composite number that divides X If N = min_prime [ N ] then N is prime ; Update min_cost , if the cost for the composite is minimum ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 1000 ; static int [ ] max_prime = new int [ MAX ] ; static int [ ] min_prime = new int [ MAX ] ; static void sieve ( int n ) { for ( int i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static int findCost ( int A , int B , int C , int X ) { sieve ( MAX ) ; int N , M ; if ( X == 1 ) { return - 1 ; } int min_cost = C + X ; int cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . Min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { int cost_for_comp = B + M ; min_cost = Math . Min ( min_cost , cost_for_comp ) ; } return min_cost ; } public static void Main ( string [ ] args ) { int A = 7 , B = 11 , C = 2 , X = 20 ; Console . WriteLine ( findCost ( A , B , C , X ) ) ; } }