codeparrot/xlcost-text-to-code huggingface dataset:
Generate all the binary strings of N bits | C ++ implementation of the above approach : ; Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTheArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; } void generateAllBinaryStrings ( int n , int arr [ ] , int i ) { if ( i == n ) { printTheArray ( arr , n ) ; return ; } arr [ i ] = 0 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; arr [ i ] = 1 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; } int main ( ) { int n = 4 ; int arr [ n ] ; generateAllBinaryStrings ( n , arr , 0 ) ; return 0 ; }
Print all the combinations of a string in lexicographical order | C ++ program to find all combinations of a string in lexicographical order ; function to print string ; Method to found all combination of string it is based in tree ; return if level is equal size of string ; if occurrence of char is 0 then skip the iteration of loop ; decrease the char occurrence by 1 ; store the char in result ; print the string till level ; call the function from level + 1 ; backtracking ; declare the map for store each char with occurrence ; initialize the input array with all unique char ; initialize the count array with occurrence the unique char ; temporary char array for store the result ; store the element of input array ; store the element of count array ; size of map ( no of unique char ) ; size of original string ; call function for print string combination ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printResult ( char * result , int len ) { for ( int i = 0 ; i <= len ; i ++ ) cout << result [ i ] ; cout << endl ; } void stringCombination ( char result [ ] , char str [ ] , int count [ ] , int level , int size , int length ) { if ( level == size ) return ; for ( int i = 0 ; i < length ; i ++ ) { if ( count [ i ] == 0 ) continue ; count [ i ] -- ; result [ level ] = str [ i ] ; printResult ( result , level ) ; stringCombination ( result , str , count , level + 1 , size , length ) ; count [ i ] ++ ; } } void combination ( string str ) { map < char , int > mp ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( mp . find ( str [ i ] ) != mp . end ( ) ) mp [ str [ i ] ] = mp [ str [ i ] ] + 1 ; else mp [ str [ i ] ] = 1 ; } char * input = new char [ mp . size ( ) ] ; int * count = new int [ mp . size ( ) ] ; char * result = new char [ str . size ( ) ] ; map < char , int > :: iterator it = mp . begin ( ) ; int i = 0 ; for ( it ; it != mp . end ( ) ; it ++ ) { input [ i ] = it -> first ; count [ i ] = it -> second ; i ++ ; } int length = mp . size ( ) ; int size = str . size ( ) ; stringCombination ( result , input , count , 0 , size , length ) ; } int main ( ) { string str = " ABC " ; combination ( str ) ; return 0 ; }
Combinations where every element appears twice and distance between appearances is equal to the value | C ++ program to find all combinations where every element appears twice and distance between appearances is equal to the value ; Find all combinations that satisfies given constraints ; if all elements are filled , print the solution ; try all possible combinations for element elem ; if position i and ( i + elem + 1 ) are not occupied in the vector ; place elem at position i and ( i + elem + 1 ) ; recurse for next element ; backtrack ( remove elem from position i and ( i + elem + 1 ) ) ; create a vector of double the size of given number with ; all its elements initialized by 1 ; start from element 1 ; Driver code ; given number
#include <bits/stdc++.h> NEW_LINE using namespace std ; void allCombinationsRec ( vector < int > & arr , int elem , int n ) { if ( elem > n ) { for ( int i : arr ) cout << i << " ▁ " ; cout << endl ; return ; } for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( arr [ i ] == -1 && ( i + elem + 1 ) < 2 * n && arr [ i + elem + 1 ] == -1 ) { arr [ i ] = elem ; arr [ i + elem + 1 ] = elem ; allCombinationsRec ( arr , elem + 1 , n ) ; arr [ i ] = -1 ; arr [ i + elem + 1 ] = -1 ; } } } void allCombinations ( int n ) { vector < int > arr ( 2 * n , -1 ) ; int elem = 1 ; allCombinationsRec ( arr , elem , n ) ; } int main ( ) { int n = 3 ; allCombinations ( n ) ; return 0 ; }
Warnsdorff 's algorithm for Knight’s tour problem | C ++ program to for Kinight ' s ▁ tour ▁ problem ▁ using ▁ Warnsdorff ' s algorithm ; Move pattern on basis of the change of x coordinates and y coordinates respectively ; function restricts the knight to remain within the 8 x8 chessboard ; Checks whether a square is valid and empty or not ; Returns the number of empty squares adjacent to ( x , y ) ; Picks next point using Warnsdorff 's heuristic. Returns false if it is not possible to pick next point. ; Try all N adjacent of ( * x , * y ) starting from a random adjacent . Find the adjacent with minimum degree . ; IF we could not find a next cell ; Store coordinates of next point ; Mark next move ; Update next point ; displays the chessboard with all the legal knight 's moves  ; If the knight ends on a square that is one knight 's move from the beginning square,  then tour is closed  ; Generates the legal moves using warnsdorff 's  heuristics. Returns false if not possible  ; Filling up the chessboard matrix with - 1 's ; Randome initial position ; Current points are same as initial points ; Keep picking next points using Warnsdorff 's heuristic ; Check if tour is closed ( Can end at starting point ) ; Driver code ; While we don 't get a solution
#include <bits/stdc++.h> NEW_LINE #define N  8 NEW_LINE static int cx [ N ] = { 1 , 1 , 2 , 2 , -1 , -1 , -2 , -2 } ; static int cy [ N ] = { 2 , -2 , 1 , -1 , 2 , -2 , 1 , -1 } ; bool limits ( int x , int y ) { return ( ( x >= 0 && y >= 0 ) && ( x < N && y < N ) ) ; } bool isempty ( int a [ ] , int x , int y ) { return ( limits ( x , y ) ) && ( a [ y * N + x ] < 0 ) ; } int getDegree ( int a [ ] , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) if ( isempty ( a , ( x + cx [ i ] ) , ( y + cy [ i ] ) ) ) count ++ ; return count ; } bool nextMove ( int a [ ] , int * x , int * y ) { int min_deg_idx = -1 , c , min_deg = ( N + 1 ) , nx , ny ; int start = rand ( ) % N ; for ( int count = 0 ; count < N ; ++ count ) { int i = ( start + count ) % N ; nx = * x + cx [ i ] ; ny = * y + cy [ i ] ; if ( ( isempty ( a , nx , ny ) ) & & ( c = getDegree ( a , nx , ny ) ) < min_deg ) { min_deg_idx = i ; min_deg = c ; } } if ( min_deg_idx == -1 ) return false ; nx = * x + cx [ min_deg_idx ] ; ny = * y + cy [ min_deg_idx ] ; a [ ny * N + nx ] = a [ ( * y ) * N + ( * x ) ] + 1 ; * x = nx ; * y = ny ; return true ; } void print ( int a [ ] ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) printf ( " % d TABSYMBOL " , a [ j * N + i ] ) ; printf ( " STRNEWLINE " ) ; } } bool neighbour ( int x , int y , int xx , int yy ) { for ( int i = 0 ; i < N ; ++ i ) if ( ( ( x + cx [ i ] ) == xx ) && ( ( y + cy [ i ] ) == yy ) ) return true ; return false ; } bool findClosedTour ( ) { int a [ N * N ] ; for ( int i = 0 ; i < N * N ; ++ i ) a [ i ] = -1 ; int sx = rand ( ) % N ; int sy = rand ( ) % N ; int x = sx , y = sy ; for ( int i = 0 ; i < N * N - 1 ; ++ i ) if ( nextMove ( a , & x , & y ) == 0 ) return false ; if ( ! neighbour ( x , y , sx , sy ) ) return false ; print ( a ) ; return true ; } int main ( ) { srand ( time ( NULL ) ) ; while ( ! findClosedTour ( ) ) { ; } return 0 ; }
Printing all solutions in N | CPP program for above approach ; Program to solve N Queens problem ; All_rows_filled is a bit mask having all N bits set ; If rowmask will have all bits set , means queen has been placed successfully in all rows and board is displayed ; We extract a bit mask ( safe ) by rowmask , ldmask and rdmask . all set bits of ' safe ' indicates the safe column index for queen placement of this iteration for row index ( row ) . ; Extracts the right - most set bit ( safe column index ) where queen can be placed for this row ; these bit masks will keep updated in each iteration for next row ; Reset right - most set bit to 0 so , next iteration will continue by placing the queen at another safe column index of this row ; Backtracking , replace ' Q ' by ' ▁ ' ; Driver Code ; Board size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > result ; void solveBoard ( vector < vector < char > > & board , int row , int rowmask , int ldmask , int rdmask , int & ways ) { int n = board . size ( ) ; int all_rows_filled = ( 1 << n ) - 1 ; if ( rowmask == all_rows_filled ) { vector < int > v ; for ( int i = 0 ; i < board . size ( ) ; i ++ ) { for ( int j = 0 ; j < board . size ( ) ; j ++ ) { if ( board [ i ] [ j ] == ' Q ' ) v . push_back ( j + 1 ) ; } } result . push_back ( v ) ; return ; } int safe = all_rows_filled & ( ~ ( rowmask ldmask rdmask ) ) ; while ( safe ) { int p = safe & ( - safe ) ; int col = ( int ) log2 ( p ) ; board [ row ] [ col ] = ' Q ' ; solveBoard ( board , row + 1 , rowmask | p , ( ldmask p ) << 1 , ( rdmask p ) >> 1 , ways ) ; safe = safe & ( safe - 1 ) ; board [ row ] [ col ] = ' ▁ ' ; } return ; } int main ( ) { int n = 4 ; int ways = 0 ; vector < vector < char > > board ; for ( int i = 0 ; i < n ; i ++ ) { vector < char > tmp ; for ( int j = 0 ; j < n ; j ++ ) { tmp . push_back ( ' ▁ ' ) ; } board . push_back ( tmp ) ; } int rowmask = 0 , ldmask = 0 , rdmask = 0 ; int row = 0 ; result . clear ( ) ; solveBoard ( board , row , rowmask , ldmask , rdmask , ways ) ; sort ( result . begin ( ) , result . end ( ) ) ; for ( auto ar : result ) { cout << " [ " ; for ( auto it : ar ) cout << it << " ▁ " ; cout << " ] " ; } return 0 ; }
Find all distinct subsets of a given set using BitMasking Approach | C ++ program to find all subsets of given set . Any repeated subset is considered only once in the output ; Utility function to split the string using a delim . Refer - http : stackoverflow . com / questions / 236129 / split - a - string - in - c ; Function to find all subsets of given set . Any repeated subset is considered only once in the output ; Run counter i from 000. . 0 to 111. . 1 ; consider each element in the set ; Check if jth bit in the i is set . If the bit is set , we consider jth element from set ; if subset is encountered for the first time If we use set < string > , we can directly insert ; consider every subset ; split the subset and print its elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > split ( const string & s , char delim ) { vector < string > elems ; stringstream ss ( s ) ; string item ; while ( getline ( ss , item , delim ) ) elems . push_back ( item ) ; return elems ; } int printPowerSet ( int arr [ ] , int n ) { vector < string > list ; for ( int i = 0 ; i < ( int ) pow ( 2 , n ) ; i ++ ) { string subset = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) subset += to_string ( arr [ j ] ) + " | " ; } if ( find ( list . begin ( ) , list . end ( ) , subset ) == list . end ( ) ) list . push_back ( subset ) ; } for ( string subset : list ) { vector < string > arr = split ( subset , ' ▁ ' ) ; for ( string str : arr ) cout << str << " ▁ " ; cout << endl ; } } int main ( ) { int arr [ ] = { 10 , 12 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPowerSet ( arr , n ) ; return 0 ; }
Fill two instances of all numbers from 1 to n in a specific way | A backtracking based C ++ Program to fill two instances of all numbers from 1 to n in a specific way ; A recursive utility function to fill two instances of numbers from 1 to n in res [ 0. .2 n - 1 ] . ' curr ' is current value of n . ; If current number becomes 0 , then all numbers are filled ; Try placing two instances of ' curr ' at all possible locations till solution is found ; Two ' curr ' should be placed at ' curr + 1' distance ; Plave two instances of ' curr ' ; Recur to check if the above placement leads to a solution ; If solution is not possible , then backtrack ; This function prints the result for input number ' n ' using fillUtil ( ) ; Create an array of size 2 n and initialize all elements in it as 0 ; If solution is possible , then print it . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool fillUtil ( int res [ ] , int curr , int n ) { if ( curr == 0 ) return true ; int i ; for ( i = 0 ; i < 2 * n - curr - 1 ; i ++ ) { if ( res [ i ] == 0 && res [ i + curr + 1 ] == 0 ) { res [ i ] = res [ i + curr + 1 ] = curr ; if ( fillUtil ( res , curr - 1 , n ) ) return true ; res [ i ] = res [ i + curr + 1 ] = 0 ; } } return false ; } void fill ( int n ) { int res [ 2 * n ] , i ; for ( i = 0 ; i < 2 * n ; i ++ ) res [ i ] = 0 ; if ( fillUtil ( res , n , n ) ) { for ( i = 0 ; i < 2 * n ; i ++ ) cout << res [ i ] << " ▁ " ; } else cout << " Not ▁ Possible " ; } int main ( ) { fill ( 7 ) ; return 0 ; }
m Coloring Problem | Backtracking | CPP program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Add edges to each node as per given input ; Connect the undirected graph ; Display final answer
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; class node { public : int color = 1 ; set < int > edges ; } ; int canPaint ( vector < node > & nodes , int n , int m ) { vector < int > visited ( n + 1 , 0 ) ; int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited [ sv ] ) continue ; visited [ sv ] = 1 ; queue < int > q ; q . push ( sv ) ; while ( ! q . empty ( ) ) { int top = q . front ( ) ; q . pop ( ) ; for ( auto it = nodes [ top ] . edges . begin ( ) ; it != nodes [ top ] . edges . end ( ) ; it ++ ) { if ( nodes [ top ] . color == nodes [ * it ] . color ) nodes [ * it ] . color += 1 ; maxColors = max ( maxColors , max ( nodes [ top ] . color , nodes [ * it ] . color ) ) ; if ( maxColors > m ) return 0 ; if ( ! visited [ * it ] ) { visited [ * it ] = 1 ; q . push ( * it ) ; } } } } return 1 ; } int main ( ) { int n = 4 ; bool graph [ n ] [ n ] = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; vector < node > nodes ( n + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i ] [ j ] ) { nodes [ i ] . edges . insert ( i ) ; nodes [ j ] . edges . insert ( j ) ; } } } cout << canPaint ( nodes , n , m ) ; cout << " STRNEWLINE " ; return 0 ; }
Fast Doubling method to find the Nth Fibonacci number | C ++ program to find the Nth Fibonacci number using Fast Doubling Method ; Function calculate the N - th fibanacci number using fast doubling method ; Base Condition ; Here a = F ( n ) ; Here b = F ( n + 1 ) ; As F ( 2 n ) = F ( n ) [ 2F ( n + 1 ) F ( n ) ] Here c = F ( 2 n ) ; As F ( 2 n + 1 ) = F ( n ) ^ 2 + F ( n + 1 ) ^ 2 Here d = F ( 2 n + 1 ) ; Check if N is odd or even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int a , b , c , d ; #define MOD  1000000007 NEW_LINE void FastDoubling ( int n , int res [ ] ) { if ( n == 0 ) { res [ 0 ] = 0 ; res [ 1 ] = 1 ; return ; } FastDoubling ( ( n / 2 ) , res ) ; a = res [ 0 ] ; b = res [ 1 ] ; c = 2 * b - a ; if ( c < 0 ) c += MOD ; c = ( a * c ) % MOD ; d = ( a * a + b * b ) % MOD ; if ( n % 2 == 0 ) { res [ 0 ] = c ; res [ 1 ] = d ; } else { res [ 0 ] = d ; res [ 1 ] = c + d ; } } int main ( ) { int N = 6 ; int res [ 2 ] = { 0 } ; FastDoubling ( N , res ) ; cout << res [ 0 ] << " STRNEWLINE " ; return 0 ; }
Frequency of an integer in the given array using Divide and Conquer | C ++ implrmrntation of the approach ; Function to return the frequency of x in the subarray arr [ low ... high ] ; If the subarray is invalid or the element is not found ; If there 's only a single element  which is equal to x ; Divide the array into two parts and then find the count of occurrences of x in both the parts ; Driver code
#include <iostream> NEW_LINE using namespace std ; int count ( int arr [ ] , int low , int high , int x ) { if ( ( low > high ) || ( low == high && arr [ low ] != x ) ) return 0 ; if ( low == high && arr [ low ] == x ) return 1 ; return count ( arr , low , ( low + high ) / 2 , x ) + count ( arr , 1 + ( low + high ) / 2 , high , x ) ; } int main ( ) { int arr [ ] = { 30 , 1 , 42 , 5 , 56 , 3 , 56 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int x = 56 ; cout << count ( arr , 0 , n - 1 , x ) ; return 0 ; }
Median of an unsorted array using Quick Select Algorithm | CPP program to find median of an array ; Utility function to swapping of element ; Returns the correct position of pivot element ; Picks a random pivot element between l and r and partitions arr [ l . . r ] around the randomly picked element using partition ( ) ; Utility function to find median ; if l < r ; Find the partition index ; If partition index = k , then we found the median of odd number element in arr [ ] ; If index = k - 1 , then we get a & b as middle element of arr [ ] ; If partitionIndex >= k then find the index in first half of the arr [ ] ; If partitionIndex <= k then find the index in second half of the arr [ ] ; Function to find Median ; If n is odd ; If n is even ; Print the Median of arr [ ] ; Driver program to test above methods
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void swap ( int * a , int * b ) { int temp = * a ; * a = * b ; * b = temp ; } int Partition ( int arr [ ] , int l , int r ) { int lst = arr [ r ] , i = l , j = l ; while ( j < r ) { if ( arr [ j ] < lst ) { swap ( & arr [ i ] , & arr [ j ] ) ; i ++ ; } j ++ ; } swap ( & arr [ i ] , & arr [ r ] ) ; return i ; } int randomPartition ( int arr [ ] , int l , int r ) { int n = r - l + 1 ; int pivot = rand ( ) % n ; swap ( & arr [ l + pivot ] , & arr [ r ] ) ; return Partition ( arr , l , r ) ; } void MedianUtil ( int arr [ ] , int l , int r , int k , int & a , int & b ) { if ( l <= r ) { int partitionIndex = randomPartition ( arr , l , r ) ; if ( partitionIndex == k ) { b = arr [ partitionIndex ] ; if ( a != -1 ) return ; } else if ( partitionIndex == k - 1 ) { a = arr [ partitionIndex ] ; if ( b != -1 ) return ; } if ( partitionIndex >= k ) return MedianUtil ( arr , l , partitionIndex - 1 , k , a , b ) ; else return MedianUtil ( arr , partitionIndex + 1 , r , k , a , b ) ; } return ; } void findMedian ( int arr [ ] , int n ) { int ans , a = -1 , b = -1 ; if ( n % 2 == 1 ) { MedianUtil ( arr , 0 , n - 1 , n / 2 , a , b ) ; ans = b ; } else { MedianUtil ( arr , 0 , n - 1 , n / 2 , a , b ) ; ans = ( a + b ) / 2 ; } cout << " Median ▁ = ▁ " << ans ; } int main ( ) { int arr [ ] = { 12 , 3 , 5 , 7 , 4 , 19 , 26 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMedian ( arr , n ) ; return 0 ; }
Largest number N which can be reduced to 0 in K steps | C ++ program to implement above approach ; Utility function to return the first digit of a number . ; Remove last digit from number till only one digit is left ; return the first digit ; Utility function that returns the count of numbers written down when starting from n ; Function to find the largest number N which can be reduced to 0 in K steps ; Get the sequence length of the mid point ; Until k sequence length is reached ; Update mid point ; Get count of the new mid point ; Update right to mid ; Update left to mid ; Increment mid point by one while count is equal to k to get the maximum value of mid point ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { while ( n >= 10 ) { n /= 10 ; } return n ; } int getCount ( int n ) { int count = 1 ; while ( n != 0 ) { int leadDigit = firstDigit ( n ) ; n -= leadDigit ; count ++ ; } return count ; } int getLargestNumber ( int k ) { int left = k ; int right = k * 10 ; int mid = ( left + right ) / 2 ; int len = getCount ( mid ) ; while ( len != k ) { mid = ( left + right ) / 2 ; len = getCount ( mid ) ; if ( len > k ) { right = mid ; } else { left = mid ; } } while ( len == k ) { if ( len != getCount ( mid + 1 ) ) { break ; } mid ++ ; } return ( mid ) ; } int main ( ) { int k = 3 ; cout << getLargestNumber ( k ) ; return 0 ; }
Check if point ( X , Y ) can be reached from origin ( 0 , 0 ) with jump of 1 and N perpendicularly simultaneously | C ++ program for the above approach ; Function to check if ( X , Y ) is reachable from ( 0 , 0 ) using the jumps of given type ; Case where source & destination are the same ; Check for even N ( X , Y ) is reachable or not ; If N is odd and parity of X and Y is different return , no valid sequence of jumps exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkReachability ( int N , int X , int Y ) { if ( X == 0 && Y == 0 ) { return " YES " ; } if ( N % 2 == 0 ) { return " YES " ; } else { if ( X % 2 != Y % 2 ) { return " NO " ; } else { return " YES " ; } } } int main ( ) { int N = 2 ; int X = 5 , Y = 4 ; cout << checkReachability ( N , X , Y ) ; return 0 ; }
Find three vertices in an N | C ++ program for the above approach ; Function to find three vertices that subtends an angle closest to A ; Stores the closest angle to A ; Stores the count of edge which subtend an angle of A ; Iterate in the range [ 1 , N - 2 ] ; Stores the angle subtended ; If absolute ( angle - A ) is less than absolute ( mi - A ) ; Update angle to mi , and also update i to ans ; Print the vertices ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define MAXN  1000000 NEW_LINE void closestsAngle ( int N , int A ) { double mi = INT_MAX ; int ans = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double angle = 180.0 * i / N ; if ( fabs ( angle - A ) < fabs ( mi - A ) ) { mi = angle ; ans = i ; } } cout << 2 << ' ▁ ' << 1 << ' ▁ ' << 2 + ans ; } int main ( ) { int N = 3 , A = 15 ; closestsAngle ( N , A ) ; return 0 ; }
Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square | C ++ program for the above approach ; Function to find the area of the triangle that inscribed in square ; Stores the length of the first side of triangle ; Stores the length of the second side of triangle ; Stores the length of the third side of triangle ; Stores the area of the triangle ; Return the resultant area ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double areaOftriangle ( int side ) { double a = sqrt ( pow ( side / 2 , 2 ) + pow ( side / 2 , 2 ) ) ; double b = sqrt ( pow ( side , 2 ) + pow ( side / 2 , 2 ) ) ; double c = sqrt ( pow ( side , 2 ) + pow ( side / 2 , 2 ) ) ; double s = ( a + b + c ) / 2 ; double area = sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; return area ; } int main ( ) { int N = 10 ; cout << areaOftriangle ( N ) ; return 0 ; }