codeparrot/xlcost-text-to-code huggingface dataset:
Significant Inversions in an Array | C ++ implementation of the approach ; Function that sorts the input array and returns the number of inversions in the array ; Recursive function that sorts the input array and returns the number of inversions in the array ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of the inversions in the left - part , the right - part and the number of inversions in merging ; Merge the two parts ; Function that merges the two sorted arrays and returns the inversion count in the arrays ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; First pass to count number of significant inversions ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; Second pass to merge the two sorted arrays ; Copy the remaining elements of the left subarray ( if there are any ) to temp ; Copy the remaining elements of the right subarray ( if there are any ) to temp ; Copy back the merged elements to the original array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) ; int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) ; int mergeSort ( int arr [ ] , int array_size ) { int temp [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int i , j , k ; int inv_count = 0 ; i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] > 2 * arr [ j ] ) { inv_count += ( mid - i ) ; j ++ ; } else { i ++ ; } } i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } int main ( ) { int arr [ ] = { 1 , 20 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << mergeSort ( arr , n ) ; return 0 ; }
Find the number of different numbers in the array after applying the given operation q times | CPP implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integers in the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; Driver code ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE int lazy [ 4 * N ] ; set < int > se ; void update ( int x , int y , int value , int id , int l , int r ) { if ( x >= r or l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } int mid = ( l + r ) / 2 ; if ( lazy [ id ] ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } void query ( int id , int l , int r ) { if ( lazy [ id ] ) { se . insert ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; int mid = ( l + r ) / 2 ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } int main ( ) { int n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; cout << se . size ( ) << endl ; return 0 ; }
Find Nth term ( A matrix exponentiation example ) | CPP program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence will be first row of res * Initial Vector . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000009 NEW_LINE #define ll  long long int NEW_LINE ll power ( ll n ) { if ( n <= 1 ) return 1 ; n -- ; ll res [ 2 ] [ 2 ] = { 1 , 0 , 0 , 1 } ; ll tMat [ 2 ] [ 2 ] = { 2 , 3 , 1 , 0 } ; while ( n ) { if ( n & 1 ) { ll tmp [ 2 ] [ 2 ] ; tmp [ 0 ] [ 0 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; res [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; res [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; res [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; res [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } n = n / 2 ; ll tmp [ 2 ] [ 2 ] ; tmp [ 0 ] [ 0 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 0 ] [ 1 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tmp [ 1 ] [ 0 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; tmp [ 1 ] [ 1 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; tMat [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; tMat [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; tMat [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; tMat [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; } return ( res [ 0 ] [ 0 ] * 1 + res [ 0 ] [ 1 ] * 1 ) % MOD ; } int main ( ) { ll n = 3 ; cout << power ( n ) ; return 0 ; }
Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | C ++ Naive program to shuffle an array of size 2 n ; function to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shuffleArray ( int a [ ] , int n ) { for ( int i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( int j = k ; j > i + q ; j -- ) swap ( a [ j - 1 ] , a [ j ] ) ; } int main ( ) { int a [ ] = { 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; shuffleArray ( a , n / 2 ) ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; return 0 ; }
Place k elements such that minimum distance is maximized | C ++ program to find largest minimum distance among k points . ; Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance here we are using right value as highest distance difference , so we remove some extra checks ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid iff all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isFeasible ( int mid , int arr [ ] , int n , int k ) { int pos = arr [ 0 ] ; int elements = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - pos >= mid ) { pos = arr [ i ] ; elements ++ ; if ( elements == k ) return true ; } } return 0 ; } int largestMinDist ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int res = -1 ; int left = 1 , right = arr [ n - 1 ] ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isFeasible ( mid , arr , n , k ) ) { res = max ( res , mid ) ; left = mid + 1 ; } else right = mid ; } return res ; } int main ( ) { int arr [ ] = { 1 , 2 , 8 , 4 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << largestMinDist ( arr , n , k ) ; 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 # include < iostream > ; It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; traverse the array ; update the frequency ; traverse the hashmap ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFrequency ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } for ( auto i : mp ) { cout << " Element ▁ " << i . first << " ▁ occurs ▁ " << i . second << " ▁ 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 ; }
Sine Rule with Derivation , Example and Implementation | C ++ program for the above approach ; Function to calculate remaining two sides ; Calculate angle B ; Convert angles to their respective radians for using trigonometric functions ; Sine rule ; Precision of 2 decimal spaces ; Print the answer ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSides ( double A , double C , double c ) { double B = 180 - ( A + C ) ; A = A * ( 3.14159 / 180 ) ; C = C * ( 3.14159 / 180 ) ; B = B * ( 3.14159 / 180 ) ; double a = ( c / sin ( C ) ) * sin ( A ) ; double b = ( c / sin ( C ) ) * sin ( B ) ; cout << fixed << setprecision ( 2 ) ; cout << a << endl ; cout << b << endl ; } int main ( ) { double A = 45.0 ; double C = 35.0 ; double c = 23 ; findSides ( A , C , c ) ; return 0 ; }
Equation of a straight line passing through a point and making a given angle with a given line | C ++ program for the above approach ; Function to find slope of given line ; Special case when slope of line is infinity or is perpendicular to x - axis ; Function to find equations of lines passing through the given point and making an angle with given line ; Store slope of given line ; Convert degrees to radians ; Special case when slope of given line is infinity : In this case slope of one line will be equal to alfa and the other line will be equal to ( 180 - alfa ) ; In this case slope of required lines can 't be  infinity ; g and f are the variables of required equations ; Print first line equation ; Print second line equation ; Special case when slope of required line becomes infinity ; General case ; g and f are the variables of required equations ; Print first line equation ; Print second line equation ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double line_slope ( double a , double b ) { if ( a != 0 ) return - b / a ; else return ( -2 ) ; } void line_equation ( double a , double b , double c , double x1 , double y1 , double alfa ) { double given_slope = line_slope ( a , b ) ; double x = alfa * 3.14159 / 180 ; if ( given_slope == -2 ) { double slope_1 = tan ( x ) ; double slope_2 = tan ( 3.14159 - x ) ; int g = x1 , f = x1 ; g *= ( - slope_1 ) ; g += y1 ; if ( g > 0 ) cout << " y ▁ = ▁ " << slope_1 << " x ▁ + " << g << endl ; if ( g <= 0 ) cout << " y ▁ = ▁ " << slope_1 << " x ▁ " << g << endl ; f *= ( - slope_2 ) ; f += y1 ; if ( f > 0 ) { cout << " y ▁ = ▁ " << slope_2 << " x ▁ + " << f << endl ; } if ( f <= 0 ) cout << " y ▁ = ▁ " << slope_2 << " x ▁ " << f << endl ; return ; } if ( 1 - tan ( x ) * given_slope == 0 ) { cout << " x ▁ = ▁ " << x1 << endl ; } if ( 1 + tan ( x ) * given_slope == 0 ) { cout << " x ▁ = ▁ " << x1 << endl ; } double slope_1 = ( given_slope + tan ( x ) ) / ( 1 - tan ( x ) * given_slope ) ; double slope_2 = ( given_slope - tan ( x ) ) / ( 1 + tan ( x ) * given_slope ) ; int g = x1 , f = x1 ; g *= ( - slope_1 ) ; g += y1 ; if ( g > 0 && 1 - tan ( x ) * given_slope != 0 ) cout << " y ▁ = ▁ " << slope_1 << " x ▁ + " << g << endl ; if ( g <= 0 && 1 - tan ( x ) * given_slope != 0 ) cout << " y ▁ = ▁ " << slope_1 << " x ▁ " << g << endl ; f *= ( - slope_2 ) ; f += y1 ; if ( f > 0 && 1 + tan ( x ) * given_slope != 0 ) { cout << " y ▁ = ▁ " << slope_2 << " x ▁ + " << f << endl ; } if ( f <= 0 && 1 + tan ( x ) * given_slope != 0 ) cout << " y ▁ = ▁ " << slope_2 << " x ▁ " << f << endl ; } int main ( ) { double a = 2 , b = 3 , c = -7 ; double x1 = 4 , y1 = 9 ; double alfa = 30 ; line_equation ( a , b , c , x1 , y1 , alfa ) ; return 0 ; } cout << fixed << setprecision ( 2 ) ;
Equation of a normal to a Circle from a given point | C ++ program for the above approach ; Function to calculate the slope ; Store the coordinates the center of the circle ; If slope becomes infinity ; Stores the slope ; If slope is zero ; Return the result ; Function to find the equation of the normal to a circle from a given point ; Stores the slope of the normal ; If slope becomes infinity ; If slope is zero ; Otherwise , print the equation of the normal ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double normal_slope ( double a , double b , double x1 , double y1 ) { double g = a / 2 ; double f = b / 2 ; if ( g - x1 == 0 ) return ( -1 ) ; double slope = ( f - y1 ) / ( g - x1 ) ; if ( slope == 0 ) return ( -2 ) ; return slope ; } void normal_equation ( double a , double b , double x1 , double y1 ) { double slope = normal_slope ( a , b , x1 , y1 ) ; if ( slope == -1 ) { cout << " x ▁ = ▁ " << x1 ; } if ( slope == -2 ) { cout << " y ▁ = ▁ " << y1 ; } if ( slope != -1 && slope != -2 ) { x1 *= - slope ; x1 += y1 ; if ( x1 > 0 ) cout << " y ▁ = ▁ " << slope << " x ▁ + ▁ " << x1 ; else cout << " y ▁ = ▁ " << slope << " x ▁ " << x1 ; } } int main ( ) { int a = 4 , b = 6 , c = 5 ; int x1 = 12 , y1 = 14 ; normal_equation ( a , b , x1 , y1 ) ; return 0 ; }
Sum of squares of distances between all pairs from given points | C ++ program for the above approach ; Function to find the sum of squares of distance between all distinct pairs ; Stores final answer ; Traverse the array ; Adding the effect of this point for all the previous x - points ; Temporarily add the square of x - coordinate ; Add the effect of this point for all the previous y - points ; Print the desired answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSquareSum ( int Coordinates [ ] [ 2 ] , int N ) { long long xq = 0 , yq = 0 ; long long xs = 0 , ys = 0 ; long long res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int a , b ; a = Coordinates [ i ] [ 0 ] ; b = Coordinates [ i ] [ 1 ] ; res += xq ; res -= 2 * xs * a ; res += i * ( long long ) ( a * a ) ; xq += a * a ; xs += a ; res += yq ; res -= 2 * ys * b ; res += i * ( long long ) b * b ; yq += b * b ; ys += b ; } cout << res ; } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 1 } , { -1 , -1 } , { 1 , -1 } , { -1 , 1 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSquareSum ( arr , N ) ; return 0 ; }
Count points from an array that lies inside a semi | C ++ program for above approach ; Traverse the array ; Stores if a point lies above the diameter or not ; Stores if the R is less than or equal to the distance between center and point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPointsIns ( int x1 , int y1 , int radius , int x2 , int y2 , vector < pair < int , int > > points ) { int ans = 0 ; for ( int i = 0 ; i < points . size ( ) ; i ++ ) { bool condOne = false , condTwo = false ; if ( ( points [ i ] . second - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] . first - x2 ) >= 0 ) { condOne = true ; } if ( radius >= ( int ) sqrt ( pow ( ( y1 - points [ i ] . second ) , 2 ) + pow ( x1 - points [ i ] . first , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } int main ( ) { int X = 0 ; int Y = 0 ; int R = 5 ; int P = 5 ; int Q = 0 ; vector < pair < int , int > > arr = { make_pair ( 2 , 3 ) , make_pair ( 5 , 6 ) , make_pair ( -1 , 4 ) , make_pair ( 5 , 5 ) } ; cout << getPointsIns ( X , Y , R , P , Q , arr ) ; return 0 ; }
Count pairs of points having distance between them equal to integral values in a K | C ++ program for the above approach ; Function to find pairs whose distance between the points of is an integer value . ; Stores count of pairs whose distance between points is an integer ; Traverse the array , points [ ] ; Stores distance between points ( i , j ) ; Traverse all the points of current pair ; Update temp ; Update dist ; If dist is a perfect square ; Update ans ; Driver Code ; Given value of K ; Given points ; Given value of N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void cntPairs ( vector < vector < int > > points , int n , int K ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int dist = 0 ; for ( int k = 0 ; k < K ; k ++ ) { int temp = ( points [ i ] [ k ] - points [ j ] [ k ] ) ; dist += temp * temp ; } if ( sqrt ( dist ) * sqrt ( dist ) == dist ) { ans += 1 ; } } } cout << ans << endl ; } int main ( ) { int K = 2 ; vector < vector < int > > points = { { 1 , 2 } , { 5 , 5 } , { -2 , 8 } } ; int n = points . size ( ) ; cntPairs ( points , n , K ) ; return 0 ; }
Circumradius of a Cyclic Quadrilateral using the length of Sides | C ++ program to find circumradius of a cyclic quadrilateral using sides ; Function to return the circumradius of a cyclic quadrilateral using sides ; Find semiperimeter ; Calculate the radius ; Driver Code ; Function call ; Print the radius
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Circumradius ( int a , int b , int c , int d ) { double s = ( a + b + c + d ) / 2.0 ; double radius = sqrt ( ( ( a * b ) + ( c * d ) ) * ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( s - a ) * ( s - b ) * ( s - c ) * ( s - d ) ) ) ; return radius / 4 ; } int main ( ) { int A = 3 ; int B = 4 ; int C = 5 ; int D = 6 ; double ans = Circumradius ( A , B , C , D ) ; cout << setprecision ( 3 ) << ans ; return 0 ; }
Area of Triangle using Side | C ++ program to calculate the area of a triangle when the length of two adjacent sides and the angle between them is provided ; Function to return the area of triangle using Side - Angle - Side formula ; Driver Code ; Function Call ; Print the final answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Area_of_Triangle ( int a , int b , int k ) { float area = ( float ) ( ( 1 / 2.0 ) * a * b * ( sin ( k ) ) ) ; return area ; } int main ( ) { int a = 9 ; int b = 12 ; int k = 2 ; float ans = Area_of_Triangle ( a , b , k ) ; cout << ans << endl ; }
Find the maximum angle at which we can tilt the bottle without spilling any water | C ++ program to find the maximum angle at which we can tilt the bottle without spilling any water ; Now we have the volume of rectangular prism a * a * b ; Now we have 2 cases ! ; Taking the tangent inverse of value d As we want to take out the required angle ; Taking the tangent inverse of value d As we want to take out the required angle ; As of now the angle is in radian . So we have to convert it in degree . ; Driver Code ; Enter the Base square side length ; Enter the Height of rectangular prism ; Enter the Base square side length
#include <bits/stdc++.h> NEW_LINE using namespace std ; float find_angle ( int x , int y , int z ) { int volume = x * x * y ; float ans = 0 ; if ( z < volume / 2 ) { float d = ( x * y * y ) / ( 2.0 * z ) ; ans = atan ( d ) ; } else { z = volume - z ; float d = ( 2 * z ) / ( float ) ( x * x * x ) ; ans = atan ( d ) ; } ans = ( ans * 180 ) / 3.14159265 ; return ans ; } int main ( ) { int x = 12 ; int y = 21 ; int z = 10 ; cout << find_angle ( x , y , z ) << endl ; }
Count of distinct rectangles inscribed in an equilateral triangle | C ++ implementation of the approach ; Function to return the count of rectangles when n is odd ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Function to return the count of rectangles when n is even ; Driver code ; If n is odd
#include <iostream> NEW_LINE using namespace std ; int countOdd ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i & 1 ) { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } int countEven ( int n ) { int coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i & 1 ) { m = ( ( n - 1 ) - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } else { m = ( n - i ) / 2 ; j = ( i * ( i + 1 ) ) / 2 ; coun += j * m ; } } return coun ; } int main ( ) { int n = 5 ; if ( n & 1 ) cout << countOdd ( n ) ; else cout << countEven ( n ) ; return 0 ; }