text
stringlengths 17
4.49k
| code
stringlengths 49
5.46k
|
---|---|
Minimum difference between adjacent elements of array which contain elements from each row of a matrix | C ++ program to find the minimum absolute difference between any of the adjacent elements of an array which is created by picking one element from each row of the matrix . ; Return smallest element greater than or equal to the current element . ; Return the minimum absolute difference adjacent elements of array ; Sort each row of the matrix . ; For each matrix element ; Search smallest element in the next row which is greater than or equal to the current element ; largest element which is smaller than the current element in the next row must be just before smallest element which is greater than or equal to the current element because rows are sorted . ; Driven Program | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define R 2 NEW_LINE #define C 2 NEW_LINE int bsearch ( int low , int high , int n , int arr [ ] ) { int mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } int mindiff ( int arr [ R ] [ C ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) sort ( arr [ i ] , arr [ i ] + m ) ; int ans = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) ; ans = min ( ans , abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) ; if ( p - 1 >= 0 ) ans = min ( ans , abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) ; } } return ans ; } int main ( ) { int m [ R ] [ C ] = { 8 , 5 , 6 , 8 , } ; cout << mindiff ( m , R , C ) << endl ; return 0 ; } |
Find bitonic point in given bitonic sequence | C ++ program to find bitonic point in a bitonic array . ; Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program to run the case | #include <bits/stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int left , int right ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return -1 ; } int main ( ) { int arr [ ] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != -1 ) cout << arr [ index ] ; return 0 ; } |
Find the only repeating element in a sorted array of size n | C ++ program to find the only repeating element in an array of size n and elements from range 1 to n - 1. ; Returns index of second appearance of a repeating element The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return -1 ; int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int index = findRepeatingElement ( arr , 0 , n - 1 ) ; if ( index != -1 ) cout << arr [ index ] ; return 0 ; } |
Find cubic root of a number | C ++ program to find cubic root of a number using Binary Search ; Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double diff ( double n , double mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } double cubicRoot ( double n ) { double start = 0 , end = n ; double e = 0.0000001 ; while ( true ) { double mid = ( start + end ) / 2 ; double error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } int main ( ) { double n = 3 ; printf ( " Cubic β root β of β % lf β is β % lf STRNEWLINE " , n , cubicRoot ( n ) ) ; return 0 ; } |
Find frequency of each element in a limited range array in less than O ( n ) time | C ++ program to count number of occurrences of each element in the array in less than O ( n ) time ; A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver function | #include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void findFrequencyUtil ( int arr [ ] , int low , int high , vector < int > & freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { int mid = ( low + high ) / 2 ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } void findFrequency ( int arr [ ] , int n ) { vector < int > freq ( arr [ n - 1 ] + 1 , 0 ) ; findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( int i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) cout << " Element β " << i << " β occurs β " << freq [ i ] << " β times " << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findFrequency ( arr , n ) ; return 0 ; } |
Square root of an integer | A C ++ program to find floor ( sqrt ( x ) ; Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } int main ( ) { int x = 11 ; cout << floorSqrt ( x ) << endl ; return 0 ; } |
Maximum number of overlapping rectangles with at least one common point | C ++ program for the above approach ; Function to find the maximum number of overlapping rectangles ; Stores the maximum count of overlapping rectangles ; Stores the X and Y coordinates ; Iterate over all pairs of Xs and Ys ; Store the count for the current X and Y ; Update the maximum count of rectangles ; Returns the total count ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void maxOverlappingRectangles ( int x1 [ ] , int y1 [ ] , int x2 [ ] , int y2 [ ] , int N ) { int max_rectangles = 0 ; vector < int > X , Y ; for ( int i = 0 ; i < N ; i ++ ) { X . push_back ( x1 [ i ] ) ; X . push_back ( x2 [ i ] - 1 ) ; Y . push_back ( y1 [ i ] ) ; Y . push_back ( y2 [ i ] - 1 ) ; } for ( int i = 0 ; i < X . size ( ) ; i ++ ) { for ( int j = 0 ; j < Y . size ( ) ; j ++ ) { int cnt = 0 ; for ( int k = 0 ; k < N ; k ++ ) { if ( X [ i ] >= x1 [ k ] && X [ i ] + 1 <= x2 [ k ] && Y [ j ] >= y1 [ k ] && Y [ j ] + 1 <= y2 [ k ] ) { cnt ++ ; } } max_rectangles = max ( max_rectangles , cnt ) ; } } cout << max_rectangles ; } int main ( ) { int x1 [ ] = { 0 , 50 } ; int y1 [ ] = { 0 , 50 } ; int x2 [ ] = { 100 , 60 } ; int y2 [ ] = { 100 , 60 } ; int N = sizeof ( x1 ) / sizeof ( x1 [ 0 ] ) ; maxOverlappingRectangles ( x1 , y1 , x2 , y2 , N ) ; return 0 ; } |
Check if it is possible to reach the point ( X , Y ) using distances given in an array | C ++ program for the above approach ; Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int isPossibleToReach ( int A [ ] , int N , int X , int Y ) { double distance = sqrt ( double ( X * X + Y * Y ) ) ; double mx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mx += double ( A [ i ] ) ; } if ( mx < distance ) { cout << " NO " ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { cout << " YES " ; return 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( distance + mx < double ( 2 ) * double ( A [ i ] ) ) { cout << " No " ; return 0 ; } } cout << " Yes " ; return 0 ; } int main ( ) { int A [ ] = { 2 , 5 } ; int X = 5 , Y = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; isPossibleToReach ( A , N , X , Y ) ; return 0 ; } |
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C ++ program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) pow ( 3 , steps ) , steps + 1 ) ) ; } int main ( ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { cout << " YES " << endl ; } else cout << " NO " << endl ; return 0 ; } |
Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C ++ program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool canReach ( int X , int Y ) { int steps = 0 ; while ( X Y ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } int main ( ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { cout << " YES " ; } else { cout << " NO " ; } } |
Program to calculate Surface Area of Ellipsoid | C ++ program for the above approach ; Function to find the surface area of the given Ellipsoid ; Formula to find surface area of an Ellipsoid ; Print the area ; Driver Code | #include <iomanip> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findArea ( double a , double b , double c ) { double area = 4 * 3.141592653 * pow ( ( pow ( a * b , 1.6 ) + pow ( a * c , 1.6 ) + pow ( b * c , 1.6 ) ) / 3 , 1 / 1.6 ) ; cout << fixed << setprecision ( 2 ) << area ; } int main ( ) { double A = 11 , B = 12 , C = 13 ; findArea ( A , B , C ) ; return 0 ; } |
Descartes ' Circle Theorem with implementation | C ++ implementation of the above formulae ; Function to find the fourth circle 's when three radius are given ; Driver code ; Radius of three circles ; Calculation of r4 using formula given above | #include <bits/stdc++.h> NEW_LINE using namespace std ; double findRadius ( double r1 , double r2 , double r3 ) { double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; return r4 ; } int main ( ) { double r1 = 1 ; double r2 = 1 ; double r3 = 1 ; double r4 = findRadius ( r1 , r2 , r3 ) ; cout << " The β radius β of β fourth β circle : β " << r4 ; return 0 ; } |
Make N pairs from Array as ( X , Y ) coordinate point that are enclosed inside a minimum area rectangle | C ++ program for tha above approach ; Function to make N pairs of coordinates such that they are enclosed in a minimum area rectangle with sides parallel to the X and Y axes ; A variable to store the answer ; For the case where the maximum and minimum are in different partitions ; For the case where the maximum and minimum are in the same partition ; Return the answer ; Driver code ; Given Input ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumRectangleArea ( int A [ ] , int N ) { int ans ; sort ( A , A + 2 * N ) ; ans = ( A [ N - 1 ] - A [ 0 ] ) * ( A [ 2 * N - 1 ] - A [ N ] ) ; for ( int i = 1 ; i < N ; i ++ ) ans = min ( ans , ( A [ 2 * N - 1 ] - A [ 0 ] ) * ( A [ i + N - 1 ] - A [ i ] ) ) ; return ans ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 5 , 3 , 6 , 7 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; N /= 2 ; cout << minimumRectangleArea ( A , N ) << endl ; return 0 ; } |
Program to find the length of Latus Rectum of a Hyperbola | C ++ program for the above approach ; Function to calculate the length of the latus rectum of a hyperbola ; Store the length of major axis ; Store the length of minor axis ; Store the length of the latus rectum ; Return the length of the latus rectum ; Driver Code | #include <iostream> NEW_LINE using namespace std ; double lengthOfLatusRectum ( double A , double B ) { double major = 2.0 * A ; double minor = 2.0 * B ; double latus_rectum = ( minor * minor ) / major ; return latus_rectum ; } int main ( ) { double A = 3.0 , B = 2.0 ; cout << lengthOfLatusRectum ( A , B ) ; return 0 ; } |
Length of intercept cut off from a line by a Circle | C ++ program for the above approach ; Function to find the radius of a circle ; g and f are the coordinates of the center ; Case of invalid circle ; Apply the radius formula ; Function to find the perpendicular distance between circle center and the line ; Store the coordinates of center ; Stores the perpendicular distance between the line and the point ; Invalid Case ; Return the distance ; Function to find the length of intercept cut off from a line by a circle ; Calculate the value of radius ; Calculate the perpendicular distance between line and center ; Invalid Case ; If line do not cut circle ; Print the intercept length ; Driver Code ; Given Input ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; double radius ( int a , int b , int c ) { int g = a / 2 ; int f = b / 2 ; if ( g * g + f * f - c < 0 ) return ( -1 ) ; return ( sqrt ( g * g + f * f - c ) ) ; } double centerDistanceFromLine ( int a , int b , int i , int j , int k ) { int g = a / 2 ; int f = b / 2 ; double distance = fabs ( i * g + j * f + k ) / ( sqrt ( i * i + j * j ) ) ; if ( distance < 0 ) return ( -1 ) ; return distance ; } void interceptLength ( int a , int b , int c , int i , int j , int k ) { double rad = radius ( a , b , c ) ; double dist = centerDistanceFromLine ( a , b , i , j , k ) ; if ( rad < 0 dist < 0 ) { cout << " circle β not β possible " ; return ; } if ( dist > rad ) { cout << " Line β not β cutting β circle " ; } else cout << 2 * sqrt ( rad * rad - dist * dist ) ; } int main ( ) { int a = 0 , b = 0 , c = -4 ; int i = 2 , j = -1 , k = 1 ; interceptLength ( a , b , c , i , j , k ) ; return 0 ; } |
Determine position of two points with respect to a 3D plane | C ++ program for the above approach ; Function to check position of two points with respect to a plane in 3D ; Put coordinates in plane equation ; If both values have same sign ; If both values have different sign ; If both values are zero ; If either of the two values is zero ; Driver Code ; Given Input ; Coordinates of points ; Function Call | #include <iostream> NEW_LINE using namespace std ; void check_position ( int a , int b , int c , int d , int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int value_1 = a * x1 + b * y1 + c * z1 + d ; int value_2 = a * x2 + b * y2 + c * z2 + d ; if ( ( value_1 > 0 && value_2 > 0 ) || ( value_1 < 0 && value_2 < 0 ) ) cout << " On β same β side " ; if ( ( value_1 > 0 && value_2 < 0 ) || ( value_1 < 0 && value_2 > 0 ) ) cout << " On β different β sides " ; if ( value_1 == 0 && value_2 == 0 ) cout << " Both β on β the β plane " ; if ( value_1 == 0 && value_2 != 0 ) cout << " Point β 1 β on β the β plane " ; if ( value_1 != 0 && value_2 == 0 ) cout << " Point β 2 β on β the β plane " ; } int main ( ) { int a = 1 , b = 2 , c = 3 , d = 4 ; int x1 = -2 , y1 = -2 , z1 = 1 ; int x2 = -4 , y2 = 11 , z2 = -1 ; check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) ; return 0 ; } |
Check if any pair of semicircles intersect or not | C ++ program for the above approach ; Function to check if any pairs of the semicircles intersects or not ; Stores the coordinates of all the semicircles ; x and y are coordinates ; Store the minimum and maximum value of the pair ; Push the pair in vector ; Compare one pair with other pairs ; Generating the second pair ; Extract all the second pairs one by one ; 1 st condition ; 2 nd condition ; If any one condition is true ; If any pair of semicircles doesn 't exists ; Driver Code | #include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; bool checkIntersection ( int arr [ ] , int N ) { vector < pair < int , int > > vec ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int x = arr [ i ] , y = arr [ i + 1 ] ; int minn = min ( x , y ) ; int maxx = max ( x , y ) ; vec . push_back ( { minn , maxx } ) ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { pair < int , int > x = vec [ i ] ; for ( int j = 0 ; j < vec . size ( ) ; j ++ ) { pair < int , int > y = vec [ j ] ; bool cond1 = ( x . first < y . first and x . second < y . second and y . first < x . second ) ; bool cond2 = ( y . first < x . first and y . second < x . second and x . first < y . second ) ; if ( cond1 or cond2 ) { return true ; } } } return false ; } int main ( ) { int arr [ ] = { 0 , 15 , 5 , 10 } ; int N = sizeof ( arr ) / sizeof ( int ) ; if ( checkIntersection ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; } |
Find the angle between tangents drawn from a given external point to a Circle | C ++ program for the above approach ; Function to find the distance between center and the exterior point ; Find the difference between the x and y coordinates ; Using the distance formula ; Function to find the angle between the pair of tangents drawn from the point ( X2 , Y2 ) to the circle . ; Calculate the distance between the center and exterior point ; Invalid Case ; Find the angle using the formula ; Print the resultant angle ; Driver Code | #include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; double point_distance ( int x1 , int y1 , int x2 , int y2 ) { int p = ( x2 - x1 ) ; int q = ( y2 - y1 ) ; double distance = sqrt ( p * p + q * q ) ; return distance ; } void tangentAngle ( int x1 , int y1 , int x2 , int y2 , double radius ) { double distance = point_distance ( x1 , y1 , x2 , y2 ) ; if ( radius / distance > 1 radius / distance < -1 ) { cout << -1 ; } double result = 2 * asin ( radius / distance ) * 180 / 3.1415 ; cout << result << " β degrees " ; } int main ( ) { int radius = 4 ; int x1 = 7 , y1 = 12 ; int x2 = 3 , y2 = 4 ; tangentAngle ( x1 , y1 , x2 , y2 , radius ) ; return 0 ; } |
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N | C ++ code for the above approach ; Function to calculate the ratio of area of N - th and ( N + 1 ) - th nested polygons formed by connecting midpoints ; Stores the value of PI ; Calculating area the factor ; Printing the ratio precise upto 6 decimal places ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void AreaFactor ( int n ) { double pi = 3.14159265 ; double areaf = 1 / ( cos ( pi / n ) * cos ( pi / n ) ) ; cout << fixed << setprecision ( 6 ) << areaf << endl ; } int main ( ) { int n = 4 ; AreaFactor ( n ) ; return 0 ; } |
Find the length of Kth N | C ++ program for the above approach ; Function to calculate the interior angle of a N - sided regular polygon ; Function to find the K - th polygon formed inside the ( K - 1 ) th polygon ; Stores the interior angle ; Stores the side length of K - th regular polygon ; Return the length ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI 3.14159265 NEW_LINE double findInteriorAngle ( int n ) { return ( n - 2 ) * PI / n ; } double calculateSideLength ( double L , int N , int K ) { double angle = findInteriorAngle ( N ) ; double length = L * pow ( sin ( angle / 2 ) , ( K - 1 ) ) ; return length ; } int main ( ) { double N = 5 , L = 21 , K = 7 ; cout << calculateSideLength ( L , N , K ) ; return 0 ; } |
Angle between a Pair of Lines | C ++ program for the above approach ; Function to find the angle between two lines ; Store the tan value of the angle ; Calculate tan inverse of the angle ; Convert the angle from radian to degree ; Print the result ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI 3.14159265 NEW_LINE void findAngle ( double M1 , double M2 ) { double angle = abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) ; double ret = atan ( angle ) ; double val = ( ret * 180 ) / PI ; cout << val ; } int main ( ) { double M1 = 1.75 , M2 = 0.27 ; findAngle ( M1 , M2 ) ; return 0 ; } |
Angle of intersection of two circles having their centers D distance apart | C ++ program for the above approach ; Function to find the cosine of the angle of the intersection of two circles with radius R1 and R2 ; Return the cosine of the angle ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float angle ( float R1 , float R2 , float D ) { float ans = ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ; return ans ; } int main ( ) { float R1 = 3 , R2 = 4 ; float D = 5 ; cout << angle ( R1 , R2 , D ) ; return 0 ; } |
Check if any point exists in a plane whose Manhattan distance is at most K from N given points | C ++ program for the above approach ; Function to check if there exists any point with at most K distance from N given points ; Traverse the given N points ; Stores the count of pairs of coordinates having Manhattan distance <= K ; For the same coordinate ; Calculate Manhattan distance ; If Manhattan distance <= K ; If all coordinates can meet ; If all coordinates can 't meet ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; string find ( int a [ ] , int b [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { continue ; } long long int dis = abs ( a [ i ] - a [ j ] ) + abs ( b [ i ] - b [ j ] ) ; if ( dis <= K ) { count ++ ; } if ( count == N - 1 ) { return " Yes " ; } } } return " No " ; } int main ( ) { int N = 5 ; int A [ ] = { 1 , 0 , 2 , 1 , 1 } ; int B [ ] = { 1 , 1 , 1 , 0 , 2 } ; int K = 1 ; cout << find ( A , B , N , K ) << endl ; } |
Minimum area of the triangle formed by any tangent to an ellipse with the coordinate axes | C ++ program for the above approach ; Function to find the minimum area of triangle formed by any tangent to ellipse with the coordinate axes ; Stores the minimum area ; Print the calculated area ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumTriangleArea ( int a , int b ) { int area = a * b ; cout << area ; } int main ( ) { int a = 1 , b = 2 ; minimumTriangleArea ( a , b ) ; return 0 ; } |
Find interior angles for each side of a given Cyclic Quadrilateral | C ++ program for the above approach ; Function to find the interior angles of the cyclic quadrilateral ; Stores the numerator and the denominator to find angle A ; Stores the numerator and the denominator to find angle B ; Stores the numerator and the denominator to find angle C : ; Stores the numerator and the denominator to find angle D : ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findAngles ( double a , double b , double c , double d ) { double numerator = a * a + d * d - b * b - c * c ; double denominator = 2 * ( a * b + c * d ) ; double x = numerator / denominator ; cout << fixed << setprecision ( 2 ) << " A : β " << ( acos ( x ) * 180 ) / 3.141592 << " β degrees " ; numerator = a * a + b * b - c * c - d * d ; x = numerator / denominator ; cout << fixed << setprecision ( 2 ) << " STRNEWLINE B : β " << ( acos ( x ) * 180 ) / 3.141592 << " β degrees " ; numerator = c * c + b * b - a * a - d * d ; x = numerator / denominator ; cout << fixed << setprecision ( 2 ) << " STRNEWLINE C : β " << ( acos ( x ) * 180 ) / 3.141592 << " β degrees " ; numerator = d * d + c * c - a * a - b * b ; x = numerator / denominator ; cout << fixed << setprecision ( 2 ) << " STRNEWLINE D : β " << ( acos ( x ) * 180 ) / 3.141592 << " β degrees " ; } int main ( ) { double A = 10 , B = 15 , C = 20 , D = 25 ; findAngles ( A , B , C , D ) ; return 0 ; } |
Number of smaller circles that can be inscribed in a larger circle | C ++ program for the above approach ; Function to count number of smaller circles that can be inscribed in the larger circle touching its boundary ; If R2 is greater than R1 ; Stores the angle made by the smaller circle ; Stores the ratio of R2 / ( R1 - R2 ) ; Stores the count of smaller circles that can be inscribed ; Stores the ratio ; If the diameter of smaller circle is greater than the radius of the larger circle ; Otherwise ; Find the angle using formula ; Divide 360 with angle and take the floor value ; Return the final result ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countInscribed ( int R1 , int R2 ) { if ( R2 > R1 ) return 0 ; double angle ; double ratio ; int number_of_circles = 0 ; ratio = R2 / ( double ) ( R1 - R2 ) ; if ( R1 < 2 * R2 ) { number_of_circles = 1 ; } else { angle = abs ( asin ( ratio ) * 180 ) / 3.14159265 ; number_of_circles = 360 / ( 2 * floor ( angle ) ) ; } return number_of_circles ; } int main ( ) { int R1 = 3 ; int R2 = 1 ; cout << countInscribed ( R1 , R2 ) ; return 0 ; } |
Count pairs of coordinates connected by a line with slope in the range [ | C ++ program for the above approach ; Function to find the number of pairs of points such that the line passing through them has a slope in the range [ - k , k ] ; Store the result ; Traverse through all the combination of points ; If pair satisfies the given condition ; Increment ans by 1 ; Print the result ; Driver Code ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( vector < int > x , vector < int > y , int K ) { int n = x . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { if ( K * abs ( x [ i ] - x [ j ] ) >= abs ( y [ i ] - y [ j ] ) ) { ++ ans ; } } } cout << ans ; } int main ( ) { vector < int > X = { 2 , 1 , 0 } , Y = { 1 , 2 , 0 } ; int K = 1 ; findPairs ( X , Y , K ) ; return 0 ; } |
Number of largest circles that can be inscribed in a rectangle | C ++ program for the above approach ; Function to count the number of largest circles in a rectangle ; If length exceeds breadth ; Swap to reduce length to smaller than breadth ; Return total count of circles inscribed ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int totalCircles ( int L , int B ) { if ( L > B ) { int temp = L ; L = B ; B = temp ; } return B / L ; } int main ( ) { int L = 3 ; int B = 8 ; cout << totalCircles ( L , B ) ; return 0 ; } |
Program to calculate length of diagonal of a square | C ++ program for the above approach ; Function to find the length of the diagonal of a square of a given side ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double findDiagonal ( double s ) { return sqrt ( 2 ) * s ; } int main ( ) { double S = 10 ; cout << findDiagonal ( S ) ; return 0 ; } |
Area of largest isosceles triangle that can be inscribed in an Ellipse whose vertex coincides with one extremity of the major axis | C ++ program for the above approach ; Function to calculate area of the isosceles triangle ; If a and b are negative ; Stores the area of the triangle ; Print the area ; Driver code ; Given value of a & b ; Function call to find the area of the isosceles triangle | #include <bits/stdc++.h> NEW_LINE using namespace std ; void triangleArea ( float a , float b ) { if ( a < 0 b < 0 ) { cout << -1 ; return ; } float area = ( 3 * sqrt ( 3 ) * a * b ) / ( 4 ) ; cout << area ; } int main ( ) { float a = 1 , b = 2 ; triangleArea ( a , b ) ; return 0 ; } |
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides | C ++ implementation of above approach ; Function to find answer of each query ; Stores the count of points with sum less than or equal to their indices ; Traverse the array ; If both x and y - coordinate < 0 ; Stores the sum of co - ordinates ; Increment count of sum by 1 ; Prefix array ; Perform queries ; Drivers Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int const MAX = 1e6 + 5 ; int query ( vector < vector < float > > arr , vector < int > Q ) { int pre [ MAX ] = { 0 } ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] [ 0 ] < 0 arr [ i ] [ 1 ] < 0 ) continue ; int sum = ceil ( ( arr [ i ] [ 0 ] + arr [ i ] [ 1 ] ) ) ; pre [ sum ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) pre [ i ] += pre [ i - 1 ] ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { cout << pre [ Q [ i ] ] << " β " ; } cout << endl ; } int main ( ) { vector < vector < float > > arr = { { 2.1 , 3.0 } , { 3.7 , 1.2 } , { 1.5 , 6.5 } , { 1.2 , 0.0 } } ; vector < int > Q = { 2 , 8 , 5 } ; int N = arr . size ( ) ; int M = Q . size ( ) ; query ( arr , Q ) ; } |
Perimeter of the Union of Two Rectangles | C ++ program for the above approach ; Function to check if two rectangles are intersecting or not ; If one rectangle is to the right of other 's right edge ; If one rectangle is on the top of other 's top edge ; Function to return the perimeter of the Union of Two Rectangles ; Stores the resultant perimeter ; If rectangles do not interesect ; Perimeter of Rectangle 1 ; Perimeter of Rectangle 2 ; If the rectangles intersect ; Get width of combined figure ; Get length of combined figure ; Return the perimeter ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool doIntersect ( vector < int > X , vector < int > Y ) { if ( X [ 0 ] > X [ 3 ] X [ 2 ] > X [ 1 ] ) return false ; if ( Y [ 0 ] > Y [ 3 ] Y [ 2 ] > Y [ 1 ] ) return false ; return true ; } int getUnionPerimeter ( vector < int > X , vector < int > Y ) { int perimeter = 0 ; if ( ! doIntersect ( X , Y ) ) { perimeter += 2 * ( abs ( X [ 1 ] - X [ 0 ] ) + abs ( Y [ 1 ] - Y [ 0 ] ) ) ; perimeter += 2 * ( abs ( X [ 3 ] - X [ 2 ] ) + abs ( Y [ 3 ] - Y [ 2 ] ) ) ; } else { int w = * max_element ( X . begin ( ) , X . end ( ) ) - * min_element ( X . begin ( ) , X . end ( ) ) ; int l = * max_element ( Y . begin ( ) , Y . end ( ) ) - * min_element ( Y . begin ( ) , Y . end ( ) ) ; perimeter = 2 * ( l + w ) ; } return perimeter ; } int main ( ) { vector < int > X { -1 , 2 , 4 , 6 } ; vector < int > Y { 2 , 5 , 3 , 7 } ; cout << getUnionPerimeter ( X , Y ) ; } |
Check whether two straight lines are parallel or not | C ++ program for the above approach ; Function to check if two lines are parallel or not ; If slopes are equal ; Driver Code ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void parallel ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { if ( ( - ( a1 / b1 ) ) == ( - ( a2 / b2 ) ) ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { float a1 = -2 , b1 = 4 , c1 = 5 ; float a2 = -6 , b2 = 12 , c2 = 6 ; parallel ( a1 , b1 , c1 , a2 , b2 , c2 ) ; return 0 ; } |
Calculate area and height of an isosceles triangle whose sides are radii of a circle | C ++ program for the above approach ; Function to convert given angle from degree to radian ; Function to calculate height and area of the triangle OAB ; Stores the angle OAB and OBA ; Stores the angle in radians ; Stores the height ; Print height of the triangle ; Stores the base of triangle OAB ; Stores the area of the triangle ; Print the area of triangle OAB ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double Convert ( double degree ) { double pi = 3.14159265359 ; return ( degree * ( pi / 180 ) ) ; } void areaAndHeightOfTraingle ( double radius , double a ) { if ( a >= 180 a == 0 ) { cout << " Not β possible " ; return ; } double base_angle = ( 180 - a ) / 2 ; double radians = Convert ( base_angle ) ; double height = sin ( radians ) * radius ; cout << " Height β of β triangle β " << height << endl ; double base = cos ( radians ) * radius ; double area = base * height ; cout << " Area β of β triangle β " << area << endl ; } int main ( ) { double R = 5 , angle = 120 ; areaAndHeightOfTraingle ( R , angle ) ; return 0 ; } |
Radius of a circle having area equal to the sum of area of the circles having given radii | C ++ implementation of the above approach ; Function to calculate radius of the circle having area equal to sum of the area of two circles with given radii ; Area of first circle ; Area of second circle ; Area of third circle ; Radius of third circle ; Driver Code ; Given radius ; Prints the radius of the required circle | #include <bits/stdc++.h> NEW_LINE using namespace std ; double findRadius ( double r1 , double r2 ) { double a1 , a2 , a3 , r3 ; a1 = 3.14 * r1 * r1 ; a2 = 3.14 * r2 * r2 ; a3 = a1 + a2 ; r3 = sqrt ( a3 / 3.14 ) ; return r3 ; } int main ( ) { double r1 = 8 , r2 = 6 ; cout << findRadius ( r1 , r2 ) ; return 0 ; } |
Minimum number of Cuboids required to form a Cube | C ++ program for the above approach ; Function to calculate and return LCM of a , b , and c ; Find GCD of a and b ; Find LCM of a and b ; LCM ( a , b , c ) = LCM ( LCM ( a , b ) , c ) ; Finding LCM of a , b , c ; return LCM ( a , b , c ) ; Function to find the minimum number of cuboids required to make the volume of a valid cube ; Find the LCM of L , B , H ; Volume of the cube ; Volume of the cuboid ; Minimum number cuboids required to form a cube ; Driver Code ; Given dimensions of cuboid ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int find_lcm ( int a , int b , int c ) { int g = __gcd ( a , b ) ; int LCM1 = ( a * b ) / g ; g = __gcd ( LCM1 , c ) ; int LCM = ( LCM1 * c ) / g ; return LCM ; } void minimumCuboids ( int L , int B , int H ) { int lcm = find_lcm ( L , B , H ) ; int volume_cube = lcm * lcm * lcm ; int volume_cuboid = L * B * H ; cout << ( volume_cube / volume_cuboid ) ; } int main ( ) { int L = 1 , B = 1 , H = 2 ; minimumCuboids ( L , B , H ) ; return 0 ; } |
Check if given polygon is a convex polygon or not | C ++ program to implement the above approach ; Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int CrossProduct ( vector < vector < int > > & A ) { int X1 = ( A [ 1 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y1 = ( A [ 1 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; int X2 = ( A [ 2 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y2 = ( A [ 2 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; return ( X1 * Y2 - Y1 * X2 ) ; } bool isConvex ( vector < vector < int > > & points ) { int N = points . size ( ) ; int prev = 0 ; int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { vector < vector < int > > temp = { points [ i ] , points [ ( i + 1 ) % N ] , points [ ( i + 2 ) % N ] } ; curr = CrossProduct ( temp ) ; if ( curr != 0 ) { if ( curr * prev < 0 ) { return false ; } else { prev = curr ; } } } return true ; } int main ( ) { vector < vector < int > > points = { { 0 , 0 } , { 0 , 1 } , { 1 , 1 } , { 1 , 0 } } ; if ( isConvex ( points ) ) { cout << " Yes " << " STRNEWLINE " ; } else { cout << " No " << " STRNEWLINE " ; } } |
Program to find all possible triangles having same Area and Perimeter | C ++ program for the above approach ; Function to print sides of all the triangles having same perimeter & area ; Stores unique sides of triangles ; i + j + k values cannot exceed 256 ; Find the value of 2 * s ; Find the value of 2 * ( s - a ) ; Find the value of 2 * ( s - b ) ; Find the value of 2 * ( s - c ) ; If triplets have same area and perimeter ; Store sides of triangle ; Sort the triplets ; Inserting in set to avoid duplicate sides ; Print sides of all desired triangles ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void samePerimeterAndArea ( ) { set < vector < int > > se ; for ( int i = 1 ; i <= 256 ; ++ i ) { for ( int j = 1 ; j <= 256 ; ++ j ) { for ( int k = 1 ; k <= 256 ; ++ k ) { int peri = i + j + k ; int mul1 = - i + j + k ; int mul2 = i - j + k ; int mul3 = i + j - k ; if ( 16 * peri == mul1 * mul2 * mul3 ) { vector < int > v = { i , j , k } ; sort ( v . begin ( ) , v . end ( ) ) ; se . insert ( v ) ; } } } } for ( auto it : se ) { cout << it [ 0 ] << " β " << it [ 1 ] << " β " << it [ 2 ] << endl ; } } int main ( ) { samePerimeterAndArea ( ) ; return 0 ; } |
Count number of triangles cut by the given horizontal and vertical line segments | C ++ program for the above approach ; Store the minimum and maximum X and Y coordinates ; Function to convert string to int ; Function to print the number of triangles cut by each line segment ; Initialize Structure ; Find maximum and minimum X and Y coordinates for each triangle ; Minimum X ; Maximum X ; Minimum Y ; Maximum Y ; Traverse each cut from 0 to M - 1 ; Store number of triangles cut ; Extract value from the line segment string ; If cut is made on X - axis ; Check for each triangle if x lies b / w max and min X coordinates ; If cut is made on Y - axis ; Check for each triangle if y lies b / w max and min Y coordinates ; Print answer for ith cut ; Driver Code ; Given coordinates of triangles ; Given cuts of lines ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; struct Tri { int MinX , MaxX , MinY , MaxY ; } ; int StringtoInt ( string s ) { stringstream geek ( s ) ; int x ; geek >> x ; return x ; } int TriangleCuts ( vector < vector < int > > Triangle , string Cuts [ ] , int N , int M , int COL ) { Tri Minimized [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int x1 = Triangle [ i ] [ 0 ] ; int y1 = Triangle [ i ] [ 1 ] ; int x2 = Triangle [ i ] [ 2 ] ; int y2 = Triangle [ i ] [ 3 ] ; int x3 = Triangle [ i ] [ 4 ] ; int y3 = Triangle [ i ] [ 5 ] ; Minimized [ i ] . MinX = min ( { x1 , x2 , x3 } ) ; Minimized [ i ] . MaxX = max ( { x1 , x2 , x3 } ) ; Minimized [ i ] . MinY = min ( { y1 , y2 , y3 } ) ; Minimized [ i ] . MaxY = max ( { y1 , y2 , y3 } ) ; } for ( int i = 0 ; i < M ; i ++ ) { string Cut = Cuts [ i ] ; int CutCount = 0 ; int CutVal = StringtoInt ( Cut . substr ( 2 , Cut . size ( ) ) ) ; if ( Cut [ 0 ] == ' X ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinX ) < ( CutVal ) & & ( Minimized [ j ] . MaxX ) > ( CutVal ) ) { CutCount ++ ; } } } else if ( Cut [ 0 ] == ' Y ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinY ) < ( CutVal ) & & ( Minimized [ j ] . MaxY ) > ( CutVal ) ) { CutCount ++ ; } } } cout << CutCount << " β " ; } } int main ( ) { vector < vector < int > > Triangle = { { 0 , 2 , 2 , 9 , 8 , 5 } , { 5 , 0 , 6 , 3 , 7 , 0 } } ; int N = Triangle . size ( ) ; int COL = 6 ; string Cuts [ ] = { " X = 2" , " Y = 2" , " Y = 9" } ; int M = sizeof ( Cuts ) / sizeof ( Cuts [ 0 ] ) ; TriangleCuts ( Triangle , Cuts , N , M , COL ) ; return 0 ; } |
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points | C ++ program to implement the above approach ; Function to get the count of ractangles ; Store distinct horizontal lines ; Store distinct Vertical lines ; Insert horizontal line passing through 0 ; Insert vertical line passing through 0. ; Insert horizontal line passing through rectangle [ 3 ] [ 0 ] ; Insert vertical line passing through rectangle [ 3 ] [ 1 ] ; Insert all horizontal and vertical lines passing through the given array ; Insert all horizontal lines ; Insert all vertical lines ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int cntRect ( int points [ ] [ 2 ] , int N , int rectangle [ ] [ 2 ] ) { unordered_set < int > cntHor ; unordered_set < int > cntVer ; cntHor . insert ( 0 ) ; cntVer . insert ( 0 ) ; cntHor . insert ( rectangle [ 3 ] [ 0 ] ) ; cntVer . insert ( rectangle [ 3 ] [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { cntHor . insert ( points [ i ] [ 0 ] ) ; cntVer . insert ( points [ i ] [ 1 ] ) ; } return ( cntHor . size ( ) - 1 ) * ( cntVer . size ( ) - 1 ) ; } int main ( ) { int rectangle [ ] [ 2 ] = { { 0 , 0 } , { 0 , 5 } , { 5 , 0 } , { 5 , 5 } } ; int points [ ] [ 2 ] = { { 1 , 2 } , { 3 , 4 } } ; int N = sizeof ( points ) / sizeof ( points [ 0 ] ) ; cout << cntRect ( points , N , rectangle ) ; } |
Count squares possible from M and N straight lines parallel to X and Y axis respectively | C ++ program for the above approach ; Function to count all the possible squares with given lines parallel to both the X and Y axis ; Stores the count of all possible distances in X [ ] & Y [ ] respectively ; Find distance between all pairs in the array X [ ] ; Add the count to m1 ; Find distance between all pairs in the array Y [ ] ; Add the count to m2 ; Find sum of m1 [ i ] * m2 [ i ] for same distance ; Find current count in m2 ; Add to the total count ; Return the final count ; Driver Code ; Given lines ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfSquares ( int X [ ] , int Y [ ] , int N , int M ) { unordered_map < int , int > m1 , m2 ; int i , j , ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { int dist = abs ( X [ i ] - X [ j ] ) ; m1 [ dist ] ++ ; } } for ( i = 0 ; i < M ; i ++ ) { for ( j = i + 1 ; j < M ; j ++ ) { int dist = abs ( Y [ i ] - Y [ j ] ) ; m2 [ dist ] ++ ; } } for ( auto i = m1 . begin ( ) ; i != m1 . end ( ) ; i ++ ) { if ( m2 . find ( i -> first ) != m2 . end ( ) ) { ans += ( i -> second * m2 [ i -> first ] ) ; } } return ans ; } int main ( ) { int X [ ] = { 1 , 3 , 7 } ; int Y [ ] = { 2 , 4 , 6 , 1 } ; int N = sizeof ( X ) / sizeof ( X [ 0 ] ) ; int M = sizeof ( Y ) / sizeof ( Y [ 0 ] ) ; cout << numberOfSquares ( X , Y , N , M ) ; return 0 ; } |
Program to calculate area of a parallelogram | C ++ program for the above approach ; Function to return the area of parallelogram using sides and angle at the intersection of diagonal ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using sides and angle at the intersection of sides ; Calculate area of parallelogram ; Return the answer ; Function to return the area of parallelogram using diagonals and angle at the intersection of diagonals ; Calculate area of parallelogram ; Return the answer ; Driver Code ; Given diagonal and angle ; Function call ; Print the area | #include <bits/stdc++.h> NEW_LINE using namespace std ; double toRadians ( int degree ) { double pi = 3.14159265359 ; return ( ( double ) degree * ( pi / 180 ) ) ; } double Area_Parallelogram1 ( int a , int b , int theta ) { double area = ( abs ( tan ( toRadians ( theta ) ) ) / 2 ) * abs ( a * a - b * b ) ; return area ; } double Area_Parallelogram2 ( int a , int b , int gamma ) { double area = ( abs ( sin ( toRadians ( gamma ) ) ) ) * abs ( a * b ) ; return area ; } static double Area_Parallelogram3 ( int d1 , int d2 , int theta ) { double area = ( abs ( sin ( toRadians ( theta ) ) ) / 2 ) * abs ( d1 * d2 ) ; return area ; } int main ( ) { int d1 = 3 ; int d2 = 5 ; int theta = 90 ; double area = Area_Parallelogram3 ( d1 , d2 , theta ) ; printf ( " % .2f " , area ) ; } |
Count squares of size K inscribed in a square of size N | C ++ implementation of the above approach ; Function to calculate the number of squares of size K in a square of size N ; Stores the number of squares ; Driver Code ; Size of the bigger square ; Size of smaller square | #include <iostream> NEW_LINE using namespace std ; int No_of_squares ( int N , int K ) { int no_of_squares = 0 ; no_of_squares = ( N - K + 1 ) * ( N - K + 1 ) ; return no_of_squares ; } int main ( ) { int N = 5 ; int K = 3 ; cout << No_of_squares ( N , K ) ; return 0 ; } |
Count cubes of size K inscribed in a cube of size N | C ++ implementation of the above approach ; Function to find the number of the cubes of the size K ; Stores the number of cubes ; Stores the number of cubes of size k ; Driver Code ; Size of the bigger cube ; Size of the smaller cube | #include <bits/stdc++.h> NEW_LINE using namespace std ; int No_of_cubes ( int N , int K ) { int No = 0 ; No = ( N - K + 1 ) ; No = pow ( No , 3 ) ; return No ; } int main ( ) { int N = 5 ; int K = 2 ; cout << No_of_cubes ( N , K ) ; return 0 ; } |
Count of smaller rectangles that can be placed inside a bigger rectangle | C ++ program for the above approach ; Function to count smaller rectangles within the larger rectangle ; If the dimension of the smaller rectangle is greater than the bigger one ; Return the number of smaller rectangles possible ; Driver Code ; Dimension of bigger rectangle ; Dimension of smaller rectangle ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int No_of_rectangles ( int L , int B , int l , int b ) { if ( ( l > L ) || ( b > B ) ) { return -1 ; } else { return ( L - l + 1 ) * ( B - b + 1 ) ; } } int main ( ) { int L = 5 , B = 3 ; int l = 4 , b = 1 ; cout << No_of_rectangles ( L , B , l , b ) ; return 0 ; } |
Program To Check whether a Triangle is Equilateral , Isosceles or Scalene | C ++ program for the above approach ; Function to check if the triangle is equilateral or isosceles or scalene ; Check for equilateral triangle ; Check for isosceles triangle ; Otherwise scalene triangle ; Driver Code ; Given sides of triangle ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void checkTriangle ( int x , int y , int z ) { if ( x == y && y == z ) cout << " Equilateral β Triangle " ; else if ( x == y y == z z == x ) cout << " Isosceles β Triangle " ; else cout << " Scalene β Triangle " ; } int main ( ) { int x = 8 , y = 7 , z = 9 ; checkTriangle ( x , y , z ) ; } |
Find the remaining vertices of a square from two given vertices | C ++ program for the above approach ; Function to find the remaining vertices of a square ; Check if the x - coordinates are equal ; Check if the y - coordinates are equal ; If the the given coordinates forms a diagonal of the square ; Otherwise ; Square does not exist ; Driver Code ; Given two vertices | #include <cstdlib> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void findVertices ( int x1 , int y1 , int x2 , int y2 ) { if ( x1 == x2 ) { cout << ( x1 + y2 - y1 ) << " , β " << y1 << endl ; cout << ( x2 + y2 - y1 ) << " , β " << y2 ; } else if ( y1 == y2 ) { cout << x1 << " , β " << ( y1 + x2 - x1 ) << endl ; cout << x2 << " , β " << ( y2 + x2 - x1 ) ; } else if ( abs ( x2 - x1 ) == abs ( y2 - y1 ) ) { cout << x1 << " , β " << y2 << endl ; cout << x2 << " , β " << y1 ; } else cout << " - 1" ; } int main ( ) { int x1 = 1 , y1 = 2 ; int x2 = 3 , y2 = 4 ; findVertices ( x1 , y1 , x2 , y2 ) ; return 0 ; } |
Find the area of rhombus from given Angle and Side length | C ++ Program to calculate area of rhombus from given angle and side length ; Function to return the area of rhombus using one angle and side . ; Driver Code ; Function Call ; Print the final answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define RADIAN 0.01745329252 NEW_LINE float Area_of_Rhombus ( int a , int theta ) { float area = ( a * a ) * sin ( ( RADIAN * theta ) ) ; return area ; } int main ( ) { int a = 4 ; int theta = 60 ; float ans = Area_of_Rhombus ( a , theta ) ; printf ( " % 0.2f " , ans ) ; return 0 ; } |
Count of Equilateral Triangles of unit length possible from a given Hexagon | C ++ program to implement the above approach ; Function to calculate the the number of Triangles possible ; Driver Code ; Regular Hexagon ; Irregular Hexagon | #include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateTriangles ( int sides [ ] ) { double count = pow ( sides [ 0 ] + sides [ 1 ] + sides [ 2 ] , 2 ) ; count -= pow ( sides [ 0 ] , 2 ) ; count -= pow ( sides [ 2 ] , 2 ) ; count -= pow ( sides [ 4 ] , 2 ) ; return ( int ) ( count ) ; } int main ( ) { int sides [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; cout << ( calculateTriangles ( sides ) ) << endl ; int sides1 [ ] = { 2 , 2 , 1 , 3 , 1 , 2 } ; cout << ( calculateTriangles ( sides1 ) ) << endl ; return 0 ; } |
Length of diagonal of a parallelogram using adjacent sides and angle between them | C ++ program to find length Of diagonal of a parallelogram Using sides and angle between them . ; Function to return the length Of diagonal of a parallelogram using sides and angle between them . ; Driver Code ; Given sides ; Given angle ; Function call ; Print the final answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI 3.147 NEW_LINE double Length_Diagonal ( int a , int b , double theta ) { double diagonal = sqrt ( ( pow ( a , 2 ) + pow ( b , 2 ) ) - 2 * a * b * cos ( theta * ( PI / 180 ) ) ) ; return diagonal ; } int main ( ) { int a = 3 ; int b = 5 ; double theta = 45 ; double ans = Length_Diagonal ( a , b , theta ) ; printf ( " % .2f " , ans ) ; } |
Maximum number of tiles required to cover the floor of given size using 2 x1 size tiles | C ++ program for the above approach ; Function to find the maximum number of tiles required to cover the floor of size m x n using 2 x 1 size tiles ; Print the answer ; Driver Code ; Given M and N ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumTiles ( int n , int m ) { cout << ( m * n ) / 2 << endl ; } int main ( ) { int M = 3 ; int N = 4 ; maximumTiles ( N , M ) ; return 0 ; } |
Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal | C ++ Program to implement the above approach ; Function to calculate the length of the diagonal of a parallelogram using two sides and other diagonal ; Driver Code ; Function Call ; Print the final answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; float Length_Diagonal ( int a , int b , int d ) { float diagonal = sqrt ( 2 * ( ( a * a ) + ( b * b ) ) - ( d * d ) ) ; return diagonal ; } int main ( ) { int A = 10 ; int B = 30 ; int D = 20 ; float ans = Length_Diagonal ( A , B , D ) ; printf ( " % 0.1f " , ans ) ; return 0 ; } |
Length of Diagonals of a Cyclic Quadrilateral using the length of Sides . | C ++ Program to implement the above approach ; Function to calculate the length of diagonals of a cyclic quadrilateral ; Driver Code ; Function Call ; Print the final answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; vector < float > Diagonals ( int a , int b , int c , int d ) { vector < float > ans ; ans . push_back ( sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( a * b ) + ( c * d ) ) ) ) ; ans . push_back ( sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * b ) + ( c * d ) ) / ( ( a * d ) + ( b * c ) ) ) ) ; return ans ; } int main ( ) { int A = 10 ; int B = 15 ; int C = 20 ; int D = 25 ; vector < float > ans = Diagonals ( A , B , C , D ) ; printf ( " % .2f β % .2f " , ( ans [ 0 ] ) + .01 , ans [ 1 ] + .01 ) ; } |
Minimum Sum of Euclidean Distances to all given Points | C ++ Program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points | #include <bits/stdc++.h> NEW_LINE using namespace std ; double find ( double x , double y , vector < vector < int > > & p ) { double mind = 0 ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) { double a = p [ i ] [ 0 ] , b = p [ i ] [ 1 ] ; mind += sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } double getMinDistSum ( vector < vector < int > > & p ) { double x = 0 , y = 0 ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) { x += p [ i ] [ 0 ] ; y += p [ i ] [ 1 ] ; } x = x / p . size ( ) ; y = y / p . size ( ) ; double mind = find ( x , y , p ) ; return mind ; } int main ( ) { vector < vector < int > > vec = { { 0 , 1 } , { 1 , 0 } , { 1 , 2 } , { 2 , 1 } } ; double d = getMinDistSum ( vec ) ; cout << d << endl ; return 0 ; } |
Coplanarity of Two Lines in 3D Geometry | C ++ program implement the above approach ; Function to generate determinant ; Return the sum ; Driver Code ; Position vector of first line ; Direction ratios of line to which first line is parallel ; Position vectors of second line ; Direction ratios of line to which second line is parallel ; Determinant to check coplanarity ; If determinant is zero ; Otherwise | #include <iostream> NEW_LINE using namespace std ; int det ( int d [ ] [ 3 ] ) { int Sum = d [ 0 ] [ 0 ] * ( ( d [ 1 ] [ 1 ] * d [ 2 ] [ 2 ] ) - ( d [ 2 ] [ 1 ] * d [ 1 ] [ 2 ] ) ) ; Sum -= d [ 0 ] [ 1 ] * ( ( d [ 1 ] [ 0 ] * d [ 2 ] [ 2 ] ) - ( d [ 1 ] [ 2 ] * d [ 2 ] [ 0 ] ) ) ; Sum += d [ 0 ] [ 2 ] * ( ( d [ 0 ] [ 1 ] * d [ 1 ] [ 2 ] ) - ( d [ 0 ] [ 2 ] * d [ 1 ] [ 1 ] ) ) ; return Sum ; } int main ( ) { int x1 = -3 , y1 = 1 , z1 = 5 ; int a1 = -3 , b1 = 1 , c1 = 5 ; int x2 = -1 , y2 = 2 , z2 = 5 ; int a2 = -1 , b2 = 2 , c2 = 5 ; int det_list [ 3 ] [ 3 ] = { { x2 - x1 , y2 - y1 , z2 - z1 } , { a1 , b1 , c1 } , { a2 , b2 , c2 } } ; if ( det ( det_list ) == 0 ) { cout << " Lines β are β coplanar " << endl ; } else { cout << " Lines β are β non β coplanar " << endl ; } return 0 ; } |
Distance between Incenter and Circumcenter of a triangle using Inradius and Circumradius | C ++ 14 program for the above approach ; Function returns the required distance ; Driver code ; Length of Inradius ; Length of Circumradius | #include <bits/stdc++.h> NEW_LINE using namespace std ; double distance ( int r , int R ) { double d = sqrt ( pow ( R , 2 ) - ( 2 * r * R ) ) ; return d ; } int main ( ) { int r = 2 ; int R = 5 ; cout << ( round ( distance ( r , R ) * 100.0 ) / 100.0 ) ; } |
Find N random points within a Circle | C ++ program for the above approach ; Return a random double between 0 & 1 ; Function to find the N random points on the given circle ; Result vector ; Get Angle in radians ; Get length from center ; Add point to results . ; Return the N points ; Function to display the content of the vector A ; Iterate over A ; Print the N random points stored ; Driver Code ; Given dimensions ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define PI 3.141592653589 NEW_LINE double uniform ( ) { return ( double ) rand ( ) / RAND_MAX ; } vector < pair < double , double > > randPoint ( int r , int x , int y , int n ) { vector < pair < double , double > > res ; for ( int i = 0 ; i < n ; i ++ ) { double theta = 2 * PI * uniform ( ) ; double len = sqrt ( uniform ( ) ) * r ; res . push_back ( { x + len * cos ( theta ) , y + len * sin ( theta ) } ) ; } return res ; } void printVector ( vector < pair < double , double > > A ) { for ( pair < double , double > P : A ) { printf ( " ( % . 2lf , β % .2lf ) STRNEWLINE " , P . first , P . second ) ; } } int main ( ) { int R = 12 ; int X = 3 ; int Y = 3 ; int N = 5 ; printVector ( randPoint ( R , X , Y , N ) ) ; return 0 ; } |
Check if an N | C ++ program for the above approach ; Function to check if the polygon exists or not ; Initialize a variable to store the sum of angles ; Loop through the array and calculate the sum of angles ; Check the condition for an N - side polygon ; Driver Code ; Given array arr [ ] ; Function Call | #include <iostream> NEW_LINE using namespace std ; void checkValidPolygon ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 180 * ( N - 2 ) ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int N = 3 ; int arr [ ] = { 60 , 60 , 60 } ; checkValidPolygon ( arr , N ) ; return 0 ; } |
Find side of Square which makes minimal area to fit two identical rectangles inside it | C ++ program of the above approach ; if ' a ' and ' b ' same then double ' a ' or ' b ' and return ( 2 * a ) or ( 2 * b ) ; check if a != b ; if a > b ; double the smaller value that is ' b ' and store it to ' newB ' ; find the difference of ' newB β and β ' a ' ; if ' newB ' < a ; then add the difference of ' newB ' and ' a ' to the ' b ' to make ' b ' and ' a ' as same ; return side of the square a or b ; if ' newB ' > a then then add the difference of ' newB ' and ' a ' to the ' a ' to make ' a ' and ' newB ' as same ; return side of the square a or newB ; if a < b ; double the smaller value that is ' a ' and store it to ' newA ' ; find the difference of ' newA β and β ' b ' ; if ' newA ' < b ; then add the difference of ' newA ' and ' b ' to the ' a ' to make ' a ' and ' b ' as same ; return side of the square a or b ; if ' newA ' > b then then add the difference of ' newA ' and ' b ' to the ' b ' to make ' b ' and ' newA ' as same ; return side of the square b or newA ; Drive Code ; Size of rectangle | #include <bits/stdc++.h> NEW_LINE using namespace std ; int minimalSquareSide ( int a , int b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { int newB = b + b ; int diff = abs ( newB - a ) ; if ( newB < a ) { b = newB + diff ; if ( a == b ) return a ; return 0 ; } else { a = a + diff ; if ( a == newB ) return a ; return 0 ; } } else { int newA = a + a ; int diff = abs ( newA - b ) ; if ( newA < b ) { a = diff + newA ; if ( a == b ) return a ; return 0 ; } else { b = b + diff ; if ( b == newA ) return b ; return 0 ; } } } } int main ( ) { int H , W ; H = 3 , W = 1 ; cout << minimalSquareSide ( H , W ) << endl ; return 0 ; } |
Find the angle of Rotational Symmetry of an N | C ++ program to find the angle of Rotational Symmetry of an N - sided regular polygon ; function to find required minimum angle of rotation ; Store the answer in a double variable ; Calculating the angle of rotation and type - casting the integer N to double type ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double minAnglRot ( int N ) { double res ; res = 360 / ( double ) N ; return res ; } int main ( ) { int N = 4 ; cout << " Angle β of β Rotational β Symmetry : β " << minAnglRot ( N ) ; return 0 ; } |
Area of a Triangle from the given lengths of medians | C ++ 14 program to calculate area of a triangle from the given lengths of medians ; Function to return the area of triangle using medians ; Driver Code ; Function call ; Print the final answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; double Area_of_Triangle ( int a , int b , int c ) { int s = ( a + b + c ) / 2 ; int x = s * ( s - a ) ; x = x * ( s - b ) ; x = x * ( s - c ) ; double area = ( 4 / ( double ) 3 ) * sqrt ( x ) ; return area ; } int main ( ) { int a = 9 ; int b = 12 ; int c = 15 ; double ans = Area_of_Triangle ( a , b , c ) ; cout << ans ; } |
Total number of unit cells covered by all given Rectangles | C ++ program to find the number of cells enclosed by the given rectangles ; Update the coordinates lying within the rectangle ; Update arr [ i ] [ j ] for all ( i , j ) lying within the rectangle ; Function to return the total area covered by rectangles ; Stores the number of cells ; arr [ i ] ] [ [ j ] == 1 means that grid is filled by some rectangle ; Driver Code ; ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) denotes the coordinate of the bottom left of the rectangle ( A [ i ] [ 2 ] , A [ i ] [ 3 ] ) denotes the coordinate of upper right of the rectangle ; Update the coordinates that lie within the rectangle | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX 1001 NEW_LINE bool arr [ MAX ] [ MAX ] ; void updateArray ( int x1 , int y1 , int x2 , int y2 ) { for ( int i = x1 ; i < x2 ; i ++ ) { for ( int j = y1 ; j < y2 ; j ++ ) { arr [ i ] [ j ] = true ; } } } int findAreaCovered ( ) { int area = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { if ( arr [ i ] [ j ] == true ) { area ++ ; } } } return area ; } int main ( ) { int N = 3 ; vector < vector < int > > A = { { 1 , 3 , 4 , 5 } , { 3 , 1 , 7 , 4 } , { 5 , 3 , 8 , 6 } } ; for ( int i = 0 ; i < N ; i ++ ) { updateArray ( A [ i ] [ 0 ] , A [ i ] [ 1 ] , A [ i ] [ 2 ] , A [ i ] [ 3 ] ) ; } int area = findAreaCovered ( ) ; cout << area ; return 0 ; } |
Find the equation of plane which passes through two points and parallel to a given axis | C ++ implementation to find the equation of plane which passes through two points and parallel to a given axis ; Find direction vector of points ( x1 , y1 , z1 ) and ( x2 , y2 , z2 ) ; Values that are calculated and simplified from the cross product ; Print the equation of plane ; Driver Code ; Point A ; Point B ; Given axis ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int d , int e , int f ) { double a = x2 - x1 ; double b = y2 - y1 ; double c = z2 - z1 ; int A = ( b * f - c * e ) ; int B = ( a * f - c * d ) ; int C = ( a * e - b * d ) ; int D = - ( A * d - B * e + C * f ) ; cout << A << " x β + β " << B << " y β + β " << C << " z β + β " << D << " = β 0" ; } int main ( ) { int x1 = 2 , y1 = 3 , z1 = 5 ; int x2 = 6 , y2 = 7 , z2 = 8 ; int a = 11 , b = 23 , c = 10 ; findEquation ( x1 , y1 , z1 , x2 , y2 , z2 , a , b , c ) ; return 0 ; } |
Find the length of the median of a Triangle if length of sides are given | C ++ program to find the length of the median using sides of the triangle ; Function to return the length of the median using sides of triangle ; Driver code ; Function call ; Print final answer with 2 digits after decimal | #include <bits/stdc++.h> NEW_LINE using namespace std ; float median ( int a , int b , int c ) { float n = sqrt ( 2 * b * b + 2 * c * c - a * a ) / 2 ; return n ; } int main ( ) { int a , b , c ; a = 4 ; b = 3 ; c = 5 ; float ans = median ( a , b , c ) ; cout << fixed << setprecision ( 2 ) << ans ; return 0 ; } |
Number of quadrilateral formed with N distinct points on circumference of Circle | C ++ implementation to find the number of quadrilaterals formed with N distinct points ; Function to find the factorial of the given number N ; Loop to find the factorial of the given number ; Function to find the number of combinations in the N ; Driver Code ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i < n + 1 ; i ++ ) res = res * i ; return res ; } int nCr ( int n , int r ) { return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) ; } int main ( ) { int n = 5 ; cout << ( nCr ( n , 4 ) ) ; } |
Length of remaining two sides of a Triangle from a given side and its adjacent angles | C ++ program for above approach ; Function for computing other 2 side of the trianlgle ; Computing angle C ; Converting A in to radian ; Converting B in to radian ; Converting C in to radian ; Computing length of side b ; Computing length of side c ; Driver code ; Calling function | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findSide ( float a , float B , float C ) { float A = 180 - C - B ; float radA = M_PI * ( A / 180 ) ; float radB = M_PI * ( B / 180 ) ; float radC = M_PI * ( C / 180 ) ; float b = a / sin ( radA ) * sin ( radB ) ; float c = a / sin ( radA ) * sin ( radC ) ; cout << fixed << setprecision ( 15 ) << b << " β " ; cout << fixed << setprecision ( 15 ) << c ; } int main ( ) { int a = 12 , B = 60 , C = 30 ; findSide ( a , B , C ) ; } |
Minimum area of square holding two identical rectangles | C ++ program for the above problem ; Function to find the area of the square ; Larger side of rectangle ; Smaller side of the rectangle ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int areaSquare ( int L , int B ) { int large = max ( L , B ) ; int small = min ( L , B ) ; if ( large >= 2 * small ) return large * large ; else return ( 2 * small ) * ( 2 * small ) ; } int main ( ) { int L = 7 ; int B = 4 ; cout << areaSquare ( L , B ) ; return 0 ; } |
Minimum side of square embedded in Regular polygon with N sides | C ++ program to find the minimum side of the square in which a regular polygon with even sides can completely embed ; PI value in C ++ using acos function ; Function to find the minimum side of the square in which a regular polygon with even sides can completely embed ; Projection angle variation from axes ; Projection angle variation when the number of sides are in multiple of 4 ; Distance between the end points ; Projection from all N points on X - axis ; Projection from all N points on Y - axis ; Maximum side ; Return the portion of side forming the square ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; const double pi = acos ( -1.0 ) ; double nGon ( int N ) { double proAngleVar ; if ( N % 4 == 0 ) { proAngleVar = pi * ( 180.0 / N ) / 180 ; } else { proAngleVar = pi * ( 180.0 / ( 2 * N ) ) / 180 ; } double negX = 1.0e+99 , posX = -1.0e+99 , negY = 1.0e+99 , posY = -1.0e+99 ; for ( int j = 0 ; j < N ; ++ j ) { double px = cos ( 2 * pi * j / N + proAngleVar ) ; double py = sin ( 2 * pi * j / N + proAngleVar ) ; negX = min ( negX , px ) ; posX = max ( posX , px ) ; negY = min ( negY , py ) ; posY = max ( posY , py ) ; } double opt2 = max ( posX - negX , posY - negY ) ; return ( double ) opt2 / sin ( pi / N ) / 2 ; } int main ( ) { int N = 10 ; cout << nGon ( N ) ; return 0 ; } |
Heptacontagon Number | C ++ program for above approach ; Finding the nth heptacontagon number ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int heptacontagonNum ( int n ) { return ( 68 * n * n - 66 * n ) / 2 ; } int main ( ) { int N = 3 ; cout << "3rd β heptacontagon β Number β is β = β " << heptacontagonNum ( N ) ; return 0 ; } |
Program to check if N is a Icositetragonal number | C ++ implementation to check that a number is icositetragonal number or not ; Function to check that the number is a icositetragonal number ; Condition to check if the number is a icositetragonal number ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isicositetragonal ( int N ) { float n = ( 10 + sqrt ( 44 * N + 100 ) ) / 22 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 24 ; if ( isicositetragonal ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Program to check if N is a Tridecagonal Number or not | C ++ implementation to check that a number is a Tridecagon number or not ; Function to check that the number is a Tridecagon number ; Condition to check if the number is a Tridecagon number ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isTridecagon ( int N ) { float n = ( 9 + sqrt ( 88 * N + 81 ) ) / 22 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 13 ; if ( isTridecagon ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Program to check if N is a Icosihenagonal number | C ++ implementation to check that a number is icosihenagonal number or not ; Function to check that the number is a icosihenagonal number ; Condition to check if the number is a icosihenagonal number ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isicosihenagonal ( int N ) { float n = ( 17 + sqrt ( 152 * N + 289 ) ) / 38 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 21 ; if ( isicosihenagonal ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Program to check if N is a Icositrigonal number | C ++ implementation to check that a number is a icositrigonal number or not ; Function to check that the number is a icositrigonal number ; Condition to check if the number is a icositrigonal number ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isicositrigonal ( int N ) { float n = ( 19 + sqrt ( 168 * N + 361 ) ) / 42 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 23 ; if ( isicositrigonal ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Number of ways to choose K intersecting line segments on X | C ++ program to find Number of ways to choose K intersecting line segments on X - axis ; Function to find ( a ^ b ) % mod in log b ; Till power becomes 0 ; If power is odd ; Multiply base ; Divide power by 1 ; Function to find nCk ; Base case ; Apply formula to find nCk ; Function to find the number of ways ; sort the given lines ; Find the number of total case ; Declare a multiset ; loop till N ; Check if smallest element is smaller than lines [ i ] ; Erase first element ; Exclude the odd cases ; Modulus operation ; Insert into multiset ; Function to precompute factorial and inverse ; Pre - compute factorial and inverse ; Driver code ; Function to pre - compute factorial and inverse | #include <bits/stdc++.h> NEW_LINE using namespace std ; const long long mod = 1000000007 ; const int MAXN = 1001 ; long long factorial [ MAXN ] , inverse [ MAXN ] ; long long power ( long long a , long long b ) { long long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) { res = ( res * a ) % mod ; } a = ( a * a ) % mod ; b >>= 1 ; } return res ; } long long nCk ( int n , int k ) { if ( k < 0 k > n ) { return 0 ; } long long ans = factorial [ n ] ; ans = ( ans * inverse [ n - k ] ) % mod ; ans = ( ans * inverse [ k ] ) % mod ; return ans ; } void numberOfWays ( vector < pair < int , int > > lines , int K , int N ) { sort ( lines . begin ( ) , lines . end ( ) ) ; long long total_case = nCk ( N , K ) ; multiset < int > m ; for ( int i = 0 ; i < N ; i ++ ) { while ( ! m . empty ( ) && ( * m . begin ( ) < lines [ i ] . first ) ) { m . erase ( m . begin ( ) ) ; } total_case -= nCk ( m . size ( ) , K - 1 ) ; total_case += mod ; total_case %= mod ; m . insert ( lines [ i ] . second ) ; } cout << total_case << endl ; } void preCompute ( ) { long long fact = 1 ; factorial [ 0 ] = 1 ; inverse [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) { fact = ( fact * i ) % mod ; factorial [ i ] = fact ; inverse [ i ] = power ( factorial [ i ] , mod - 2 ) ; } } int main ( ) { int N = 3 , K = 2 ; vector < pair < int , int > > lines ; preCompute ( ) ; lines . push_back ( { 1 , 3 } ) ; lines . push_back ( { 4 , 5 } ) ; lines . push_back ( { 5 , 7 } ) ; numberOfWays ( lines , K , N ) ; return 0 ; } |
Check whether triangle is valid or not if three points are given | C ++ implementation to check if three points form a triangle ; Function to check if three points make a triangle ; Calculation the area of triangle . We have skipped multiplication with 0.5 to avoid floating point computations ; Condition to check if area is not equal to 0 ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void checkTriangle ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ; if ( a == 0 ) cout << " No " ; else cout << " Yes " ; } int main ( ) { int x1 = 1 , x2 = 2 , x3 = 3 , y1 = 1 , y2 = 2 , y3 = 3 ; checkTriangle ( x1 , y1 , x2 , y2 , x3 , y3 ) ; return 0 ; } |
Total number of triplets ( A , B , C ) in which the points B and C are Equidistant to A | C ++ implementation to Find the total number of Triplets in which the points are Equidistant ; function to count such triplets ; Iterate over all the points ; Iterate over all points other than the current point ; Compute squared euclidean distance for the current point ; Compute nP2 that is n * ( n - 1 ) ; Return the final result ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int numTrip ( vector < pair < int , int > > & points ) { int res = 0 ; for ( int i = 0 ; i < points . size ( ) ; ++ i ) { unordered_map < long , int > map ( points . size ( ) ) ; for ( int j = 0 ; j < points . size ( ) ; ++ j ) { if ( j == i ) continue ; int dy = points [ i ] . second - points [ j ] . second ; int dx = points [ i ] . first - points [ j ] . first ; int key = dy * dy ; key += dx * dx ; map [ key ] ++ ; } for ( auto & p : map ) res += p . second * ( p . second - 1 ) ; } return res ; } int main ( ) { vector < pair < int , int > > mat = { { 0 , 0 } , { 1 , 0 } , { 2 , 0 } } ; cout << numTrip ( mat ) ; return 0 ; } |
Check if any point overlaps the given Circle and Rectangle | C ++ implementation to check if any point overlaps the given circle and rectangle ; Function to check if any point overlaps the given circle and rectangle ; Find the nearest point on the rectangle to the center of the circle ; Find the distance between the nearest point and the center of the circle Distance between 2 points , ( x1 , y1 ) & ( x2 , y2 ) in 2D Euclidean space is ( ( x1 - x2 ) * * 2 + ( y1 - y2 ) * * 2 ) * * 0.5 ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkOverlap ( int R , int Xc , int Yc , int X1 , int Y1 , int X2 , int Y2 ) { int Xn = max ( X1 , min ( Xc , X2 ) ) ; int Yn = max ( Y1 , min ( Yc , Y2 ) ) ; int Dx = Xn - Xc ; int Dy = Yn - Yc ; return ( Dx * Dx + Dy * Dy ) <= R * R ; } int main ( ) { int R = 1 ; int Xc = 0 , Yc = 0 ; int X1 = 1 , Y1 = -1 ; int X2 = 3 , Y2 = 1 ; if ( checkOverlap ( R , Xc , Yc , X1 , Y1 , X2 , Y2 ) ) { cout << " True " << endl ; } else { cout << " False " ; } } |
Check if the tower of sight issue occurs or not | C ++ program to check if tower of sight issue occurs or not ; Function to check if point p lies in between the line joining p1 and p2 ; If parallel to X - axis ; Point p lies between p1 and p2 ; If parallel to Y - axis ; Point p lies between p1 and p2 ; If point p satisfies the equation of line joining p1 and p2 ; Function to check if tower of sight issue occurred ; B lies between AC ; D lies between AC ; A lies between BD ; C lies between BD ; Driver code ; Point A ; Point B ; Point C ; Point D | #include <bits/stdc++.h> NEW_LINE using namespace std ; int checkIntersection ( pair < int , int > p1 , pair < int , int > p2 , pair < int , int > p ) { int val ; if ( p1 . second == p2 . second && p1 . second == p . second ) { if ( p . first <= max ( p1 . first , p2 . first ) && ( p . first >= min ( p1 . first , p2 . first ) ) ) return 1 ; } if ( p1 . first == p2 . first && p1 . first == p . first ) { if ( p . second <= max ( p1 . second , p2 . second ) && ( p . second >= min ( p1 . second , p2 . second ) ) ) return 1 ; } else { val = ( p . second - p1 . second ) * ( p2 . first - p1 . first ) - ( p . first - p1 . first ) * ( p2 . second - p1 . second ) ; if ( val == 0 ) if ( ( p . first <= max ( p1 . first , p2 . first ) && ( p . first >= min ( p1 . first , p2 . first ) ) ) && ( p . second <= max ( p1 . second , p2 . second ) && ( p . second >= min ( p1 . second , p2 . second ) ) ) ) return 1 ; } return 0 ; } void towerOfSight ( pair < int , int > a , pair < int , int > b , pair < int , int > c , pair < int , int > d ) { int flag = 0 ; if ( checkIntersection ( a , c , b ) ) flag = 1 ; else if ( checkIntersection ( a , c , d ) ) flag = 1 ; else if ( checkIntersection ( b , d , a ) ) flag = 1 ; else if ( checkIntersection ( b , d , c ) ) flag = 1 ; flag ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; } int main ( ) { pair < int , int > a = { 0 , 0 } ; pair < int , int > b = { 0 , -2 } ; pair < int , int > c = { 2 , 0 } ; pair < int , int > d = { 0 , 2 } ; towerOfSight ( a , b , c , d ) ; return 0 ; } |
Number of lines from given N points not parallel to X or Y axis | C ++ program to find the number of lines which are formed from given N points and not parallel to X or Y axis ; Function to find the number of lines which are formed from given N points and not parallel to X or Y axis ; This will store the number of points has same x or y coordinates using the map as the value of coordinate can be very large ; Counting frequency of each x and y coordinates ; Total number of pairs can be formed ; We can not choose pairs from these as they have same x coordinatethus they will result line segment parallel to y axis ; we can not choose pairs from these as they have same y coordinate thus they will result line segment parallel to x - axis ; Return the required answer ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int NotParallel ( int p [ ] [ 2 ] , int n ) { map < int , int > x_axis , y_axis ; for ( int i = 0 ; i < n ; i ++ ) { x_axis [ p [ i ] [ 0 ] ] ++ ; y_axis [ p [ i ] [ 1 ] ] ++ ; } int total = ( n * ( n - 1 ) ) / 2 ; for ( auto i : x_axis ) { int c = i . second ; total -= ( c * ( c - 1 ) ) / 2 ; } for ( auto i : y_axis ) { int c = i . second ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } int main ( ) { int p [ ] [ 2 ] = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = sizeof ( p ) / sizeof ( p [ 0 ] ) ; cout << NotParallel ( p , n ) ; return 0 ; } |
Check whether two convex regular polygon have same center or not | C ++ implementation to check whether two convex polygons have same center ; Function to check whether two convex polygons have the same center or not ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int n , int m ) { if ( m % n == 0 ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; } int main ( ) { int n = 5 ; int m = 10 ; check ( n , m ) ; return 0 ; } |
Modulus of a Complex Number | C ++ program to find the Modulus of a Complex Number ; Function to find modulus of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findModulo ( string s ) { int l = s . length ( ) ; int i , modulus = 0 ; if ( s . find ( ' + ' ) < l ) { i = s . find ( ' + ' ) ; } else { i = s . find ( ' - ' ) ; } string real = s . substr ( 0 , i ) ; string imaginary = s . substr ( i + 1 , l - 1 ) ; int x = stoi ( real ) ; int y = stoi ( imaginary ) ; cout << sqrt ( x * x + y * y ) << " STRNEWLINE " ; } int main ( ) { string s = "3 + 4i " ; findModulo ( s ) ; return 0 ; } |
Minimum number of blocks required to form Hollow Rectangular Prism | C ++ Implementation to find the minimum no of blocks required to form hollow rectangular prism ; Function to display output ; Function to return minimum no of layers required to form the hollow prism ; Function to calculate no of blocks required for each layer ; No of blocks required for each row ; Check for no of layers is minimum ; Driver function ; Length , width , height of each block ; Side of one wall ; height of each wall | #include <bits/stdc++.h> NEW_LINE using namespace std ; void disp ( int row_no , int block ) { cout << row_no * block ; } int row ( int ht , int h ) { return ht / h ; } void calculate ( int l , int w , int h , int a , int ht ) { int no_block = ( 4 * a ) / l ; int row_no ; if ( h < w ) row_no = row ( ht , w ) ; else row_no = row ( ht , h ) ; disp ( row_no , no_block ) ; } int main ( ) { int l = 50 , w = 20 , h = 35 ; int a = 700 ; int ht = 140 ; calculate ( l , w , h , a , ht ) ; return 0 ; } |
Maximum area of rectangle inscribed in an equilateral triangle | CPP implementation to find the maximum area inscribed in an equilateral triangle ; Function to find the maximum area of the rectangle inscribed in an equilateral triangle of side S ; Maximum area of the rectangle inscribed in an equilateral triangle of side S ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double solve ( int s ) { double area = ( 1.732 * pow ( s , 2 ) ) / 8 ; return area ; } int main ( ) { int n = 14 ; cout << solve ( n ) ; } |
Area of largest semicircle that can be drawn inside a square | C ++ program to find Area of semicircle in a square ; Function to find area of semicircle ; Driver code ; side of a square ; Call Function to find the area of semicircle | #include <bits/stdc++.h> NEW_LINE using namespace std ; float find_Area ( float a ) { float R = a * ( 2.0 - sqrt ( 2 ) ) ; float area = 3.14 * R * R / 2.0 ; return area ; } int main ( ) { float a = 4 ; cout << " β Area β of β semicircle β = β " << find_Area ( a ) ; return 0 ; } |
Count the number of times graph crosses X | C ++ implementation to count the number of times the graph crosses the x - axis . ; Function to to count the number of times the graph crosses the x - axis . ; Iterate over the steps array ; Update the previous level and current level by value given in the steps array ; Condition to check that the graph crosses the origin . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int times ( int steps [ ] , int n ) { int current_level = 0 ; int previous_level = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { previous_level = current_level ; current_level = current_level + steps [ i ] ; if ( ( previous_level < 0 && current_level >= 0 ) || ( previous_level > 0 && current_level <= 0 ) ) { count ++ ; } } return count ; } int main ( ) { int steps [ 12 ] = { 1 , -1 , 0 , 0 , 1 , 1 , -3 , 2 } ; int n = sizeof ( steps ) / sizeof ( int ) ; cout << times ( steps , n ) ; return 0 ; } |
Program to find X , Y and Z intercepts of a plane | C ++ program to find the X , Y and Z intercepts of a plane ; For finding the x - intercept put y = 0 and z = 0 ; For finding the y - intercept put x = 0 and z = 0 ; For finding the z - intercept put x = 0 and y = 0 ; For Finding value of A , B , C , D ; Calling the first created function ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float * XandYandZintercept ( float A , float B , float C , float D ) { static float rslt [ 3 ] ; float x = - D / A ; float y = - D / B ; float z = - D / C ; rslt [ 0 ] = x ; rslt [ 1 ] = y ; rslt [ 2 ] = z ; return rslt ; } void equation_plane ( int p [ ] , int q [ ] , int r [ ] ) { int x1 = p [ 0 ] ; int y1 = p [ 1 ] ; int z1 = p [ 2 ] ; int x2 = q [ 0 ] ; int y2 = q [ 1 ] ; int z2 = q [ 2 ] ; int x3 = r [ 0 ] ; int y3 = r [ 1 ] ; int z3 = r [ 2 ] ; int a1 = x2 - x1 ; int b1 = y2 - y1 ; int c1 = z2 - z1 ; int a2 = x3 - x1 ; int b2 = y3 - y1 ; int c2 = z3 - z1 ; int A = b1 * c2 - b2 * c1 ; int B = a2 * c1 - a1 * c2 ; int C = a1 * b2 - b1 * a2 ; int D = ( - A * x1 - B * y1 - C * z1 ) ; float * rslt = XandYandZintercept ( A , B , C , D ) ; for ( int i = 0 ; i < 3 ; i ++ ) { cout << rslt [ i ] << " β " ; } } int main ( ) { int x1 = -1 ; int y1 = 2 ; int z1 = 1 ; int x2 = 0 ; int y2 = -3 ; int z2 = 2 ; int x3 = 1 ; int y3 = 1 ; int z3 = -4 ; int p [ 3 ] = { x1 , y1 , z1 } ; int q [ 3 ] = { x2 , y2 , z2 } ; int r [ 3 ] = { x3 , y3 , z3 } ; equation_plane ( p , q , r ) ; } |
Percentage change in Hemisphere volume if radius is changed | C ++ program to find percentage change in hemisphere volume wrt change in radius ; Function to find the change in hemispheric volume ; Driver code ; Get the change in radius ; Calculate the change in hemispheric volume | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void new_vol ( double x ) { if ( x > 0 ) { cout << " % β change β in β the β " << " volume β of β the β hemisphere : β " << pow ( x , 3 ) / 10000 + 3 * x + ( 3 * pow ( x , 2 ) ) / 100 << " % " << " β increase STRNEWLINE " ; } else if ( x < 0 ) { cout << " % β change β in β the β " << " volume β of β the β hemisphere : β " << pow ( x , 3 ) / 10000 + 3 * x + ( 3 * pow ( x , 2 ) ) / 100 << " % β decrease STRNEWLINE " ; } else { cout << " Volume β remains β the β same . " ; } } int main ( ) { double x = -10.0 ; new_vol ( x ) ; return 0 ; } |
Find the integer points ( x , y ) with Manhattan distance atleast N | C ++ code to Find the integer points ( x , y ) with Manhattan distance atleast N ; C ++ function to find all possible point ; Find all 4 corners of the square whose side length is n ; If n is even then the middle point of the square will be an integer , so we will take that point ; Driver Code ; Printing all possible points | #include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > FindPoints ( int n ) { vector < pair < int , int > > v ; v . push_back ( { 0 , 0 } ) ; v . push_back ( { 0 , n } ) ; v . push_back ( { n , 0 } ) ; v . push_back ( { n , n } ) ; if ( n % 2 == 0 ) v . push_back ( { n / 2 , n / 2 } ) ; return v ; } int main ( ) { int N = 8 ; vector < pair < int , int > > v = FindPoints ( N ) ; for ( auto i : v ) { cout << " ( " << i . first << " , β " << i . second << " ) β " ; } return 0 ; } |
Find if the glass will be empty or not when the rate of drinking is given | C ++ implementation of the approach ; Function to return the time when the glass will be empty ; Check the condition when the glass will never be empty ; Find the time ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double pie = 3.1415926535897 ; double findsolution ( double d , double h , double m , double n ) { double k = ( 4 * m ) / ( pie * d * d ) ; if ( n > k ) return -1 ; double ans = ( h / ( k - n ) ) ; return ans ; } int main ( ) { double d = 1 , h = 1 , m = 1 , n = 1 ; cout << findsolution ( d , h , m , n ) ; return 0 ; } |
Sum of internal angles of a Polygon | C ++ implementation of the approach ; Function to return the sum of internal angles of an n - sided polygon ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfInternalAngles ( int n ) { if ( n < 3 ) return 0 ; return ( n - 2 ) * 180 ; } int main ( ) { int n = 5 ; cout << sumOfInternalAngles ( n ) ; return 0 ; } |
Number of ways N can be divided into four parts to construct a rectangle | C ++ implementation of the approach ; Function to return the required number of ways ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int cntWays ( int n ) { if ( n % 2 == 1 ) { return 0 ; } else { return ( n - 2 ) / 4 ; } } int main ( ) { int n = 18 ; cout << cntWays ( n ) ; return 0 ; } |
Forming triangles using points on a square | C ++ implementation of the approach ; Function to return the count of possible triangles ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfTriangles ( int n ) { int y = 4 * n ; return ( ( y * ( y - 2 ) * ( y - 1 ) ) - ( 4 * n * ( n - 2 ) * ( n - 1 ) ) ) / 6 ; } int main ( ) { int n = 1 ; cout << noOfTriangles ( n ) ; return 0 ; } |
Angle subtended by an arc at the centre of a circle | C ++ implementation of the approach ; Function to find Angle subtended by an arc at the centre of a circle ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int angle ( int n ) { return 2 * n ; } int main ( ) { int n = 30 ; cout << angle ( n ) ; return 0 ; } |
Check if two given Circles are Orthogonal or not | C ++ program to check if two circles are orthogonal or not ; Function to Check if the given circles are orthogonal ; calculating the square of the distance between C1 and C2 ; Check if the given circles are orthogonal ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool orthogonality ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int dsquare = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; if ( dsquare == r1 * r1 + r2 * r2 ) return true ; else return false ; } int main ( ) { int x1 = 4 , y1 = 3 ; int x2 = 0 , y2 = 1 ; int r1 = 2 , r2 = 4 ; bool f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( f ) cout << " Given β circles β are " << " β orthogonal . " ; else cout << " Given β circles β are " << " β not β orthogonal . " ; return 0 ; } |
Check if a right | C ++ implementation of the above approach ; Storing all the possible changes to make the triangle right - angled ; Function to check if the triangle is right - angled or not ; Function to check if the triangle can be transformed to right - angled ; Boolean variable to return true or false ; If it is already right - angled ; Applying the changes on the co - ordinates ; Applying on the first co - ordinate ; Applying on the second co - ordinate ; Applying on the third co - ordinate ; If can 't be transformed ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int dx [ ] = { -1 , 0 , 1 , 0 } ; int dy [ ] = { 0 , 1 , 0 , -1 } ; int ifRight ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) ; int b = ( ( x1 - x3 ) * ( x1 - x3 ) ) + ( ( y1 - y3 ) * ( y1 - y3 ) ) ; int c = ( ( x2 - x3 ) * ( x2 - x3 ) ) + ( ( y2 - y3 ) * ( y2 - y3 ) ) ; if ( ( a == ( b + c ) && a != 0 && b != 0 && c != 0 ) || ( b == ( a + c ) && a != 0 && b != 0 && c != 0 ) || ( c == ( a + b ) && a != 0 && b != 0 && c != 0 ) ) { return 1 ; } return 0 ; } void isValidCombination ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int x , y ; bool possible = 0 ; if ( ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) ) { cout << " ALREADY β RIGHT β ANGLED " ; return ; } else { for ( int i = 0 ; i < 4 ; i ++ ) { x = dx [ i ] + x1 ; y = dy [ i ] + y1 ; if ( ifRight ( x , y , x2 , y2 , x3 , y3 ) ) { cout << " POSSIBLE " ; return ; } x = dx [ i ] + x2 ; y = dy [ i ] + y2 ; if ( ifRight ( x1 , y1 , x , y , x3 , y3 ) ) { cout << " POSSIBLE " ; return ; } x = dx [ i ] + x3 ; y = dy [ i ] + y3 ; if ( ifRight ( x1 , y1 , x2 , y2 , x , y ) ) { cout << " POSSIBLE " ; return ; } } } if ( ! possible ) cout << " NOT β POSSIBLE " << endl ; } int main ( ) { int x1 = -49 , y1 = 0 ; int x2 = 0 , y2 = 50 ; int x3 = 0 , y3 = -50 ; isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) ; return 0 ; } |
Program to find Area of Triangle inscribed in N | C ++ Program to find the area of a triangle inscribed in N - sided regular polygon ; Function to find the area of the polygon ; area of a regular polygon with N sides and side length len ; Function to find the area of a triangle ; area of one triangle in an N - sided regular polygon ; area of inscribed triangle ; Driver code | #include <bits/stdc++.h> NEW_LINE #include <cmath> NEW_LINE using namespace std ; double area_of_regular_polygon ( double n , double len ) { double P = ( len * n ) ; double A = len / ( 2 * tan ( ( 180 / n ) * 3.14159 / 180 ) ) ; double area = ( P * A ) / 2 ; return area ; } double area_of_triangle_inscribed ( double n , double len ) { double area = area_of_regular_polygon ( n , len ) ; double triangle = area / n ; double ins_tri = ( triangle * 3 ) ; return ins_tri ; } int main ( ) { double n = 6 , len = 10 ; cout << area_of_triangle_inscribed ( n , len ) << endl ; return 0 ; } |
Find the area of quadrilateral when diagonal and the perpendiculars to it from opposite vertices are given | C ++ program to find the area of quadrilateral ; Function to find the area of quadrilateral ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float Area ( int d , int h1 , int h2 ) { float area ; area = 0.5 * d * ( h1 + h2 ) ; return area ; } int main ( ) { int d = 6 , h1 = 4 , h2 = 3 ; cout << " Area β of β Quadrilateral β = β " << ( Area ( d , h1 , h2 ) ) ; return 0 ; } |
Find the diagonal of the Cube | CPP program to find length of the diagonal of the cube ; Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; float diagonal_length ( float a ) { float L ; L = a * sqrt ( 3 ) ; return L ; } int main ( ) { float a = 5 ; cout << diagonal_length ( a ) ; return 0 ; } |
Concentric Hexagonal Numbers | CPP program to find nth concentric hexagon number ; Function to find nth concentric hexagon number ; Driver code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; int concentric_Hexagon ( int n ) { return 3 * pow ( n , 2 ) / 2 ; } int main ( ) { int n = 3 ; cout << concentric_Hexagon ( n ) ; return 0 ; } |
Find area of the larger circle when radius of the smaller circle and difference in the area is given | C ++ implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; const double PI = 3.14 ; double find_area ( int r , int d ) { double R = d / PI ; R += pow ( r , 2 ) ; R = sqrt ( R ) ; double area = PI * pow ( R , 2 ) ; return area ; } int main ( ) { int r = 4 , d = 5 ; cout << find_area ( r , d ) ; return 0 ; } |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.