codeparrot/xlcost-text-to-code huggingface dataset:
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD | C # program for the above approach ; Function to check if array arr [ ] can be obtained from first N natural numbers or not ; Driver code ; Function Call
using System ; class GFG { static void isSequenceValid ( int [ ] B , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int N = 4 ; int [ ] arr = { 1 , 2 , 3 , 2 } ; isSequenceValid ( arr , N ) ; } }
Construct an array of first N natural numbers such that every adjacent pair is coprime | C # program to implement the above approach ; Function to construct an arrary with adjacent elements as co - prime numbers ; Iterate over the range [ 1 , N ] ; Print i ; Driver Code
using System ; class GFG { static void ConstArrayAdjacentCoprime ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int N = 6 ; ConstArrayAdjacentCoprime ( N ) ; } }
Maximize count of rows consisting of equal elements by flipping columns of a Matrix | C # program to implement the above approach ; Function to find the maximum number of rows containing all equal elements ; Store each row of the matrix ; Traverse each row of the matrix ; Update frequency of current row ; Stores maximum count of rows containing all equal elements ; Traverse each row of the matrix ; Stores 1 's complement  of the current row ; Traverse current row of the given matrix ; Stores 1 s complement of mat [ i ] [ j ] ; Update cntMaxRows ; Driver code ; Stores count of rows ; Stores count of columns
using System ; using System . Collections . Generic ; class GFG { static int maxEqrows ( List < List < int > > mat , int N , int M ) { Dictionary < List < int > , int > mp = new Dictionary < List < int > , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( mat [ i ] ) ) { mp [ mat [ i ] ] ++ ; } else { mp [ mat [ i ] ] = 1 ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { List < int > onesCompRow = new List < int > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . Add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow [ j ] = ( mat [ i ] [ j ] ^ 1 ) ; } if ( ! mp . ContainsKey ( mat [ i ] ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ onesCompRow ] + 1 ) ; } else if ( ! mp . ContainsKey ( onesCompRow ) ) { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + 1 ) ; } else { cntMaxRows = Math . Max ( cntMaxRows , mp [ mat [ i ] ] + mp [ onesCompRow ] + 1 ) ; } } return cntMaxRows ; } static void Main ( ) { List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > { 0 , 1 , 0 , 0 , 1 } ) ; mat . Add ( new List < int > { 1 , 1 , 0 , 1 , 1 } ) ; mat . Add ( new List < int > { 1 , 0 , 1 , 1 , 0 } ) ; int N = mat . Count ; int M = mat [ 0 ] . Count ; Console . WriteLine ( maxEqrows ( mat , N , M ) ) ; } }
Minimum cost to convert one given string to another using swap , insert or delete operations | C # program for the above approach ; Function to find the minimum cost to convert string a to b ; Stores the frequency of string a and b respectively ; Store the frequencies of characters in a ; Store the frequencies of characters in b ; Minimum cost to convert A to B ; Find the minimum cost ; Print the minimum cost ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static void minimumCost ( string a , string b ) { int [ ] fre1 = new int [ 256 ] ; int [ ] fre2 = new int [ 256 ] ; foreach ( char c in a . ToCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; foreach ( char c in b . ToCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . Abs ( fre1 [ i ] - fre2 [ i ] ) ; } Console . Write ( mincost ) ; } public static void Main ( ) { string A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }
Minimum cost required to move all elements to the same position | C # program to implement the above approach ; Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call
using System ; class GFG { public void minCost ( int [ ] arr ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } Console . Write ( Math . Min ( even , odd ) ) ; } public static void Main ( ) { GFG obj = new GFG ( ) ; int [ ] arr = { 1 , 2 , 3 } ; obj . minCost ( arr ) ; } }
Maximize array sum by alternating the signs of adjacent elements | C # program to implement the above approach ; Function to find the maximum sum by alternating the signs of adjacent elements of the array ; Stores count of negative elements in the array ; Stores maximum sum by alternating the signs of adjacent elements ; Stores smallest absolute value of array elements ; Stores sum of absolute value of array elements ; Traverse the array ; If arr [ i ] is a negative number ; Update cntNeg ; Update sum ; Update SmValue ; Update MaxAltSum ; If cntNeg is an odd number ; Update MaxAltSum ; Driver Code
using System ; class GFG { static int findMaxSumByAlternatingSign ( int [ ] arr , int N ) { int cntNeg = 0 ; int MaxAltSum = 0 ; int SmValue = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += Math . Abs ( arr [ i ] ) ; SmValue = Math . Min ( SmValue , Math . Abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg % 2 == 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , - 2 , - 4 , 5 } ; int N = arr . Length ; Console . Write ( findMaxSumByAlternatingSign ( arr , N ) ) ; } }
Construct array with sum of product of same indexed elements in the given array equal to zero | C # program to implement the above approach ; Function to generate a new array with product of same indexed elements with arr [ ] equal to 0 ; Stores sum of same indexed array elements of arr and new array ; Traverse the array ; If i is an even number ; Insert arr [ i + 1 ] into the new array newArr [ ] ; Insert - arr [ i - 1 ] into the new array newArr [ ] ; Print new array elements ; Driver Code
using System ; class GFG { static void constructNewArraySumZero ( int [ ] arr , int N ) { int [ ] newArr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( newArr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , - 5 , - 6 , 8 } ; int N = arr . Length ; constructNewArraySumZero ( arr , N ) ; } }
Generate Bitonic Sequence of length N from integers in a given range | C # program for the above approach ; Function to construct bitonic sequence of length N from integers in the range [ L , R ] ; If sequence is not possible ; Store the resultant list ; If size of deque < n ; Add elements from start ; Print the stored in the list ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static void bitonicSequence ( int num , int lower , int upper ) { if ( num > ( upper - lower ) * 2 + 1 ) { Console . WriteLine ( - 1 ) ; return ; } List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < Math . Min ( upper - lower + 1 , num - 1 ) ; i ++ ) ans . Add ( upper - i ) ; for ( int i = 0 ; i < num - ans . Count ; i ++ ) ans . Insert ( 0 , upper - i - 1 ) ; Console . Write ( " [ " ) ; foreach ( int x in ans ) Console . Write ( x + " , ▁ " ) ; Console . Write ( " ] " ) ; } public static void Main ( String [ ] args ) { int N = 5 , L = 3 , R = 10 ; bitonicSequence ( N , L , R ) ; } }
Count substrings of same length differing by a single character from two given strings | C # program for the above approach ; Function to count the number of subStrings of equal length which differ by a single character ; Stores the count of pairs of subStrings ; Traverse the String s ; Traverse the String t ; Different character ; Increment the answer ; Count equal subStrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the readonly count ; Driver Code ; Function Call
using System ; class GFG { static int countSubStrings ( String s , String t ) { int answ = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { for ( int j = 0 ; j < t . Length ; j ++ ) { if ( t [ j ] != s [ i ] ) { answ += 1 ; int k = 1 ; int z = - 1 ; int q = 1 ; while ( j + z >= 0 && 0 <= i + z && s [ i + z ] == t [ j + z ] ) { z -= 1 ; answ += 1 ; q += 1 ; } while ( s . Length > i + k && j + k < t . Length && s [ i + k ] == t [ j + k ] ) { k += 1 ; answ += q ; z = - 1 ; } } } } return answ ; } public static void Main ( String [ ] args ) { String S = " aba " ; String T = " baba " ; Console . WriteLine ( countSubStrings ( S , T ) ) ; } }
Minimum flips to make all 1 s in right and 0 s in left | C # program to implement the above approach ; Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and 1 s on the right ; Stores count of 1 s on the left side of each index ; Stores count of flips required to make all 0 s on the left and 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver code
using System ; class GFG { public static int minimumCntOfFlipsRequired ( string str ) { int n = str . Length ; int zeros = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } int minFlips = Int32 . MaxValue ; int currOnes = 0 ; int flips ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . Min ( minFlips , flips ) ; } return minFlips ; } public static void Main ( ) { string s1 = "100101" ; Console . WriteLine ( minimumCntOfFlipsRequired ( s1 ) ) ; } }
Maximize length of longest non | C # program to implement the above approach ; Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
using System ; class GFG { static int findLongestNonDecreasing ( int [ ] A , int N ) { int res = 0 ; int start = 0 ; int end = N - 1 ; int prev = - 1 ; while ( start <= end ) { if ( A [ start ] <= A [ end ] ) { if ( prev == - 1 ) { prev = A [ start ] ; res ++ ; start ++ ; } else { if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else { break ; } } } else { if ( prev == - 1 ) { prev = A [ end ] ; res ++ ; end -- ; } else { if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else { break ; } } } } return res ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 } ; int N = A . Length ; Console . Write ( findLongestNonDecreasing ( A , N ) ) ; } }
Non | C # program for the above approach ; Function to print all pairs whose sum of Bitwise OR and AND is N ; Iterate from i = 0 to N ; Print pairs ( i , N - i ) ; Driver code
using System ; class GFG { static void findPairs ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { Console . Write ( " ( " + i + " , ▁ " + ( N - i ) + " ) , ▁ " ) ; } } public static void Main ( ) { int N = 5 ; findPairs ( N ) ; } }
Minimum index to split array into subarrays with co | C # program for the above approach ; Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver code ; Function call
using System ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findPartition ( int [ ] nums , int N ) { int [ ] prefix = new int [ N ] ; int [ ] suffix = new int [ N ] ; int i , k ; prefix [ 0 ] = nums [ 0 ] ; for ( i = 1 ; i < N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] * nums [ i ] ; } suffix [ N - 1 ] = nums [ N - 1 ] ; for ( i = N - 2 ; i >= 0 ; i -- ) { suffix [ i ] = suffix [ i + 1 ] * nums [ i ] ; } for ( k = 0 ; k < N - 1 ; k ++ ) { if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) { return k ; } } return - 1 ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( findPartition ( arr , N ) ) ; } }
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | C # program for the above approach ; Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having integers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to print the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Driver code ; Given array arr [ ] ; Size of array ; Function Call
using System ; class GFG { static int separate ( int [ ] arr , int n , int parity ) { int count = 1 , res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) != 0 && ( ( arr [ i - 1 ] + parity ) & 1 ) != 0 ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void requiredOps ( int [ ] arr , int N ) { int res1 = separate ( arr , N , 0 ) ; int res2 = separate ( arr , N , 1 ) ; Console . Write ( Math . Max ( res1 , res2 ) ) ; } public static void Main ( ) { int [ ] arr = { 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 } ; int N = arr . Length ; requiredOps ( arr , N ) ; } }
Count prime pairs whose difference is also a Prime Number | C # program to implement the above approach ; Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code
using System ; class GFG { public static bool [ ] SieveOfEratosthenes ( int N ) { bool [ ] isPrime = new bool [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } public static int cntPairsdiffOfPrimeisPrime ( int N ) { int cntPairs = 0 ; bool [ ] isPrime = SieveOfEratosthenes ( N ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( cntPairsdiffOfPrimeisPrime ( N ) ) ; } }
Length of longest subsequence consisting of distinct adjacent elements | C # program for the above approach ; Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Print the maximum length ; Driver Code ; Size of Array ; Function Call
using System ; class GFG { static void longestSubsequence ( int [ ] arr , int N ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 } ; int N = arr . Length ; longestSubsequence ( arr , N ) ; } }
Count of substrings having the most frequent character in the string as first character | C # program for the above approach ; Function to find all substrings whose first character occurs maximum number of times ; Stores frequency of characters ; Stores character that appears maximum number of times ; Stores max frequency of character ; Updates frequency of characters ; Update maxfreq ; Character that occures maximum number of times ; Update the maximum frequency character ; Stores all count of substrings ; Traverse over string ; Get the current character ; Update count of substrings ; Return the count of all valid substrings ; Driver Code ; Function Call
using System ; class GFG { static int substringCount ( string s ) { int [ ] freq = new int [ 26 ] ; char max_char = ' # ' ; int maxfreq = Int32 . MinValue ; for ( int i = 0 ; i < s . Length ; i ++ ) { freq [ s [ i ] - ' a ' ] ++ ; if ( maxfreq < freq [ s [ i ] - ' a ' ] ) maxfreq = freq [ s [ i ] - ' a ' ] ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( maxfreq == freq [ i ] ) { max_char = ( char ) ( i + ' a ' ) ; break ; } } int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { char ch = s [ i ] ; if ( max_char == ch ) { ans += ( s . Length - i ) ; } } return ans ; } public static void Main ( ) { string S = " abcab " ; Console . WriteLine ( substringCount ( S ) ) ; } }
Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | C # program for the above approach ; Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Driver Code ; Given array [ ] arr ; Function Call
using System ; class GFG { static void minSum ( int [ ] arr , int n ) { int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } int s = 0 ; foreach ( int x in ans ) { s = s + x ; } Console . Write ( s + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 } ; int N = arr . Length ; minSum ( arr , N ) ; } }
Minimum removal of consecutive similar characters required to empty a Binary String | C # program for the above approach ; Function to find minimum steps to make the string empty ; Stores the modified string ; Size of string ; Removing substring of same character from modified string ; Print the minimum steps required ; Driver Code ; Given string S ; Function Call
using System ; class GFG { static void minSteps ( string S ) { string new_str = " " ; int N = S . Length ; int i = 0 ; while ( i < N ) { new_str += S [ i ] ; int j = i ; while ( i < N && S [ i ] == S [ j ] ) ++ i ; } Console . Write ( ( int ) Math . Ceiling ( ( new_str . Length + 1 ) / 2.0 ) ) ; } public static void Main ( ) { string S = "0010100" ; minSteps ( S ) ; } }
Count pairs with equal Bitwise AND and Bitwise OR value | C # program to implement the above approach ; Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] arr , int N ) { int cntPairs = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp . Add ( arr [ i ] , 1 ) ; else mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } foreach ( KeyValuePair < int , int > freq in mp ) { cntPairs += ( freq . Value * ( freq . Value - 1 ) ) / 2 ; } return cntPairs ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }
Minimum increments required to make given matrix palindromic | C # program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
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 palindromeMatrix ( int N , int M , int [ , ] arr ) { int ans = 0 ; for ( int i = 0 ; i < ( N + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < ( M + 1 ) / 2 ; j ++ ) { HashSet < pair > s = new HashSet < pair > ( ) ; s . Add ( new pair ( i , j ) ) ; s . Add ( new pair ( i , M - j - 1 ) ) ; s . Add ( new pair ( N - i - 1 , j ) ) ; s . Add ( new pair ( N - i - 1 , M - j - 1 ) ) ; List < int > values = new List < int > ( ) ; foreach ( pair p in s ) { values . Add ( arr [ p . first , p . second ] ) ; } values . Sort ( ) ; int max = values [ values . Count - 1 ] ; for ( int k = 1 ; k < values . Count ; k ++ ) { ans += max - values [ k ] ; } } } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int N = 3 , M = 3 ; int [ , ] arr = { { 1 , 2 , 1 } , { 3 , 4 , 1 } , { 1 , 2 , 1 } } ; palindromeMatrix ( N , M , arr ) ; } }