codeparrot/xlcost-text-to-code huggingface dataset:
All possible values of floor ( N / K ) for all values of K | Java program for the above approach ; Function to print all possible values of Math . floor ( N / K ) ; loop from 1 to N + 1 ; Driver code
import java . util . * ; class GFG { static void allQuotients ( int N ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int k = 1 ; k <= N + 1 ; k ++ ) { s . add ( N / k ) ; } for ( int it : s ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 5 ; allQuotients ( N ) ; } }
Greatest odd factor of an even number | Java program for the above approach ; Function to print greatest odd factor ; Initialize i with 1 ; Iterate till i <= pow_2 ; Find the pow ( 2 , i ) ; If factor is odd , then print the number and break ; Driver code ; Given Number ; Function Call
class GFG { public static int greatestOddFactor ( int n ) { int pow_2 = ( int ) ( Math . log ( n ) ) ; int i = 1 ; while ( i <= pow_2 ) { int fac_2 = ( 2 * i ) ; if ( n % fac_2 == 0 ) { if ( ( n / fac_2 ) % 2 == 1 ) { return ( n / fac_2 ) ; } } i += 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 8642 ; System . out . println ( greatestOddFactor ( N ) ) ; } }
Find the element in a linked list with frequency at least N / 3 | Java program to find an element with frequency of at least N / 3 in a linked list ; Structure of a node for the linked list ; Utility function to create a node ; Function to find and return the element with frequency of at least N / 3 ; Candidates for being the required majority element ; Store the frequencies of the respective candidates ; Iterate all nodes ; Increase frequency of candidate s ; Increase frequency of candidate t ; Set the new sting as candidate for majority ; Set the new sting as second candidate for majority ; Decrease the frequency ; Check the frequency of two final selected candidate linklist ; Increase the frequency of first candidate ; Increase the frequency of second candidate ; Return the String with higher frequency ; Driver Code
class GFG { static class node { String i ; node next = null ; } ; static node newnode ( String s ) { node temp = new node ( ) ; temp . i = s ; temp . next = null ; return temp ; } static String Majority_in_linklist ( node head ) { String s = " " ; String t = " " ; int p = 0 , q = 0 ; node ptr = null ; while ( head != null ) { if ( s . equals ( head . i ) ) { p = p + 1 ; } else { if ( t . equals ( head . i ) ) { q = q + 1 ; } else { if ( p == 0 ) { s = head . i ; p = 1 ; } else { if ( q == 0 ) { t = head . i ; q = 1 ; } else { p = p - 1 ; q = q - 1 ; } } } } head = head . next ; } head = ptr ; p = 0 ; q = 0 ; while ( head != null ) { if ( s . equals ( head . i ) ) { p = 1 ; } else { if ( t . equals ( head . i ) ) { q = 1 ; } } head = head . next ; } if ( p > q ) { return s ; } else { return t ; } } public static void main ( String [ ] arg ) { node ptr = null ; node head = newnode ( " geeks " ) ; head . next = newnode ( " geeks " ) ; head . next . next = newnode ( " abcd " ) ; head . next . next . next = newnode ( " game " ) ; head . next . next . next . next = newnode ( " game " ) ; head . next . next . next . next . next = newnode ( " knight " ) ; head . next . next . next . next . next . next = newnode ( " harry " ) ; head . next . next . next . next . next . next . next = newnode ( " geeks " ) ; System . out . println ( Majority_in_linklist ( head ) ) ; } }
Form smallest number using indices of numbers chosen from Array with sum less than S | Java implementation to find minimum number which have a maximum length ; Function to find the minimum number which have maximum length ; Find Maximum length of number ; Find minimum number WHich have maximum length ; Driver Code
class GFG { static String max_number ( int arr [ ] , int sum ) { int frac [ ] = new int [ 9 ] ; int maxi = Integer . MIN_VALUE ; StringBuilder ans = new StringBuilder ( ) ; int pos = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { frac [ i ] = sum / arr [ i ] ; if ( frac [ i ] > maxi ) { pos = i ; maxi = frac [ i ] ; } } for ( int i = 0 ; i < maxi ; i ++ ) { ans . append ( ( char ) ( ( pos + 1 ) + '0' ) ) ; } sum -= maxi * arr [ pos ] ; for ( int i = 0 ; i < maxi ; i ++ ) { for ( int j = 1 ; j <= 9 ; j ++ ) { if ( sum + arr [ pos ] - arr [ j - 1 ] >= 0 ) { ans . setCharAt ( i , ( char ) ( j + '0' ) ) ; sum += arr [ pos ] - arr [ j - 1 ] ; break ; } } } if ( maxi == 0 ) { return "0" ; } else { return ans . toString ( ) ; } } public static void main ( String str [ ] ) { int arr [ ] = { 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 } ; int s = 13 ; System . out . println ( max_number ( arr , s ) ) ; } }
Check if given intervals can be made non | Java implementation to check if the intervals can be non - overlapping by by adding or subtracting X to each interval ; Function to check if two intervals overlap with each other ; Condition to check if the intervals overlap ; Function to check if there is a existing overlapping intervals ; Path compression ; Union of two intervals Returns True . If there is a overlapping with the same another interval ; Both have same another overlapping interval ; Function to check if the intervals can be added by X to form non - overlapping intervals ; If the intervals overlaps we will union them ; There is no cycle ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static Boolean checkOverlapping ( ArrayList < Integer > a , ArrayList < Integer > b ) { if ( a . get ( 0 ) < b . get ( 0 ) ) { int temp = a . get ( 0 ) ; a . set ( 0 , b . get ( 0 ) ) ; b . set ( 0 , temp ) ; temp = a . get ( 1 ) ; a . set ( 1 , b . get ( 1 ) ) ; b . set ( 1 , temp ) ; } if ( b . get ( 0 ) <= a . get ( 0 ) && a . get ( 0 ) <= b . get ( 1 ) ) return true ; return false ; } public static int find ( ArrayList < Integer > a , int i ) { if ( a . get ( i ) == i ) { return i ; } a . set ( i , find ( a , a . get ( i ) ) ) ; return a . get ( i ) ; } public static Boolean union ( ArrayList < Integer > a , int x , int y ) { int xs = find ( a , x ) ; int ys = find ( a , y ) ; if ( xs == ys ) { return true ; } a . set ( ys , xs ) ; return false ; } public static Boolean checkNonOverlapping ( ArrayList < ArrayList < Integer > > arr , int n ) { ArrayList < Integer > dsu = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dsu . add ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( checkOverlapping ( arr . get ( i ) , arr . get ( j ) ) ) { if ( union ( dsu , i , j ) ) { return false ; } } } } return true ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > arr = new ArrayList < ArrayList < Integer > > ( ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 4 ) ) ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 2 ) ) ) ; arr . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; int n = arr . size ( ) ; if ( checkNonOverlapping ( arr , n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Number formed after K times repeated addition of smallest divisor of N | Java program to find the Kth number formed after repeated addition of smallest divisor of N ; If N is even ; If N is odd ; Add smallest divisor to N ; Updated N is even ; Driver code
import java . util . * ; class GFG { static void FindValue ( int N , int K ) { if ( N % 2 == 0 ) { N = N + 2 * K ; } else { int i ; for ( i = 2 ; i < Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) break ; } N = N + i ; N = N + 2 * ( K - 1 ) ; } System . out . print ( N ) ; } public static void main ( String args [ ] ) { int N = 9 ; int K = 4 ; FindValue ( N , K ) ; } }
Find last digit in factorial | Java program to find last digit in factorial n . ; Explicitly handle all numbers less than or equal to 4 ; For all numbers greater than 4 the last digit is 0 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int lastDigitFactorial ( int n ) { if ( n == 0 ) return 1 ; else if ( n <= 2 ) return n ; else if ( n == 3 ) return 6 ; else if ( n == 4 ) return 4 ; else return 0 ; } public static void main ( String [ ] args ) { System . out . println ( lastDigitFactorial ( 6 ) ) ; } }
Program to convert Hexa | Java implementation to convert the given HexaDecimal number to its equivalent BCD . ; Function to convert HexaDecimal to its BCD ; Iterating through the digits ; check whether s [ i ] is a character or a integer between 0 to 9 and compute its equivalent BCD ; Driver Code ; Function Call
public class Main { public static void HexaDecimaltoBCD ( String s ) { int len = s . length ( ) , check = 0 ; int num = 0 , sum = 0 , mul = 1 ; for ( int i = 0 ; i <= len - 1 ; i ++ ) { if ( s . charAt ( i ) >= 47 && s . charAt ( i ) <= 52 ) { String result = Integer . toBinaryString ( ( int ) s . charAt ( i ) ) ; System . out . print ( result . substring ( result . length ( ) - 4 ) + " ▁ " ) ; } else { String result = Integer . toBinaryString ( ( int ) s . charAt ( i ) - 55 ) ; System . out . print ( result . substring ( result . length ( ) - 4 ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String s = "11F " ; HexaDecimaltoBCD ( s ) ; } }
Find the sum of the first Nth Heptadecagonal Number | Java program to find the sum of the first N heptadecagonal numbers ; Function to find the N - th heptadecagonal number ; Formula to calculate nth heptadecagonal number ; Function to find the sum of the first N heptadecagonal numbers ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code
class GFG { public static int heptadecagonal_num ( int n ) { return ( ( 15 * n * n ) - 13 * n ) / 2 ; } public static int sum_heptadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += heptadecagonal_num ( i ) ; } return summ ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sum_heptadecagonal_num ( n ) ) ; } }
Program to check if N is a Centered Pentagonal Number or not | Java program for the above approach ; Function to check if number N is a centered pentagonal number ; Condition to check if N is a centered pentagonal number ; Driver Code ; Given Number ; Function call
import java . util . * ; class GFG { static boolean isCenteredpentagonal ( int N ) { float n = ( float ) ( ( 5 + Math . sqrt ( 40 * N - 15 ) ) / 10 ) ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( isCenteredpentagonal ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Program to check if N is a Dodecagonal Number | Java program for the above approach ; Function to check if number N is a dodecagonal number or not ; Condition to check if the N is a dodecagonal number ; Driver Code ; Given Number ; Function call
import java . util . * ; class GFG { static boolean isdodecagonal ( int N ) { float n = ( float ) ( ( 4 + Math . sqrt ( 20 * N + 16 ) ) / 10 ) ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 12 ; if ( isdodecagonal ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Logarithm tricks for Competitive Programming | Java implementation to find the previous and next power of K ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver Code
class GFG { static int prevPowerofK ( int n , int k ) { int p = ( int ) ( Math . log ( n ) / Math . log ( k ) ) ; return ( int ) Math . pow ( k , p ) ; } static int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } public static void main ( String [ ] args ) { int N = 7 ; int K = 2 ; System . out . print ( prevPowerofK ( N , K ) + " ▁ " ) ; System . out . println ( nextPowerOfK ( N , K ) ) ; } }
Sum of all composite numbers lying in the range [ L , R ] for Q queries | Java implementation to find the sum of all Composite numbers in the given range ; Prefix array to precompute the sum of all Composite number ; Function that return number num if num is Composite else return 0 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to precompute the sum of all Composite numbers upto 100000 ; checkComposite ( ) return the number i if i is Composite else return 0 ; Function to print the sum for each query ; Function to print sum of all Composite numbers between [ L , R ] ; Function that pre computes the sum of all Composite numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all Composite number in Range [ L , R ]
import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int isComposite ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return n ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return n ; return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isComposite ( i ) ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + "NEW_LINE"); } static void printSumComposite ( int arr [ ] [ ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int Q = 2 ; int arr [ ] [ ] = { { 10 , 13 } , { 12 , 21 } } ; printSumComposite ( arr , Q ) ; } }
Probability of getting a perfect square when a random number is chosen in a given range | Java implementation to find the probability of getting a perfect square number ; Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver Code ; Function Call
class GFG { static float findProb ( int l , int r ) { float countOfPS = ( float ) ( Math . floor ( Math . sqrt ( r ) ) - Math . ceil ( Math . sqrt ( l ) ) + 1 ) ; float total = r - l + 1 ; float prob = ( float ) countOfPS / ( float ) total ; return prob ; } public static void main ( String [ ] args ) { int L = 16 , R = 25 ; System . out . print ( findProb ( L , R ) ) ; } }
Count of groups having largest size while grouping according to sum of its digits | Java implementation to Count the number of groups having the largest size where groups are according to the sum of its digits ; Function to return sum of digits of i ; Create the dictionary of unique sum ; dictionary that contain unique sum count ; Calculate the sum of its digits ; Function to find the largest size of group ; Count of largest size group ; Driver code
import java . util . HashMap ; import java . util . Map ; class GFG { public static int sumDigits ( int n ) { int sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } public static HashMap < Integer , Integer > constDict ( int n ) { HashMap < Integer , Integer > d = new HashMap < > ( ) ; for ( int i = 1 ; i < n + 1 ; ++ i ) { int sum1 = sumDigits ( i ) ; if ( ! d . containsKey ( sum1 ) ) d . put ( sum1 , 1 ) ; else d . put ( sum1 , d . get ( sum1 ) + 1 ) ; } return d ; } public static int countLargest ( int n ) { HashMap < Integer , Integer > d = constDict ( n ) ; int size = 0 ; int count = 0 ; for ( Map . Entry < Integer , Integer > it : d . entrySet ( ) ) { int k = it . getKey ( ) ; int val = it . getValue ( ) ; if ( val > size ) { size = val ; count = 1 ; } else if ( val == size ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 13 ; int group = countLargest ( n ) ; System . out . println ( group ) ; } }
Find the length of factorial of a number in any given base | A optimised program to find the number of digits in a factorial in base b ; Returns the number of digits present in n ! in base b Since the result can be large long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code ; calling findDigits ( Number , Base )
class GFG { static long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double M_PI = 3.141592 ; double M_E = 2.7182 ; double x = ( ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . log10 ( b ) ) ; return ( long ) ( Math . floor ( x ) + 1 ) ; } public static void main ( String [ ] args ) { System . out . print ( findDigits ( 4 , 16 ) + "NEW_LINE"); System . out . print ( findDigits ( 5 , 8 ) + "NEW_LINE"); System . out . print ( findDigits ( 12 , 16 ) + "NEW_LINE"); System . out . print ( findDigits ( 19 , 13 ) + "NEW_LINE"); } }
Sum of all Non | Java implementation to find the sum of all non - fibonacci numbers in a range from L to R ; Array to precompute the sum of non - fibonacci numbers ; Function to find if a number is a perfect square ; Function that returns N if N is non - fibonacci number ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to precompute sum of non - fibonacci Numbers ; Function to find the sum of all non - fibonacci numbers in a range ; Driver Code ; Pre - computation ; Loop to find the sum for each query
import java . util . * ; class GFG { static long pref [ ] = new long [ 100010 ] ; static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static int isNonFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } static void printSum ( int L , int R ) { int sum = ( int ) ( pref [ R ] - pref [ L - 1 ] ) ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 2 ; int arr [ ] [ ] = { { 1 , 5 } , { 6 , 10 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }
Count twin prime pairs in an Array | Java program to count Twin Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are Twin Primes or not ; Function to find Twin Prime pairs from the given array ; Iterate through all pairs ; Increment count if twin prime pair ; Driver 's code ; Function call to find Twin Primes pair
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } static int countTwinPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( twinPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 11 } ; int n = arr . length ; System . out . print ( countTwinPairs ( arr , n ) ) ; } }
Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | Java implementation of above approach ; Function to fill the position with arr [ i ] = 0 ; Inserting all elements in missing [ ] set from 1 to N ; Inserting unfilled positions ; Removing allocated_elements ; Loop for filling the positions with arr [ i ] != i ; Checking for any arr [ i ] = i ; Finding the suitable position in the array to swap with found i for which arr [ i ] = i ; Checking if the position is present in unfilled_position ; Swapping arr [ i ] & arr [ pos ] ( arr [ pos ] = pos ) ; Function to Print the array ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void solve ( int arr [ ] , int n ) { Set < Integer > unfilled_indices = new HashSet < Integer > ( ) ; Set < Integer > missing = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { missing . add ( i ) ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { unfilled_indices . add ( i ) ; } else { missing . remove ( arr [ i ] ) ; } } int [ ] mi = new int [ missing . size ( ) ] ; int l = 0 ; for ( int x : missing ) { mi [ l ++ ] = x ; } int m = missing . size ( ) ; for ( int it : unfilled_indices ) { arr [ it ] = mi [ m - 1 ] ; m -- ; } int pos = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == i ) { pos = i ; } } int x ; if ( pos != 0 ) { for ( int i = 1 ; i < n ; i ++ ) { if ( pos != i ) { if ( unfilled_indices . contains ( i ) ) { x = arr [ i ] ; arr [ i ] = pos ; arr [ pos ] = x ; break ; } } } } printArray ( arr , n ) ; } static void printArray ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }
Find minimum value of y for the given x values in Q queries from all the given set of lines | Java implementation of the above approach ; Sort the line in decreasing order of their slopes ; If slopes arent equal ; If the slopes are equal ; Checks if line L3 or L1 is better than L2 Intersection of Line 1 and Line 2 has x - coordinate ( b1 - b2 ) / ( m2 - m1 ) Similarly for Line 1 and Line 3 has x - coordinate ( b1 - b3 ) / ( m3 - m1 ) Cross multiplication will give the below result ; To store the lines ; Add the line to the set of lines ; To check if after adding the new line whether old lines are losing significance or not ; Add the present line ; Function to return the y coordinate of the specified line for the given coordinate ; Function to Return the minimum value of y for the given x coordinate ; If there is no lines ; Binary search ; Driver code ; Sort the lines ; Add the lines ; For each query in Q
import java . util . ArrayList ; import java . util . Arrays ; class GFG { static class Line implements Comparable < Line > { int m , c ; public Line ( int m , int c ) { this . m = m ; this . c = c ; } @ Override public int compareTo ( Line l ) { if ( m != l . m ) return l . m - this . m ; else return l . c - this . c ; } boolean check ( Line L1 , Line L2 , Line L3 ) { return ( L3 . c - L1 . c ) * ( L1 . m - L2 . m ) < ( L2 . c - L1 . c ) * ( L1 . m - L3 . m ) ; } } static class Convex_HULL_Trick { ArrayList < Line > l = new ArrayList < > ( ) ; void add ( Line newLine ) { int n = l . size ( ) ; while ( n >= 2 && newLine . check ( l . get ( n - 2 ) , l . get ( n - 1 ) , newLine ) ) { n -- ; } l = new Line [ n ] ; l . add ( newLine ) ; } int value ( int in , int x ) { return l . get ( in ) . m * x + l . get ( in ) . c ; } int minQuery ( int x ) { if ( l . isEmpty ( ) ) return Integer . MAX_VALUE ; int low = 0 , high = ( int ) l . size ( ) - 2 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( value ( mid , x ) > value ( mid + 1 , x ) ) low = mid + 1 ; else high = mid - 1 ; } return value ( low , x ) ; } } ; public static void main ( String [ ] args ) { Line [ ] lines = { new Line ( 1 , 1 ) , new Line ( 0 , 0 ) , new Line ( - 3 , 3 ) } ; int Q [ ] = { - 2 , 2 , 0 } ; int n = 3 , q = 3 ; Convex_HULL_Trick cht = new Convex_HULL_Trick ( ) ; Arrays . sort ( lines ) ; for ( int i = 0 ; i < n ; i ++ ) cht . add ( lines [ i ] ) ; for ( int i = 0 ; i < q ; i ++ ) { int x = Q [ i ] ; System . out . println ( cht . minQuery ( x ) ) ; } } }