codeparrot/xlcost-text-to-code huggingface dataset:
Merge two unsorted linked lists to get a sorted list | C # program for the above approach ; Create structure for a node ; Function to print the linked list ; Store the head of the linked list into a temporary node and iterate ; Function takes the head of the List and the data as argument and if no List exists , it creates one with the head pointing to first node . If it exists already , it appends given node at end of the last node ; Create a new node ; Insert data into the temporary node and point it 's next to null ; Check if head is null , create a linked list with temp as head and tail of the list ; Else insert the temporary node after the tail of the existing node and make the temporary node as the tail of the linked list ; Return the list ; Function to concatenate the two lists ; Iterate through the head1 to find the last node join the next of last node of head1 to the 1 st node of head2 ; return the concatenated lists as a single list - head1 ; Sort the linked list using bubble sort ; Compares two adjacent elements and swaps if the first element is greater than the other one . ; Driver Code ; Given Linked List 1 ; Given Linked List 2 ; Merge the two lists in a single list ; Sort the unsorted merged list ; Print the final sorted merged list
using System ; class GFG { static node head1 = null ; static node head2 = null ; class node { public int data ; public node next ; } ; static void setData ( node head ) { node tmp ; tmp = head ; while ( tmp != null ) { Console . Write ( tmp . data + " ▁ - > ▁ " ) ; tmp = tmp . next ; } } static node getData ( node head , int num ) { node temp = new node ( ) ; node tail = head ; temp . data = num ; temp . next = null ; if ( head == null ) { head = temp ; tail = temp ; } else { while ( tail != null ) { if ( tail . next == null ) { tail . next = temp ; tail = tail . next ; } tail = tail . next ; } } return head ; } static node mergelists ( ) { node tail = head1 ; while ( tail != null ) { if ( tail . next == null && head2 != null ) { tail . next = head2 ; break ; } tail = tail . next ; } return head1 ; } static void sortlist ( ) { node curr = head1 ; node temp = head1 ; while ( curr . next != null ) { temp = curr . next ; while ( temp != null ) { if ( temp . data < curr . data ) { int t = temp . data ; temp . data = curr . data ; curr . data = t ; } temp = temp . next ; } curr = curr . next ; } } public static void Main ( String [ ] args ) { head1 = getData ( head1 , 4 ) ; head1 = getData ( head1 , 7 ) ; head1 = getData ( head1 , 5 ) ; head2 = getData ( head2 , 2 ) ; head2 = getData ( head2 , 1 ) ; head2 = getData ( head2 , 8 ) ; head2 = getData ( head2 , 1 ) ; head1 = mergelists ( ) ; sortlist ( ) ; setData ( head1 ) ; } }
Find K elements whose absolute difference with median of array is maximum | C # implementation to find first K elements whose difference with the median of array is maximum ; Function for calculating median ; Check for even case ; Function to find the K maximum absolute difference with the median of the array ; Sort the array . ; Store median ; Find and store difference ; If diff [ i ] is greater print it Else print diff [ j ] ; Driver Code
using System ; class GFG { static double findMedian ( int [ ] a , int n ) { if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } static void kStrongest ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; double median = findMedian ( arr , n ) ; int [ ] diff = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) { diff [ i ] = ( int ) Math . Abs ( median - arr [ i ] ) ; } int j = n - 1 ; i = 0 ; while ( k > 0 ) { if ( diff [ i ] > diff [ j ] ) { Console . Write ( arr [ i ] + " ▁ " ) ; i ++ ; } else { Console . Write ( arr [ j ] + " ▁ " ) ; j -- ; } k -- ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int k = 3 ; int n = arr . Length ; kStrongest ( arr , n , k ) ; } }
Sort an array by swapping elements of different type specified by another array | C # program to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Function to check if it is possible to sort the array in non - decreasing order by swapping elements of different types ; Consider the array to be already sorted ; Checking if array is already sorted ; Check for a pair which is in decreasing order ; Count the frequency of each type of element ; type0 stores count of elements of type 0 ; type1 stores count of elements of type 1 ; Return true if array is already sorted ; Return false if all elements are of same type and array is unsorted ; Possible for all other cases ; Driver code
using System ; class GFG { static bool sorting_possible ( int [ ] a , int [ ] b , int n ) { bool sorted = true ; int type1 = 0 , type0 = 0 , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { sorted = false ; break ; } } for ( i = 0 ; i < n ; i ++ ) { if ( b [ i ] == 0 ) type0 ++ ; else type1 ++ ; } if ( sorted ) return true ; else if ( type1 == n type0 == n ) return false ; else return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 15 , 1 , 2 , 17 , 6 } ; int [ ] b = { 1 , 1 , 0 , 1 , 1 } ; int n = a . Length ; bool res = sorting_possible ( a , b , n ) ; if ( res ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Maximize array sum by X increments when each element is divided by 10 | C # program for the above approach ; Initialize variables ; Add the current contribution of the element to the answer ; If the value is already maximum then we can 't change it ; Moves required to move to the next multiple of 10 ; No of times we can add 10 to this value so that its value does not exceed 1000. ; Sort the array ; Adding the values to increase the numbers to the next multiple of 10 ; If the total moves are less than X then increase the answer ; If the moves exceed X then we cannot increase numbers ; If there still remain some moves ; Remaining moves ; Add minimim of increments and remaining / 10 to the answer ; Output the readonly answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void maximizeval10 ( int [ ] a , int n , int k ) { int increments = 0 ; int ans = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . Add ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } v . Sort ( ) ; int sum = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += Math . Min ( increments , remaining / 10 ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int N = 4 ; int X = 6 ; int [ ] A = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; } }
Split array into two subarrays such that difference of their maximum is minimum | C # Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver code
using System ; class GFG { static int findMinDif ( int [ ] arr , int N ) { Array . Sort ( arr ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } public static void Main ( ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int N = arr . Length ; Console . Write ( findMinDif ( arr , N ) ) ; } }
Count of available non | C # implementation of the above approach ; Function to find the pivot index ; Function to implement Quick Sort ; Pivot is pointing to pivot index before which every element is smaller and after pivot , every element is greater ; Sort the array before pivot element ; Sort the array after pivot element ; Function to count the available intervals from the given range of numbers ; If range starts after 0 then an interval is available from 0 to start [ 0 ] ; When a new interval starts ; Since index variable i is being incremented , the current active interval will also get incremented ; When the current interval ends ; Since index variable j is being decremented , the currect active interval will also get decremented ; When start and end both are same there is no change in currActive ; If the end of interval is before the range so interval is available at the end ; Driver code ; Sort the start array ; Sort the end array ; Calling the function
using System ; class GFG { static int partition ( int [ ] arr , int l , int h ) { int pivot = arr [ l ] ; int i = l + 1 ; int j = h ; while ( i <= j ) { while ( i <= h && arr [ i ] < pivot ) { i ++ ; } while ( j > l && arr [ j ] > pivot ) { j -- ; } if ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } else i ++ ; } arr [ l ] = arr [ j ] ; arr [ j ] = pivot ; return j ; } static void sortArray ( int [ ] arr , int l , int h ) { if ( l >= h ) return ; int pivot = partition ( arr , l , h ) ; sortArray ( arr , l , pivot - 1 ) ; sortArray ( arr , pivot + 1 , h ) ; } static int findMaxIntervals ( int [ ] start , int [ ] end , int n , int R ) { int ans = 0 ; int prev = 0 ; int currActive = 0 ; int i = 0 ; int j = 0 ; if ( start [ 0 ] > 0 ) ans ++ ; while ( i < n && j < n ) { if ( start [ i ] < end [ j ] ) { i ++ ; currActive ++ ; } else if ( start [ i ] > end [ j ] ) { j ++ ; currActive -- ; } else { i ++ ; j ++ ; } if ( currActive == 0 ) { ans ++ ; } } if ( end [ n - 1 ] < R ) ans ++ ; return ans ; } public static void Main ( ) { int R , N ; R = 10 ; N = 3 ; int [ ] start = new int [ ] { 2 , 5 , 8 } ; int [ ] end = new int [ ] { 3 , 9 , 10 } ; sortArray ( start , 0 , N - 1 ) ; sortArray ( end , 0 , N - 1 ) ; Console . Write ( findMaxIntervals ( start , end , N , R ) ) ; } }
Maximum items that can be bought from the cost Array based on given conditions | C # program to find the maximum number of items that can be bought from the given cost array ; Function to find the maximum number of items that can be bought from the given cost array ; Sort the given array ; Variables to store the prefix sum , answer and the counter variables ; Initializing the first element of the prefix array ; If we can buy at least one item ; Iterating through the first K items and finding the prefix sum ; Check the number of items that can be bought ; Finding the prefix sum for the remaining elements ; Check the number of iteams that can be bought ; Driver code
using System ; using System . Collections ; class GFG { static int number ( int [ ] a , int n , int p , int k ) { Array . Sort ( a ) ; int [ ] pre = new int [ n ] ; int i , ans = 0 ; pre [ 0 ] = a [ 0 ] ; if ( pre [ 0 ] <= p ) ans = 1 ; for ( i = 1 ; i < k - 1 ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i ] ; if ( pre [ i ] <= p ) ans = i + 1 ; } pre [ k - 1 ] = a [ k - 1 ] ; for ( i = k - 1 ; i < n ; i ++ ) { if ( i >= k ) { pre [ i ] += pre [ i - k ] + a [ i ] ; } if ( pre [ i ] <= p ) ans = i + 1 ; } return ans ; } static public void Main ( ) { int n = 5 ; int [ ] arr = { 2 , 4 , 3 , 5 , 7 } ; int p = 11 ; int k = 2 ; Console . WriteLine ( number ( arr , n , p , k ) ) ; } }
Sort all special primes in their relative positions | C # implementation of the above approach ; Function for the Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to sort the special primes in their relative positions ; Maximum element from the array ; prime [ i ] will be true if i is a prime ; To store the special primes from the array ; If current element is a special prime ; Add it to the List and set arr [ i ] to - 1 ; Sort the special primes ; Position of a special prime ; Driver code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void sieveOfEratosthenes ( bool [ ] prime , int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } static void sortSpecialPrimes ( int [ ] arr , int n ) { int maxVal = arr . Max ( ) ; bool [ ] prime = new bool [ maxVal + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; sieveOfEratosthenes ( prime , maxVal ) ; List < int > list = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] && prime [ arr [ i ] - 2 ] ) { list . Add ( arr [ i ] ) ; arr [ i ] = - 1 ; } } list . Sort ( ) ; int j = 0 ; for ( int 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 = { 31 , 5 , 2 , 1 , 7 } ; int n = arr . Length ; sortSpecialPrimes ( arr , n ) ; } }
Find the Maximum possible Sum for the given conditions | C # program to find the maximum possible Sum for the given conditions ; Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; Print profit ; Driver code
using System ; class GFG { static int maxProfit ( int [ ] arr ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } static public void Main ( ) { int [ ] arr = { 6 , 6 , 6 } ; Console . Write ( maxProfit ( arr ) ) ; } }
Check if the array can be sorted only if the elements on given positions can be swapped | C # program to check if the array can be sorted only if the elements on the given positions can be swapped ; Function to check if the array can be sorted only if the elements on the given positions can be swapped ; Creating an array for marking the positions ; Iterating through the array and mark the positions ; Iterating through the given array ; If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos ; Sorting the required segment ; Checking if the vector is sorted or not ; Print yes if it is sorted ; Driver code
using System ; class GFG { public static void check_vector ( int [ ] A , int n , int [ ] p ) { int [ ] pos = new int [ A . Length ] ; for ( int i = 0 ; i < p . Length ; i ++ ) { pos [ p [ i ] - 1 ] = 1 ; } int flag = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos [ i ] == 0 ) continue ; int j = i ; while ( j < n && pos [ j ] != 0 ) ++ j ; Array . Sort ( A , i , j + 1 ) ; i = j ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { int [ ] A = { 3 , 2 , 1 } ; int [ ] p = { 1 , 2 } ; check_vector ( A , A . Length , p ) ; } }
Sort elements of an array in increasing order of absolute difference of adjacent elements | C # implementation to sort the elements of the array in such a way that absolute difference between the adjacent elements are in increasing order ; Function to sort the elements of the array by difference ; Sorting the array ; Array to store the elements of the array ; Iterating over the length of the array to include each middle element of array ; Appending the middle element of the array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void sortDiff ( List < int > arr , int n ) { arr . Sort ( ) ; List < int > Out = new List < int > ( ) ; while ( n > 0 ) { Out . Add ( arr [ n / 2 ] ) ; arr . RemoveAt ( n / 2 ) ; n = n - 1 ; } foreach ( int i in Out ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 8 , 1 , 2 , 3 , 0 } ; List < int > arr = new List < int > ( a ) ; int n = 5 ; sortDiff ( arr , n ) ; } }
Minimum cost of choosing the array element | C # program for the above approach ; Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of [ ] arr ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Print the pref [ ] for the result ; Driver Code
using System ; class GFG { static void minimumCost ( int [ ] arr , int N , int M ) { Array . Sort ( arr ) ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = arr [ i ] + pref [ i - 1 ] ; } for ( int i = M ; i < N ; i ++ ) { pref [ i ] += pref [ i - M ] ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( pref [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 } ; int M = 2 ; int N = arr . Length ; minimumCost ( arr , N , M ) ; } }
Find sum of all unique elements in the array for K queries | C # implementation to find the sum of all unique elements of the array after Q queries ; Function to find the sum of unique elements after Q Query ; Updating the array after processing each query ; Making it to 0 - indexing ; Iterating over the array to get the readonly array ; Variable to store the sum ; Hash to maintain perviously occured elements ; Loop to find the maximum sum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int uniqueSum ( int [ ] A , int [ , ] R , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int ans = 0 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . Contains ( A [ i ] ) ) ans += A [ i ] ; s . Add ( A [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 0 , 0 , 0 , 0 , 0 } ; int [ , ] R = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } , { 3 , 3 } } ; int N = A . Length ; int M = R . GetLength ( 0 ) ; Console . Write ( uniqueSum ( A , R , N , M ) ) ; } }
Find the Kth pair in ordered list of all possible sorted pairs of the Array | C # program to find the K - th pair in a lexicographically sorted array ; Function to find the k - th pair ; Sorting the array ; Iterating through the array ; Finding the number of same elements ; Checking if N * T is less than the remaining K . If it is , then arr [ i ] is the first element in the required pair ; Printing the K - th pair ; Driver code
using System ; class GFG { static void kthpair ( int n , int k , int [ ] arr ) { int i , t = 0 ; Array . Sort ( arr ) ; -- k ; for ( i = 0 ; i < n ; i += t ) { for ( t = 1 ; arr [ i ] == arr [ i + t ] ; ++ t ) ; if ( t * n > k ) break ; k = k - t * n ; } Console . Write ( arr [ i ] + " ▁ " + arr [ k / t ] ) ; } static public void Main ( ) { int n = 3 , k = 2 ; int [ ] arr = { 3 , 1 , 5 } ; kthpair ( n , k , arr ) ; } }
Minimum characters to be replaced to make frequency of all characters same | C # implementation to find the minimum characters to be replaced to make frequency of all characters same ; Function to find the minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code
using System ; class GFG { static int minOperations ( String s ) { int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' A ' ] ++ ; } Array . Sort ( freq ) ; Array . Reverse ( freq ) ; int answer = n ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( n % i == 0 ) { int x = n / i ; int y = 0 ; for ( int j = 0 ; j < i ; j ++ ) { y += Math . Min ( freq [ j ] , x ) ; } answer = Math . Min ( answer , n - y ) ; } } return answer ; } public static void Main ( String [ ] args ) { String s = " BBC " ; Console . Write ( minOperations ( s ) ) ; } }
Check if it is possible to sort an array with conditional swapping of elements at distance K | C # implementation to check if it is possible to sort an array with conditional swapping of elements at distance K ; Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static bool fun ( int [ ] arr , int n , int k ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . Add ( arr [ j ] ) ; } v . Sort ( ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v [ x ] ; x ++ ; } v . Clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = arr . Length ; if ( fun ( arr , n , K ) ) { Console . WriteLine ( " yes " ) ; } else { Console . WriteLine ( " no " ) ; } } }
Sort an Array based on the absolute difference of adjacent elements | C # implementation of the above approach ; Function that arrange the array such that all absolute difference between adjacent element are sorted ; To store the resultant array ; Sorting the given array in ascending order ; Variable to represent left and right ends of the given array ; Traversing the answer array in reverse order and arrange the array elements from arr [ ] in reverse order ; Inserting elements in zig - zag manner ; Displaying the resultant array ; Driver Code ; Function Call
using System ; class GFG { static void sortedAdjacentDifferences ( int [ ] arr , int n ) { int [ ] ans = new int [ n ] ; Array . Sort ( arr ) ; int l = 0 , r = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i % 2 != 0 ) { ans [ i ] = arr [ l ] ; l ++ ; } else { ans [ i ] = arr [ r ] ; r -- ; } } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 5 , - 2 , 4 , 8 , 6 , 4 , 5 } ; int n = arr . Length ; sortedAdjacentDifferences ( arr , n ) ; } }
Minimum steps to convert an Array into permutation of numbers from 1 to N | C # program to find minimum number of steps to convert a given sequence into a permutation ; Function to find minimum number of steps to convert a given sequence into a permutation ; Sort the given array ; To store the required minimum number of operations ; Find the operations on each step ; Return the answer ; Driver Code ; Function call
using System ; class GFG { static int get_permutation ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result += Math . Abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . Length ; Console . Write ( get_permutation ( arr , n ) ) ; } }