codeparrot/xlcost-text-to-code huggingface dataset:
Path length having maximum number of bends | C ++ program to find path length having maximum number of bends ; structure node ; Utility function to create a new node ; Recursive function to calculate the path length having maximum number of bends . The following are parameters for this function . node -- > pointer to the current node dir -- > determines whether the current node is left or right child of it 's parent node bends --> number of bends so far in the current path. maxBends --> maximum number of bends in a path from root to leaf soFar --> length of the current path so far traversed len --> length of the path having maximum number of bends  ; Base Case ; Leaf node ; Recurring for both left and right child ; Helper function to call findMaxBendsUtil ( ) ; Call for left subtree of the root ; Call for right subtree of the root ; Include the root node as well in the path length ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2 \ 1 / 9
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( int key ) { struct Node * node = new Node ( ) ; node -> left = NULL ; node -> right = NULL ; node -> key = key ; return node ; } void findMaxBendsUtil ( struct Node * node , char dir , int bends , int * maxBends , int soFar , int * len ) { if ( node == NULL ) return ; if ( node -> left == NULL && node -> right == NULL ) { if ( bends > * maxBends ) { * maxBends = bends ; * len = soFar ; } } else { if ( dir == ' l ' ) { findMaxBendsUtil ( node -> left , dir , bends , maxBends , soFar + 1 , len ) ; findMaxBendsUtil ( node -> right , ' r ' , bends + 1 , maxBends , soFar + 1 , len ) ; } else { findMaxBendsUtil ( node -> right , dir , bends , maxBends , soFar + 1 , len ) ; findMaxBendsUtil ( node -> left , ' l ' , bends + 1 , maxBends , soFar + 1 , len ) ; } } } int findMaxBends ( struct Node * node ) { if ( node == NULL ) return 0 ; int len = 0 , bends = 0 , maxBends = -1 ; if ( node -> left ) findMaxBendsUtil ( node -> left , ' l ' , bends , & maxBends , 1 , & len ) ; if ( node -> right ) findMaxBendsUtil ( node -> right , ' r ' , bends , & maxBends , 1 , & len ) ; len ++ ; return len ; } int main ( ) { struct Node * root = newNode ( 10 ) ; root -> left = newNode ( 8 ) ; root -> right = newNode ( 2 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> left -> right = newNode ( 1 ) ; root -> right -> left -> right -> left = newNode ( 9 ) ; cout << findMaxBends ( root ) - 1 ; return 0 ; }
Program to calculate angle between two N | C ++ program for the above approach ; Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Driver Code ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
#include <bits/stdc++.h> NEW_LINE using namespace std ; double magnitude ( double arr [ ] , int N ) { double magnitude = 0 ; for ( int i = 0 ; i < N ; i ++ ) magnitude += arr [ i ] * arr [ i ] ; return sqrt ( magnitude ) ; } double dotProduct ( double arr [ ] , double brr [ ] , int N ) { double product = 0 ; for ( int i = 0 ; i < N ; i ++ ) product = product + arr [ i ] * brr [ i ] ; return product ; } void angleBetweenVectors ( double arr [ ] , double brr [ ] , int N ) { double dotProductOfVectors = dotProduct ( arr , brr , N ) ; double magnitudeOfA = magnitude ( arr , N ) ; double magnitudeOfB = magnitude ( brr , N ) ; double angle = dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ; cout << angle ; } int main ( ) { double arr [ ] = { -0.5 , -2 , 1 } ; double brr [ ] = { -1 , -1 , 0.3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; angleBetweenVectors ( arr , brr , N ) ; return 0 ; }
Sum of sides of largest and smallest child polygons possible from a given polygon |  ; Function to find the sum of largest and smallest secondary polygons if possible ; Count edges of primary polygon ; Calculate edges present in the largest secondary polygon ; Driver Code ; Given Exterior Angle
#include <bits/stdc++.h> NEW_LINE using namespace std ; void secondary_polygon ( int Angle ) { int edges_primary = 360 / Angle ; if ( edges_primary >= 6 ) { int edges_max_secondary = edges_primary / 2 ; cout << edges_max_secondary + 3 ; } else cout << " Not ▁ Possible " ; } int main ( ) { int Angle = 45 ; secondary_polygon ( Angle ) ; return 0 ; }
Area of Circumcircle of an Equilateral Triangle using Median | C ++ implementation to find the equation of circle which inscribes equilateral triangle of median M ; Function to find the equation of circle whose center is ( x1 , y1 ) and the radius of circle is r ; Function to find the equation of circle which inscribes equilateral triangle of median M ; Util Function to find the circle equation ; Driver code ; Function Call
#include <iostream> NEW_LINE const double pi = 3.14159265358979323846 ; using namespace std ; void circleArea ( double r ) { cout << ( pi * r * r ) ; } void findCircleAreaByMedian ( double m ) { double r = 2 * m / 3 ; circleArea ( r ) ; } int main ( ) { double m = 3 ; findCircleAreaByMedian ( m ) ; return 0 ; }
Find the type of triangle from the given sides | C ++ implementation to find the type of triangle with the help of the sides ; Function to find the type of triangle with the help of sides ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkTypeOfTriangle ( int a , int b , int c ) { int sqa = pow ( a , 2 ) ; int sqb = pow ( b , 2 ) ; int sqc = pow ( c , 2 ) ; if ( sqa == sqb + sqc sqb == sqc + sqa sqc == sqa + sqb ) { cout << " Right - angled ▁ Triangle " ; } else if ( sqa > sqc + sqb sqb > sqa + sqc sqc > sqa + sqb ) { cout << " Obtuse - angled ▁ Triangle " ; } else { cout << " Acute - angled ▁ Triangle " ; } } int main ( ) { int a , b , c ; a = 2 ; b = 2 ; c = 2 ; checkTypeOfTriangle ( a , b , c ) ; return 0 ; }
Check whether the triangle is valid or not if angles are given | C ++ implementation of the approach ; Function to check if sum of the three angles is 180 or not ; Check condition ; Driver code ; function call ing and print output
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Valid ( int a , int b , int c ) { if ( a + b + c == 180 && a != 0 && b != 0 && c != 0 ) return true ; else return false ; } int main ( ) { int a = 60 , b = 40 , c = 80 ; if ( Valid ( a , b , c ) ) cout << " Valid " ; else cout << " Invalid " ; }
Area of the Largest Triangle inscribed in a Hexagon | C ++ Program to find the biggest triangle which can be inscribed within the hexagon ; Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float trianglearea ( float a ) { if ( a < 0 ) return -1 ; float area = ( 3 * sqrt ( 3 ) * pow ( a , 2 ) ) / 4 ; return area ; } int main ( ) { float a = 6 ; cout << trianglearea ( a ) << endl ; return 0 ; }
Equation of ellipse from its focus , directrix , and eccentricity | C ++ program to find equation of an ellipse using focus and directrix . ; Function to find equation of ellipse . ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iomanip> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void equation_ellipse ( float x1 , float y1 , float a , float b , float c , float e ) { float t = a * a + b * b ; float a1 = t - e * ( a * a ) ; float b1 = t - e * ( b * b ) ; float c1 = ( -2 * t * x1 ) - ( 2 * e * c * a ) ; float d1 = ( -2 * t * y1 ) - ( 2 * e * c * b ) ; float e1 = -2 * e * a * b ; float f1 = ( - e * c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) ; cout << fixed ; cout << setprecision ( 2 ) ; cout << " Equation ▁ of ▁ ellipse ▁ is ▁ STRNEWLINE " << a1 << " ▁ x ^ 2 ▁ + ▁ " << b1 << " ▁ y ^ 2 ▁ + ▁ " << c1 << " ▁ x ▁ + ▁ " << d1 << " ▁ y ▁ + ▁ " << e1 << " ▁ xy ▁ + ▁ " << f1 << " ▁ = ▁ 0" ; } int main ( ) { float x1 = 1 , y1 = 1 , a = 1 , b = -1 , c = 3 , e = 0.5 * 0.5 ; equation_ellipse ( x1 , y1 , a , b , c , e ) ; return 0 ; }
Create loops of even and odd values in a binary tree | C ++ implementation to create odd and even loops in a binary tree ; structure of a node ; Utility function to create a new node ; preorder traversal to place the node pointer in the respective even_ptrs or odd_ptrs list ; place node ptr in even_ptrs list if node contains even number ; else place node ptr in odd_ptrs list ; function to create the even and odd loops ; forming even loop ; for the last element ; Similarly forming odd loop ; traversing the loop from any random node in the loop ; Driver program to test above ; Binary tree formation ; traversing odd loop from any random odd node ; traversing even loop from any random even node
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right , * abtr ; } ; struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = node -> abtr = NULL ; return node ; } void preorderTraversal ( Node * root , vector < Node * > * even_ptrs , vector < Node * > * odd_ptrs ) { if ( ! root ) return ; if ( root -> data % 2 == 0 ) ( * even_ptrs ) . push_back ( root ) ; else ( * odd_ptrs ) . push_back ( root ) ; preorderTraversal ( root -> left , even_ptrs , odd_ptrs ) ; preorderTraversal ( root -> right , even_ptrs , odd_ptrs ) ; } void createLoops ( Node * root ) { vector < Node * > even_ptrs , odd_ptrs ; preorderTraversal ( root , & even_ptrs , & odd_ptrs ) ; int i ; for ( i = 1 ; i < even_ptrs . size ( ) ; i ++ ) even_ptrs [ i - 1 ] -> abtr = even_ptrs [ i ] ; even_ptrs [ i - 1 ] -> abtr = even_ptrs [ 0 ] ; for ( i = 1 ; i < odd_ptrs . size ( ) ; i ++ ) odd_ptrs [ i - 1 ] -> abtr = odd_ptrs [ i ] ; odd_ptrs [ i - 1 ] -> abtr = odd_ptrs [ 0 ] ; } void traverseLoop ( Node * start ) { Node * curr = start ; do { cout << curr -> data << " ▁ " ; curr = curr -> abtr ; } while ( curr != start ) ; } int main ( ) { struct Node * root = NULL ; root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; createLoops ( root ) ; cout << " Odd ▁ nodes : ▁ " ; traverseLoop ( root -> right ) ; cout << endl << " Even ▁ nodes : ▁ " ; traverseLoop ( root -> left ) ; return 0 ; }
Area of circle which is inscribed in equilateral triangle | C ++ program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code
# include <bits/stdc++.h> NEW_LINE # define PI  3.14 NEW_LINE using namespace std ; float circle_inscribed ( int a ) { return PI * ( a * a ) / 12 ; } int main ( ) { int a = 4 ; cout << circle_inscribed ( a ) ; return 0 ; }
Program to find the Volume of an irregular tetrahedron | C ++ implementation of above approach ; Function to find the volume ; Steps to calculate volume of a Tetrahedron using formula ; Driver code ; edge lengths
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define db  double NEW_LINE void findVolume ( db u , db v , db w , db U , db V , db W , db b ) { db uPow = pow ( u , 2 ) ; db vPow = pow ( v , 2 ) ; db wPow = pow ( w , 2 ) ; db UPow = pow ( U , 2 ) ; db VPow = pow ( V , 2 ) ; db WPow = pow ( W , 2 ) ; db a = 4 * ( uPow * vPow * wPow ) - uPow * pow ( ( vPow + wPow - UPow ) , 2 ) - vPow * pow ( ( wPow + uPow - VPow ) , 2 ) - wPow * pow ( ( uPow + vPow - WPow ) , 2 ) + ( vPow + wPow - UPow ) * ( wPow + uPow - VPow ) * ( uPow + vPow - WPow ) ; db vol = sqrt ( a ) ; vol /= b ; cout << fixed << setprecision ( 4 ) << vol ; } int main ( ) { db u = 1000 , v = 1000 , w = 1000 ; db U = 3 , V = 4 , W = 5 ; db b = 12 ; findVolume ( u , v , w , U , V , W , b ) ; return 0 ; }
Check if it is possible to create a polygon with a given angle | C ++ implementation of above approach ; Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makePolygon ( float a ) { float n = 360 / ( 180 - a ) ; if ( n == ( int ) n ) cout << " YES " ; else cout << " NO " ; } int main ( ) { float a = 90 ; makePolygon ( a ) ; return 0 ; }
Finding Quadrant of a Coordinate with respect to a Circle | CPP Program to find the quadrant of a given coordinate with respect to the centre of a circle ; Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Driver Code ; Coordinates of centre ; Radius of circle ; Coordinates of the given point
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getQuadrant ( int X , int Y , int R , int PX , int PY ) { if ( PX == X && PY == Y ) return 0 ; int val = pow ( ( PX - X ) , 2 ) + pow ( ( PY - Y ) , 2 ) ; if ( val > pow ( R , 2 ) ) return -1 ; if ( PX > X && PY >= Y ) return 1 ; if ( PX <= X && PY > Y ) return 2 ; if ( PX < X && PY <= Y ) return 3 ; if ( PX >= X && PY < Y ) return 4 ; } int main ( ) { int X = 0 , Y = 3 ; int R = 2 ; int PX = 1 , PY = 4 ; int ans = getQuadrant ( X , Y , R , PX , PY ) ; if ( ans == -1 ) cout << " Lies ▁ Outside ▁ the ▁ circle " << endl ; else if ( ans == 0 ) cout << " Coincides ▁ with ▁ centre " << endl ; else cout << ans << " ▁ Quadrant " << endl ; return 0 ; }
Hexadecagonal number | C ++ program to find Nth hexadecagon number ; Function to calculate hexadecagonal number ; Drivers Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hexadecagonalNum ( long int n ) { return ( ( 14 * n * n ) - 12 * n ) / 2 ; } int main ( ) { long int n = 5 ; cout << n << " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; cout << hexadecagonalNum ( n ) ; cout << endl ; n = 9 ; cout << n << " th ▁ Hexadecagonal ▁ number ▁ : ▁ " ; cout << hexadecagonalNum ( n ) ; return 0 ; }
Find first non matching leaves in two binary trees | C ++ program to find first leaves that are not same . ; Tree node ; Utility method to create a new node ; Prints the first non - matching leaf node in two trees if it exists , else prints nothing . ; If any of the tree is empty ; Create two stacks for preorder traversals ; If traversal of one tree is over and other tree still has nodes . ; Do iterative traversal of first tree and find first lead node in it as " temp1" ; pushing right childfirst so that left child comes first while popping . ; Do iterative traversal of second tree and find first lead node in it as " temp2" ; If we found leaves in both trees ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int x ) { Node * temp = new Node ; temp -> data = x ; temp -> left = temp -> right = NULL ; return temp ; } bool isLeaf ( Node * t ) { return ( ( t -> left == NULL ) && ( t -> right == NULL ) ) ; } void findFirstUnmatch ( Node * root1 , Node * root2 ) { if ( root1 == NULL root2 == NULL ) return ; stack < Node * > s1 , s2 ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { if ( s1 . empty ( ) || s2 . empty ( ) ) return ; Node * temp1 = s1 . top ( ) ; s1 . pop ( ) ; while ( temp1 && ! isLeaf ( temp1 ) ) { s1 . push ( temp1 -> right ) ; s1 . push ( temp1 -> left ) ; temp1 = s1 . top ( ) ; s1 . pop ( ) ; } Node * temp2 = s2 . top ( ) ; s2 . pop ( ) ; while ( temp2 && ! isLeaf ( temp2 ) ) { s2 . push ( temp2 -> right ) ; s2 . push ( temp2 -> left ) ; temp2 = s2 . top ( ) ; s2 . pop ( ) ; } if ( temp1 != NULL && temp2 != NULL ) { if ( temp1 -> data != temp2 -> data ) { cout << " First ▁ non ▁ matching ▁ leaves ▁ : ▁ " << temp1 -> data << " ▁ " << temp2 -> data << endl ; return ; } } } } int main ( ) { struct Node * root1 = newNode ( 5 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 7 ) ; root1 -> left -> left = newNode ( 10 ) ; root1 -> left -> right = newNode ( 11 ) ; struct Node * root2 = newNode ( 6 ) ; root2 -> left = newNode ( 10 ) ; root2 -> right = newNode ( 15 ) ; findFirstUnmatch ( root1 , root2 ) ; return 0 ; }
Maximum points of intersection n circles | CPP program to find maximum umber of intersections of n circles ; Returns maximum number of intersections ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int intersection ( int n ) { return n * ( n - 1 ) ; } int main ( ) { cout << intersection ( 3 ) << endl ; return 0 ; }
Find the perimeter of a cylinder | CPP program to find perimeter of cylinder ; Function to calculate perimeter ; Driver function
#include <iostream> NEW_LINE using namespace std ; int perimeter ( int diameter , int height ) { return 2 * ( diameter + height ) ; } int main ( ) { int diameter = 5 ; int height = 10 ; cout << " Perimeter ▁ = ▁ " ; cout << perimeter ( diameter , height ) ; cout << " ▁ units STRNEWLINE " ; return 0 ; }
Find all possible coordinates of parallelogram | C ++ program to all possible points of a parallelogram ; main method ; coordinates of A ; coordinates of B ; coordinates of C
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; cout << ax + bx - cx << " , ▁ " << ay + by - cy << endl ; cout << ax + cx - bx << " , ▁ " << ay + cy - by << endl ; cout << cx + bx - ax << " , ▁ " << cy + by - ax << endl ; return 0 ; }
Check whether a given point lies inside a rectangle or not |  ; A utility function to calculate area of triangle formed by ( x1 , y1 ) , ( x2 , y2 ) and ( x3 , y3 ) ; A function to check whether point P ( x , y ) lies inside the rectangle formed by A ( x1 , y1 ) , B ( x2 , y2 ) , C ( x3 , y3 ) and D ( x4 , y4 ) ; Calculate area of rectangle ABCD ; Calculate area of triangle PAB ; Calculate area of triangle PBC ; Calculate area of triangle PCD ; Calculate area of triangle PAD ; Check if sum of A1 , A2 , A3 and A4 is same as A ; Driver program to test above function ; Let us check whether the point P ( 10 , 15 ) lies inside the rectangle formed by A ( 0 , 10 ) , B ( 10 , 0 ) C ( 0 , - 10 ) D ( - 10 , 0 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; float area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } bool check ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 , int x , int y ) { float A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ; float A1 = area ( x , y , x1 , y1 , x2 , y2 ) ; float A2 = area ( x , y , x2 , y2 , x3 , y3 ) ; float A3 = area ( x , y , x3 , y3 , x4 , y4 ) ; float A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; return ( A == A1 + A2 + A3 + A4 ) ; } int main ( ) { if ( check ( 0 , 10 , 10 , 0 , 0 , -10 , -10 , 0 , 10 , 15 ) ) cout << " yes " ; else cout << " no " ; return 0 ; }
Get maximum left node in binary tree | CPP program to print maximum element in left node . ; A Binary Tree Node ; Get max of left element using Inorder traversal ; Return maximum of three values 1 ) Recursive max in left subtree 2 ) Value in left node 3 ) Recursive max in right subtree ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; int maxOfLeftElement ( Node * root ) { int res = INT_MIN ; if ( root == NULL ) return res ; if ( root -> left != NULL ) res = root -> left -> data ; return max ( { maxOfLeftElement ( root -> left ) , res , maxOfLeftElement ( root -> right ) } ) ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 7 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> right = newNode ( 1 ) ; cout << maxOfLeftElement ( root ) ; return 0 ; }
Pizza cut problem ( Or Circle Division by Lines ) | C ++ program to find maximum no of pieces by given number of cuts ; Function for finding maximum pieces with n cuts . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; } int main ( ) { cout << findMaximumPieces ( 3 ) ; return 0 ; }
Optimum location of point to minimize total distance | C / C ++ program to find optimum location and total cost ; structure defining a point ; structure defining a line of ax + by + c = 0 form ; method to get distance of point ( x , y ) from point p ; Utility method to compute total distance all points when choose point on given line has x - coordinate value as X ; calculating Y of chosen point by line equation ; Utility method to find minimum total distance ; loop until difference between low and high become less than EPS ; mid1 and mid2 are representative x co - ordiantes of search space ; if mid2 point gives more total distance , skip third part ; if mid1 point gives more total distance , skip first part ; compute optimum distance cost by sending average of low and high as X ; method to find optimum cost ; converting 2D array input to point array ; Driver code to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define sq ( x )  ((x) * (x)) NEW_LINE #define EPS  1e-6 NEW_LINE #define N  5 NEW_LINE struct point { int x , y ; point ( ) { } point ( int x , int y ) : x ( x ) , y ( y ) { } } ; struct line { int a , b , c ; line ( int a , int b , int c ) : a ( a ) , b ( b ) , c ( c ) { } } ; double dist ( double x , double y , point p ) { return sqrt ( sq ( x - p . x ) + sq ( y - p . y ) ) ; } double compute ( point p [ ] , int n , line l , double X ) { double res = 0 ; double Y = -1 * ( l . c + l . a * X ) / l . b ; for ( int i = 0 ; i < n ; i ++ ) res += dist ( X , Y , p [ i ] ) ; return res ; } double findOptimumCostUtil ( point p [ ] , int n , line l ) { double low = -1e6 ; double high = 1e6 ; while ( ( high - low ) > EPS ) { double mid1 = low + ( high - low ) / 3 ; double mid2 = high - ( high - low ) / 3 ; double dist1 = compute ( p , n , l , mid1 ) ; double dist2 = compute ( p , n , l , mid2 ) ; if ( dist1 < dist2 ) high = mid2 ; else low = mid1 ; } return compute ( p , n , l , ( low + high ) / 2 ) ; } double findOptimumCost ( int points [ N ] [ 2 ] , line l ) { point p [ N ] ; for ( int i = 0 ; i < N ; i ++ ) p [ i ] = point ( points [ i ] [ 0 ] , points [ i ] [ 1 ] ) ; return findOptimumCostUtil ( p , N , l ) ; } int main ( ) { line l ( 1 , -1 , -3 ) ; int points [ N ] [ 2 ] = { { -3 , -2 } , { -1 , 0 } , { -1 , 2 } , { 1 , 2 } , { 3 , 4 } } ; cout << findOptimumCost ( points , l ) << endl ; return 0 ; }