codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Manhattan distance between a distinct pair from N coordinates | C # program for the above approach ; Pair class ; Function to calculate the maximum Manhattan distance ; Stores the maximum distance ; Find Manhattan distance using the formula | x1 - x2 | + | y1 - y2 | ; Updating the maximum ; Driver Code ; Given Co - ordinates ; Function call
using System ; using System . Collections . Generic ; class GFG { public class Pair { public int x ; public int y ; public Pair ( int x , int y ) { this . x = x ; this . y = y ; } } static void MaxDist ( List < Pair > A , int N ) { int maximum = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { sum = Math . Abs ( A [ i ] . x - A [ j ] . x ) + Math . Abs ( A [ i ] . y - A [ j ] . y ) ; maximum = Math . Max ( maximum , sum ) ; } } Console . WriteLine ( maximum ) ; } public static void Main ( String [ ] args ) { int n = 3 ; List < Pair > al = new List < Pair > ( ) ; Pair p1 = new Pair ( 1 , 2 ) ; al . Add ( p1 ) ; Pair p2 = new Pair ( 2 , 3 ) ; al . Add ( p2 ) ; Pair p3 = new Pair ( 3 , 4 ) ; al . Add ( p3 ) ; MaxDist ( al , n ) ; } }
Longest increasing subsequence which forms a subarray in the sorted representation of the array | C # program to implement the above approach ; Function to find the length of the longest increasing sorted sequence ; Stores the count of all elements ; Store the original array ; Sort the array ; If adjacent element are not same ; Increment count ; Store frequency of each element ; Initialize a DP array ; Iterate over the array ar [ ] ; Length of the longest increasing sorted sequence ; Iterate over the array ; Current element ; If the element has been encountered the first time ; If all the x - 1 previous elements have already appeared ; Otherwise ; If all x - 1 elements have already been encountered ; Increment the count of the current element ; Update maximum subsequence size ; Return the maximum subsequence size ; Driver code ; Function Call
using System . Collections . Generic ; using System ; class GFG { static int LongestSequence ( int [ ] a , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int [ ] ar = new int [ n + 1 ] ; int i = 0 ; int j = 0 ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = a [ i - 1 ] ; } Array . Sort ( a ) ; int c = 1 ; m [ a [ 0 ] ] = c ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { c ++ ; m [ a [ i ] ] = c ; } } Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; int [ , ] dp = new int [ n + 1 , 3 ] ; cnt [ 0 ] = 0 ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = m [ ar [ i ] ] ; if ( cnt . ContainsKey ( ar [ i ] ) ) cnt [ ar [ i ] ] = cnt [ ar [ i ] ] + 1 ; else cnt [ ar [ i ] ] = 1 ; } int ans = 0 , x ; for ( i = 1 ; i <= n ; i ++ ) { x = ar [ i ] ; if ( dp [ x , 0 ] == 0 ) { if ( dp [ x - 1 , 0 ] == cnt [ x - 1 ] ) { dp [ x , 1 ] = dp [ x - 1 , 1 ] ; dp [ x , 2 ] = dp [ x - 1 , 1 ] ; } else { dp [ x , 1 ] = dp [ x - 1 , 0 ] ; } } dp [ x , 2 ] = Math . Max ( dp [ x - 1 , 0 ] , dp [ x , 2 ] ) ; if ( dp [ x - 1 , 0 ] == cnt [ x - 1 ] ) { dp [ x , 2 ] = Math . Max ( dp [ x , 2 ] , dp [ x - 1 , 1 ] ) ; } for ( j = 0 ; j < 3 ; j ++ ) { dp [ x , j ] ++ ; ans = Math . Max ( ans , dp [ x , j ] ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 6 , 4 , 8 , 2 , 9 } ; int n = arr . Length ; Console . WriteLine ( LongestSequence ( arr , n ) ) ; } } Console . WriteLine ( LongestSequence ( arr , n ) ) ;
Maximize the sum of Kth column of a Matrix | C # program to implement the above approach ; Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code
using System ; class GFG { static int [ , ] findMatrix ( int N , int K ) { int [ , ] mat = new int [ N , N ] ; int element = 1 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < K - 1 ; ++ j ) { mat [ i , j ] = element ++ ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = K - 1 ; j < N ; ++ j ) { mat [ i , j ] = element ++ ; } } return mat ; } static void printMatrix ( int [ , ] mat , int N ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 3 , K = 2 ; int [ , ] mat = findMatrix ( N , K ) ; printMatrix ( mat , N ) ; } }
Range sum queries based on given conditions | C # program for the above approach ; Function to calculate the sum between the given range as per value of m ; Stores the sum ; Condition for a to print the sum between ranges [ a , b ] ; Return sum ; Function to precalculate the sum of both the vectors ; Make Prefix sum array ; Function to compute the result for each query ; Take a dummy vector and copy the element of arr in brr ; Sort the dummy vector ; Compute prefix sum of both vectors ; Performs operations ; Function call to find sum ; Function call to find sum ; Driver Code ; Given [ ] arr ; Number of queries ; Given queries ; Function call
using System ; class GFG { static int range_sum ( int [ ] arr , int a , int b ) { int sum = 0 ; if ( a - 2 < 0 ) sum = arr [ b - 1 ] ; else sum = arr [ b - 1 ] - arr [ a - 2 ] ; return sum ; } static void precompute_sum ( int [ ] arr , int [ ] brr ) { int N = ( int ) arr . Length ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; brr [ i ] = brr [ i ] + brr [ i - 1 ] ; } } static void find_sum ( int [ ] arr , int q , int [ , ] Queries ) { int [ ] brr = new int [ arr . Length ] ; arr . CopyTo ( brr , 0 ) ; int N = ( int ) arr . Length ; Array . Sort ( brr ) ; precompute_sum ( arr , brr ) ; for ( int i = 0 ; i < q ; i ++ ) { int m = Queries [ i , 0 ] ; int a = Queries [ i , 1 ] ; int b = Queries [ i , 2 ] ; if ( m == 1 ) { Console . Write ( range_sum ( arr , a , b ) + " ▁ " ) ; } else if ( m == 2 ) { Console . Write ( range_sum ( brr , a , b ) + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 6 , 4 , 2 , 7 , 2 , 7 } ; int Q = 1 ; int [ , ] Queries = { { 2 , 3 , 6 } } ; find_sum ( arr , Q , Queries ) ; } }
XOR of all possible pairwise sum from two given Arrays | C # program to implement the above approach ; Lower bound ; Function to calculate the XOR of the sum of every pair ; Stores the maximum bit ; Look for all the k - th bit ; Stores the modulo of elements B [ ] with ( 2 ^ ( k + 1 ) ) ; Calculate modulo of array B [ ] with ( 2 ^ ( k + 1 ) ) ; Sort the array C [ ] ; Stores the total number whose k - th bit is set ; Calculate and store the modulo of array A [ ] with ( 2 ^ ( k + 1 ) ) ; Lower bound to count the number of elements having k - th bit in the range ( 2 ^ k - x , 2 * 2 ^ ( k ) - x ) ; Add total number i . e ( r - l ) whose k - th bit is one ; Lower bound to count the number of elements having k - th bit in range ( 3 * 2 ^ k - x , 4 * 2 ^ ( k ) - x ) ; If count is even , Xor of k - th bit becomes zero , no need to add to the answer . If count is odd , only then , add to the final answer ; Return answer ; Driver code ; Function call
using System ; class GFG { static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int XorSum ( int [ ] A , int [ ] B , int N ) { int maxBit = 29 ; int ans = 0 ; for ( int k = 0 ; k < maxBit ; k ++ ) { int [ ] C = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { C [ i ] = B [ i ] % ( 1 << ( k + 1 ) ) ; } Array . Sort ( C ) ; long count = 0 ; long l , r ; for ( int i = 0 ; i < N ; i ++ ) { int x = A [ i ] % ( 1 << ( k + 1 ) ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 2 - x ) ; count += ( r - l ) ; l = lower_bound ( C , 0 , N , ( 1 << k ) * 3 - x ) ; r = lower_bound ( C , 0 , N , ( 1 << k ) * 4 - x ) ; count += ( r - l ) ; } if ( ( count & 1 ) != 0 ) ans += ( 1 << k ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] A = { 4 , 6 , 0 , 0 , 3 , 3 } ; int [ ] B = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = A . Length ; Console . Write ( XorSum ( A , B , N ) + " STRNEWLINE " ) ; } }
Minimize count of Subsets with difference between maximum and minimum element not exceeding K | C # program to implement above approach ; Function to find the minimum count of subsets of required type ; Stores the result ; Store the maximum and minimum element of the current subset ; Update current maximum ; If difference exceeds K ; Update count ; Update maximum and minimum to the current subset ; Driver Code
using System ; class GFG { static int findCount ( int [ ] arr , int N , int K ) { Array . Sort ( arr ) ; int result = 1 ; int cur_max = arr [ 0 ] ; int cur_min = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { cur_max = arr [ i ] ; if ( cur_max - cur_min > K ) { result ++ ; cur_max = arr [ i ] ; cur_min = arr [ i ] ; } } return result ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 8 , 3 , 9 } ; int K = 3 ; int N = arr . Length ; Console . Write ( findCount ( arr , N , K ) ) ; } }
Maximum number of unique Triplets such that each element is selected only once | C # program for the above approach ; Function that finds maximum number of triplets with different elements ; Map M will store the frequency of each element in the array ; Priority queue of pairs { frequency , value } ; ans will store possible triplets ; Extract top 3 elements ; Make a triplet ; Decrease frequency and push back into priority queue if non - zero frequency ; Print the triplets ; Print the triplets ; Driver code ; Given array arr [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int x = 0 ; x < n ; x ++ ) { if ( mp . ContainsKey ( arr [ x ] ) ) { mp [ arr [ x ] ] ++ ; } else { mp [ arr [ x ] ] = 1 ; } } List < Tuple < int , int > > pq = new List < Tuple < int , int > > ( ) ; int cnt = 0 ; foreach ( KeyValuePair < int , int > pa in mp ) pq . Add ( new Tuple < int , int > ( pa . Value , pa . Key ) ) ; List < List < int > > ans = new List < List < int > > ( ) ; pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count >= 3 ) { Tuple < int , int > [ ] ar = new Tuple < int , int > [ 3 ] ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; } ans . Add ( new List < int > ( ) ) ; ans [ cnt ] . Add ( ar [ 0 ] . Item2 ) ; ans [ cnt ] . Add ( ar [ 1 ] . Item2 ) ; ans [ cnt ] . Add ( ar [ 2 ] . Item2 ) ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = new Tuple < int , int > ( ar [ x ] . Item1 - 1 , ar [ x ] . Item2 ) ; if ( ar [ x ] . Item1 != 0 ) { pq . Add ( ar [ x ] ) ; pq . Sort ( ) ; pq . Reverse ( ) ; } } cnt ++ ; } Console . Write ( " Maximum ▁ number ▁ of ▁ possible ▁ triples : ▁ " ) ; Console . WriteLine ( ans . Count ) ; foreach ( List < int > pa in ans ) { foreach ( int v in pa ) Console . Write ( v + " ▁ " ) ; Console . WriteLine ( ) ; } } static void Main ( ) { int [ ] arr = { 2 , 2 , 3 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = arr . Length ; findTriplets ( arr , n ) ; } }
Sort Array such that smallest is at 0 th index and next smallest it at last index and so on | C # program for the above approach ; Function to perform the rearrangement ; Initialize variables ; Loop until i crosses j ; This check is to find the minimum values in the ascending order ; Condition to alternatively iterate variable i and j ; Perform swap operation ; Increment i ; Assign the value of min ; Perform swap ; Decrement i ; Assign the value of min ; Print the array ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static void rearrange ( int [ ] a ) { int i = 0 , j = a . Length - 1 ; int min = 0 , k , x = 0 , temp ; while ( i < j ) { for ( k = i ; k <= j ; k ++ ) { if ( a [ k ] < a [ min ] ) min = k ; } if ( x % 2 == 0 ) { temp = a [ i ] ; a [ i ] = a [ min ] ; a [ min ] = temp ; i ++ ; min = i ; } else { temp = a [ j ] ; a [ j ] = a [ min ] ; a [ min ] = temp ; j -- ; min = j ; } x ++ ; } for ( i = 0 ; i < a . Length ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 3 , 4 , 5 } ; rearrange ( arr ) ; } }
Maximum number of elements greater than X after equally distributing subset of array | C # implementation to find the maximum number of elements greater than X by equally distributing ; Function to find the maximum number of elements greater than X by equally distributing ; Sorting the array ; Loop to iterate over the elements of the array ; If no more elements can become larger than x ; Driver Code
using System ; class GFG { static void redistribute ( int [ ] arr , int n , int x ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { Console . Write ( i + " STRNEWLINE " ) ; break ; } } if ( i == n ) Console . Write ( n + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 2 , 1 } ; int x = 3 ; redistribute ( arr , 4 , x ) ; } }
Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal | C # program for the above approach ; Function to print the remaining element ; Priority queue can be used to construct max - heap ; Insert all element of arr [ ] into priority queue ; Perform operation until heap size becomes 0 or 1 ; Remove largest element ; Remove 2 nd largest element ; If extracted element are not equal ; Find X - Y and push it to heap ; If heap size is 1 , then print the remaining element ; Else print " - 1" ; Driver code ; Given array arr [ ] ; Size of array arr [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void final_element ( int [ ] arr , int n ) { List < int > heap = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) heap . Add ( arr [ i ] ) ; while ( heap . Count > 1 ) { heap . Sort ( ) ; heap . Reverse ( ) ; int X = heap [ 0 ] ; heap . RemoveAt ( 0 ) ; int Y = heap [ 0 ] ; heap . RemoveAt ( 0 ) ; if ( X != Y ) { int diff = Math . Abs ( X - Y ) ; heap . Add ( diff ) ; } } if ( heap . Count == 1 ) { heap . Sort ( ) ; heap . Reverse ( ) ; Console . Write ( heap [ 0 ] ) ; } else { Console . Write ( - 1 ) ; } } static void Main ( ) { int [ ] arr = { 3 , 5 , 2 , 7 } ; int n = arr . Length ; final_element ( arr , n ) ; } }
Maximum sum after rearranging the array for K queries | C # program to find the maximum sum after rearranging the array for K queries ; Function to find maximum sum after rearranging array elements ; Auxiliary array to find the count of each selected elements ; Finding count of every element to be selected ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the count array to get the readonly array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code
using System ; class GFG { static int maxSumArrangement ( int [ ] A , int [ , ] R , int N , int M ) { int [ ] count = new int [ N ] ; int i ; for ( i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; count [ l ] ++ ; if ( r < N ) count [ r ] -- ; } for ( i = 1 ; i < N ; ++ i ) { count [ i ] += count [ i - 1 ] ; } int ans = 0 ; Array . Sort ( count ) ; Array . Sort ( A ) ; for ( i = N - 1 ; i >= 0 ; -- i ) { ans += A [ i ] * count [ i ] ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 6 , 10 , 1 , 5 , 6 } ; int [ , ] R = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } } ; int N = A . Length ; int M = R . GetLength ( 0 ) ; Console . Write ( maxSumArrangement ( A , R , N , M ) ) ; } }
Sorting objects using In | C # implementation of the approach ; Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; Create a hash table ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Driver code ; Let 's represent objects as strings
using System ; using System . Collections . Generic ; class GFG { static int partition ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { int j = l - 1 ; String temp ; int last_element = hash [ objects [ r ] ] ; for ( int i = l ; i < r ; i ++ ) { if ( hash [ objects [ i ] ] <= last_element ) { j ++ ; temp = objects [ i ] ; objects [ i ] = objects [ j ] ; objects [ j ] = temp ; } } j ++ ; temp = objects [ r ] ; objects [ r ] = objects [ j ] ; objects [ j ] = temp ; return j ; } static void quicksort ( List < String > objects , int l , int r , Dictionary < String , int > hash ) { if ( l < r ) { int mid = partition ( objects , l , r , hash ) ; quicksort ( objects , l , mid - 1 , hash ) ; quicksort ( objects , mid + 1 , r , hash ) ; } } static void sortObj ( List < String > objects ) { Dictionary < String , int > hash = new Dictionary < String , int > ( ) ; hash . Add ( " blue " , 1 ) ; hash . Add ( " red " , 2 ) ; hash . Add ( " yellow " , 3 ) ; quicksort ( objects , 0 , objects . Count - 1 , hash ) ; for ( int i = 0 ; i < objects . Count ; i ++ ) Console . Write ( objects [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > objects = new List < String > { " red " , " blue " , " red " , " yellow " , " blue " } ; sortObj ( objects ) ; } }
Minimum steps required to reduce all the elements of the array to zero | C # implementation of the approach ; method to get maximum number from array elements ; Function to return the minimum steps required to reduce all the elements to 0 ; Maximum element from the array ; Driver code
using System ; class GFG { static int getMax ( int [ ] inputArray ) { int maxValue = inputArray [ 0 ] ; for ( int i = 1 ; i < inputArray . Length ; i ++ ) { if ( inputArray [ i ] > maxValue ) { maxValue = inputArray [ i ] ; } } return maxValue ; } static int minSteps ( int [ ] arr , int n ) { int maxVal = getMax ( arr ) ; return maxVal ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 } ; int n = arr . Length ; Console . WriteLine ( minSteps ( arr , n ) ) ; } }
Maximum water that can be stored between two buildings | C # implementation of the above approach ; Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver code
using System ; class GFG { static int maxWater ( int [ ] height , int n ) { int max = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int current = ( Math . Min ( height [ i ] , height [ j ] ) * ( j - i - 1 ) ) ; max = Math . Max ( max , current ) ; } } return max ; } static public void Main ( ) { int [ ] height = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . Length ; Console . WriteLine ( maxWater ( height , n ) ) ; } }
Check if the given array contains all the divisors of some integer | C # implementation of the approach ; returns th maximum element of the array ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int max_element ( int [ ] a ) { int m = a [ 0 ] ; for ( int i = 0 ; i < a . Length ; i ++ ) m = Math . Max ( a [ i ] , m ) ; return m ; } static bool checkDivisors ( int [ ] a , int n ) { int X = max_element ( a ) ; List < int > b = new List < int > ( ) ; for ( int i = 1 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { b . Add ( i ) ; if ( X / i != i ) b . Add ( X / i ) ; } } if ( b . Count != n ) return false ; Array . Sort ( a ) ; b . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] != a [ i ] ) return false ; } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 } ; int N = arr . Length ; if ( checkDivisors ( arr , N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Kth largest node among all directly connected nodes to the given node in an undirected graph | C # implementation of the approach ; pair class ; Function to print Kth node for each node ; Vector to store nodes directly connected to ith node along with their values ; Add edges to the Vector along with the values of the node ; Sort neighbors of every node and find the Kth node ; Get the kth node ; If total nodes are < k ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int a , int b ) { first = a ; second = b ; } } class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { pair first = ( pair ) a ; pair second = ( pair ) b ; return first . first - second . first ; } } static void findKthNode ( int [ ] u , int [ ] v , int n , int [ ] val , int V , int k ) { ArrayList g = new ArrayList ( ) ; for ( int i = 0 ; i < V ; i ++ ) g . Add ( new ArrayList ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { ( ( ArrayList ) g [ u [ i ] ] ) . Add ( new pair ( val [ v [ i ] ] , v [ i ] ) ) ; ( ( ArrayList ) g [ v [ i ] ] ) . Add ( new pair ( val [ u [ i ] ] , u [ i ] ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { if ( ( ( ArrayList ) g [ i ] ) . Count > 0 ) { ArrayList tmp = ( ArrayList ) g [ i ] ; tmp . Sort ( new sortHelper ( ) ) ; g [ i ] = tmp ; } if ( k <= ( ( ArrayList ) g [ i ] ) . Count ) Console . Write ( ( ( pair ) ( ( ArrayList ) g [ i ] ) [ ( ( ArrayList ) g [ i ] ) . Count - k ] ) . second + " STRNEWLINE " ) ; else Console . Write ( " - 1 STRNEWLINE " ) ; } return ; } public static void Main ( string [ ] args ) { int V = 3 ; int [ ] val = { 2 , 4 , 3 } ; int [ ] u = { 0 , 0 , 1 } ; int [ ] v = { 2 , 1 , 2 } ; int n = u . Length ; int k = 2 ; findKthNode ( u , v , n , val , V , k ) ; } }
Minimum length of square to contain at least half of the given Coordinates | C # program to find next identical year ; Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
using System ; class GFG { static int mod ( int x ) { if ( x >= 0 ) return x ; return - x ; } static void findSquare ( int n ) { int [ , ] points = new int [ 4 , 2 ] { { 1 , 2 } , { - 3 , 4 } , { 1 , 78 } , { - 3 , - 7 } } ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = points [ i , 0 ] ; y = points [ i , 1 ] ; a [ i ] = Math . Max ( mod ( x ) , mod ( y ) ) ; } Array . Sort ( a ) ; int index = ( int ) ( n / 2 - 1 ) ; Console . WriteLine ( " Minimum ▁ M ▁ required ▁ is : ▁ " + a [ index ] ) ; } public static void Main ( String [ ] args ) { int N ; N = 4 ; findSquare ( N ) ; } }