codeparrot/xlcost-text-to-code huggingface dataset:
Represent a given set of points by the best possible straight line | C ++ Program to find m and c for a straight line given , x and y ; function to calculate m and c that best fit points represented by x [ ] and y [ ] ; Driver main function
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void bestApproximate ( int x [ ] , int y [ ] , int n ) { float m , c , sum_x = 0 , sum_y = 0 , sum_xy = 0 , sum_x2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_x += x [ i ] ; sum_y += y [ i ] ; sum_xy += x [ i ] * y [ i ] ; sum_x2 += pow ( x [ i ] , 2 ) ; } m = ( n * sum_xy - sum_x * sum_y ) / ( n * sum_x2 - pow ( sum_x , 2 ) ) ; c = ( sum_y - m * sum_x ) / n ; cout << " m ▁ = " << m ; cout << " c = " } int main ( ) { int x [ ] = { 1 , 2 , 3 , 4 , 5 } ; int y [ ] = { 14 , 27 , 40 , 55 , 68 } ; int n = sizeof ( x ) / sizeof ( x [ 0 ] ) ; bestApproximate ( x , y , n ) ; return 0 ; }
Check for star graph | CPP to find whether given graph is star or not ; define the size of incidence matrix ; function to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define size  4 NEW_LINE bool checkStar ( int mat [ ] [ size ] ) { int vertexD1 = 0 , vertexDn_1 = 0 ; if ( size == 1 ) return ( mat [ 0 ] [ 0 ] == 0 ) ; if ( size == 2 ) return ( mat [ 0 ] [ 0 ] == 0 && mat [ 0 ] [ 1 ] == 1 && mat [ 1 ] [ 0 ] == 1 && mat [ 1 ] [ 1 ] == 0 ) ; for ( int i = 0 ; i < size ; i ++ ) { int degreeI = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( mat [ i ] [ j ] ) degreeI ++ ; if ( degreeI == 1 ) vertexD1 ++ ; else if ( degreeI == size - 1 ) vertexDn_1 ++ ; } return ( vertexD1 == ( size - 1 ) && vertexDn_1 == 1 ) ; } int main ( ) { int mat [ size ] [ size ] = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } } ; checkStar ( mat ) ? cout << " Star ▁ Graph " : cout << " Not ▁ a ▁ Star ▁ Graph " ; return 0 ; }
Find all angles of a given triangle | C ++ Code to find all three angles of a triangle given coordinate of all three vertices ; returns square of distance b / w two points ; Square of lengths be a2 , b2 , c2 ; length of sides be a , b , c ; From Cosine law ; Converting to degree ; printing all the angles ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define PI  3.1415926535 NEW_LINE int lengthSquare ( pair < int , int > X , pair < int , int > Y ) { int xDiff = X . first - Y . first ; int yDiff = X . second - Y . second ; return xDiff * xDiff + yDiff * yDiff ; } void printAngle ( pair < int , int > A , pair < int , int > B , pair < int , int > C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = sqrt ( a2 ) ; float b = sqrt ( b2 ) ; float c = sqrt ( c2 ) ; float alpha = acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = alpha * 180 / PI ; betta = betta * 180 / PI ; gamma = gamma * 180 / PI ; cout << " alpha ▁ : ▁ " << alpha << endl ; cout << " betta ▁ : ▁ " << betta << endl ; cout << " gamma ▁ : ▁ " << gamma << endl ; } int main ( ) { pair < int , int > A = make_pair ( 0 , 0 ) ; pair < int , int > B = make_pair ( 0 , 1 ) ; pair < int , int > C = make_pair ( 1 , 0 ) ; printAngle ( A , B , C ) ; return 0 ; }
Triangle with no point inside | C / C ++ program to find triangle with no point inside ; method to get square of distance between ( x1 , y1 ) and ( x2 , y2 ) ; Method prints points which make triangle with no point inside ; any point can be chosen as first point of triangle ; choose nearest point as second point of triangle ; Get distance from first point and choose nearest one ; Pick third point by finding the second closest point with different slope . ; if already chosen point then skip them ; get distance from first point ; here cross multiplication is compared instead of division comparison ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getDistance ( int x1 , int y1 , int x2 , int y2 ) { return ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ; } void triangleWithNoPointInside ( int points [ ] [ 2 ] , int N ) { int first = 0 ; int second , third ; int minD = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == first ) continue ; int d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( minD > d ) { minD = d ; second = i ; } } minD = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == first i == second ) continue ; int d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( ( points [ i ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ second ] [ 1 ] - points [ first ] [ 1 ] ) != ( points [ second ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ i ] [ 1 ] - points [ first ] [ 1 ] ) && minD > d ) { minD = d ; third = i ; } } cout << points [ first ] [ 0 ] << " , ▁ " << points [ first ] [ 1 ] << endl ; cout << points [ second ] [ 0 ] << " , ▁ " << points [ second ] [ 1 ] << endl ; cout << points [ third ] [ 0 ] << " , ▁ " << points [ third ] [ 1 ] << endl ; } int main ( ) { int points [ ] [ 2 ] = { { 0 , 0 } , { 0 , 2 } , { 2 , 0 } , { 2 , 2 } , { 1 , 1 } } ; int N = sizeof ( points ) / sizeof ( points [ 0 ] ) ; triangleWithNoPointInside ( points , N ) ; return 0 ; }
Minimum steps to minimize n as per given condition | A tabulation based solution ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinSteps ( int n ) { int table [ n + 1 ] ; table [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( ! ( i % 2 ) && ( i % 3 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , table [ i / 2 ] ) ; else if ( ! ( i % 3 ) && ( i % 2 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , table [ i / 3 ] ) ; else if ( ! ( i % 2 ) && ! ( i % 3 ) ) table [ i ] = 1 + min ( table [ i - 1 ] , min ( table [ i / 2 ] , table [ i / 3 ] ) ) ; else table [ i ] = 1 + table [ i - 1 ] ; } return table [ n ] ; } int main ( ) { int n = 14 ; cout << getMinSteps ( n ) ; return 0 ; }
Minimize swaps required to make all prime | Function to pre - calculate the prime [ ] prime [ i ] denotes whether i is prime or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Driver Code ; Pre - calculate prime [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mxn = 1e4 + 1 ; bool prime [ mxn + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= mxn ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= mxn ; i += p ) prime [ i ] = false ; } } } int countMin ( int arr [ ] , int n ) { int cMinSwaps = 0 ; int cPrimeIndices = 0 ; int cPrimeNos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ i + 1 ] ) { cPrimeIndices ++ ; if ( ! prime [ arr [ i ] ] ) cMinSwaps ++ ; else cPrimeNos ++ ; } else if ( prime [ arr [ i ] ] ) { cPrimeNos ++ ; } } if ( cPrimeNos >= cPrimeIndices ) return cMinSwaps ; else return -1 ; } int main ( ) { SieveOfEratosthenes ( ) ; int n = 5 ; int arr [ 5 ] = { 2 , 7 , 8 , 5 , 13 } ; cout << countMin ( arr , n ) ; return 0 ; }
Find the size of Largest Subset with positive Bitwise AND | C ++ program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void largestSubset ( int a [ ] , int N ) { int bit [ 32 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { int x = 31 ; while ( a [ i ] > 0 ) { if ( a [ i ] & 1 == 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } cout << * max_element ( bit , bit + 32 ) ; } int main ( ) { int arr [ ] = { 7 , 13 , 8 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; largestSubset ( arr , N ) ; return 0 ; }
Maximum element in connected component of given node for Q queries | C ++ program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; If the rank are the same ; Update the maximum value ; Function to find the maximum element of the set which belongs to the element queries [ i ] ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the maxValue of the sets ; Update parent [ i ] and maxValue [ i ] to i ; Add arr [ i ] . first and arr [ i ] . second elements to the same set ; Find the parent element of the element queries [ i ] ; Print the maximum value of the set which belongs to the element P ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Find ( vector < int > & parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } void Union ( vector < int > & parent , vector < int > & rank , vector < int > & maxValue , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] == rank [ b ] ) { rank [ a ] ++ ; } if ( rank [ a ] < rank [ b ] ) { int temp = a ; a = b ; b = temp ; } parent [ b ] = a ; maxValue [ a ] = max ( maxValue [ a ] , maxValue [ b ] ) ; } void findMaxValueOfSet ( vector < pair < int , int > > & arr , vector < int > & queries , int R , int N , int M ) { vector < int > parent ( R + 1 ) ; vector < int > rank ( R + 1 , 0 ) ; vector < int > maxValue ( R + 1 ) ; for ( int i = 1 ; i < R + 1 ; i ++ ) { parent [ i ] = maxValue [ i ] = i ; } for ( int i = 0 ; i < N ; i ++ ) { Union ( parent , rank , maxValue , arr [ i ] . first , arr [ i ] . second ) ; } for ( int i = 0 ; i < M ; i ++ ) { int P = Find ( parent , queries [ i ] ) ; cout << maxValue [ P ] << " ▁ " ; } } int main ( ) { int R = 5 ; vector < pair < int , int > > arr { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } } ; vector < int > queries { 2 , 4 , 1 , 3 } ; int N = arr . size ( ) ; int M = queries . size ( ) ; findMaxValueOfSet ( arr , queries , R , N , M ) ; return 0 ; }
Maximum sum of segments among all segments formed in array after Q queries | C ++ program for the above approach ; Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAns = INT_MIN ; int Find ( vector < int > & parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } void Union ( vector < int > & parent , vector < int > & rank , vector < int > & setSum , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] > rank [ b ] ) rank [ a ] ++ ; if ( rank [ b ] > rank [ a ] ) swap ( a , b ) ; parent [ b ] = a ; setSum [ a ] += setSum [ b ] ; } void maxValues ( vector < int > arr , vector < int > queries , int N ) { vector < int > parent ( N + 1 ) ; vector < int > rank ( N + 1 , 0 ) ; vector < int > setSum ( N + 1 , 0 ) ; vector < int > currMax ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = -1 ; setSum [ i ] = arr [ i - 1 ] ; } currMax . push_back ( 0 ) ; for ( int i = N - 1 ; i > 0 ; i -- ) { if ( parent [ queries [ i ] ] == -1 ) { parent [ queries [ i ] ] = queries [ i ] ; } if ( queries [ i ] - 1 >= 0 && parent [ queries [ i ] - 1 ] != -1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) ; } if ( queries [ i ] + 1 <= N && parent [ queries [ i ] + 1 ] != -1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) ; } maxAns = max ( setSum [ queries [ i ] ] , maxAns ) ; currMax . push_back ( maxAns ) ; } for ( int i = currMax . size ( ) - 1 ; i >= 0 ; i -- ) { cout << currMax [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 1 , 3 , 2 , 5 } ; vector < int > queries = { 3 , 4 , 1 , 2 } ; int N = arr . size ( ) ; maxValues ( arr , queries , N ) ; return 0 ; }
ZigZag Level Order Traversal of an N | C ++ program for the above approach ; Structure of a tree node ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int val ; vector < Node * > child ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> val = key ; return temp ; } void zigzagLevelOrder ( Node * root ) { if ( root == NULL ) return ; vector < vector < int > > result ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; vector < int > curLevel ; for ( int i = 0 ; i < size ; i ++ ) { Node * node = q . front ( ) ; q . pop ( ) ; curLevel . push_back ( node -> val ) ; for ( int j = 0 ; j < node -> child . size ( ) ; j ++ ) { q . push ( node -> child [ j ] ) ; } } result . push_back ( curLevel ) ; } for ( int i = 0 ; i < result . size ( ) ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) { reverse ( result [ i ] . begin ( ) , result [ i ] . end ( ) ) ; } for ( int j = 0 ; j < result [ i ] . size ( ) ; j ++ ) { cout << result [ i ] [ j ] << " ▁ " ; } cout << endl ; } } zigzagLevelOrder ( root ) ; return 0 ; }
Minimum length paths between 1 to N including each node | C ++ program for the above approach ; Function to calculate the distances from node 1 to N ; Vector to store our edges ; Storing the edgees in the Vector ; Initialize queue ; BFS from first node using queue ; Pop from queue ; Traversing its adjacency list ; Initialize queue ; BFS from last node using queue ; Pop from queue ; Traversing its adjacency list ; Printing the minimum distance including node i ; If not reachable ; Path exists ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE void minDisIncludingNode ( int n , int m , int edges [ ] [ 2 ] ) { vector < ll > g [ 10005 ] ; for ( int i = 0 ; i < m ; i ++ ) { int a = edges [ i ] [ 0 ] - 1 ; int b = edges [ i ] [ 1 ] - 1 ; g [ a ] . push_back ( b ) ; g [ b ] . push_back ( a ) ; } queue < pair < ll , ll > > q ; q . push ( { 0 , 0 } ) ; vector < int > dist ( n , 1e9 ) ; dist [ 0 ] = 0 ; while ( ! q . empty ( ) ) { auto up = q . front ( ) ; q . pop ( ) ; int x = up . first ; int lev = up . second ; if ( lev > dist [ x ] ) continue ; if ( x == n - 1 ) continue ; for ( ll y : g [ x ] ) { if ( dist [ y ] > lev + 1 ) { dist [ y ] = lev + 1 ; q . push ( { y , lev + 1 } ) ; } } } queue < pair < ll , ll > > q1 ; q1 . push ( { n - 1 , 0 } ) ; vector < int > dist1 ( n , 1e9 ) ; dist1 [ n - 1 ] = 0 ; while ( ! q1 . empty ( ) ) { auto up = q1 . front ( ) ; q1 . pop ( ) ; int x = up . first ; int lev = up . second ; if ( lev > dist1 [ x ] ) continue ; if ( x == 0 ) continue ; for ( ll y : g [ x ] ) { if ( dist1 [ y ] > lev + 1 ) { dist1 [ y ] = lev + 1 ; q1 . push ( { y , lev + 1 } ) ; } } } for ( int i = 0 ; i < n ; i ++ ) { if ( dist [ i ] + dist1 [ i ] > 1e9 ) cout << -1 << " ▁ " ; else cout << dist [ i ] + dist1 [ i ] << " ▁ " ; } } int main ( ) { int n = 5 ; int m = 7 ; int edges [ m ] [ 2 ] = { { 1 , 2 } , { 1 , 4 } , { 2 , 3 } , { 2 , 5 } , { 4 , 3 } , { 4 , 5 } , { 1 , 5 } } ; minDisIncludingNode ( n , m , edges ) ; return 0 ; }
Check if possible to make Array sum equal to Array product by replacing exactly one element | C ++ Program to implement the above approach ; Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int canPossibleReplacement ( int N , int arr [ ] ) { int S = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) S += arr [ i ] ; int P = 1 ; for ( i = 0 ; i < N ; i ++ ) { P *= i ; } for ( int i = 0 ; i < N ; i ++ ) { int x = arr [ i ] ; int y = ( S - x ) / ( P / x - 1 ) ; if ( ( S - x + y ) == ( P * y ) / x ) return 1 ; } return 0 ; } int main ( ) { int N = 3 ; int arr [ ] = { 1 , 3 , 4 } ; if ( canPossibleReplacement ( N , arr ) == 1 ) cout << ( " Yes " ) ; else cout << ( " No " ) ; return 0 ; }
Check if all the digits of the given number are same | Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code
#include <iostream> NEW_LINE using namespace std ; string checkSameDigits ( int N ) { int length = ( N ) + 1 ; int M = ( ( 10 , length ) - 1 ) / ( 10 - 1 ) ; M *= N % 10 ; if ( M = N ) return " Yes " ; return " No " ; } int main ( ) { int N = 222 ; cout << checkSameDigits ( N ) ; }
Maximize sum of path from the Root to a Leaf node in N | C ++ program for the above approach ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int val ; vector < Node * > child ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> val = key ; return temp ; } void DFS ( Node * root , int sum , int & ans ) { if ( root -> child . size ( ) == 0 ) { ans = max ( ans , sum ) ; return ; } for ( int i = 0 ; i < root -> child . size ( ) ; i ++ ) { DFS ( root -> child [ i ] , sum + root -> child [ i ] -> val , ans ) ; } } Node * root = newNode ( 1 ) ; ( root -> child ) . push_back ( newNode ( 2 ) ) ; ( root -> child ) . push_back ( newNode ( 3 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( newNode ( 4 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( newNode ( 6 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( newNode ( 5 ) ) ; ( root -> child [ 1 ] ) -> child . push_back ( newNode ( 7 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( newNode ( 8 ) ) ; int maxSumPath = 0 ; DFS ( root , root -> val , maxSumPath ) ; cout << maxSumPath ; return 0 ; }
Count of even sum triplets in the array for Q range queries | C ++ program for above approach ; Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Printing the ans ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countTriplets ( int size , int queries , int arr [ ] , int Q [ ] [ 2 ] ) { int arr_even [ size + 1 ] , arr_odd [ size + 1 ] ; int even = 0 , odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( int i = 0 ; i < queries ; i ++ ) { int l = Q [ i ] [ 0 ] , r = Q [ i ] [ 1 ] ; int odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; int even = arr_even [ r ] - arr_even [ l - 1 ] ; int ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; cout << ans << " ▁ " ; } } int main ( ) { int N = 6 , Q = 2 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int queries [ ] [ 2 ] = { { 1 , 3 } , { 2 , 5 } } ; countTriplets ( N , Q , arr , queries ) ; return 0 ; }
Maximize the rightmost element of an array in k operations in Linear Time | C ++ program for above approach ; Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; Printing rightmost element ; Driver Code ; Given Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; void maxRightmostElement ( int N , int k , int p , int arr [ ] ) { while ( k ) { for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= p ) { arr [ i ] = arr [ i ] - p ; arr [ i + 1 ] = arr [ i + 1 ] + p ; break ; } } k -- ; } cout << arr [ N - 1 ] << endl ; } int main ( ) { int N = 4 , p = 2 , k = 5 , arr [ ] = { 3 , 8 , 1 , 4 } ; maxRightmostElement ( N , k , p , arr ) ; return 0 ; }
Mean of minimum of all possible K | C ++ program for the above approach ; Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nCr ( int n , int r , int f [ ] ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } int findMean ( int N , int X ) { int f [ N + 1 ] ; f [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } int total = nCr ( N , X , f ) ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } double E_X = double ( count ) / double ( total ) ; cout << setprecision ( 10 ) << E_X ; return 0 ; } int main ( ) { int N = 3 , X = 2 ; findMean ( N , X ) ; return 0 ; }