codeparrot/xlcost-text-to-code huggingface dataset:
Unprimeable Numbers | C ++ Program to check if n is unprimeable Number ; function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a unprimeable Number ; number should be composite ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool 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 = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool isUnPrimeableNum ( int N ) { if ( isPrime ( N ) ) return false ; string s = to_string ( N ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { string str = s ; for ( int i = 0 ; i <= 9 ; i ++ ) { char c = '0' + i ; str [ j ] = c ; int Num = stoi ( str ) ; if ( str [ j ] != s [ j ] && isPrime ( Num ) ) { return false ; } } } return true ; } int main ( ) { int n = 200 ; if ( isUnPrimeableNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the Nth row in Pascal 's Triangle | C ++ program to find the Nth index row in Pascal 's triangle ; Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getRow ( int rowIndex ) { vector < int > currow ; currow . push_back ( 1 ) ; if ( rowIndex == 0 ) { return currow ; } vector < int > prev = getRow ( rowIndex - 1 ) ; for ( int i = 1 ; i < prev . size ( ) ; i ++ ) { int curr = prev [ i - 1 ] + prev [ i ] ; currow . push_back ( curr ) ; } currow . push_back ( 1 ) ; return currow ; } int main ( ) { int n = 3 ; vector < int > arr = getRow ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( i == arr . size ( ) - 1 ) cout << arr [ i ] ; else cout << arr [ i ] << " , ▁ " ; } return 0 ; }
All possible values of floor ( N / K ) for all values of K | C ++ Program for the above approach ; Function to print all possible values of floor ( N / K ) ; loop from 1 to N + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void allQuotients ( int N ) { set < int > s ; for ( int k = 1 ; k <= N + 1 ; k ++ ) { s . insert ( N / k ) ; } for ( auto it : s ) cout << it << " ▁ " ; } int main ( ) { int N = 5 ; allQuotients ( N ) ; return 0 ; }
Greatest odd factor of an even number | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int greatestOddFactor ( int n ) { int pow_2 = ( int ) ( 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 ; } } int main ( ) { int N = 8642 ; cout << greatestOddFactor ( N ) ; return 0 ; }
Find the element in a linked list with frequency at least N / 3 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { string i ; node * next = NULL ; } ; struct node * newnode ( string s ) { struct node * temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> i = s ; temp -> next = NULL ; return temp ; } string Majority_in_linklist ( node * head ) { string s = " " , t = " " ; int p = 0 , q = 0 ; node * ptr = NULL ; while ( head != NULL ) { if ( s . compare ( head -> i ) == 0 ) { p = p + 1 ; } else { if ( t . compare ( head -> i ) == 0 ) { 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 . compare ( head -> i ) == 0 ) { p = 1 ; } else { if ( t . compare ( head -> i ) == 0 ) { q = 1 ; } } head = head -> next ; } if ( p > q ) { return s ; } else { return t ; } } int main ( ) { 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 " ) ; cout << Majority_in_linklist ( head ) << endl ; return 0 ; }
Form smallest number using indices of numbers chosen from Array with sum less than S | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string max_number ( int arr [ ] , int sum ) { int frac [ 9 ] ; int maxi = INT_MIN ; string ans ; int pos ; for ( int i = 0 ; i < 9 ; i ++ ) { frac [ i ] = sum / arr [ i ] ; if ( frac [ i ] > maxi ) { pos = i ; maxi = frac [ i ] ; } } ans . insert ( 0 , string ( maxi , ( 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 [ i ] = ( j + '0' ) ; sum += arr [ pos ] - arr [ j - 1 ] ; break ; } } } if ( maxi == 0 ) { return 0 ; } else { return ans ; } } int main ( ) { int arr [ 9 ] = { 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 } ; int s = 13 ; cout << max_number ( arr , s ) ; return 0 ; }
Check if given intervals can be made non | C ++ implementation to check if the intervals can be non - overlapping 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkOverlapping ( vector < int > a , vector < int > b ) { if ( a [ 0 ] < b [ 0 ] ) { a . swap ( b ) ; } if ( b [ 0 ] <= a [ 0 ] <= b [ 1 ] ) return true ; return false ; } int find ( int a [ ] , int i ) { if ( a [ i ] == i ) return i ; a [ i ] = find ( a , a [ i ] ) ; return a [ i ] ; } bool Union ( int a [ ] , int x , int y ) { int xs = find ( a , x ) ; int ys = find ( a , y ) ; if ( xs == ys ) { return true ; } a [ ys ] = xs ; return false ; } bool checkNonOverlapping ( vector < vector < int > > arr , int n ) { int dsu [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dsu [ i ] = i ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( checkOverlapping ( arr [ i ] , arr [ j ] ) ) { if ( Union ( dsu , i , j ) ) { return false ; } } } } return true ; } int main ( ) { vector < vector < int > > arr = { { 1 , 4 } , { 2 , 2 } , { 2 , 3 } } ; int n = arr . size ( ) ; if ( checkNonOverlapping ( arr , n ) ) { cout << " YES " << endl ; } else { cout << " NO " << endl ; } return 0 ; }
Number formed after K times repeated addition of smallest divisor of N | C ++ 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
#include <bits/stdc++.h> NEW_LINE #include <cmath> NEW_LINE using namespace std ; void FindValue ( int N , int K ) { if ( N % 2 == 0 ) { N = N + 2 * K ; } else { int i ; for ( i = 2 ; i < sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) break ; } N = N + i ; N = N + 2 * ( K - 1 ) ; } cout << N << endl ; } int main ( ) { int N = 9 ; int K = 4 ; FindValue ( N , K ) ; }
Find last digit in factorial | C ++ 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
#include <iostream> NEW_LINE using namespace std ; int lastDigitFactorial ( unsigned 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 ; } int main ( ) { cout << lastDigitFactorial ( 6 ) ; return 0 ; }
Program to convert Hexa | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 [ i ] >= 47 && s [ i ] <= 52 ) cout << bitset < 4 > ( s [ i ] ) << " ▁ " ; else cout << bitset < 4 > ( s [ i ] - 55 ) << " ▁ " ; } } int main ( ) { string s = "11F " ; HexaDecimaltoBCD ( s ) ; return 0 ; }
Find the sum of the first Nth Heptadecagonal Number | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int heptadecagonal_num ( int n ) { return ( ( 15 * n * n ) - 13 * n ) / 2 ; } int sum_heptadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += heptadecagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout << sum_heptadecagonal_num ( n ) ; }
Program to check if N is a Centered Pentagonal Number or not | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenteredpentagonal ( int N ) { float n = ( 5 + sqrt ( 40 * N - 15 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 6 ; if ( isCenteredpentagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Dodecagonal Number | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isdodecagonal ( int N ) { float n = ( 4 + sqrt ( 20 * N + 16 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 12 ; if ( isdodecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Logarithm tricks for Competitive Programming | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prevPowerofK ( int n , int k ) { int p = ( int ) ( log ( n ) / log ( k ) ) ; return ( int ) pow ( k , p ) ; } int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } int main ( ) { int N = 7 ; int K = 2 ; cout << prevPowerofK ( N , K ) << " ▁ " ; cout << nextPowerOfK ( N , K ) << endl ; return 0 ; }
Sum of all composite numbers lying in the range [ L , R ] for Q queries | C ++ implementation to find the sum of all composite numbers in the given range ; Prefix array to precompute the sum of all composite numbers ; 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 10 ^ 5 ; isComposite ( ) 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 ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long pref [ 100001 ] ; 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 ; } void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isComposite ( i ) ; } } void printSum ( int L , int R ) { cout << pref [ R ] - pref [ L - 1 ] << endl ; } void printSumComposite ( int arr [ ] [ 2 ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } int main ( ) { int Q = 2 ; int arr [ ] [ 2 ] = { { 10 , 13 } , { 12 , 21 } } ; printSumComposite ( arr , Q ) ; return 0 ; }
Probability of getting a perfect square when a random number is chosen in a given range | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; float findProb ( int l , int r ) { float countOfPS = floor ( sqrt ( r ) ) - ceil ( sqrt ( l ) ) + 1 ; float total = r - l + 1 ; float prob = ( float ) countOfPS / ( float ) total ; return prob ; } int main ( ) { int L = 16 , R = 25 ; cout << findProb ( L , R ) ; return 0 ; }
Count of groups having largest size while grouping according to sum of its digits | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumDigits ( int n ) { int sum = 0 ; while ( n ) { sum += n % 10 ; n /= 10 ; } return sum ; } map < int , int > constDict ( int n ) { map < int , int > d ; for ( int i = 1 ; i < n + 1 ; ++ i ) { int sum1 = sumDigits ( i ) ; if ( d . find ( sum1 ) == d . end ( ) ) d [ sum1 ] = 1 ; else d [ sum1 ] += 1 ; } return d ; } int countLargest ( int n ) { map < int , int > d = constDict ( n ) ; int size = 0 ; int count = 0 ; for ( auto it = d . begin ( ) ; it != d . end ( ) ; ++ it ) { int k = it -> first ; int val = it -> second ; if ( val > size ) { size = val ; count = 1 ; } else if ( val == size ) count += 1 ; } return count ; } int main ( ) { int n = 13 ; int group = countLargest ( n ) ; cout << group << endl ; return 0 ; }
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 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 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( ( n * log10 ( n / M_E ) + log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( log10 ( b ) ) ; return floor ( x ) + 1 ; } int main ( ) { cout << findDigits ( 4 , 16 ) << endl ; cout << findDigits ( 5 , 8 ) << endl ; cout << findDigits ( 12 , 16 ) << endl ; cout << findDigits ( 19 , 13 ) << endl ; return 0 ; }
Sum of all Non | C ++ 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
#include <bits/stdc++.h> NEW_LINE #define ll  int NEW_LINE using namespace std ; long long pref [ 100010 ] ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } int isNonFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } void printSum ( int L , int R ) { int sum = pref [ R ] - pref [ L - 1 ] ; cout << sum << " ▁ " ; } int main ( ) { compute ( ) ; int Q = 2 ; int arr [ ] [ 2 ] = { { 1 , 5 } , { 6 , 10 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } return 0 ; }
Count twin prime pairs in an Array | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool 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 ; } bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && abs ( n1 - n2 ) == 2 ) ; } 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 ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countTwinPairs ( arr , n ) ; return 0 ; }
Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int [ ] , int ) ; void solve ( int arr [ ] , int n ) { set < int > unfilled_indices ; set < int > missing ; for ( int i = 1 ; i < n ; i ++ ) missing . insert ( i ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) unfilled_indices . insert ( i ) ; else { auto it = missing . find ( arr [ i ] ) ; missing . erase ( it ) ; } } auto it2 = missing . end ( ) ; it2 -- ; for ( auto it = unfilled_indices . begin ( ) ; it != unfilled_indices . end ( ) ; it ++ , it2 -- ) { arr [ * it ] = * it2 ; } 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 . find ( i ) != unfilled_indices . end ( ) ) { x = arr [ i ] ; arr [ i ] = pos ; arr [ pos ] = x ; break ; } } } } printArray ( arr , n ) ; } void printArray ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( arr , n ) ; return 0 ; }
Find minimum value of y for the given x values in Q queries from all the given set of lines | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Line { int m , c ; public : bool operator< ( Line l ) { if ( m != l . m ) return m > l . m ; else return c > l . c ; } bool check ( Line L1 , Line L2 , Line L3 ) { return ( L3 . c - L1 . c ) * ( L1 . m - L2 . m ) < ( L2 . c - L1 . c ) * ( L1 . m - L3 . m ) ; } } ; struct Convex_HULL_Trick { vector < Line > l ; void add ( Line newLine ) { int n = l . size ( ) ; while ( n >= 2 && newLine . check ( l [ n - 2 ] , l [ n - 1 ] , newLine ) ) { n -- ; } l . resize ( n ) ; l = new Line [ n ] ; l . push_back ( newLine ) ; } int value ( int in , int x ) { return l [ in ] . m * x + l [ in ] . c ; } int minQuery ( int x ) { if ( l . empty ( ) ) return INT_MAX ; 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 ) ; } } ; int main ( ) { Line lines [ ] = { { 1 , 1 } , { 0 , 0 } , { -3 , 3 } } ; int Q [ ] = { -2 , 2 , 0 } ; int n = 3 , q = 3 ; Convex_HULL_Trick cht ; sort ( lines , lines + n ) ; for ( int i = 0 ; i < n ; i ++ ) cht . add ( lines [ i ] ) ; for ( int i = 0 ; i < q ; i ++ ) { int x = Q [ i ] ; cout << cht . minQuery ( x ) << endl ; } return 0 ; }