codeparrot/xlcost-text-to-code huggingface dataset:
Print uncommon elements from two sorted arrays | C # program to find uncommon elements of two sorted arrays ; If not common , print smaller ; Skip common element ; printing remaining elements ; Driver Code
using System ; class GFG { static void printUncommon ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) { Console . Write ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } else if ( arr2 [ j ] < arr1 [ i ] ) { Console . Write ( arr2 [ j ] + " ▁ " ) ; k ++ ; j ++ ; } else { i ++ ; j ++ ; } } while ( i < n1 ) { Console . Write ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } while ( j < n2 ) { Console . Write ( arr2 [ j ] + " ▁ " ) ; j ++ ; k ++ ; } } public static void Main ( ) { int [ ] arr1 = { 10 , 20 , 30 } ; int [ ] arr2 = { 20 , 25 , 30 , 40 , 50 } ; int n1 = arr1 . Length ; int n2 = arr2 . Length ; printUncommon ( arr1 , arr2 , n1 , n2 ) ; } }
Least frequent element in an array | C # program to find the least frequent element in an array . ; Sort the array ; find the min frequency using linear traversal ; If last element is least frequent ; Driver code
using System ; class GFG { static int leastFrequent ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int min_count = n + 1 , res = - 1 ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count < min_count ) { min_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 } ; int n = arr . Length ; Console . Write ( leastFrequent ( arr , n ) ) ; } }
Least frequent element in an array | C # program to find the least frequent element in an array . ; Insert all elements in hash . ; find the min frequency ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int leastFrequent ( int [ ] arr , int n ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( count . ContainsKey ( key ) ) { int freq = count [ key ] ; freq ++ ; count [ key ] = freq ; } else count . Add ( key , 1 ) ; } int min_count = n + 1 , res = - 1 ; foreach ( KeyValuePair < int , int > pair in count ) { if ( min_count >= pair . Value ) { res = pair . Key ; min_count = pair . Value ; } } return res ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 } ; int n = arr . Length ; Console . Write ( leastFrequent ( arr , n ) ) ; } }
Maximum sum of increasing order elements from n arrays | C # program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Sort each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; j = - 1 means no element is found in a [ i ] so return 0 ; Driver Code
using System ; class GFG { static int M = 4 ; static void sort ( ref int [ , ] a , int row , int n ) { for ( int i = 0 ; i < M - 1 ; i ++ ) { if ( a [ row , i ] > a [ row , i + 1 ] ) { int temp = a [ row , i ] ; a [ row , i ] = a [ row , i + 1 ] ; a [ row , i + 1 ] = temp ; } } } static int maximumSum ( int [ , ] a , int n ) { int i = 0 , j = 0 ; for ( i = 0 ; i < n ; i ++ ) sort ( ref a , i , n ) ; int sum = a [ n - 1 , M - 1 ] ; int prev = a [ n - 1 , M - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i , j ] < prev ) { prev = a [ i , j ] ; sum += prev ; break ; } } if ( j == - 1 ) return 0 ; } return sum ; } static void Main ( ) { int [ , ] arr = new int [ , ] { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( maximumSum ( arr , n ) ) ; } }
Maximum sum of increasing order elements from n arrays | C # program to find maximum sum by selecting a element from n arrays ; To calculate maximum sum by selecting element from each array ; Store maximum element of last array ; Selecting maximum element from previoulsy selected element ; max_smaller equals to INT_MIN means no element is found in a [ i ] so return 0 ; Driver code
using System ; class GFG { static int M = 4 ; static int maximumSum ( int [ , ] a , int n ) { int prev = Math . Max ( a [ n - 1 , 0 ] , a [ n - 1 , M - 1 ] + 1 ) ; int sum = prev ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int max_smaller = Int32 . MinValue ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i , j ] < prev && a [ i , j ] > max_smaller ) { max_smaller = a [ i , j ] ; } } if ( max_smaller == Int32 . MinValue ) { return 0 ; } prev = max_smaller ; sum += max_smaller ; } return sum ; } static public void Main ( ) { int [ , ] arr = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( maximumSum ( arr , n ) ) ; } }
Pairs such that one is a power multiple of other | C # program to find pairs count ; function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; Driver program
using System ; class GFG { static int countPairs ( int [ ] A , int n , int k ) { int ans = 0 ; Array . Sort ( A ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int x = 0 ; while ( ( A [ i ] * Math . Pow ( k , x ) ) <= A [ j ] ) { if ( ( A [ i ] * Math . Pow ( k , x ) ) == A [ j ] ) { ans ++ ; break ; } x ++ ; } } } return ans ; } public static void Main ( ) { int [ ] A = { 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 } ; int n = A . Length ; int k = 3 ; Console . WriteLine ( countPairs ( A , n , k ) ) ; } }
Minimum distance between two occurrences of maximum | C # program to find Min distance of maximum element ; function to return min distance ; case a ; case b ; case c ; Driver code
using System ; class GFG { static int minDistance ( int [ ] arr , int n ) { int maximum_element = arr [ 0 ] ; int min_dis = n ; int index = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( maximum_element == arr [ i ] ) { min_dis = Math . Min ( min_dis , ( i - index ) ) ; index = i ; } else if ( maximum_element < arr [ i ] ) { maximum_element = arr [ i ] ; min_dis = n ; index = i ; } else continue ; } return min_dis ; } public static void Main ( ) { int [ ] arr = { 6 , 3 , 1 , 3 , 6 , 4 , 6 } ; int n = arr . Length ; Console . WriteLine ( " Minimum ▁ distance ▁ = ▁ " + minDistance ( arr , n ) ) ; } }
Find final value if we double after every successful search in array | C # program to find value if we double the value after every successful search ; Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver Code
using System ; class GFG { static int findValue ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == k ) k *= 2 ; return k ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 8 , 1 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findValue ( arr , n , k ) ) ; } }
Last duplicate element in a sorted array | C # code to print last duplicate element and its index in a sorted array ; if array is null or size is less than equal to 0 return ; compare elements and return last duplicate and its index ; If we reach here , then no duplicate found . ; Driver code
using System ; class GFG { static void dupLastIndex ( int [ ] arr , int n ) { if ( arr == null n <= 0 ) return ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] == arr [ i - 1 ] ) { Console . WriteLine ( " Last ▁ index : " + i ) ; Console . WriteLine ( " Last ▁ duplicate ▁ item : ▁ " + arr [ i ] ) ; return ; } } Console . WriteLine ( " no ▁ duplicate ▁ found " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 5 , 6 , 6 , 7 , 9 } ; int n = arr . Length ; dupLastIndex ( arr , n ) ; } }
Find an array element such that all elements are divisible by it | C # program to find an array element that divides all numbers in the array using naive approach ; function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; Driver code
using System ; class GFG { static int findSmallest ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] a = { 25 , 20 , 5 , 10 , 100 } ; int n = a . Length ; Console . WriteLine ( findSmallest ( a , n ) ) ; } }
Find an array element such that all elements are divisible by it | C # Program to find the smallest number that divides all numbers in an array ; function to find the smallest element ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code
using System ; class GFG { static int min_element ( int [ ] a ) { int min = int . MaxValue ; int i ; for ( i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } static int findSmallest ( int [ ] a , int n ) { int smallest = min_element ( a ) ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } public static void Main ( ) { int [ ] a = { 25 , 20 , 5 , 10 , 100 } ; int n = a . Length ; Console . WriteLine ( findSmallest ( a , n ) ) ; } }
Maximum in array which is at | C # program for Maximum of the array which is at least twice of other elements of the array . ; Function to find the index of Max element that satisfies the condition ; Finding index of max of the array ; Returns - 1 if the max element is not twice of the i - th element . ; Driver function
using System ; class GfG { public static int findIndex ( int [ ] arr ) { int maxIndex = 0 ; for ( int i = 0 ; i < arr . Length ; ++ i ) if ( arr [ i ] > arr [ maxIndex ] ) maxIndex = i ; for ( int i = 0 ; i < arr . Length ; ++ i ) if ( maxIndex != i && arr [ maxIndex ] < 2 * arr [ i ] ) return - 1 ; return maxIndex ; } public static void Main ( ) { int [ ] arr = new int [ ] { 3 , 6 , 1 , 0 } ; Console . WriteLine ( findIndex ( arr ) ) ; } }
Consecutive steps to roof top | C # code to find maximum number of consecutive steps . ; Function to count consecutive steps ; count the number of consecutive increasing height building ; Driver code
using System ; class GFG { static int find_consecutive_steps ( int [ ] arr , int len ) { int count = 0 ; int maximum = 0 ; for ( int index = 1 ; index < len ; index ++ ) { if ( arr [ index ] > arr [ index - 1 ] ) count ++ ; else { maximum = Math . Max ( maximum , count ) ; count = 0 ; } } return Math . Max ( maximum , count ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int len = arr . Length ; Console . WriteLine ( find_consecutive_steps ( arr , len ) ) ; } }
Maximum difference between groups of size two | C # program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Driver code
using System ; public class GFG { static int CalculateMax ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int min_sum = arr [ 0 ] + arr [ 1 ] ; int max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . Abs ( max_sum - min_sum ) ) ; } static public void Main ( ) { int [ ] arr = { 6 , 7 , 1 , 11 } ; int n = arr . Length ; Console . WriteLine ( CalculateMax ( arr , n ) ) ; } }
Minimum difference between groups of size two | C # program to find minimum difference between groups of highest and lowest sums . ; Sorting the whole array . ; Generating sum groups . ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static long calculate ( long [ ] a , int n ) { Array . Sort ( a ) ; int i , j ; List < long > s = new List < long > ( ) ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . Add ( ( a [ i ] + a [ j ] ) ) ; long mini = s . Min ( ) ; long maxi = s . Max ( ) ; return Math . Abs ( maxi - mini ) ; } public static void Main ( ) { long [ ] a = { 2 , 6 , 4 , 3 } ; int n = a . Length ; Console . WriteLine ( calculate ( a , n ) ) ; } }
Closest numbers from a list of unsorted integers | C # program to find minimum difference an unsorted array . ; Returns minimum difference between any two pair in arr [ 0. . n - 1 ] ; Sort array elements ; Compare differences of adjacent pairs to find the minimum difference . ; Traverse array again and print all pairs with difference as minDiff . ; Driver code
using System ; class GFG { static void printMinDiffPairs ( int [ ] arr , int n ) { if ( n <= 1 ) return ; Array . Sort ( arr ) ; int minDiff = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) minDiff = Math . Min ( minDiff , arr [ i ] - arr [ i - 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( arr [ i ] - arr [ i - 1 ] ) == minDiff ) { Console . Write ( " ▁ ( " + arr [ i - 1 ] + " , ▁ " + arr [ i ] + " ) , ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 5 , 3 , 2 , 4 , 1 } ; int n = arr . Length ; printMinDiffPairs ( arr , n ) ; } }
Maximum absolute difference of value and index sums | C # program to calculate the maximum absolute difference of an array . ; Utility function to calculate the value of absolute difference for the pair ( i , j ) . ; Function to return maximum absolute difference in brute force . ; Variable for storing the maximum absolute distance throughout the traversal of loops . ; Iterate through all pairs . ; If the absolute difference of current pair ( i , j ) is greater than the maximum difference calculated till now , update the value of result . ; Driver program
using System ; public class MaximumAbsoluteDifference { private static int calculateDiff ( int i , int j , int [ ] array ) { return Math . Abs ( array [ i ] - array [ j ] ) + Math . Abs ( i - j ) ; } private static int maxDistance ( int [ ] array ) { int result = 0 ; for ( int i = 0 ; i < array . Length ; i ++ ) { for ( int j = i ; j < array . Length ; j ++ ) { result = Math . Max ( result , calculateDiff ( i , j , array ) ) ; } } return result ; } public static void Main ( ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; Console . WriteLine ( maxDistance ( array ) ) ; } }
Maximum absolute difference of value and index sums | C # program to calculate the maximum absolute difference of an array . ; Function to return maximum absolute difference in linear time . ; max and min variables as described in algorithm . ; Updating max and min variables as described in algorithm . ; Calculating maximum absolute difference . ; Driver program
using System ; public class MaximumAbsoluteDifference { private static int maxDistance ( int [ ] array ) { int max1 = int . MinValue ; int min1 = int . MaxValue ; int max2 = int . MinValue ; int min2 = int . MaxValue ; for ( int i = 0 ; i < array . Length ; i ++ ) { max1 = Math . Max ( max1 , array [ i ] + i ) ; min1 = Math . Min ( min1 , array [ i ] + i ) ; max2 = Math . Max ( max2 , array [ i ] - i ) ; min2 = Math . Min ( min2 , array [ i ] - i ) ; } return Math . Max ( max1 - min1 , max2 - min2 ) ; } public static void Main ( ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; Console . WriteLine ( maxDistance ( array ) ) ; } }
Number of local extrema in an array | C # to find number of extrema ; function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; Driver program
using System ; class GFG { static int extrema ( int [ ] a , int n ) { int count = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } public static void Main ( ) { int [ ] a = { 1 , 0 , 2 , 1 } ; int n = a . Length ; Console . WriteLine ( extrema ( a , n ) ) ; } }
Find closest number in array | C # program to find element closet to given target . ; Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver code
using System ; class GFG { public static int findClosest ( int [ ] arr , int target ) { int n = arr . Length ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } public static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 } ; int target = 11 ; Console . WriteLine ( findClosest ( arr , target ) ) ; } }
Number of pairs with maximum sum | C # program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; traverse through all the pairs ; traverse through all pairs and keep a count of the number of maximum pairs ; driver program to test the above function
using System ; class GFG { static int sum ( int [ ] a , int n ) { int maxSum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) maxSum = Math . Max ( maxSum , a [ i ] + a [ j ] ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } public static void Main ( ) { int [ ] array = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = array . Length ; Console . WriteLine ( sum ( array , n ) ) ; } }
Number of pairs with maximum sum | C # program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; driver program
using System ; class GFG { static int sum ( int [ ] a , int n ) { int maxVal = a [ 0 ] , maxCount = 1 ; int secondMax = int . MinValue ; int secondMaxCount = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * ( maxCount - 1 ) / 2 ; return secondMaxCount ; } public static void Main ( ) { int [ ] array = { 1 , 1 , 1 , 2 , 2 , 2 , 3 } ; int n = array . Length ; Console . WriteLine ( sum ( array , n ) ) ; } }
Find first k natural numbers missing in given array | C # program to find missing k numbers in an array . ; Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code
using System ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { Console . Write ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { Console . Write ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . Length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }
Find first k natural numbers missing in given array | C # code for the above approach ; Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void printmissingk ( int [ ] arr , int n , int k ) { Dictionary < int , int > d = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { d . Add ( arr [ i ] , arr [ i ] ) ; } int cnt = 1 ; int fl = 0 ; for ( int i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . ContainsKey ( cnt ) ) { fl += 1 ; Console . Write ( cnt + " ▁ " ) ; if ( fl == k ) break ; } cnt += 1 ; } } static public void Main ( ) { int [ ] arr = { 1 , 4 , 3 } ; int n = arr . Length ; int k = 3 ; printmissingk ( arr , n , k ) ; } }
Noble integers in an array ( count of greater elements is equal to value ) | C # program to find Noble elements in an array . ; Returns a Noble integer if present , else returns - 1. ; If count of greater elements is equal to arr [ i ] ; Driver code
using System ; class GFG { public static int nobleInteger ( int [ ] arr ) { int size = arr . Length ; for ( int i = 0 ; i < size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( arr [ i ] < arr [ j ] ) count ++ ; if ( count == arr [ i ] ) return arr [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) Console . Write ( " The ▁ noble ▁ integer " + " ▁ is ▁ " + res ) ; else Console . Write ( " No ▁ Noble ▁ Integer " + " ▁ Found " ) ; } }
Noble integers in an array ( count of greater elements is equal to value ) | C # program to find Noble elements in an array . ; Returns a Noble integer if present , else returns - 1. ; Return a Noble element if present before last . ; In case of duplicates , we reach last occurrence here . ; Driver code
using System ; public class GFG { public static int nobleInteger ( int [ ] arr ) { Array . Sort ( arr ) ; int n = arr . Length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; } static public void Main ( ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) Console . Write ( " The ▁ noble ▁ integer ▁ is ▁ " + res ) ; else Console . Write ( " No ▁ Noble ▁ Integer ▁ " + " Found " ) ; } }
Minimum sum of absolute difference of pairs of two arrays | C # program to find minimum sum of absolute differences of two arrays . ; Returns minimum possible pairwise absolute difference of two arrays . ; Sort both arrays ; Find sum of absolute differences ; Driver code ; Both a [ ] and b [ ] must be of same size .
using System ; class MinSum { static long findMinSum ( long [ ] a , long [ ] b , long n ) { Array . Sort ( a ) ; Array . Sort ( b ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + Math . Abs ( a [ i ] - b [ i ] ) ; return sum ; } public static void Main ( String [ ] args ) { long [ ] a = { 4 , 1 , 8 , 7 } ; long [ ] b = { 2 , 3 , 6 , 5 } ; int n = a . Length ; Console . Write ( findMinSum ( a , b , n ) ) ; } }