codeparrot/xlcost-text-to-code huggingface dataset:
Sudo Placement | Range Queries | CPP program to find the numbers having atmost 3 set bits within a given range ; This function prints the required answer for each query ; Set of Numbers having at most 3 set bits arranged in non - descending order ; 0 set bits ; Iterate over all possible combinations of i , j and k for 60 bits ; 1 set bit ; 2 set bits ; 3 set bits ; Answer Queries by applying binary search ; Swap both the numbers if L is greater than R ; Driver Code ; Number of Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define LL  long long int NEW_LINE void answerQueries ( LL Q , vector < pair < LL , LL > > query ) { set < LL > s ; s . insert ( 0 ) ; for ( int i = 0 ; i <= 60 ; i ++ ) { for ( int j = i ; j <= 60 ; j ++ ) { for ( int k = j ; k <= 60 ; k ++ ) { if ( j == i && i == k ) s . insert ( 1LL << i ) ; else if ( j == k && i != j ) { LL x = ( 1LL << i ) + ( 1LL << j ) ; s . insert ( x ) ; } else if ( i == j && i != k ) { LL x = ( 1LL << i ) + ( 1LL << k ) ; s . insert ( x ) ; } else if ( i == k && i != j ) { LL x = ( 1LL << k ) + ( 1LL << j ) ; s . insert ( x ) ; } else { LL x = ( 1LL << i ) + ( 1LL << j ) + ( 1LL << k ) ; s . insert ( x ) ; } } } } vector < LL > validNumbers ; for ( auto val : s ) validNumbers . push_back ( val ) ; for ( int i = 0 ; i < Q ; i ++ ) { LL L = query [ i ] . first ; LL R = query [ i ] . second ; if ( R < L ) swap ( L , R ) ; if ( L == 0 ) cout << ( upper_bound ( validNumbers . begin ( ) , validNumbers . end ( ) , R ) - validNumbers . begin ( ) ) << endl ; else cout << ( upper_bound ( validNumbers . begin ( ) , validNumbers . end ( ) , R ) - upper_bound ( validNumbers . begin ( ) , validNumbers . end ( ) , L - 1 ) ) << endl ; } } int main ( ) { int Q = 2 ; vector < pair < LL , LL > > query ( Q ) ; query [ 0 ] . first = 3 ; query [ 0 ] . second = 7 ; query [ 1 ] . first = 10 ; query [ 1 ] . second = 16 ; answerQueries ( Q , query ) ; return 0 ; }
Collect all coins in minimum number of steps | C ++ program to find minimum number of steps to collect stack of coins ; recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minStepsRecur ( int height [ ] , int l , int r , int h ) { if ( l >= r ) return 0 ; int m = l ; for ( int i = l ; i < r ; i ++ ) if ( height [ i ] < height [ m ] ) m = i ; return min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) ; } int minSteps ( int height [ ] , int N ) { return minStepsRecur ( height , 0 , N , 0 ) ; } int main ( ) { int height [ ] = { 2 , 1 , 2 , 5 , 1 } ; int N = sizeof ( height ) / sizeof ( int ) ; cout << minSteps ( height , N ) << endl ; return 0 ; }
Count of Unique Direct Path Between N Points On a Plane | C ++ program for the above approach ; Function to count the total number of direct paths ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDirectPath ( int N ) { return N + ( N * ( N - 3 ) ) / 2 ; } int main ( ) { int N = 5 ; cout << countDirectPath ( N ) ; return 0 ; }
Find the coordinate that does not belong to any square | C ++ program for the above approach ; Function to find the point that is not a part of the side of a square ; Traverse each pair of coordinates ; Minimize x - coordinate in all the points except current point ; Maximize x - coordinate in all the points except the current point ; Minimize y - coordinate in all the points except current point ; Maximize y - coordinate in all the points except current point ; If x - coordinate matches with other same line ; If y coordinate matches with other same line ; Check if the condition for square exists or not ; Print the output ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; #define fi  first NEW_LINE #define se  second NEW_LINE void findPoint ( int n , vector < pair < int , int > > p ) { for ( int i = 0 ; i < n * 4 + 1 ; ++ i ) { int x1 = 2e9 , x2 = -2e9 ; int y1 = 2e9 , y2 = -2e9 ; for ( int j = 0 ; j < n * 4 + 1 ; ++ j ) if ( i != j ) { x1 = min ( x1 , p [ j ] . fi ) ; x2 = max ( x2 , p [ j ] . fi ) ; y1 = min ( y1 , p [ j ] . se ) ; y2 = max ( y2 , p [ j ] . se ) ; } bool ok = 1 ; int c1 = 0 , c2 = 0 ; int c3 = 0 , c4 = 0 ; for ( int j = 1 ; j <= n * 4 + 1 ; ++ j ) if ( i != j ) { if ( ( p [ j ] . fi == x1 p [ j ] . fi == x2 ) || ( ( p [ j ] . se == y1 p [ j ] . se == y2 ) ) ) { if ( p [ j ] . fi == x1 ) ++ c1 ; if ( p [ j ] . fi == x2 ) ++ c2 ; if ( p [ j ] . se == y1 ) ++ c3 ; if ( p [ j ] . se == y2 ) ++ c4 ; } else ok = 0 ; } if ( ok && c1 >= n && c2 >= n && c3 >= n && c4 >= n && x2 - x1 == y2 - y1 ) { cout << p [ i ] . fi << " ▁ " << p [ i ] . se << " STRNEWLINE " ; } } } int main ( ) { int N = 2 ; vector < pair < int , int > > arr = { { 0 , 0 } , { 0 , 1 } , { 0 , 2 } , { 1 , 0 } , { 1 , 1 } , { 1 , 2 } , { 2 , 0 } , { 2 , 1 } , { 2 , 2 } } ; findPoint ( N , arr ) ; return 0 ; }
Triacontagon Number | C ++ program for above approach ; Finding the nth triacontagonal number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int triacontagonalNum ( int n ) { return ( 28 * n * n - 26 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ triacontagonal ▁ Number ▁ is ▁ = ▁ " << triacontagonalNum ( n ) ; return 0 ; }
Hexacontagon Number | C ++ program for above approach ; Finding the nth hexacontagon number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int hexacontagonNum ( int n ) { return ( 58 * n * n - 56 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ hexacontagon ▁ Number ▁ is ▁ = ▁ " << hexacontagonNum ( n ) ; return 0 ; }
Enneacontagon Number | C ++ program for above approach ; Finding the nth enneacontagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int enneacontagonNum ( int n ) { return ( 88 * n * n - 86 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ " << enneacontagonNum ( n ) ; return 0 ; }
Triacontakaidigon Number | C ++ program for above approach ; Finding the nth triacontakaidigon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int triacontakaidigonNum ( int n ) { return ( 30 * n * n - 28 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ triacontakaidigon ▁ Number ▁ is ▁ = ▁ " << triacontakaidigonNum ( n ) ; return 0 ; }
Icosihexagonal Number | C ++ program for above approach ; Finding the nth Icosihexagonal Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int IcosihexagonalNum ( int n ) { return ( 24 * n * n - 22 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ Icosihexagonal ▁ Number ▁ is ▁ = ▁ " << IcosihexagonalNum ( n ) ; return 0 ; }
Icosikaioctagon or Icosioctagon Number | C ++ program for above approach ; Finding the nth icosikaioctagonal number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int icosikaioctagonalNum ( int n ) { return ( 26 * n * n - 24 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ " << icosikaioctagonalNum ( n ) ; return 0 ; }
Octacontagon Number | C ++ program for above approach ; Finding the nth octacontagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int octacontagonNum ( int n ) { return ( 78 * n * n - 76 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ octacontagon ▁ Number ▁ is ▁ = ▁ " << octacontagonNum ( n ) ; return 0 ; }
Hectagon Number | C ++ program for above approach ; Finding the nth hectagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hectagonNum ( int n ) { return ( 98 * n * n - 96 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ hectagon ▁ Number ▁ is ▁ = ▁ " << hectagonNum ( n ) ; return 0 ; }
Tetracontagon Number | C ++ program for above approach ; Finding the nth tetracontagon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int tetracontagonNum ( int n ) { return ( 38 * n * n - 36 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ tetracontagon ▁ Number ▁ is ▁ = ▁ " << tetracontagonNum ( n ) ; return 0 ; }
Perimeter of an Ellipse | C ++ program to find perimeter of an Ellipse ; Function to find the perimeter of an Ellipse ; Compute perimeter ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Perimeter ( int a , int b ) { float perimeter ; perimeter = 2 * 3.14 * sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ; cout << perimeter ; } int main ( ) { int a = 3 , b = 2 ; Perimeter ( a , b ) ; return 0 ; }
Biggest Reuleaux Triangle within A Square | C ++ Program to find the area of the biggest Reuleaux triangle that can be inscribed within a square ; Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float ReuleauxArea ( float a ) { if ( a < 0 ) return -1 ; float A = 0.70477 * pow ( a , 2 ) ; return A ; } int main ( ) { float a = 6 ; cout << ReuleauxArea ( a ) << endl ; return 0 ; }
Largest hexagon that can be inscribed within a square | C ++ Program to find the biggest hexagon which can be inscribed within the given square ; Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float hexagonside ( float a ) { if ( a < 0 ) return -1 ; float x = 0.5176 * a ; return x ; } int main ( ) { float a = 6 ; cout << hexagonside ( a ) << endl ; return 0 ; }
Largest hexagon that can be inscribed within an equilateral triangle | C ++ program to find the side of the largest hexagon which can be inscribed within an equilateral triangle ; Function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float hexagonside ( float a ) { if ( a < 0 ) return -1 ; float x = a / 3 ; return x ; } int main ( ) { float a = 6 ; cout << hexagonside ( a ) << endl ; return 0 ; }
Find middle point segment from given segment lengths | C / C ++ implementation of the approach ; Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSegment ( int n , int m , int segment_length [ ] ) { double meet_point = ( 1.0 * n ) / 2.0 ; int sum = 0 ; int segment_number = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( ( double ) sum == meet_point ) { segment_number = -1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } int main ( ) { int n = 13 ; int m = 3 ; int segment_length [ ] = { 3 , 2 , 8 } ; int ans = findSegment ( n , m , segment_length ) ; cout << ( ans ) ; return 0 ; }
Maximum points of intersection n lines | CPP program to find maximum intersecting points ; nC2 = ( n ) * ( n - 1 ) / 2 ; ; Driver code ; n is number of line
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long int NEW_LINE ll countMaxIntersect ( ll n ) { return ( n ) * ( n - 1 ) / 2 ; } int main ( ) { ll n = 8 ; cout << countMaxIntersect ( n ) << endl ; return 0 ; }
Program to find volume and surface area of pentagonal prism | CPP program to find surface area and volume of the Pentagonal Prism ; function for surface area ; function for VOlume ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; float surfaceArea ( float a , float b , float h ) { return 5 * a * b + 5 * b * h ; } float volume ( float b , float h ) { return ( 5 * b * h ) / 2 ; } int main ( ) { float a = 5 ; float b = 3 ; float h = 7 ; cout << " surface ▁ area = ▁ " << surfaceArea ( a , b , h ) << " , ▁ " ; cout << " volume = ▁ " << volume ( b , h ) ; }
Check if a point is inside , outside or on the parabola | C ++ Program to check if the point lies within the parabola or not ; Function to check the point ; checking the equation of parabola with the given point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkpoint ( int h , int k , int x , int y , int a ) { int p = pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) ; return p ; } int main ( ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 ; if ( checkpoint ( h , k , x , y , a ) > 0 ) cout << " Outside " << endl ; else if ( checkpoint ( h , k , x , y , a ) == 0 ) cout << " On ▁ the ▁ parabola " << endl ; else cout << " Inside " << endl ; return 0 ; }
Check if a point is inside , outside or on the ellipse | C ++ Program to check if the point lies within the ellipse or not ; Function to check the point ; checking the equation of ellipse with the given point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( pow ( ( x - h ) , 2 ) / pow ( a , 2 ) ) + ( pow ( ( y - k ) , 2 ) / pow ( b , 2 ) ) ; return p ; } int main ( ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 , b = 5 ; if ( checkpoint ( h , k , x , y , a , b ) > 1 ) cout << " Outside " << endl ; else if ( checkpoint ( h , k , x , y , a , b ) == 1 ) cout << " On ▁ the ▁ ellipse " << endl ; else cout << " Inside " << endl ; return 0 ; }
Area of circle inscribed within rhombus | C ++ Program to find the area of the circle which can be inscribed within the rhombus ; Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float circlearea ( float a , float b ) { if ( a < 0 b < 0 ) return -1 ; float A = ( 3.14 * pow ( a , 2 ) * pow ( b , 2 ) ) / ( 4 * ( pow ( a , 2 ) + pow ( b , 2 ) ) ) ; return A ; } int main ( ) { float a = 8 , b = 10 ; cout << circlearea ( a , b ) << endl ; return 0 ; }
The biggest possible circle that can be inscribed in a rectangle | C ++ Program to find the biggest circle which can be inscribed within the rectangle ; Function to find the area of the biggest circle ; the length and breadth cannot be negative ; area of the circle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float circlearea ( float l , float b ) { if ( l < 0 b < 0 ) return -1 ; if ( l < b ) return 3.14 * pow ( l / 2 , 2 ) ; else return 3.14 * pow ( b / 2 , 2 ) ; } int main ( ) { float l = 4 , b = 8 ; cout << circlearea ( l , b ) << endl ; return 0 ; }
Centered cube number | Program to find nth Centered cube number ; Function to find Centered cube number ; Formula to calculate nth Centered cube number & return it into main function . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centered_cube ( int n ) { return ( 2 * n + 1 ) * ( n * n + n + 1 ) ; } int main ( ) { int n = 3 ; cout << n << " th ▁ Centered ▁ cube ▁ number : ▁ " ; cout << centered_cube ( n ) ; cout << endl ; n = 10 ; cout << n << " th ▁ Centered ▁ cube ▁ number : ▁ " ; cout << centered_cube ( n ) ; return 0 ; }
Find the center of the circle using endpoints of diameter | C ++ program to find the center of the circle ; function to find the center of the circle ; Driven Program
#include <iostream> NEW_LINE using namespace std ; void center ( int x1 , int x2 , int y1 , int y2 ) { cout << ( float ) ( x1 + x2 ) / 2 << " , ▁ " << ( float ) ( y1 + y2 ) / 2 ; } int main ( ) { int x1 = -9 , y1 = 3 , x2 = 5 , y2 = -7 ; center ( x1 , x2 , y1 , y2 ) ; return 0 ; }
Program to calculate volume of Octahedron | CPP Program to calculate volume of Octahedron ; utility Function ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double vol_of_octahedron ( double side ) { return ( ( side * side * side ) * ( sqrt ( 2 ) / 3 ) ) ; } int main ( ) { double side = 3 ; cout << " Volume ▁ of ▁ octahedron ▁ = " << vol_of_octahedron ( side ) << endl ; }
Program to calculate volume of Ellipsoid | CPP program to find the volume of Ellipsoid . ; Function to find the volume ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) { float pi = 3.14 ; return 1.33 * pi * r1 * r2 * r3 ; } int main ( ) { float r1 = 2.3 , r2 = 3.4 , r3 = 5.7 ; cout << " volume ▁ of ▁ ellipsoid ▁ is ▁ : ▁ " << volumeOfEllipsoid ( r1 , r2 , r3 ) ; return 0 ; }
Program to calculate Area Of Octagon | CPP program to find area of octagon ; Utility function ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ; } int main ( ) { double side = 4 ; cout << " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " << areaOctagon ( side ) << endl ; return 0 ; }
Program for Volume and Surface Area of Cube | CPP program to find area and total surface area of cube ; utility function ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double areaCube ( double a ) { return ( a * a * a ) ; } double surfaceCube ( double a ) { return ( 6 * a * a ) ; } int main ( ) { double a = 5 ; cout << " Area ▁ = ▁ " << areaCube ( a ) << endl ; cout << " Total ▁ surface ▁ area ▁ = ▁ " << surfaceCube ( a ) ; return 0 ; }
Find mirror image of a point in 2 | C ++ code to find mirror image ; C ++ function which finds coordinates of mirror image . This function return a pair of double ; Driver code to test above function
#include <iostream> NEW_LINE using namespace std ; pair < double , double > mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = -2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return make_pair ( x , y ) ; } int main ( ) { double a = -1.0 ; double b = 1.0 ; double c = 0.0 ; double x1 = 1.0 ; double y1 = 0.0 ; pair < double , double > image = mirrorImage ( a , b , c , x1 , y1 ) ; cout << " Image ▁ of ▁ point ▁ ( " << x1 << " , ▁ " << y1 << " ) ▁ " ; cout << " by ▁ mirror ▁ ( " << a << " ) x ▁ + ▁ ( " << b << " ) y ▁ + ▁ ( " << c << " ) ▁ = ▁ 0 , ▁ is ▁ : " ; cout << " ( " << image . first << " , ▁ " << image . second << " ) " << endl ; return 0 ; }
Program for Point of Intersection of Two Lines | C ++ Implementation . To find the point of intersection of two lines ; This pair is used to store the X and Y coordinates of a point respectively ; Function used to display X and Y coordinates of a point ; Line AB represented as a1x + b1y = c1 ; Line CD represented as a2x + b2y = c2 ; The lines are parallel . This is simplified by returning a pair of FLT_MAX ; Driver code ; NOTE : Further check can be applied in case of line segments . Here , we have considered AB and CD as lines
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define pdd  pair<double, double> NEW_LINE void displayPoint ( pdd P ) { cout << " ( " << P . first << " , ▁ " << P . second << " ) " << endl ; } pdd lineLineIntersection ( pdd A , pdd B , pdd C , pdd D ) { double a1 = B . second - A . second ; double b1 = A . first - B . first ; double c1 = a1 * ( A . first ) + b1 * ( A . second ) ; double a2 = D . second - C . second ; double b2 = C . first - D . first ; double c2 = a2 * ( C . first ) + b2 * ( C . second ) ; double determinant = a1 * b2 - a2 * b1 ; if ( determinant == 0 ) { return make_pair ( FLT_MAX , FLT_MAX ) ; } else { double x = ( b2 * c1 - b1 * c2 ) / determinant ; double y = ( a1 * c2 - a2 * c1 ) / determinant ; return make_pair ( x , y ) ; } } int main ( ) { pdd A = make_pair ( 1 , 1 ) ; pdd B = make_pair ( 4 , 4 ) ; pdd C = make_pair ( 1 , 8 ) ; pdd D = make_pair ( 2 , 4 ) ; pdd intersection = lineLineIntersection ( A , B , C , D ) ; if ( intersection . first == FLT_MAX && intersection . second == FLT_MAX ) { cout << " The ▁ given ▁ lines ▁ AB ▁ and ▁ CD ▁ are ▁ parallel . STRNEWLINE " ; } else { cout << " The ▁ intersection ▁ of ▁ the ▁ given ▁ lines ▁ AB ▁ " " and ▁ CD ▁ is : ▁ " ; displayPoint ( intersection ) ; } return 0 ; }
Minimum revolutions to move center of a circle to a target | C ++ program to find minimum number of revolutions to reach a target center ; Minimum revolutions to move center from ( x1 , y1 ) to ( x2 , y2 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return ceil ( d / ( 2 * r ) ) ; } int main ( ) { int r = 2 , x1 = 0 , y1 = 0 , x2 = 0 , y2 = 4 ; cout << minRevolutions ( r , x1 , y1 , x2 , y2 ) ; return 0 ; }
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | C ++ program to get right angle triangle , given hypotenuse and area of triangle ; limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define eps  1e-6 NEW_LINE double getArea ( double base , double hypotenuse ) { double height = sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } void printRightAngleTriangle ( int hypotenuse , int area ) { int hsquare = hypotenuse * hypotenuse ; double sideForMaxArea = sqrt ( hsquare / 2.0 ) ; double maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { cout << " Not ▁ possiblen " ; return ; } double low = 0.0 ; double high = sideForMaxArea ; double base ; while ( abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) high = base ; else low = base ; } double height = sqrt ( hsquare - base * base ) ; cout << base << " ▁ " << height << endl ; } int main ( ) { int hypotenuse = 5 ; int area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ; return 0 ; }
Circle and Lattice Points | C ++ program to find countLattice points on a circle ; Function to count Lattice points on a circle ; Initialize result as 4 for ( r , 0 ) , ( - r . 0 ) , ( 0 , r ) and ( 0 , - r ) ; Check every value that can be potential x ; Find a potential y ; checking whether square root is an integer or not . Count increments by 4 for four different quadrant values ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countLattice ( int r ) { if ( r <= 0 ) return 0 ; int result = 4 ; for ( int x = 1 ; x < r ; x ++ ) { int ySquare = r * r - x * x ; int y = sqrt ( ySquare ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } int main ( ) { int r = 5 ; cout << countLattice ( r ) ; return 0 ; }
Count of subarrays of size K with average at least M | C ++ program for the above approach ; Function to count the subarrays of size K having average at least M ; Stores the resultant count of subarray ; Stores the sum of subarrays of size K ; Add the values of first K elements to the sum ; Increment the count if the current subarray is valid ; Traverse the given array ; Find the updated sum ; Check if current subarray is valid or not ; Return the count of subarrays ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countSubArrays ( int arr [ ] , int N , int K , int M ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum += arr [ i ] ; } if ( sum >= K * M ) count ++ ; for ( int i = K ; i < N ; i ++ ) { sum += ( arr [ i ] - arr [ i - K ] ) ; if ( sum >= K * M ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 3 , 6 , 3 , 2 , 1 , 3 , 9 } ; int K = 2 , M = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubArrays ( arr , N , K , M ) ; return 0 ; }