codeparrot/xlcost-text-to-code huggingface dataset:
Range sum queries based on given conditions | Java 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
import java . util . * ; 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 = arr . clone ( ) ; int N = ( int ) arr . length ; Arrays . 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 ) { System . out . print ( range_sum ( arr , a , b ) + " ▁ " ) ; } else if ( m == 2 ) { System . out . print ( 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 | Java 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
import java . util . * ; 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 ) { final 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 ) ) ; } Arrays . 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 ; System . out . print ( XorSum ( A , B , N ) + "NEW_LINE"); } }
Minimize count of Subsets with difference between maximum and minimum element not exceeding K | Java 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
import java . util . * ; class GFG { static int findCount ( int arr [ ] , int N , int K ) { Arrays . 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 ; System . out . print ( findCount ( arr , N , K ) ) ; } }
Minimize the Sum of all the subarrays made up of the products of same | Java program to implement the above approach ; Function to rearrange the second array such that the sum of its product of same indexed elements from both the arrays is minimized ; Stores ( i - 1 ) * ( n - i ) * a [ i ] for every i - th element ; Updating the value of pro according to the function ; Sort the array in reverse order ; Sort the products ; Updating the ans ; Return the ans ; Driver code ; Function call
import java . util . * ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int findMinValue ( int [ ] a , int [ ] b ) { int n = a . length ; int [ ] pro = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { pro [ i ] = ( ( i + 1 ) * ( n - i ) ) ; pro [ i ] *= ( 1L * a [ i ] ) ; ; } Integer [ ] input = Arrays . stream ( b ) . boxed ( ) . toArray ( Integer [ ] :: new ) ; Arrays . sort ( input , ( x , y ) -> y - x ) ; b = Arrays . stream ( input ) . mapToInt ( Integer :: intValue ) . toArray ( ) ; Arrays . sort ( pro ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { ans += ( pro [ i ] % mod * b [ i ] ) % mod ; ans %= mod ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 } ; int [ ] b = { 2 , 3 , 2 } ; System . out . print ( findMinValue ( a , b ) + "NEW_LINE"); } }
Maximum number of unique Triplets such that each element is selected only once | Java 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 function ; Given array arr [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findTriplets ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int x = 0 ; x < n ; x ++ ) mp . put ( arr [ x ] , mp . getOrDefault ( arr [ x ] , 0 ) + 1 ) ; PriorityQueue < pair > pq = new PriorityQueue < > ( ( a , b ) -> a . first - b . first ) ; for ( Map . Entry < Integer , Integer > k : mp . entrySet ( ) ) pq . add ( new pair ( k . getValue ( ) , k . getKey ( ) ) ) ; ArrayList < List < Integer > > ans = new ArrayList < > ( ) ; while ( pq . size ( ) >= 3 ) { pair [ ] ar = new pair [ 3 ] ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] = pq . peek ( ) ; pq . poll ( ) ; } ans . add ( Arrays . asList ( ar [ 0 ] . second , ar [ 1 ] . second , ar [ 2 ] . second ) ) ; for ( int x = 0 ; x < 3 ; x ++ ) { ar [ x ] . first -- ; if ( ar [ x ] . first != 0 ) pq . add ( ar [ x ] ) ; } } System . out . println ( " Maximum ▁ number ▁ of ▁ " + " possible ▁ triples : ▁ " + ans . size ( ) ) ; for ( List < Integer > pa : ans ) { for ( Integer v : pa ) System . out . print ( v + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { 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 | Java 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
import java . io . * ; import java . util . * ; public 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 ++ ) System . out . print ( 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 | Java 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
import java . util . * ; class GFG { static void redistribute ( Integer arr [ ] , int n , int x ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum / ( i + 1 ) < x ) { System . out . print ( i + "NEW_LINE"); break ; } } if ( i == n ) System . out . print ( n + "NEW_LINE"); } public static void main ( String [ ] args ) { Integer 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 | Java 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
import java . io . * ; import java . util . Collections ; import java . util . * ; class GFG { static public int final_element ( Integer [ ] arr , int n ) { if ( arr == null ) { return 0 ; } PriorityQueue < Integer > heap = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { heap . offer ( arr [ i ] ) ; } while ( heap . size ( ) > 1 ) { int X = heap . poll ( ) ; int Y = heap . poll ( ) ; if ( X != Y ) { int diff = Math . abs ( X - Y ) ; heap . offer ( diff ) ; } } return heap . size ( ) == 1 ? heap . poll ( ) : - 1 ; } public static void main ( String [ ] args ) { Integer arr [ ] = new Integer [ ] { 3 , 5 , 2 , 7 } ; int n = arr . length ; System . out . println ( final_element ( arr , n ) ) ; } }
Maximum sum after rearranging the array for K queries | Java 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 final array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code
import java . util . * ; 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 ; Arrays . sort ( count ) ; Arrays . 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 . length ; System . out . print ( maxSumArrangement ( A , R , N , M ) ) ; } }
Sum of M maximum distinct digit sum from 1 to N that are factors of K | Java implementation to find the sum of maximum distinct digit sum of at most M numbers from 1 to N that are factors of K ; Function to find the factors of K in N ; Initialise a vector ; Find out the factors of K less than N ; Find the digit sum of each factor ; Sum of digits for each element in vector ; Find the largest M distinct digit sum from the digitSum vector ; Find the sum of last M numbers . ; Find the at most M numbers from N natural numbers whose digit sum is distinct and those M numbers are factors of K . ; Find out the factors of K less than N ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; " HashSet " is stores only unique elements ; Finding the sum and returning it ; Driver Code ; Function Call
import java . util . * ; class GFG { public static Vector < Integer > findFactors ( int n , int k ) { Vector < Integer > factors = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( k ) ; i ++ ) { if ( k % i == 0 ) { if ( k / i == i && i <= n ) factors . add ( i ) ; else { if ( i <= n ) factors . add ( i ) ; if ( k / i <= n ) factors . add ( k / i ) ; } } } return factors ; } public static Vector < Integer > findDigitSum ( Vector < Integer > a ) { for ( int i = 0 ; i < a . size ( ) ; i ++ ) { int c = 0 ; while ( a . get ( i ) > 0 ) { c += ( a . get ( i ) % 10 ) ; a . set ( i , ( a . get ( i ) / 10 ) ) ; } a . set ( i , c ) ; } return a ; } public static int findMMaxDistinctDigitSum ( Vector < Integer > distinctDigitSum , int m ) { int sum = 0 ; for ( int i = distinctDigitSum . size ( ) - 1 ; i >= 0 && m > 0 ; i -- , m -- ) sum += distinctDigitSum . get ( i ) ; return sum ; } public static int findDistinctMnumbers ( int n , int k , int m ) { Vector < Integer > factors = findFactors ( n , k ) ; Vector < Integer > digitSum = findDigitSum ( factors ) ; Collections . sort ( digitSum ) ; HashSet < Integer > hs1 = new HashSet < Integer > ( digitSum ) ; Vector < Integer > vect2 = new Vector < Integer > ( hs1 ) ; return findMMaxDistinctDigitSum ( vect2 , m ) ; } public static void main ( String args [ ] ) { int n = 100 , k = 80 , m = 4 ; System . out . println ( findDistinctMnumbers ( n , k , m ) ) ; } }
Sorting objects using In | Java 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
import java . util . * ; class GFG { static int partition ( Vector < String > objects , int l , int r , Map < String , Integer > hash ) { int j = l - 1 ; int last_element = hash . get ( objects . get ( r ) ) ; for ( int i = l ; i < r ; i ++ ) { if ( hash . get ( objects . get ( i ) ) <= last_element ) { j ++ ; Collections . swap ( objects , i , j ) ; } } j ++ ; Collections . swap ( objects , j , r ) ; return j ; } static void quicksort ( Vector < String > objects , int l , int r , Map < String , Integer > 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 ( Vector < String > objects ) { Map < String , Integer > hash = new HashMap < > ( ) ; hash . put ( " blue " , 1 ) ; hash . put ( " red " , 2 ) ; hash . put ( " yellow " , 3 ) ; quicksort ( objects , 0 , objects . size ( ) - 1 , hash ) ; for ( int i = 0 ; i < objects . size ( ) ; i ++ ) System . out . print ( objects . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > objects = new Vector < > ( Arrays . asList ( " red " , " blue " , " red " , " yellow " , " blue " ) ) ; sortObj ( objects ) ; } }
Minimum steps required to reduce all the elements of the array to zero | Java 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
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 ; System . out . println ( minSteps ( arr , n ) ) ; } }
Maximum water that can be stored between two buildings | Java implementation of the above approach ; Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver code
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 ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . length ; System . out . print ( maxWater ( height , n ) ) ; } }
Maximum water that can be stored between two buildings | Java implementation of the above approach ; Return the maximum water that can be stored ; Make pairs with indices ; Sort array based on heights ; To store the min and max index so far from the right ; Current building paired with the building greater in height and on the extreme left ; Current building paired with the building greater in height and on the extreme right ; Maximum so far ; Update the maximum and minimum so far ; Driver code
import java . util . Arrays ; class Pair implements Comparable < Pair > { int ind , val ; Pair ( int ind , int val ) { this . ind = ind ; this . val = val ; } @ Override public int compareTo ( Pair o ) { if ( this . val > o . val ) return 1 ; return - 1 ; } } class GFG { static int maxWater ( int height [ ] , int n ) { Pair pairs [ ] = new Pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) pairs [ i ] = new Pair ( i , height [ i ] ) ; Arrays . sort ( pairs ) ; int minIndSoFar = pairs [ n - 1 ] . ind ; int maxIndSoFar = pairs [ n - 1 ] . ind ; int max = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int left = 0 ; if ( minIndSoFar < pairs [ i ] . ind ) { left = ( pairs [ i ] . val * ( pairs [ i ] . ind - minIndSoFar - 1 ) ) ; } int right = 0 ; if ( maxIndSoFar > pairs [ i ] . ind ) { right = ( pairs [ i ] . val * ( maxIndSoFar - pairs [ i ] . ind - 1 ) ) ; } max = Math . max ( left , Math . max ( right , max ) ) ; minIndSoFar = Math . min ( minIndSoFar , pairs [ i ] . ind ) ; maxIndSoFar = Math . max ( maxIndSoFar , pairs [ i ] . ind ) ; } return max ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . length ; System . out . print ( maxWater ( height , n ) ) ; } }
Check if the given array contains all the divisors of some integer | Java 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
import java . util . * ; 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 boolean checkDivisors ( int a [ ] , int n ) { int X = max_element ( a ) ; Vector < Integer > b = new Vector < Integer > ( ) ; 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 . size ( ) != n ) return false ; Arrays . sort ( a ) ; Collections . sort ( b ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b . get ( 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 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Kth largest node among all directly connected nodes to the given node in an undirected graph | Java 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
import java . util . * ; class GFG { static class pair { int first , second ; pair ( int a , int b ) { first = a ; second = b ; } } static void findKthNode ( int u [ ] , int v [ ] , int n , int val [ ] , int V , int k ) { Vector < Vector < pair > > g = new Vector < Vector < pair > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) g . add ( new Vector < pair > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { g . get ( u [ i ] ) . add ( new pair ( val [ v [ i ] ] , v [ i ] ) ) ; g . get ( v [ i ] ) . add ( new pair ( val [ u [ i ] ] , u [ i ] ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { if ( g . get ( i ) . size ( ) > 0 ) Collections . sort ( g . get ( i ) , new Comparator < pair > ( ) { public int compare ( pair p1 , pair p2 ) { return p1 . first - p2 . first ; } } ) ; if ( k <= g . get ( i ) . size ( ) ) System . out . printf ( "%dNEW_LINE", g.get(i).get(g.get(i).size() - k).second); else System . out . printf ( "-1NEW_LINE"); } 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 | Java 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
import java . util . * ; class GFG { static int mod ( int x ) { if ( x >= 0 ) return x ; return - x ; } static void findSquare ( int n ) { int points [ ] [ ] = { { 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 ) ) ; } Arrays . sort ( a ) ; int index = ( int ) ( Math . floor ( n / 2 ) - 1 ) ; System . out . println ( " Minimum ▁ M ▁ required ▁ is : ▁ " + a [ index ] ) ; } public static void main ( String [ ] args ) { int N ; N = 4 ; findSquare ( N ) ; } }