codeparrot/xlcost-text-to-code huggingface dataset:
Probability of a random pair being the maximum weighted pair | C # program to find Probability of a random pair being the maximum weighted pair ; Function to return probability ; Count occurrences of maximum element in A [ ] ; Count occurrences of maximum element in B [ ] ; Returning probability ; Driver code
using System ; class GFG { static float probability ( int [ ] a , int [ ] b , int size1 , int size2 ) { int max1 = int . MinValue , count1 = 0 ; for ( int i = 0 ; i < size1 ; i ++ ) { if ( a [ i ] > max1 ) { max1 = a [ i ] ; count1 = 1 ; } else if ( a [ i ] == max1 ) { count1 ++ ; } } int max2 = int . MinValue , count2 = 0 ; for ( int i = 0 ; i < size2 ; i ++ ) { if ( b [ i ] > max2 ) { max2 = b [ i ] ; count2 = 1 ; } else if ( b [ i ] == max2 ) { count2 ++ ; } } return ( float ) ( count1 * count2 ) / ( size1 * size2 ) ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int [ ] b = { 1 , 3 , 3 } ; int size1 = a . Length ; int size2 = b . Length ; Console . WriteLine ( probability ( a , b , size1 , size2 ) ) ; } }
Minimum De | C # code for counting minimum de - arrangements present in an array . ; function to count Dearrangement ; create a copy of original array ; sort the array ; traverse sorted array for counting mismatches ; reverse the sorted array ; traverse reverse sorted array for counting mismatches ; return minimum mismatch count ; Driver code
using System ; class GFG { public static int countDe ( int [ ] arr , int n ) { int [ ] v = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = arr [ i ] ; Array . Sort ( arr ) ; int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count1 ++ ; Array . Reverse ( arr ) ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count2 ++ ; return ( Math . Min ( count1 , count2 ) ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 5 , 9 , 21 , 17 , 13 } ; int n = 5 ; Console . WriteLine ( " Minimum ▁ Dearrangement ▁ = ▁ " + countDe ( arr , n ) ) ; } }
Divide an array into k segments to maximize maximum of segment minimums | C # Program to find maximum value of maximum of minimums of k segments . ; function to calculate the max of all the minimum segments ; if we have to divide it into 1 segment then the min will be the answer ; If k >= 3 , return maximum of all elements . ; Driver function
using System ; using System . Linq ; public class GFG { static int maxOfSegmentMins ( int [ ] a , int n , int k ) { if ( k == 1 ) return a . Min ( ) ; if ( k == 2 ) return Math . Max ( a [ 0 ] , a [ n - 1 ] ) ; return a . Max ( ) ; } static public void Main ( ) { int [ ] a = { - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 } ; int n = a . Length ; int k = 2 ; Console . WriteLine ( maxOfSegmentMins ( a , n , k ) ) ; } }
Minimum product pair an array of positive Integers | C # program to calculate minimum product of a pair ; Function to calculate minimum product of pair ; Initialize first and second minimums . It is assumed that the array has at least two elements . ; Traverse remaining array and keep track of two minimum elements ( Note that the two minimum elements may be same if minimum element appears more than once ) ; Driver program to test above function
using System ; class GFG { static int printMinimumProduct ( int [ ] arr , int n ) { int first_min = Math . Min ( arr [ 0 ] , arr [ 1 ] ) ; int second_min = Math . Max ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] < first_min ) { second_min = first_min ; first_min = arr [ i ] ; } else if ( arr [ i ] < second_min ) second_min = arr [ i ] ; } return first_min * second_min ; } public static void Main ( ) { int [ ] a = { 11 , 8 , 5 , 7 , 5 , 100 } ; int n = a . Length ; Console . WriteLine ( printMinimumProduct ( a , n ) ) ; } }
Count ways to form minimum product triplets | C # program to count number of ways we can form triplets with minimum product . ; function to calculate number of triples ; Sort the array ; Count occurrences of third element ; If all three elements are same ( minimum element appears at least 3 times ) . Answer is nC3 . ; If minimum element appears once . Answer is nC2 . ; Minimum two elements are distinct . Answer is nC1 . ; driver code
using System ; class GFG { static long noOfTriples ( long [ ] arr , int n ) { Array . Sort ( arr ) ; long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == arr [ 2 ] ) count ++ ; if ( arr [ 0 ] == arr [ 2 ] ) return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 ; else if ( arr [ 1 ] == arr [ 2 ] ) return ( count - 1 ) * ( count ) / 2 ; return count ; } public static void Main ( ) { long [ ] arr = { 1 , 3 , 3 , 4 } ; int n = arr . Length ; Console . Write ( noOfTriples ( arr , n ) ) ; } }
Check if reversing a sub array make the array sorted | C # program to check whether reversing a sub array make the array sorted or not ; Return true , if reversing the subarray will sort the array , else return false . ; Copying the array . ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driven Program
using System ; class GFG { static bool checkReverse ( int [ ] arr , int n ) { int [ ] temp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { temp [ i ] = arr [ i ] ; } Array . Sort ( temp ) ; int front ; for ( front = 0 ; front < n ; front ++ ) { if ( temp [ front ] != arr [ front ] ) { break ; } } int back ; for ( back = n - 1 ; back >= 0 ; back -- ) { if ( temp [ back ] != arr [ back ] ) { break ; } } if ( front >= back ) { return true ; } do { front ++ ; if ( arr [ front - 1 ] < arr [ front ] ) { return false ; } } while ( front != back ) ; return true ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 4 , 3 } ; int n = arr . Length ; if ( checkReverse ( arr , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Check if reversing a sub array make the array sorted | C # program to check whether reversing a sub array make the array sorted or not ; Return true , if reversing the subarray will sort t he array , else return false . ; Find first increasing part ; Find reversed part ; Find last increasing part ; To handle cases like { 1 , 2 , 3 , 4 , 20 , 9 , 16 , 17 } ; Driven Program
using System ; public class GFG { static bool checkReverse ( int [ ] arr , int n ) { if ( n == 1 ) { return true ; } int i ; for ( i = 1 ; arr [ i - 1 ] < arr [ i ] && i < n ; i ++ ) ; if ( i == n ) { return true ; } int j = i ; while ( j < n && arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) { return false ; } j ++ ; } if ( j == n ) { return true ; } int k = j ; if ( arr [ k ] < arr [ i - 1 ] ) { return false ; } while ( k > 1 && k < n ) { if ( arr [ k ] < arr [ k - 1 ] ) { return false ; } k ++ ; } return true ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 10 , 9 , 8 } ; int n = arr . Length ; if ( checkReverse ( arr , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Making elements of two arrays same with minimum increment / decrement | C # program to find minimum increment / decrement operations to make array elements same . ; sorting both arrays in ascending order ; variable to store the final result ; After sorting both arrays Now each array is in non - decreasing order . Thus , we will now compare each element of the array and do the increment or decrement operation depending upon the value of array b [ ] . ; Driver code
using System ; public class GFG { static int MinOperation ( int [ ] a , int [ ] b , int n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] > b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; else if ( a [ i ] < b [ i ] ) result = result + Math . Abs ( a [ i ] - b [ i ] ) ; } return result ; } public static void Main ( ) { int [ ] a = { 3 , 1 , 1 } ; int [ ] b = { 1 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( MinOperation ( a , b , n ) ) ; } }
Sorting array except elements in a subarray | C # program to sort all elements except given subarray . ; Sort whole array a [ ] except elements in range a [ l . . r ] ; Copy all those element that need to be sorted to an auxiliary array b [ ] ; sort the array b ; Copy sorted elements back to a [ ] ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void sortExceptUandL ( int [ ] a , int l , int u , int n ) { int [ ] b = new int [ n - ( u - l + 1 ) ] ; for ( int i = 0 ; i < l ; i ++ ) b [ i ] = a [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) b [ l + ( i - ( u + 1 ) ) ] = a [ i ] ; Array . Sort < int > ( b , 0 , n - ( u - l + 1 ) ) ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = b [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) a [ i ] = b [ l + ( i - ( u + 1 ) ) ] ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 3 , 12 , 14 , 9 } ; int n = a . Length ; int l = 2 , u = 4 ; sortExceptUandL ( a , l , u , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Sorting all array elements except one | C # program to sort all elements except element at index k . ; Move k - th element to end ; Sort all elements except last ; Store last element ( originally k - th ) ; Move all elements from k - th to one position ahead . ; Restore k - th element ; Driver code
using System ; public class GFG { static int sortExceptK ( int [ ] arr , int k , int n ) { int temp = arr [ k ] ; arr [ k ] = arr [ n - 1 ] ; arr [ n - 1 ] = temp ; Array . Sort ( arr , 0 , n - 1 ) ; int last = arr [ n - 1 ] ; for ( int i = n - 1 ; i > k ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ k ] = last ; return 0 ; } public static void Main ( ) { int [ ] a = { 10 , 4 , 11 , 7 , 6 , 20 } ; int k = 2 ; int n = a . Length ; sortExceptK ( a , k , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Sort the linked list in the order of elements appearing in the array | Efficient C # program to sort given list in order elements are appearing in an array ; Linked list node ; Function to insert a node at the beginning of the linked list ; function to print the linked list ; Function that sort list in order of apperaing elements in an array ; Store frequencies of elements in a hash table . ; One by one put elements in lis according to their appearance in array ; Update ' frequency ' nodes with value equal to arr [ i ] ; Modify list data as element appear in an array ; Driver Code ; creating the linked list ; Function call to sort the list in order elements are apperaing in an array ; print the modified linked list
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node head ) { while ( head != null ) { Console . Write ( head . data + " - > " ) ; head = head . next ; } } static void sortlist ( int [ ] arr , int N , Node head ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; Node temp = head ; while ( temp != null ) { if ( hash . ContainsKey ( temp . data ) ) hash [ temp . data ] = hash [ temp . data ] + 1 ; else hash . Add ( temp . data , 1 ) ; temp = temp . next ; } temp = head ; for ( int i = 0 ; i < N ; i ++ ) { int frequency = hash [ arr [ i ] ] ; while ( frequency -- > 0 ) { temp . data = arr [ i ] ; temp = temp . next ; } } } public static void Main ( String [ ] args ) { Node head = null ; int [ ] arr = { 5 , 1 , 3 , 2 , 8 } ; int N = arr . Length ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 5 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; sortlist ( arr , N , head ) ; Console . Write ( " Sorted ▁ List : " + " STRNEWLINE " ) ; printList ( head ) ; } }
Maximum number of partitions that can be sorted individually to make sorted | C # program to find Maximum number of partitions such that we can get a sorted array ; Function to find maximum partitions . ; Find maximum in prefix arr [ 0. . i ] ; If maximum so far is equal to index , we can make a new partition ending at index i . ; Driver code
using System ; class GFG { static int maxPartitions ( int [ ] arr , int n ) { int ans = 0 , max_so_far = 0 ; for ( int i = 0 ; i < n ; ++ i ) { max_so_far = Math . Max ( max_so_far , arr [ i ] ) ; if ( max_so_far == i ) ans ++ ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 2 , 3 , 4 } ; int n = arr . Length ; Console . Write ( maxPartitions ( arr , n ) ) ; } }
Ropes left after every removal of smallest | C # program to print how many Ropes are Left After Every Cut ; function print how many Ropes are Left After Every Cutting operation ; sort all Ropes in increasing order of their length ; min length rope ; now traverse through the given Ropes in increase order of length ; After cutting if current rope length is greater than '0' that mean all ropes to it 's right side are also  greater than 0 ; now current rope become min length rope ; after first operation all ropes length become zero ; Driver code
using System ; class GFG { public static void cuttringRopes ( int [ ] Ropes , int n ) { Array . Sort ( Ropes ) ; int singleOperation = 0 ; int cuttingLenght = Ropes [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( Ropes [ i ] - cuttingLenght > 0 ) { Console . Write ( n - i + " ▁ " ) ; cuttingLenght = Ropes [ i ] ; singleOperation ++ ; } } if ( singleOperation == 0 ) Console . Write ( "0" ) ; } public static void Main ( ) { int [ ] Ropes = { 5 , 1 , 1 , 2 , 3 , 5 } ; int n = Ropes . Length ; cuttringRopes ( Ropes , n ) ; } }
Rank of all elements in an array | C # Code to find rank of elements ; Function to print m Maximum elements ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s ; Use formula to obtain rank ; Driver code
using System ; public class GfG { public static void rankify ( int [ ] A , int n ) { float [ ] R = new float [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int r = 1 , s = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != i && A [ j ] < A [ i ] ) r += 1 ; if ( j != i && A [ j ] == A [ i ] ) s += 1 ; } R [ i ] = r + ( float ) ( s - 1 ) / ( float ) 2 ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( R [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 5 , 2 , 1 , 25 , 2 } ; int n = A . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; rankify ( A , n ) ; } }
Minimum number of subtract operation to make an array decreasing | C # program to make an array decreasing ; Function to count minimum no of operation ; Count how many times we have to subtract . ; Check an additional subtraction is required or not . ; Modify the value of arr [ i ] ; Count total no of subtraction ; driver function
using System ; public class GfG { public static int min_noOf_operation ( int [ ] arr , int n , int k ) { int noOfSubtraction ; int res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { noOfSubtraction = 0 ; if ( arr [ i ] > arr [ i - 1 ] ) { noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) noOfSubtraction ++ ; arr [ i ] = arr [ i ] - k * noOfSubtraction ; } res = res + noOfSubtraction ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 } ; int N = 4 ; int k = 5 ; Console . WriteLine ( min_noOf_operation ( arr , N , k ) ) ; } }
Maximize the sum of arr [ i ] * i | C # program to find the maximum value of i * arr [ i ] ; Sort the array ; Finding the sum of arr [ i ] * i ; Driver code
using System ; class GFG { static int maxSum ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( arr [ i ] * i ) ; return sum ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 6 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }
Pairs with Difference less than K | C # code to find count of Pairs with difference less than K . ; Function to count pairs ; Driver code
using System ; class GFG { static int countPairs ( int [ ] a , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . Abs ( a [ j ] - a [ i ] ) < k ) res ++ ; return res ; } public static void Main ( ) { int [ ] a = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n , k ) ) ; } }
Pairs with Difference less than K | C # code to find count of Pairs with difference less than K . ; to sort the array . ; Keep incrementing result while subsequent elements are within limits . ; Driver code
using System ; class GFG { static int countPairs ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && a [ j ] - a [ i ] < k ) { res ++ ; j ++ ; } } return res ; } public static void Main ( ) { int [ ] a = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n , k ) ) ; } }
Merging two unsorted arrays in sorted order | C # Code for Merging two unsorted arrays in sorted order ; Function to merge array in sorted order ; Sorting a [ ] and b [ ] ; Merge two sorted arrays into res [ ] ; Merging remaining elements of a [ ] ( if any ) ; Merging remaining elements of b [ ] ( if any ) ; Driver program to test above function ; Final merge list
using System ; class GFG { static void sortedMerge ( int [ ] a , int [ ] b , int [ ] res , int n , int m ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m ) { if ( a [ i ] <= b [ j ] ) { res [ k ] = a [ i ] ; i += 1 ; k += 1 ; } else { res [ k ] = b [ j ] ; j += 1 ; k += 1 ; } } while ( i < n ) { res [ k ] = a [ i ] ; i += 1 ; k += 1 ; } while ( j < m ) { res [ k ] = b [ j ] ; j += 1 ; k += 1 ; } } public static void Main ( ) { int [ ] a = { 10 , 5 , 15 } ; int [ ] b = { 20 , 3 , 2 , 12 } ; int n = a . Length ; int m = b . Length ; int [ ] res = new int [ n + m ] ; sortedMerge ( a , b , res , n , m ) ; Console . Write ( " Sorted ▁ merged ▁ list ▁ : " ) ; for ( int i = 0 ; i < n + m ; i ++ ) Console . Write ( " ▁ " + res [ i ] ) ; } }
Maximizing Unique Pairs from two arrays | C # implementation of above approach ; Returns count of maximum pairs that caan be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; To keep track of visited elements of b [ ] ; For every element of a [ ] , find a pair for it and break as soon as a pair is found . ; Increasing the count if a pair is formed . ; Making the corresponding flag array element as 1 indicating the element in the second array element has been used . ; We break the loop to make sure an element of a [ ] is used only once . ; Driver code
using System ; class GFG { static int findMaxPairs ( int [ ] a , int [ ] b , int n , int k ) { Array . Sort ( a ) ; Array . Sort ( b ) ; bool [ ] flag = new bool [ n ] ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( Math . Abs ( a [ i ] - b [ j ] ) <= k && flag [ j ] == false ) { result ++ ; flag [ j ] = true ; break ; } } } return result ; } public static void Main ( ) { int [ ] a = { 10 , 15 , 20 } ; int [ ] b = { 17 , 12 , 24 } ; int n = a . Length ; int k = 3 ; Console . WriteLine ( findMaxPairs ( a , b , n , k ) ) ; } }
Maximizing Unique Pairs from two arrays | C # program for Maximizing Unique Pairs from two arrays ; Returns count of maximum pairs that caan be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; Increasing array pointer of both the first and the second array . ; Increasing array pointer of the second array . ; Increasing array pointer of the first array . ; Driver code
using System ; class GFG { static int findMaxPairs ( int [ ] a , int [ ] b , int n , int k ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int result = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { if ( Math . Abs ( a [ i ] - b [ j ] ) <= k ) { result ++ ; i ++ ; j ++ ; } else if ( a [ i ] > b [ j ] ) j ++ ; else i ++ ; } return result ; } public static void Main ( String [ ] args ) { int [ ] a = { 10 , 15 , 20 } ; int [ ] b = { 17 , 12 , 24 } ; int n = a . Length ; int k = 3 ; Console . WriteLine ( findMaxPairs ( a , b , n , k ) ) ; } }
Sum of minimum absolute difference of each array element | C # implementation to find the sum of minimum absolute difference of each array element ; function to find the sum of minimum absolute difference ; sort the given array ; initialize sum ; min absolute difference for the 1 st array element ; min absolute difference for the last array element ; find min absolute difference for rest of the array elements and add them to sum ; required sum ; Driver code
using System ; public class GFG { static int sumOfMinAbsDifferences ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int sum = 0 ; sum += Math . Abs ( arr [ 0 ] - arr [ 1 ] ) ; sum += Math . Abs ( arr [ n - 1 ] - arr [ n - 2 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) sum += Math . Min ( Math . Abs ( arr [ i ] - arr [ i - 1 ] ) , Math . Abs ( arr [ i ] - arr [ i + 1 ] ) ) ; return sum ; } public static void Main ( ) { int [ ] arr = { 5 , 10 , 1 , 4 , 8 , 7 } ; int n = arr . Length ; Console . Write ( " Sum ▁ = ▁ " + sumOfMinAbsDifferences ( arr , n ) ) ; } }
Smallest Difference pair of values between two unsorted Arrays | C # Code to find Smallest Difference between two Arrays ; function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; return final sma result ; Driver Code ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result
using System ; class GFG { static int findSmallestDifference ( int [ ] A , int [ ] B , int m , int n ) { Array . Sort ( A ) ; Array . Sort ( B ) ; int a = 0 , b = 0 ; int result = int . MaxValue ; while ( a < m && b < n ) { if ( Math . Abs ( A [ a ] - B [ b ] ) < result ) result = Math . Abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else b ++ ; } return result ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 11 , 5 } ; int [ ] B = { 4 , 12 , 19 , 23 , 127 , 235 } ; int m = A . Length ; int n = B . Length ; Console . Write ( findSmallestDifference ( A , B , m , n ) ) ; } }
Find elements larger than half of the elements in an array | C # program to find elements that are larger than half of the elements in array ; Prints elements larger than n / 2 element ; Sort the array in ascending order ; Print last ceil ( n / 2 ) elements ; Driver Code
using System ; class GFG { static void findLarger ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 6 , 1 , 0 , 9 } ; int n = arr . Length ; findLarger ( arr , n ) ; } }
Find the element that appears once in an array where every other element appears twice |  ; singleelement function ; Driver code
using System ; using System . Collections ; class GFG { static int singleelement ( int [ ] arr , int n ) { int low = 0 , high = n - 2 ; int mid ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( arr [ mid ] == arr [ mid ^ 1 ] ) { low = mid + 1 ; } else { high = mid - 1 ; } } return arr [ low ] ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int size = 7 ; Array . Sort ( arr ) ; Console . WriteLine ( singleelement ( arr , size ) ) ; } }