codeparrot/xlcost-text-to-code huggingface dataset:
Check whether an array can be made strictly increasing by removing at most one element | C # program for the above approach ; Function to find if is it possible to make the array strictly increasing by removing at most one element ; Stores the count of numbers that are needed to be removed ; Store the index of the element that needs to be removed ; Traverse the range [ 1 , N - 1 ] ; If arr [ i - 1 ] is greater than or equal to arr [ i ] ; Increment the count by 1 ; Update index ; If count is greater than one ; If no element is removed ; If only the last or the first element is removed ; If a [ index ] is removed ; If a [ index - 1 ] is removed ; Driver Code
using System ; class GFG { public static bool check ( int [ ] arr , int n ) { int count = 0 ; int index = - 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] ) { count ++ ; index = i ; } } if ( count > 1 ) return false ; if ( count == 0 ) return true ; if ( index == n - 1 index == 1 ) return true ; if ( arr [ index - 1 ] < arr [ index + 1 ] ) return true ; if ( arr [ index - 2 ] < arr [ index ] ) return true ; return false ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 5 } ; int N = arr . Length ; if ( check ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Modify string by rearranging vowels in alphabetical order at their respective indices | C # program for the above approach ; Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Convert vow to char array ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
using System ; public class GFG { static void sortVowels ( string S ) { int n = S . Length ; string vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { vow = vow . Substring ( 0 , vow . Length ) + S [ i ] ; } } if ( vow . Length == 0 ) { Console . Write ( S ) ; return ; } char [ ] tempArray = vow . ToCharArray ( ) ; Array . Sort ( tempArray ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { S = S . Substring ( 0 , i ) + tempArray [ j ++ ] + S . Substring ( i + 1 , n - i - 1 ) ; } } Console . Write ( S ) ; } public static void Main ( string [ ] args ) { string S = " geeksforgeeks " ; sortVowels ( S ) ; } }
Maximum number of buckets that can be filled | C # program to implement the above approach ; Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void getBuckets ( int [ ] arr , int N ) { int availableWater = N * ( N - 1 ) / 2 ; Array . Sort ( arr ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } Console . Write ( i - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = arr . Length ; getBuckets ( arr , N ) ; } }
Minimize the number of strictly increasing subsequences in an array | Set 2 | C # program to implement the above approach ; Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores readonly count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the readonly count ; Driver Code ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences
using System ; public class GFG { static void minimumIncreasingSubsequences ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int count = 0 ; int i = 0 ; while ( i < N ) { int x = arr [ i ] ; int freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = Math . Max ( count , freqX ) ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 2 , 1 , 4 , 3 } ; int N = arr . Length ; minimumIncreasingSubsequences ( arr , N ) ; } }
Count triplets from an array which can form quadratic equations with real roots | C # program for the above approach ; Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values of a and c are equal to b ; Increment a ; Decrement c ; Condition for having real roots for a quadratic equation ; If b lies in between a and c ; Update count ; Update count ; Increment a ; Decrement c ; For each pair two values are possible of a and c ; Driver Code ; Function Call
using System ; class GFG { static int getCount ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { int a = 0 , c = N - 1 ; int d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } static public void Main ( ) { int [ ] arr = { 3 , 6 , 10 , 13 , 21 } ; int N = arr . Length ; Console . Write ( getCount ( arr , N ) ) ; } }
Maximize difference between the sum of absolute differences of each element with the remaining array | C # Program to implement the above approach ; Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Print the result ; Driver Code
using System ; class GFG { static void findMaxDifference ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int Leftsum = 0 ; int Totalsum = 0 ; int Minn = Int32 . MaxValue , Maxx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int leftNumbers = i ; int rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; int sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Minn = Math . Min ( Minn , sum ) ; Maxx = Math . Max ( Maxx , sum ) ; Leftsum += arr [ i ] ; } Console . WriteLine ( Maxx - Minn ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 7 } ; int N = arr . Length ; findMaxDifference ( arr , N ) ; } }
Minimum pairs required to be removed such that the array does not contain any pair with sum K | C # program for the above approach ; Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
using System ; class GFG { static int maxcntPairsSumKRemoved ( int [ ] arr , int k ) { int cntPairs = 0 ; if ( arr . Length <= 1 ) return cntPairs ; Array . Sort ( arr ) ; int left = 0 ; int right = arr . Length - 1 ; while ( left < right ) { int s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; Console . WriteLine ( maxcntPairsSumKRemoved ( arr , K ) ) ; } }
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | C # program to implement the above approach ; reverses an array ; Function to find the minimum cost to split array into K subsets ; Sort the array in descending order ; Stores minimum cost to split the array into K subsets ; Stores position of elements of a subset ; Iterate over the range [ 1 , N ] ; Calculate the cost to select X - th element of every subset ; Update min_cost ; Update X ; Driver code ; Function call
using System ; class GFG { static void reverse ( int [ ] a , int n ) { int i , k , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int getMinCost ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; reverse ( arr , n ) ; int min_cost = 0 ; int X = 0 ; for ( int i = 0 ; i < n ; i += k ) { for ( int j = i ; j < i + k && j < n ; j ++ ) { min_cost += arr [ j ] * ( X + 1 ) ; } X ++ ; } return min_cost ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 20 , 7 , 8 } ; int K = 2 ; int N = arr . Length ; Console . WriteLine ( getMinCost ( arr , N , K ) ) ; } }
Minimize difference between the largest and smallest array elements by K replacements | C # program for the above approach ; Function to find minimum difference between largest and smallest element after K replacements ; Sort array in ascending order ; Length of array ; Minimum difference ; Check for all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Prints the minimum possible difference
using System ; class GFG { static int minDiff ( int [ ] A , int K ) { Array . Sort ( A ) ; int n = A . Length ; if ( n <= K ) return 0 ; int mindiff = A [ n - 1 ] - A [ 0 ] ; if ( K == 0 ) return mindiff ; for ( int i = 0 , j = n - 1 - K ; j < n ; ) { mindiff = Math . Min ( mindiff , A [ j ] - A [ i ] ) ; i ++ ; j ++ ; } return mindiff ; } public static void Main ( String [ ] args ) { int [ ] A = { - 1 , 3 , - 1 , 8 , 5 , 4 } ; int K = 3 ; Console . WriteLine ( minDiff ( A , K ) ) ; } }
Minimize difference between the largest and smallest array elements by K replacements | C # program for above approach ; Function to find minimum difference between the largest and smallest element after K replacements ; Create a MaxHeap ; Create a MinHeap ; Update maxHeap and MinHeap with highest and smallest K elements respectively ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Store all max element from maxHeap ; Store all min element from minHeap ; Generating all K + 1 possibilities ; Return answer ; Driver code ; Given array ; Function call
using System ; using System . Collections . Generic ; class GFG { static int minDiff ( int [ ] A , int K ) { if ( A . Length <= K + 1 ) return 0 ; List < int > maxHeap = new List < int > ( ) ; List < int > minHeap = new List < int > ( ) ; foreach ( int n in A ) { maxHeap . Add ( n ) ; maxHeap . Sort ( ) ; if ( maxHeap . Count > K + 1 ) maxHeap . RemoveAt ( 0 ) ; minHeap . Add ( n ) ; minHeap . Sort ( ) ; minHeap . Reverse ( ) ; if ( minHeap . Count > K + 1 ) minHeap . RemoveAt ( 0 ) ; } List < int > maxList = new List < int > ( ) ; while ( maxHeap . Count > 0 ) { maxList . Add ( maxHeap [ 0 ] ) ; maxHeap . RemoveAt ( 0 ) ; } List < int > minList = new List < int > ( ) ; while ( minHeap . Count > 0 ) { minList . Add ( minHeap [ 0 ] ) ; minHeap . RemoveAt ( 0 ) ; } int mindiff = Int32 . MaxValue ; for ( int i = 0 ; i < K ; i ++ ) { mindiff = Math . Min ( mindiff , maxList [ i ] - minList [ K - i ] ) ; } return mindiff ; } static void Main ( ) { int [ ] A = { - 1 , 3 , - 1 , 8 , 5 , 4 } ; int K = 3 ; Console . WriteLine ( minDiff ( A , K ) ) ; } }
Check if all K | C # program to implement the above approach ; Function reverses the elements of the array ; Function to check all subset - sums of K - length subsets in [ ] A is greater that that in the array [ ] B or not ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of [ ] A ; Stores sum of first K elements of [ ] B ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
using System ; public class GFG { static void reverse ( int [ ] myArray ) { Array . Sort ( myArray ) ; Array . Reverse ( myArray ) ; } static bool checkSubsetSum ( int [ ] A , int [ ] B , int N , int K ) { Array . Sort ( A ) ; Array . Sort ( B ) ; reverse ( B ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum1 += A [ i ] ; sum2 += B [ i ] ; } if ( sum1 > sum2 ) { return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] A = { 12 , 11 , 10 , 13 } ; int [ ] B = { 7 , 10 , 6 , 2 } ; int N = A . Length ; int K = 3 ; if ( checkSubsetSum ( A , B , N , K ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }
Sort given array to descending | C # program to implement the above approach ; Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Sort last ( N - K ) array elements in ascending order ; Print array elements ; Driver code
using System ; class GFG { static void sortArrayInDescAsc ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int temp = 0 ; for ( int i = K ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 } ; int N = arr . Length ; int K = 6 ; sortArrayInDescAsc ( arr , N , K ) ; } }
Median of all non | C # program to implement the above approach ; Function to calculate the median of all possible subsets by given operations ; Stores sum of elements of arr [ ] ; Traverse the array arr [ ] ; Update sum ; Sort the array ; DP [ i ] [ j ] : Stores total number of ways to form the sum j by either selecting ith element or not selecting ith item . ; Initialize all the DP states ; Base case ; Fill dp [ i ] [ 0 ] ; Base case ; Fill all the DP states based on the mentioned DP relation ; If j is greater than or equal to arr [ i ] ; Update dp [ i ] [ j ] ; Update dp [ i ] [ j ] ; Stores all possible subset sum ; Traverse all possible subset sum ; Stores count of subsets whose sum is j ; Itearate over the range [ 1 , M ] ; Insert j into sumSub ; Stores middle element of sumSub ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findMedianOfsubSum ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } Array . Sort ( arr ) ; int [ , ] dp = new int [ N , sum + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < sum + 1 ; j ++ ) dp [ i , j ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { dp [ i , 0 ] = 1 ; } dp [ 0 , arr [ 0 ] ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j >= arr [ i ] ) { dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - arr [ i ] ] ; } else { dp [ i , j ] = dp [ i - 1 , j ] ; } } } List < int > sumSub = new List < int > ( ) ; for ( int j = 1 ; j <= sum ; j ++ ) { int M = dp [ N - 1 , j ] ; for ( int i = 1 ; i <= M ; i ++ ) { sumSub . Add ( j ) ; } } int mid = sumSub [ sumSub . Count / 2 ] ; return mid ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 3 } ; int N = arr . Length ; Console . Write ( findMedianOfsubSum ( arr , N ) ) ; } }
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | C # program for the above approach ; Function to print the index from where the operation can be started ; Initialize [ ] B ; Initialize save ; Make [ ] B equals to [ ] arr ; Sort the array [ ] B ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array [ ] arr ; Length of array ; Function Call
using System ; class GFG { static void printIndex ( int [ ] arr , int N ) { int [ ] B = new int [ N ] ; int save = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } Array . Sort ( B ) ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == - 1 ) { Console . Write ( - 1 + " STRNEWLINE " ) ; return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { Console . Write ( 1 ) ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { Console . Write ( N ) ; } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { Console . Write ( i + 1 ) ; break ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 4 , 4 , 5 } ; int N = arr . Length ; printIndex ( arr , N ) ; } }
Lexicographically smallest subsequence possible by removing a character from given string | C # program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Generate all subsequence of length N - 1 ; Store main value of string str ; Sort the vector ; Print first element of vector ; Driver Code ; Given string S ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void firstSubsequence ( string s ) { List < string > allsubseq = new List < string > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { string k = " " ; for ( int j = 0 ; j < s . Length ; j ++ ) { if ( i != j ) { k += s [ j ] ; } } allsubseq . Add ( k ) ; } allsubseq . Sort ( ) ; Console . WriteLine ( allsubseq [ 0 ] ) ; } public static void Main ( ) { string S = " geeksforgeeks " ; firstSubsequence ( S ) ; } }
Maximum even sum subsequence of length K | C # program to implement the above approach ; Function to find the maximum even sum of any subsequence of length K ; If count of elements is less than K ; Stores maximum even subsequence sum ; Stores Even numbers ; Stores Odd numbers ; Traverse the array ; If current element is an odd number ; Insert odd number ; Insert even numbers ; Sort Odd [ ] array ; Sort Even [ ] array ; Stores current index Of Even [ ] array ; Stores current index Of Odd [ ] array ; If K is odd ; If count of elements in Even [ ] >= 1 ; Update maxSum ; Update i ; If count of elements in Even [ ] array is 0. ; Update K ; If count of elements in Even [ ] and odd [ ] >= 2 ; Update maxSum ; Update j ; Update maxSum ; Update i ; Update K ; If count of elements in Even [ ] array >= 2. ; Update maxSum ; Update i ; Update K ; If count of elements in Odd [ ] array >= 2 ; Update maxSum ; Update i . ; Update K . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int evenSumK ( int [ ] arr , int N , int K ) { if ( K > N ) { return - 1 ; } int maxSum = 0 ; List < int > Even = new List < int > ( ) ; List < int > Odd = new List < int > ( ) ; for ( int l = 0 ; l < N ; l ++ ) { if ( arr [ l ] % 2 == 1 ) { Odd . Add ( arr [ l ] ) ; } else { Even . Add ( arr [ l ] ) ; } } Odd . Sort ( ) ; Even . Sort ( ) ; int i = Even . Count - 1 ; int j = Odd . Count - 1 ; while ( K > 0 ) { if ( K % 2 == 1 ) { if ( i >= 0 ) { maxSum += Even [ i ] ; i -- ; } else { return - 1 ; } K -- ; } else if ( i >= 1 && j >= 1 ) { if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; } else { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; } K -= 2 ; } else if ( i >= 1 ) { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; K -= 2 ; } else if ( j >= 1 ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; K -= 2 ; } } return maxSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 10 , 3 , 5 } ; int N = arr . Length ; int K = 3 ; Console . WriteLine ( evenSumK ( arr , N , K ) ) ; } }
Program to find weighted median of a given array | C # program for the above approach ; Function to calculate weighted median ; Store pr of arr [ i ] and W [ i ] ; Sort the list of pr w . r . t . to their arr [ ] values ; If N is odd ; Traverse the set pr from left to right ; Update sums ; If sum becomes > 0.5 ; If N is even ; For lower median traverse the set pr from left ; Update sums ; When sum >= 0.5 ; For upper median traverse the set pr from right ; Update sums ; When sum >= 0.5 ; Driver code ; Given array arr [ ] ; Given weights W [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void weightedMedian ( int [ ] arr , float [ ] W ) { List < Tuple < int , float > > pr = new List < Tuple < int , float > > ( ) ; for ( int index = 0 ; index < arr . Length ; index ++ ) pr . Add ( new Tuple < int , float > ( arr [ index ] , W [ index ] ) ) ; pr . Sort ( ) ; if ( arr . Length % 2 != 0 ) { float sums = 0 ; foreach ( Tuple < int , float > element in pr ) { sums += element . Item2 ; if ( sums > 0.5 ) Console . WriteLine ( " The ▁ Weighted ▁ Median ▁ " + " is ▁ element ▁ " + element . Item1 ) ; } } else { float sums = 0 ; foreach ( Tuple < int , float > element in pr ) { sums += element . Item2 ; if ( sums >= 0.5 ) { Console . WriteLine ( " Lower ▁ Weighted ▁ Median ▁ " + " is ▁ element ▁ " + element . Item1 ) ; break ; } } sums = 0 ; for ( int index = pr . Count - 1 ; index >= 0 ; index -- ) { int element = pr [ index ] . Item1 ; float weight = pr [ index ] . Item2 ; sums += weight ; if ( sums >= 0.5 ) { Console . Write ( " Upper ▁ Weighted ▁ Median ▁ " + " is ▁ element ▁ " + element ) ; break ; } } } } static void Main ( ) { int [ ] arr = { 4 , 1 , 3 , 2 } ; float [ ] W = { 0.25f , 0.49f , 0.25f , 0.01f } ; weightedMedian ( arr , W ) ; } }
Kth smallest element from an array of intervals | C # Program to implement the above approach ; Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Remove minimum element ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Driver code ; Intervals given ; Size of the arr
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int KthSmallestNum ( int [ , ] arr , int n , int k ) { ArrayList pq = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . Add ( new Tuple < int , int > ( arr [ i , 0 ] , arr [ i , 1 ] ) ) ; } int cnt = 1 ; while ( cnt < k ) { pq . Sort ( ) ; pq . Reverse ( ) ; Tuple < int , int > interval = ( Tuple < int , int > ) pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( interval . Item1 < interval . Item2 ) { pq . Add ( new Tuple < int , int > ( interval . Item1 + 1 , interval . Item2 ) ) ; } cnt += 1 ; } pq . Sort ( ) ; pq . Reverse ( ) ; return ( ( Tuple < int , int > ) pq [ 0 ] ) . Item1 + 1 ; } static void Main ( ) { int [ , ] arr = { { 5 , 11 } , { 10 , 15 } , { 12 , 20 } } ; int n = arr . GetLength ( 0 ) ; int k = 12 ; Console . WriteLine ( KthSmallestNum ( arr , n , k ) ) ; } }