codeparrot/xlcost-text-to-code huggingface dataset:
Find the other end point of a line with given one end and mid | CPP program to find the end point of a line ; CPP function to find the end point of a line ; find end point for x coordinates ; find end point for y coordinates ; Driven Program
#include <iostream> NEW_LINE using namespace std ; void otherEndPoint ( int x1 , int y1 , int m1 , int m2 ) { float x2 = ( float ) ( 2 * m1 - x1 ) ; float y2 = ( float ) ( 2 * m2 - y1 ) ; cout << " x2 ▁ = ▁ " << x2 << " , ▁ " << " y2 ▁ = ▁ " << y2 ; } int main ( ) { int x1 = -4 , y1 = -1 , m1 = 3 , m2 = 5 ; otherEndPoint ( x1 , y1 , m1 , m2 ) ; return 0 ; }
Coordinates of rectangle with given points lie inside | Program to find smallest rectangle to conquer all points ; function to print coordinate of smallest rectangle ; find Xmax and Xmin ; find Ymax and Ymin ; print all four coordinates ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRect ( int X [ ] , int Y [ ] , int n ) { int Xmax = * max_element ( X , X + n ) ; int Xmin = * min_element ( X , X + n ) ; int Ymax = * max_element ( Y , Y + n ) ; int Ymin = * min_element ( Y , Y + n ) ; cout << " { " << Xmin << " , ▁ " << Ymin << " } " << endl ; cout << " { " << Xmin << " , ▁ " << Ymax << " } " << endl ; cout << " { " << Xmax << " , ▁ " << Ymax << " } " << endl ; cout << " { " << Xmax << " , ▁ " << Ymin << " } " << endl ; } int main ( ) { int X [ ] = { 4 , 3 , 6 , 1 , -1 , 12 } ; int Y [ ] = { 4 , 1 , 10 , 3 , 7 , -1 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; printRect ( X , Y , n ) ; return 0 ; }
Check if a line passes through the origin | C ++ program to find if line passing through two coordinates also passes through origin or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkOrigin ( int x1 , int y1 , int x2 , int y2 ) { return ( x1 * ( y2 - y1 ) == y1 * ( x2 - x1 ) ) ; } int main ( ) { if ( checkOrigin ( 1 , 28 , 2 , 56 ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of horizontal or vertical line segments to connect 3 points | CPP program to find number of horizontal ( or vertical line segments needed to connect three points . ; Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isBetween ( int a , int b , int c ) { return min ( a , b ) <= c && c <= max ( a , b ) ; } bool canJoin ( int x [ ] , int y [ ] , int i , int j , int k ) { return ( x [ k ] == x [ i ] x [ k ] == x [ j ] ) && isBetween ( y [ i ] , y [ j ] , y [ k ] ) || ( y [ k ] == y [ i ] y [ k ] == y [ j ] ) && isBetween ( x [ i ] , x [ j ] , x [ k ] ) ; } int countLineSegments ( int x [ ] , int y [ ] ) { if ( ( x [ 0 ] == x [ 1 ] && x [ 1 ] == x [ 2 ] ) || ( y [ 0 ] == y [ 1 ] && y [ 1 ] == y [ 2 ] ) ) return 1 ; else if ( canJoin ( x , y , 0 , 1 , 2 ) || canJoin ( x , y , 0 , 2 , 1 ) || canJoin ( x , y , 1 , 2 , 0 ) ) return 2 ; else return 3 ; } int main ( ) { int x [ 3 ] , y [ 3 ] ; x [ 0 ] = -1 ; y [ 0 ] = -1 ; x [ 1 ] = -1 ; y [ 1 ] = 3 ; x [ 2 ] = 4 ; y [ 2 ] = 3 ; cout << countLineSegments ( x , y ) ; return 0 ; }
Pythagorean Quadruple | C ++ code to detect Pythagorean Quadruples . ; function for checking ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool pythagorean_quadruple ( int a , int b , int c , int d ) { int sum = a * a + b * b + c * c ; if ( d * d == sum ) return true ; else return false ; } int main ( ) { int a = 1 , b = 2 , c = 2 , d = 3 ; if ( pythagorean_quadruple ( a , b , c , d ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }
Program for Volume and Surface area of Frustum of Cone | CPP program to calculate Volume and Surface area of frustum of cone ; Function to calculate Volume of frustum of cone ; Function to calculate Curved Surface area of frustum of cone ; Function to calculate Total Surface area of frustum of cone ; Driver function ; Printing value of volume and surface area
#include <iostream> NEW_LINE using namespace std ; float pi = 3.14159 ; float volume ( float r , float R , float h ) { return ( float ( 1 ) / float ( 3 ) ) * pi * h * ( r * r + R * R + r * R ) ; } float curved_surface_area ( float r , float R , float l ) { return pi * l * ( R + r ) ; } float total_surface_area ( float r , float R , float l , float h ) { return pi * l * ( R + r ) + pi * ( r * r + R * R ) ; } int main ( ) { float small_radius = 3 ; float big_radius = 8 ; float slant_height = 13 ; float height = 12 ; cout << " Volume ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " << volume ( small_radius , big_radius , height ) << endl ; cout << " Curved ▁ Surface ▁ Area ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " << curved_surface_area ( small_radius , big_radius , slant_height ) << endl ; cout << " Total ▁ Surface ▁ Area ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " << total_surface_area ( small_radius , big_radius , slant_height , height ) ; return 0 ; }
Program to find Perimeter / Circumference of Square and Rectangle | CPP program to find Circumference of a square ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Circumference ( int a ) { return 4 * a ; } int main ( ) { int a = 5 ; cout << " Circumference ▁ of " << " ▁ a ▁ square ▁ is ▁ " << Circumference ( a ) ; return 0 ; }
Maximum area of quadrilateral | CPP program to find maximum are of a quadrilateral ; Calculating the semi - perimeter of the given quadrilateral ; Applying Brahmagupta 's formula to  get maximum area of quadrilateral ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; double maxArea ( double a , double b , double c , double d ) { double semiperimeter = ( a + b + c + d ) / 2 ; return sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ; } int main ( ) { double a = 1 , b = 2 , c = 1 , d = 2 ; cout << maxArea ( a , b , c , d ) ; return 0 ; }
Direction of a Point from a Line Segment | C ++ Program to Determine Direction of Point from line segment ; structure for point in cartesian plane . ; constant integers for directions ; subtracting co - ordinates of point A from B and P , to make A as origin ; Determining cross Product ; return RIGHT if cross product is positive ; return LEFT if cross product is negative ; return ZERO if cross product is zero . ; Driver code ; A . y = 10 ; A ( - 30 , 10 ) ; B . y = - 15 ; B ( 29 , - 15 ) ; P . y = 28 ; P ( 15 , 28 )
#include <iostream> NEW_LINE using namespace std ; struct point { int x , y ; } ; const int RIGHT = 1 , LEFT = -1 , ZERO = 0 ; int directionOfPoint ( point A , point B , point P ) { B . x -= A . x ; B . y -= A . y ; P . x -= A . x ; P . y -= A . y ; int cross_product = B . x * P . y - B . y * P . x ; if ( cross_product > 0 ) return RIGHT ; if ( cross_product < 0 ) return LEFT ; return ZERO ; } int main ( ) { point A , B , P ; A . x = -30 ; B . x = 29 ; P . x = 15 ; int direction = directionOfPoint ( A , B , P ) ; if ( direction == 1 ) cout << " Right ▁ Direction " << endl ; else if ( direction == -1 ) cout << " Left ▁ Direction " << endl ; else cout << " Point ▁ is ▁ on ▁ the ▁ Line " << endl ; return 0 ; }
Find minimum radius such that atleast k point lie inside the circle | C ++ program to find minimum radius such that atleast k point lie inside the circle ; Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRadius ( int k , int x [ ] , int y [ ] , int n ) { int dis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; sort ( dis , dis + n ) ; return dis [ k - 1 ] ; } int main ( ) { int k = 3 ; int x [ ] = { 1 , -1 , 1 } ; int y [ ] = { 1 , -1 , -1 } ; int n = sizeof ( x ) / sizeof ( x [ 0 ] ) ; cout << minRadius ( k , x , y , n ) << endl ; return 0 ; }
Program for Area And Perimeter Of Rectangle | CPP program to find area and perimeter of rectangle ; Utility function ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int areaRectangle ( int a , int b ) { int area = a * b ; return area ; } int perimeterRectangle ( int a , int b ) { int perimeter = 2 * ( a + b ) ; return perimeter ; } int main ( ) { int a = 5 ; int b = 6 ; cout << " Area ▁ = ▁ " << areaRectangle ( a , b ) << endl ; cout << " Perimeter ▁ = ▁ " << perimeterRectangle ( a , b ) ; return 0 ; }
Program for Area Of Square | CPP program to find the area of the square ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int areaSquare ( int side ) { int area = side * side ; return area ; } int main ( ) { int side = 4 ; cout << areaSquare ( side ) ; return 0 ; }
Minimum Perimeter of n blocks | CPP program to find minimum perimeter using n blocks . ; if n is a perfect square ; Number of rows ; perimeter of the rectangular grid ; if there are blocks left ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPerimeter ( int n ) { int l = sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long long int row = n / l ; long long int perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } int main ( ) { int n = 10 ; cout << minPerimeter ( n ) ; return 0 ; }
Find if it 's possible to rotate the page by an angle or not. | C ++ program to find if its possible to rotate page or not ; function to find if it 's possible to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Driver Code ; Points a , b , and c
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) { long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ; long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) cout << " No " ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b2 == ( ( a2 + c2 ) / 2.0 ) ) cout << " No " ; else cout << " Yes " ; } int main ( ) { ios_base :: sync_with_stdio ( false ) ; cin . tie ( NULL ) ; long long a1 = 1 , a2 = 0 , b1 = 2 , b2 = 0 , c1 = 3 , c2 = 0 ; possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) ; return 0 ; }
Check if two given circles touch or intersect each other | C ++ program to check if two circles touch each other or not . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return -1 ; else return 0 ; } int main ( ) { int x1 = -10 , y1 = 8 ; int x2 = 14 , y2 = -24 ; int r1 = 30 , r2 = 10 ; int t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) cout << " Circle ▁ touch ▁ to " << " ▁ each ▁ other . " ; else if ( t < 0 ) cout << " Circle ▁ not ▁ touch " << " ▁ to ▁ each ▁ other . " ; else cout << " Circle ▁ intersect " << " ▁ to ▁ each ▁ other . " ; return 0 ; }
Count of obtuse angles in a circle with ' k ' equidistant points between 2 given points | C ++ program to count number of obtuse angles for given two points . ; There are two arcs connecting a and b . Let us count points on both arcs . ; Both arcs have same number of points ; Points on smaller arc is answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countObtuseAngles ( int a , int b , int k ) { int c1 = ( b - a ) - 1 ; int c2 = ( k - b ) + ( a - 1 ) ; if ( c1 == c2 ) return 0 ; return min ( c1 , c2 ) ; } int main ( ) { int k = 6 , a = 1 , b = 3 ; cout << countObtuseAngles ( a , b , k ) ; return 0 ; }
Find points at a given distance on a line of given slope | C ++ program to find the points on a line of slope M at distance L ; structure to represent a co - ordinate point ; Function to print pair of points at distance ' l ' and having a slope ' m ' from the source ; m is the slope of line , and the required Point lies distance l away from the source Point ; slope is 0 ; if slope is infinite ; print the first Point ; print the second Point ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Point { float x , y ; Point ( ) { x = y = 0 ; } Point ( float a , float b ) { x = a , y = b ; } } ; void printPoints ( Point source , float l , int m ) { Point a , b ; if ( m == 0 ) { a . x = source . x + l ; a . y = source . y ; b . x = source . x - l ; b . y = source . y ; } else if ( m == std :: numeric_limits < float > :: max ( ) ) { a . x = source . x ; a . y = source . y + l ; b . x = source . x ; b . y = source . y - l ; } else { float dx = ( l / sqrt ( 1 + ( m * m ) ) ) ; float dy = m * dx ; a . x = source . x + dx ; a . y = source . y + dy ; b . x = source . x - dx ; b . y = source . y - dy ; } cout << a . x << " , ▁ " << a . y << endl ; cout << b . x << " , ▁ " << b . y << endl ; } int main ( ) { Point p ( 2 , 1 ) , q ( 1 , 0 ) ; printPoints ( p , sqrt ( 2 ) , 1 ) ; cout << endl ; printPoints ( q , 5 , 0 ) ; return 0 ; }
Minimum block jumps to reach destination | C ++ program to find minimum jumps to reach a given destination from a given source ; To represent point in 2D space ; To represent line of ( ax + by + c ) format ; Returns 1 if evaluation is greater > 0 , else returns - 1 ; Returns minimum jumps to reach dest point from start point ; get sign of evaluation from point co - ordinate and line equation ; if both evaluation are of opposite sign , increase jump by 1 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct point { int x , y ; 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 ) { } line ( ) { } } ; int evalPointOnLine ( point p , line curLine ) { int eval = curLine . a * p . x + curLine . b * p . y + curLine . c ; if ( eval > 0 ) return 1 ; return -1 ; } int minJumpToReachDestination ( point start , point dest , line lines [ ] , int N ) { int jumps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int signStart = evalPointOnLine ( start , lines [ i ] ) ; int signDest = evalPointOnLine ( dest , lines [ i ] ) ; if ( signStart * signDest < 0 ) jumps ++ ; } return jumps ; } int main ( ) { point start ( 1 , 1 ) ; point dest ( -2 , -1 ) ; line lines [ 3 ] ; lines [ 0 ] = line ( 1 , 0 , 0 ) ; lines [ 1 ] = line ( 0 , 1 , 0 ) ; lines [ 2 ] = line ( 1 , 1 , -2 ) ; cout << minJumpToReachDestination ( start , dest , lines , 3 ) ; return 0 ; }
Minimum distance to travel to cover all intervals | C ++ program to find minimum distance to travel to cover all intervals ; structure to store an interval ; Method returns minimum distance to travel to cover all intervals ; looping over all intervals to get right most start and left most end ; if rightmost start > leftmost end then all intervals are not aligned and it is not possible to cover all of them ; if x is in between rightmoststart and leftmostend then no need to travel any distance ; choose minimum according to current position x ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Interval { int start , end ; Interval ( int start , int end ) : start ( start ) , end ( end ) { } } ; int minDistanceToCoverIntervals ( Interval intervals [ ] , int N , int x ) { int rightMostStart = INT_MIN ; int leftMostEnd = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( rightMostStart < intervals [ i ] . start ) rightMostStart = intervals [ i ] . start ; if ( leftMostEnd > intervals [ i ] . end ) leftMostEnd = intervals [ i ] . end ; } int res ; if ( rightMostStart > leftMostEnd ) res = -1 ; else if ( rightMostStart <= x && x <= leftMostEnd ) res = 0 ; else res = ( x < rightMostStart ) ? ( rightMostStart - x ) : ( x - leftMostEnd ) ; return res ; } int main ( ) { int x = 3 ; Interval intervals [ ] = { { 0 , 7 } , { 2 , 14 } , { 4 , 6 } } ; int N = sizeof ( intervals ) / sizeof ( intervals [ 0 ] ) ; int res = minDistanceToCoverIntervals ( intervals , N , x ) ; if ( res == -1 ) cout << " Not ▁ Possible ▁ to ▁ cover ▁ all ▁ intervals STRNEWLINE " ; else cout << res << endl ; }
Count of acute , obtuse and right triangles with given sides | C ++ program to count of acute , obtuse and right triangles in an array ; Find the number of acute , right , obtuse triangle that can be formed from given array . ; Finding the square of each element of array . ; Sort the sides of array and their squares . ; x for acute triangles y for right triangles z for obtuse triangles ; Finding the farthest point p where a ^ 2 + b ^ 2 >= c ^ 2. ; Finding the farthest point q where a + b > c . ; If point p make right triangle . ; All triangle between j and p are acute triangles . So add p - j - 1 in x . ; Increment y by 1. ; All triangle between q and p are acute triangles . So add q - p in z . ; If no right triangle ; All triangle between j and p are acute triangles . So add p - j in x . ; All triangle between q and p are acute triangles . So add q - p in z . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTriangle ( int a [ ] , int n ) { int b [ n + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = a [ i ] * a [ i ] ; sort ( a , a + n ) ; sort ( b , b + n ) ; int x = 0 , y = 0 , z = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int p = i + 1 ; int q = i + 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { while ( p < n - 1 && b [ i ] + b [ j ] >= b [ p + 1 ] ) p ++ ; q = max ( q , p ) ; while ( q < n - 1 && a [ i ] + a [ j ] > a [ q + 1 ] ) q ++ ; if ( b [ i ] + b [ j ] == b [ p ] ) { x += max ( p - j - 1 , 0 ) ; y ++ ; z += q - p ; } else { x += max ( p - j , 0 ) ; z += q - p ; } } } cout << " Acute ▁ Triangle : ▁ " << x << endl ; cout << " Right ▁ Triangle : ▁ " << y << endl ; cout << " Obtuse ▁ Triangle : ▁ " << z << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 9 , 10 , 12 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findTriangle ( arr , n ) ; return 0 ; }
Check whether a point exists in circle sector or not . | C ++ program to check if a point lies inside a circle sector . ; calculate endAngle ; Calculate polar co - ordinates ; Check whether polarradius is less then radius of circle or not and Angle is between startAngle and endAngle or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPoint ( int radius , int x , int y , float percent , float startAngle ) { float endAngle = 360 / percent + startAngle ; float polarradius = sqrt ( x * x + y * y ) ; float Angle = atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( " Point ▁ ( % d , ▁ % d ) ▁ exist ▁ in ▁ the ▁ circle ▁ sector STRNEWLINE " , x , y ) ; else printf ( " Point ▁ ( % d , ▁ % d ) ▁ does ▁ not ▁ exist ▁ in ▁ the ▁ circle ▁ sector STRNEWLINE " , x , y ) ; } int main ( ) { int radius = 8 , x = 3 , y = 4 ; float percent = 12 , startAngle = 0 ; checkPoint ( radius , x , y , percent , startAngle ) ; return 0 ; }
Program to find the Type of Triangle from the given Coordinates | C / C ++ program to classify a given triangle ; Utility method to return square of x ; Utility method to sort a , b , c ; after this method a <= b <= c ; Utility method to return Square of distance between two points ; Method to classify side ; if all sides are equal ; if any two sides are equal ; Method to classify angle ; If addition of sum of square of two side is less , then acute ; by pythagoras theorem ; Method to classify the triangle by sides and angles ; Find squares of distances between points ; Sort all squares of distances in increasing order ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct point { int x , y ; point ( ) { } point ( int x , int y ) : x ( x ) , y ( y ) { } } ; int square ( int x ) { return x * x ; } void order ( int & a , int & b , int & c ) { int copy [ 3 ] ; copy [ 0 ] = a ; copy [ 1 ] = b ; copy [ 2 ] = c ; sort ( copy , copy + 3 ) ; a = copy [ 0 ] ; b = copy [ 1 ] ; c = copy [ 2 ] ; } int euclidDistSquare ( point p1 , point p2 ) { return square ( p1 . x - p2 . x ) + square ( p1 . y - p2 . y ) ; } string getSideClassification ( int a , int b , int c ) { if ( a == b && b == c ) return " Equilateral " ; else if ( a == b b == c ) return " Isosceles " ; else return " Scalene " ; } string getAngleClassification ( int a , int b , int c ) { if ( a + b > c ) return " acute " ; else if ( a + b == c ) return " right " ; else return " obtuse " ; } void classifyTriangle ( point p1 , point p2 , point p3 ) { int a = euclidDistSquare ( p1 , p2 ) ; int b = euclidDistSquare ( p1 , p3 ) ; int c = euclidDistSquare ( p2 , p3 ) ; order ( a , b , c ) ; cout << " Triangle ▁ is ▁ " + getAngleClassification ( a , b , c ) + " ▁ and ▁ " + getSideClassification ( a , b , c ) << endl ; } int main ( ) { point p1 , p2 , p3 ; p1 = point ( 3 , 0 ) ; p2 = point ( 0 , 4 ) ; p3 = point ( 4 , 7 ) ; classifyTriangle ( p1 , p2 , p3 ) ; p1 = point ( 0 , 0 ) ; p2 = point ( 1 , 1 ) ; p3 = point ( 1 , 2 ) ; classifyTriangle ( p1 , p2 , p3 ) ; return 0 ; }
Area of a polygon with given n ordered vertices | C ++ program to evaluate area of a polygon using shoelace formula ; ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j = i ; j is previous vertex to i ; Return absolute value ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double polygonArea ( double X [ ] , double Y [ ] , int n ) { double area = 0.0 ; int j = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ; } return abs ( area / 2.0 ) ; } int main ( ) { double X [ ] = { 0 , 2 , 4 } ; double Y [ ] = { 1 , 3 , 7 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << polygonArea ( X , Y , n ) ; }
Difference of count of distinct elements present to left and right for each array element | C ++ program for the above approach ; Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void findDifferenceArray ( int arr [ ] , int N ) { set < int > S1 ; set < int > S2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { S1 . insert ( arr [ j ] ) ; } for ( int j = i + 1 ; j < N ; j ++ ) { S2 . insert ( arr [ j ] ) ; } cout << abs ( ( int ) S1 . size ( ) - ( int ) S2 . size ( ) ) << ' ▁ ' ; S1 . clear ( ) ; S2 . clear ( ) ; } } int main ( ) { int arr [ ] = { 7 , 7 , 3 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDifferenceArray ( arr , N ) ; return 0 ; }
Maximum subarray sum with same first and last element formed by removing elements | C ++ program for the above approach ; Function to find the maximum sum of sub - array ; Initialize the variables ; Traverse over the range ; Add the current value to the variable currsum for prefix sum ; Calculate the result ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSubarraySum ( vector < int > & arr ) { int N = arr . size ( ) ; unordered_map < int , int > memo ; int res = INT_MIN ; int currsum = 0 , currval = 0 ; for ( int i = 0 ; i < N ; ++ i ) { currval = arr [ i ] ; currsum = currsum + currval ; if ( memo . count ( currval ) != 0 ) { if ( currval > 0 ) res = max ( res , currsum - memo [ currval ] + currval ) ; else res = max ( res , currsum - memo [ currval ] + 2 * currval ) ; } else memo [ currval ] = currsum ; if ( currval < 0 ) currsum = currsum - currval ; } return res ; } int main ( ) { vector < int > arr = { -1 , -3 , 4 , 0 , -1 , -2 } ; cout << maximumSubarraySum ( arr ) ; return 0 ; }