codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the size of array by deleting exactly k sub | C # implementation of the approach ; Sieve of Eratosthenes ; Function to return the size of the maximized array ; Insert the indices of composite numbers int num = 0 ; ; Compute the number of prime between two consecutive composite num = 0 ; ; Sort the diff vector ; Compute the prefix sum of diff vector ; Impossible case ; Delete sub - arrays of length 1 ; Find the number of primes to be deleted when deleting the sub - arrays ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int N = 1000005 ; static int [ ] prime = new int [ N ] ; static void seive ( ) { for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i + i ; j < N ; j += i ) { prime [ j ] = 1 ; } } } prime [ 1 ] = 1 ; } static int maxSizeArr ( int [ ] arr , int n , int k ) { List < int > v = new List < int > ( ) ; List < int > diff = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 1 ) { v . Add ( i ) ; } } for ( int i = 1 ; i < v . Count ; i ++ ) { diff . Add ( v [ i ] - v [ i - 1 ] - 1 ) ; } diff . Sort ( ) ; for ( int i = 1 ; i < diff . Count ; i ++ ) { diff [ i ] = diff [ i ] + diff [ i - 1 ] ; } if ( k > n || ( k == 0 && v . Count > 0 ) ) { return - 1 ; } else if ( v . Count <= k ) { return ( n - k ) ; } else if ( v . Count > k ) { int tt = v . Count - k ; int sum = 0 ; sum += diff [ tt - 1 ] ; int res = n - ( v . Count + sum ) ; return res ; } return 1 ; } public static void Main ( String [ ] args ) { seive ( ) ; int [ ] arr = { 2 , 4 , 2 , 2 , 4 , 2 , 4 , 2 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( maxSizeArr ( arr , n , k ) ) ; } }
Count number of subsets whose median is also present in the same subset | C # implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
using System ; class GFG { static long mod = 1000000007 ; static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static long powmod ( long a , long n ) { if ( n == 0 ) return 1 ; long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) return ( pt * a ) % mod ; else return pt ; } static long CountSubset ( int [ ] arr , int n ) { long ans = powmod ( 2 , n - 1 ) ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && arr [ j ] == arr [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + nCr ( l + r , l ) ) % mod ; j ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( CountSubset ( arr , n ) ) ; } }
Count number of subsets whose median is also present in the same subset | C # implementation of the above approach ; Function to store pascal triangle in 2 - d array ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code
using System ; class GFG { static long mod = 1000000007 ; static long [ , ] arr = new long [ 1001 , 1001 ] ; static void Preprocess ( ) { arr [ 0 , 0 ] = 1 ; for ( int i = 1 ; i <= 1000 ; ++ i ) { arr [ i , 0 ] = 1 ; for ( int j = 1 ; j < i ; ++ j ) { arr [ i , j ] = ( arr [ i - 1 , j - 1 ] + arr [ i - 1 , j ] ) % mod ; } arr [ i , i ] = 1 ; } } static long powmod ( long a , long n ) { if ( n == 0 ) { return 1 ; } long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) { return ( pt * a ) % mod ; } else { return pt ; } } static long CountSubset ( int [ ] val , int n ) { long ans = powmod ( 2 , n - 1 ) ; Array . Sort ( val ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && val [ j ] == val [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + arr [ l + r , l ] ) % mod ; j ++ ; } } return ans ; } public static void Main ( String [ ] args ) { Preprocess ( ) ; int [ ] val = { 2 , 3 , 2 } ; int n = val . Length ; Console . WriteLine ( CountSubset ( val , n ) ) ; } }
Reorder the position of the words in alphabetical order | C # implementation of the approach ; Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void reArrange ( String [ ] words , int n ) { Dictionary < String , int > freq = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq . Add ( words [ i ] , ( i + 1 ) ) ; } Array . Sort ( words ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( freq [ words [ i ] ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { String [ ] words = { " live " , " place " , " travel " , " word " , " sky " } ; int n = words . Length ; reArrange ( words , n ) ; } }
Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | C # implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code
using System ; class GFG { static int MAX = 100000 ; static int findSumofEle ( int [ ] arr1 , int m , int [ ] arr2 , int n ) { int [ ] hash = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr2 [ i ] ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } int maximumFreq = 0 ; for ( int i = 0 ; i < m ; i ++ ) { maximumFreq = Math . Max ( maximumFreq , hash [ arr1 [ i ] ] ) ; } int sumOfElements = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sumOfElements += ( maximumFreq == hash [ arr1 [ i ] ] ) ? arr1 [ i ] : 0 ; } return sumOfElements ; } public static void Main ( ) { int [ ] arr1 = { 2 , 5 , 6 , 8 } ; int [ ] arr2 = { 4 , 10 } ; int m = arr1 . Length ; int n = arr2 . Length ; Console . WriteLine ( findSumofEle ( arr1 , m , arr2 , n ) ) ; } }
Print 2 | C # implementation of the above approach ; Function to print the coordinates along with their frequency in ascending order ; Map to store the pairs and their frequencies ; Store the coordinates along with their frequencies ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class store : IComparer < KeyValuePair < int , int > > { public int Compare ( KeyValuePair < int , int > x , KeyValuePair < int , int > y ) { if ( x . Key != y . Key ) { return x . Key . CompareTo ( y . Key ) ; } else { return x . Value . CompareTo ( y . Value ) ; } } } static void Print ( int [ ] x , int [ ] y , int n ) { SortedDictionary < KeyValuePair < int , int > , int > m = new SortedDictionary < KeyValuePair < int , int > , int > ( new store ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { KeyValuePair < int , int > tmp = new KeyValuePair < int , int > ( x [ i ] , y [ i ] ) ; if ( m . ContainsKey ( tmp ) ) { m [ tmp ] ++ ; } else { m [ tmp ] = 1 ; } } foreach ( KeyValuePair < KeyValuePair < int , int > , int > i in m ) { Console . Write ( i . Key . Key + " ▁ " + i . Key . Value + " ▁ " + i . Value + " STRNEWLINE " ) ; } } public static void Main ( string [ ] args ) { int [ ] x = { 1 , 2 , 1 , 1 , 1 } ; int [ ] y = { 1 , 1 , 3 , 1 , 3 } ; int n = x . Length ; Print ( x , y , n ) ; } }
Split the array elements into strictly increasing and decreasing sequence | C # program to implement the above approach ; Function to print both the arrays ; Store both arrays ; Used for hashing ; Iterate for every element ; Increase the Count ; If first occurrence ; If second occurrence ; If occurs more than 2 times ; Sort in increasing order ; Print the increasing array ; Sort ; Print the decreasing array ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void PrintBothArrays ( int [ ] a , int n ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mpp . ContainsKey ( a [ i ] ) ) mpp [ a [ i ] ] = mpp [ a [ i ] ] + 1 ; else mpp [ a [ i ] ] = 1 ; if ( mpp [ a [ i ] ] == 1 ) v1 . Add ( a [ i ] ) ; else if ( mpp [ a [ i ] ] == 2 ) v2 . Add ( a [ i ] ) ; else { Console . WriteLine ( " Not ▁ possible " ) ; return ; } } v1 . Sort ( ) ; Console . WriteLine ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v1 . Count ; i ++ ) Console . Write ( v1 [ i ] + " ▁ " ) ; v2 . Sort ( ) ; v2 . Reverse ( ) ; Console . WriteLine ( " STRNEWLINE Strictly ▁ decreasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v2 . Count ; i ++ ) Console . Write ( v2 [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 7 , 2 , 7 , 3 , 3 , 1 , 4 } ; int n = a . Length ; PrintBothArrays ( a , n ) ; } }
Iterative selection sort for linked list |  ; Traverse the List ; Traverse the unsorted sublist ; Swap Data
static void selectionSort ( node head ) { node temp = head ; while ( temp ) { node min = temp ; node r = temp . next ; while ( r ) { if ( min . data > r . data ) min = r ; r = r . next ; } int x = temp . data ; temp . data = min . data ; min . data = x ; temp = temp . next ; } }
Sort ugly numbers in an array at their relative positions | C # implementation of the approach ; Function that returns true if n is an ugly number ; While divisible by 2 , keep dividing ; While divisible by 3 , keep dividing ; While divisible by 5 , keep dividing ; n must be 1 if it was ugly ; Function to sort ugly numbers in their relative positions ; To store the ugly numbers from the array ; If current element is an ugly number ; Add it to the ArrayList and set arr [ i ] to - 1 ; Sort the ugly numbers ; Position of an ugly number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isUgly ( int n ) { while ( n % 2 == 0 ) n = n / 2 ; while ( n % 3 == 0 ) n = n / 3 ; while ( n % 5 == 0 ) n = n / 5 ; if ( n == 1 ) return true ; return false ; } static void sortUglyNumbers ( int [ ] arr , int n ) { List < int > list = new List < int > ( ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( isUgly ( arr [ i ] ) ) { list . Add ( arr [ i ] ) ; arr [ i ] = - 1 ; } } list . Sort ( ) ; int j = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) Console . Write ( list [ j ++ ] + " ▁ " ) ; else Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 7 , 12 , 10 } ; int n = arr . Length ; sortUglyNumbers ( arr , n ) ; } }
Sort elements of the array that occurs in between multiples of K | C # implementation of the approach ; Utility function to print the contents of an array ; Function to sort elements in between multiples of k ; To store the index of previous multiple of k ; If it is not the first multiple of k ; Sort the elements in between the previous and the current multiple of k ; Update previous to be current ; Print the updated array ; Driver code
using System . Collections ; using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void sortArr ( int [ ] arr , int n , int k ) { int prev = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { if ( prev != - 1 ) Array . Sort ( arr , prev + 1 , i - ( prev + 1 ) ) ; prev = i ; } } printArr ( arr , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 13 , 3 , 7 , 8 , 21 , 13 , 12 } ; int n = arr . Length ; int k = 2 ; sortArr ( arr , n , k ) ; } }
Find A and B from list of divisors | C # implementation of the approach ; Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code
using System . Collections ; using System ; class GfG { static void printNumbers ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int A = arr [ n - 1 ] , B = - 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( A % arr [ i ] != 0 ) { B = arr [ i ] ; break ; } if ( i - 1 >= 0 && arr [ i ] == arr [ i - 1 ] ) { B = arr [ i ] ; break ; } } Console . WriteLine ( " A ▁ = ▁ " + A + " , ▁ B ▁ = ▁ " + B ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 } ; int n = arr . Length ; printNumbers ( arr , n ) ; } }
Find the modified array after performing k operations of given type | C # implementation of the approach ; Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code
using System ; class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void removeMin ( int [ ] arr , int n ) { int i , minVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) minVal = Math . Min ( minVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] - minVal ; } static void removeFromMax ( int [ ] arr , int n ) { int i , maxVal = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) maxVal = Math . Max ( maxVal , arr [ i ] ) ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = maxVal - arr [ i ] ; } static void modifyArray ( int [ ] arr , int n , int k ) { if ( k % 2 == 0 ) removeMin ( arr , n ) ; else removeFromMax ( arr , n ) ; printArray ( arr , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 12 , 16 } ; int n = arr . Length ; int k = 2 ; modifyArray ( arr , n , k ) ; } }
Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k | C # implementation of the approach ; Function to return the maximum product value ; To store the product ; Sort the array ; Efficiently finding product including every element once ; Storing values in hash map ; Including the greater repeating values so that product can be maximized ; Driver code
using System ; using System . Collections . Generic ; class GFG { static long maxProd ( int [ ] arr , int n , int k ) { long product = 1 ; Dictionary < int , int > s = new Dictionary < int , int > ( ) ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . ContainsKey ( arr [ i ] ) ) { product = product * arr [ i ] ; s [ arr [ i ] ] = 1 ; } else s [ arr [ i ] ] ++ ; } for ( int j = n - 1 ; j >= 0 && k > 0 ; j -- ) { if ( ( k > ( s [ arr [ j ] ] - 1 ) ) && ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) { product *= ( long ) Math . Pow ( arr [ j ] , s [ arr [ j ] ] - 1 ) ; k = k - s [ arr [ j ] ] + 1 ; s [ arr [ j ] ] = 0 ; } if ( k <= ( s [ arr [ j ] ] - 1 ) && ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) { product *= ( long ) Math . Pow ( arr [ j ] , k ) ; break ; } } return product ; } public static void Main ( ) { int [ ] arr = { 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( maxProd ( arr , n , k ) ) ; } }
Minimize the sum of the squares of the sum of elements of each group the array is divided into | C # implementation of the approach ; Function to return the minimized sum ; Sort the array to pair the elements ; Variable to hold the answer ; Pair smallest with largest , second smallest with second largest , and so on ; Driver code
using System ; class GFG { static int findAnswer ( int n , int [ ] arr ) { Array . Sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } static void Main ( ) { int [ ] arr = { 53 , 28 , 143 , 5 } ; int n = arr . Length ; Console . WriteLine ( findAnswer ( n , arr ) ) ; } }
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | C # program to merge K sorted arrays ; Function to perform merge operation ; to store the starting point of left and right array ; to store the size of left and right array ; array to temporarily store left and right array ; storing data in left array ; storing data in right array ; to store the current index of temporary left and right array ; to store the current index for output array ; two pointer merge for two sorted arrays ; Code to drive merge - sort and create recursion tree ; base step to initialize the output array before performing merge operation ; to sort left half ; to sort right half ; merge the left and right half ; Driver Code ; input 2D - array ; Number of arrays ; Output array ; Print merged array
using System ; class GFG { static int n = 4 ; static void merge ( int l , int r , int [ ] output ) { int l_in = l * n , r_in = ( ( l + r ) / 2 + 1 ) * n ; int l_c = ( ( l + r ) / 2 - l + 1 ) * n ; int r_c = ( r - ( l + r ) / 2 ) * n ; int [ ] l_arr = new int [ l_c ] ; int [ ] r_arr = new int [ r_c ] ; for ( int i = 0 ; i < l_c ; i ++ ) l_arr [ i ] = output [ l_in + i ] ; for ( int i = 0 ; i < r_c ; i ++ ) r_arr [ i ] = output [ r_in + i ] ; int l_curr = 0 , r_curr = 0 ; int index = l_in ; while ( l_curr + r_curr < l_c + r_c ) { if ( r_curr == r_c || ( l_curr != l_c && l_arr [ l_curr ] < r_arr [ r_curr ] ) ) { output [ index ] = l_arr [ l_curr ] ; l_curr ++ ; index ++ ; } else { output [ index ] = r_arr [ r_curr ] ; r_curr ++ ; index ++ ; } } } static void divide ( int l , int r , int [ ] output , int [ , ] arr ) { if ( l == r ) { for ( int i = 0 ; i < n ; i ++ ) output [ l * n + i ] = arr [ l , i ] ; return ; } divide ( l , ( l + r ) / 2 , output , arr ) ; divide ( ( l + r ) / 2 + 1 , r , output , arr ) ; merge ( l , r , output ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int k = arr . GetLength ( 0 ) ; int [ ] output = new int [ n * k ] ; divide ( 0 , k - 1 , output , arr ) ; for ( int i = 0 ; i < n * k ; i ++ ) Console . Write ( output [ i ] + " ▁ " ) ; } }
Make palindromic string non | C # Program to rearrange letters of string to find a non - palindromic string if it exists ; Function to print the non - palindromic string if it exists , otherwise prints - 1 ; If all characters are not same , set flag to 1 ; Update frequency of the current character ; If all characters are same ; Print characters in sorted manner ; Driver Code
using System ; class GfG { static void findNonPalinString ( char [ ] s ) { int [ ] freq = new int [ 26 ] ; int flag = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( flag == 0 ) Console . WriteLine ( " - 1" ) ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) Console . Write ( ( char ) ( ' a ' + i ) ) ; } } public static void Main ( ) { string s = " abba " ; findNonPalinString ( s . ToCharArray ( ) ) ; } }
Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; If not possible ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; Return minimum operations required ; Driver code
using System ; class GFG { static int minOperations ( int n , int m , int k , int [ , ] matrix ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 , 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i , j ] ; if ( matrix [ i , j ] % k != mod ) { return - 1 ; } } } Array . Sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void Main ( ) { int [ , ] matrix = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . GetLength ( 0 ) ; int m = matrix . GetLength ( 1 ) ; int k = 2 ; Console . WriteLine ( minOperations ( n , m , k , matrix ) ) ; } }
Count distinct elements in an array | C # program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver code
using System ; class GFG { static int countDistinct ( int [ ] arr , int n ) { int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) res ++ ; } return res ; } public static void Main ( ) { int [ ] arr = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . Length ; Console . WriteLine ( countDistinct ( arr , n ) ) ; } }
Count distinct elements in an array | C # program to count all distinct elements in a given array ; First sort the array so that all occurrences become consecutive ; Traverse the sorted array ; Move the index ahead while there are duplicates ; Driver code
using System ; class GFG { static int countDistinct ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } public static void Main ( ) { int [ ] arr = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . Length ; Console . WriteLine ( countDistinct ( arr , n ) ) ; } }
Choose n elements such that their mean is maximum | C # implementation of the approach ; Utility function to print the contents of an array ; Function to print the array with maximum mean ; Sort the original array ; Construct new array ; Print the resultant array ; Driver code
using System ; class GfG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void printMaxMean ( int [ ] arr , int n ) { int [ ] newArr = new int [ n ] ; Array . Sort ( arr , 0 , 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = arr . Length ; printMaxMean ( arr , n / 2 ) ; } }