codeparrot/xlcost-text-to-code huggingface dataset:
Product of unique prime factors of a number | C ++ program to find product of unique prime factors of a number ; A function to print all prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int productPrimeFactors ( int n ) { long long int product = 1 ; if ( n % 2 == 0 ) { product *= 2 ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { product = product * i ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) product = product * n ; return product ; } int main ( ) { int n = 44 ; cout << productPrimeFactors ( n ) ; return 0 ; }
Maximizing Probability of one type from N containers | CPP program to find maximum probability of getting a copy ' A ' from N ; Returns the Maximum probability for Drawing 1 copy of number A from N containers with N copies each of numbers A and B ; Pmax = N / ( N + 1 ) ; Driver code ; 1. N = 1 ; 2. N = 2 ; 3. N = 10
#include <bits/stdc++.h> NEW_LINE using namespace std ; double calculateProbability ( int N ) { double probability = ( double ) N / ( N + 1 ) ; return probability ; } int main ( ) { int N ; double probabilityMax ; N = 1 ; probabilityMax = calculateProbability ( N ) ; cout << " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " << N << " ▁ is , ▁ " << setprecision ( 4 ) << fixed << probabilityMax << endl ; N = 2 ; probabilityMax = calculateProbability ( N ) ; cout << " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " << N << " ▁ is , ▁ " << setprecision ( 4 ) << fixed << probabilityMax << endl ; N = 10 ; probabilityMax = calculateProbability ( N ) ; cout << " Maximum ▁ Probability ▁ for ▁ N ▁ = ▁ " << N << " ▁ is , ▁ " << setprecision ( 4 ) << fixed << probabilityMax << endl ; return 0 ; }
Program to implement standard deviation of grouped data | CPP Program to implement standard deviation of grouped data . ; Function to find mean of grouped data . ; Function to find standard deviation of grouped data . ; Formula to find standard deviation of grouped data . ; Driver function . ; Declare and initialize the lower limit of interval . ; Declare and initialize the upper limit of interval . ; Calculating the size of array .
#include <bits/stdc++.h> NEW_LINE using namespace std ; float mean ( float mid [ ] , int freq [ ] , int n ) { float sum = 0 , freqSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + mid [ i ] * freq [ i ] ; freqSum = freqSum + freq [ i ] ; } return sum / freqSum ; } float groupedSD ( float lower_limit [ ] , float upper_limit [ ] , int freq [ ] , int n ) { float mid [ n ] , sum = 0 , freqSum = 0 , sd ; for ( int i = 0 ; i < n ; i ++ ) { mid [ i ] = ( lower_limit [ i ] + upper_limit [ i ] ) / 2 ; sum = sum + freq [ i ] * mid [ i ] * mid [ i ] ; freqSum = freqSum + freq [ i ] ; } sd = sqrt ( ( sum - freqSum * mean ( mid , freq , n ) * mean ( mid , freq , n ) ) / ( freqSum - 1 ) ) ; return sd ; } int main ( ) { float lower_limit [ ] = { 50 , 61 , 71 , 86 , 96 } ; float upper_limit [ ] = { 60 , 70 , 85 , 95 , 100 } ; int freq [ ] = { 9 , 7 , 9 , 12 , 8 } ; int n = sizeof ( lower_limit ) / sizeof ( lower_limit [ 0 ] ) ; cout << groupedSD ( lower_limit , upper_limit , freq , n ) ; return 0 ; }
Average of first n even natural numbers | C ++ implementation to find Average of sum of first n natural even numbers ; function to find average of sum of first n even numbers ; sum of first n even numbers ; calculating Average ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int avg_of_even_num ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 2 * i ; return sum / n ; } int main ( ) { int n = 9 ; cout << avg_of_even_num ( n ) ; return 0 ; }
Average of first n even natural numbers | CPP Program to find the average of sum of first n even numbers ; Return the average of sum of first n even numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int avg_of_even_num ( int n ) { return n + 1 ; } int main ( ) { int n = 8 ; cout << avg_of_even_num ( n ) << endl ; return 0 ; }
Sum of square of first n odd numbers | Efficient C ++ method to find sum of square of first n odd numbers . ; driver code
#include <iostream> NEW_LINE using namespace std ; int squareSum ( int n ) { return n * ( 4 * n * n - 1 ) / 3 ; } int main ( ) { cout << squareSum ( 8 ) ; return 0 ; }
Check whether given three numbers are adjacent primes | CPP program to check given three numbers are primes are not . ; checks weather given number is prime or not . ; check if n is a multiple of 2 ; if not , then just check the odds ; return next prime number ; start with next number . ; breaks after finding next prime number ; check given three numbers are adjacent primes are not . ; check given three numbers are primes are not . ; find next prime of a ; If next is not same as ' a ' ; If next next is not same as ' c ' ; Driver code for above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i * i <= n ; i += 2 ) if ( n % i == 0 ) return false ; return true ; } int nextPrime ( int start ) { int next = start + 1 ; while ( ! isPrime ( next ) ) next ++ ; return next ; } bool areAdjacentPrimes ( int a , int b , int c ) { if ( ! isPrime ( a ) || ! isPrime ( b ) || ! isPrime ( c ) ) return false ; int next = nextPrime ( a ) ; if ( next != b ) return false ; if ( nextPrime ( b ) != c ) return false ; return true ; } int main ( ) { if ( areAdjacentPrimes ( 11 , 13 , 19 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether a number is semiprime or not | C ++ Program to check whether number is semiprime or not ; Utility function to check whether number is semiprime or not ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; Return '1' if count is equal to '2' else return '0' ; Function to print ' True ' or ' False ' according to condition of semiprime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) num /= i , ++ cnt ; if ( num > 1 ) ++ cnt ; return cnt == 2 ; } void semiprime ( int n ) { if ( checkSemiprime ( n ) ) cout << " True STRNEWLINE " ; else cout << " False STRNEWLINE " ; } int main ( ) { int n = 6 ; semiprime ( n ) ; n = 8 ; semiprime ( n ) ; return 0 ; }
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | Program to find sum of series 1 + 2 + 2 + 3 + . . . + n ; Function to find sum of series . ; Driver function . ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + i * i ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | C ++ Program to find sum of series 1 + 2 + 2 + 3 + . . . + n ; Function to find sum of series . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Maximum binomial coefficient term value | CPP Program to find maximum binomial coefficient term ; Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return maximum binomial coefficient term value . ; if n is even ; if n is odd ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int maxcoefficientvalue ( int n ) { if ( n % 2 == 0 ) return binomialCoeff ( n , n / 2 ) ; else return binomialCoeff ( n , ( n + 1 ) / 2 ) ; } int main ( ) { int n = 4 ; cout << maxcoefficientvalue ( n ) << endl ; return 0 ; }
Smallest n digit number divisible by given three numbers | C ++ program to find smallest n digit number which is divisible by x , y and z . ; LCM for x , y , z ; returns smallest n digit number divisible by x , y and z ; find the LCM ; find power of 10 for least number ; reminder after ; If smallest number itself divides lcm . ; add lcm - reminder number for next n digit number ; this condition check the n digit number is possible or not if it is possible it return the number else return 0 ; driver code ; if number is possible then it print the number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LCM ( int x , int y , int z ) { int ans = ( ( x * y ) / ( __gcd ( x , y ) ) ) ; return ( ( z * ans ) / ( __gcd ( ans , z ) ) ) ; } int findDivisible ( int n , int x , int y , int z ) { int lcm = LCM ( x , y , z ) ; int ndigitnumber = pow ( 10 , n - 1 ) ; int reminder = ndigitnumber % lcm ; if ( reminder == 0 ) return ndigitnumber ; ndigitnumber += lcm - reminder ; if ( ndigitnumber < pow ( 10 , n ) ) return ndigitnumber ; else return 0 ; } int main ( ) { int n = 4 , x = 2 , y = 3 , z = 5 ; int res = findDivisible ( n , x , y , z ) ; if ( res != 0 ) cout << res ; else cout << " Not ▁ possible " ; return 0 ; }
Sum of squares of first n natural numbers | CPP Program to find sum of square of first n natural numbers . This program avoids overflow upto some extent for large value of n . ; Return the sum of square of first n natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int squaresum ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } int main ( ) { int n = 4 ; cout << squaresum ( n ) << endl ; return 0 ; }
Program to calculate distance between two points | C ++ code to compute distance ; Function to calculate distance ; Calculating distance ; Drivers Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float distance ( int x1 , int y1 , int x2 , int y2 ) { return sqrt ( pow ( x2 - x1 , 2 ) + pow ( y2 - y1 , 2 ) * 1.0 ) ; } int main ( ) { cout << distance ( 3 , 4 , 4 , 3 ) ; return 0 ; }
Check if a large number is divisibility by 15 | CPP program to check if a large number is divisible by 15 ; function to check if a large number is divisible by 15 ; length of string ; check divisibility by 5 ; Sum of digits ; if divisible by 3 ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( string s ) { int n = s . length ( ) ; if ( s [ n - 1 ] != '5' and s [ n - 1 ] != '0' ) return false ; int sum = accumulate ( begin ( s ) , end ( s ) , 0 ) - '0' * n ; return ( sum % 3 == 0 ) ; } int main ( ) { string s = "15645746327462384723984023940239" ; isDivisible ( s ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; string s1 = "15645746327462384723984023940235" ; isDivisible ( s1 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Shortest path with exactly k edges in a directed and weighted graph | C ++ program to find shortest path with exactly k edges ; Define number of vertices in the graph and inifinite value ; A naive recursive function to count walks from u to v with k edges ; Base cases ; Initialize result ; Go to all adjacents of u and recur ; driver program to test above function ; Let us create the graph shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define V  4 NEW_LINE #define INF  INT_MAX NEW_LINE int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 0 ; if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ; if ( k <= 0 ) return INF ; int res = INF ; for ( int i = 0 ; i < V ; i ++ ) { if ( graph [ u ] [ i ] != INF && u != i && v != i ) { int rec_res = shortestPath ( graph , i , v , k - 1 ) ; if ( rec_res != INF ) res = min ( res , graph [ u ] [ i ] + rec_res ) ; } } return res ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 10 , 3 , 2 } , { INF , 0 , INF , 7 } , { INF , INF , 0 , 6 } , { INF , INF , INF , 0 } } ; int u = 0 , v = 3 , k = 2 ; cout << " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is ▁ " << shortestPath ( graph , u , v , k ) ; return 0 ; }
Shortest path with exactly k edges in a directed and weighted graph | Dynamic Programming based C ++ program to find shortest path with exactly k edges ; Define number of vertices in the graph and inifinite value ; A Dynamic programming based function to find the shortest path from u to v with exactly k edges . ; Table to be filled up using DP . The value sp [ i ] [ j ] [ e ] will store weight of the shortest path from i to j with exactly k edges ; Loop for number of edges from 0 to k ; for source ; for destination ; initialize value ; from base cases ; go to adjacent only when number of edges is more than 1 ; There should be an edge from i to a and a should not be same as either i or j ; driver program to test above function ; Let us create the graph shown in above diagram
#include <iostream> NEW_LINE #include <climits> NEW_LINE using namespace std ; #define V  4 NEW_LINE #define INF  INT_MAX NEW_LINE int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) { int sp [ V ] [ V ] [ k + 1 ] ; for ( int e = 0 ; e <= k ; e ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { sp [ i ] [ j ] [ e ] = INF ; if ( e == 0 && i == j ) sp [ i ] [ j ] [ e ] = 0 ; if ( e == 1 && graph [ i ] [ j ] != INF ) sp [ i ] [ j ] [ e ] = graph [ i ] [ j ] ; if ( e > 1 ) { for ( int a = 0 ; a < V ; a ++ ) { if ( graph [ i ] [ a ] != INF && i != a && j != a && sp [ a ] [ j ] [ e - 1 ] != INF ) sp [ i ] [ j ] [ e ] = min ( sp [ i ] [ j ] [ e ] , graph [ i ] [ a ] + sp [ a ] [ j ] [ e - 1 ] ) ; } } } } } return sp [ u ] [ v ] [ k ] ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 10 , 3 , 2 } , { INF , 0 , INF , 7 } , { INF , INF , 0 , 6 } , { INF , INF , INF , 0 } } ; int u = 0 , v = 3 , k = 2 ; cout << shortestPath ( graph , u , v , k ) ; return 0 ; }
Multistage Graph ( Shortest Path ) | CPP program to find shortest distance in a multistage graph . ; Returns shortest distance from 0 to N - 1. ; dist [ i ] is going to store shortest distance from node i to node N - 1. ; Calculating shortest path for rest of the nodes ; Initialize distance from i to destination ( N - 1 ) ; Check all nodes of next stages to find shortest distance from i to N - 1. ; Reject if no edge exists ; We apply recursive equation to distance to target through j . and compare with minimum distance so far . ; Driver code ; Graph stored in the form of an adjacency Matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  8 NEW_LINE #define INF  INT_MAX NEW_LINE int shortestDist ( int graph [ N ] [ N ] ) { int dist [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i ] [ j ] == INF ) continue ; dist [ i ] = min ( dist [ i ] , graph [ i ] [ j ] + dist [ j ] ) ; } } return dist [ 0 ] ; } int main ( ) { int graph [ N ] [ N ] = { { INF , 1 , 2 , 5 , INF , INF , INF , INF } , { INF , INF , INF , INF , 4 , 11 , INF , INF } , { INF , INF , INF , INF , 9 , 5 , 16 , INF } , { INF , INF , INF , INF , INF , INF , 2 , INF } , { INF , INF , INF , INF , INF , INF , INF , 18 } , { INF , INF , INF , INF , INF , INF , INF , 13 } , { INF , INF , INF , INF , INF , INF , INF , 2 } , { INF , INF , INF , INF , INF , INF , INF , INF } } ; cout << shortestDist ( graph ) ; return 0 ; }
Reverse Level Order Traversal | A recursive C ++ program to print REVERSE level order traversal ; A binary tree node has data , pointer to left and right child ; Function protoypes ; Function to print REVERSE level order traversal a tree ; Print nodes at a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver code ; Let us create trees shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; void printGivenLevel ( node * root , int level ) ; int height ( node * node ) ; node * newNode ( int data ) ; void reverseLevelOrder ( node * root ) { int h = height ( root ) ; int i ; for ( i = h ; i >= 1 ; i -- ) printGivenLevel ( root , i ) ; } void printGivenLevel ( node * root , int level ) { if ( root == NULL ) return ; if ( level == 1 ) cout << root -> data << " ▁ " ; else if ( level > 1 ) { printGivenLevel ( root -> left , level - 1 ) ; printGivenLevel ( root -> right , level - 1 ) ; } } int height ( node * node ) { if ( node == NULL ) return 0 ; else { int lheight = height ( node -> left ) ; int rheight = height ( node -> right ) ; if ( lheight > rheight ) return ( lheight + 1 ) ; else return ( rheight + 1 ) ; } } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ STRNEWLINE " ; reverseLevelOrder ( root ) ; return 0 ; }
Minimize the number of weakly connected nodes | C ++ code to minimize the number of weakly connected nodes ; Set of nodes which are traversed in each launch of the DFS ; Function traversing the graph using DFS approach and updating the set of nodes ; building a undirected graph ; computes the minimum number of disconnected components when a bi - directed graph is converted to a undirected graph ; Declaring and initializing a visited array ; We check if each node is visited once or not ; We only launch DFS from a node iff it is unvisited . ; Clearing the set of nodes on every relaunch of DFS ; relaunching DFS from an unvisited node . ; iterating over the node set to count the number of nodes visited after making the graph directed and storing it in the variable count . If count / 2 == number of nodes - 1 , then increment count by 1. ; Driver function ; Building graph in the form of a adjacency list
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < int > node ; vector < int > Graph [ 10001 ] ; void dfs ( bool visit [ ] , int src ) { visit [ src ] = true ; node . insert ( src ) ; int len = Graph [ src ] . size ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( ! visit [ Graph [ src ] [ i ] ] ) dfs ( visit , Graph [ src ] [ i ] ) ; } void buildGraph ( int x [ ] , int y [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int p = x [ i ] ; int q = y [ i ] ; Graph [ p ] . push_back ( q ) ; Graph [ q ] . push_back ( p ) ; } } int compute ( int n ) { bool visit [ n + 5 ] ; memset ( visit , false , sizeof ( visit ) ) ; int number_of_nodes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visit [ i ] ) { node . clear ( ) ; dfs ( visit , i ) ; int count = 0 ; for ( auto it = node . begin ( ) ; it != node . end ( ) ; ++ it ) count += Graph [ ( * it ) ] . size ( ) ; count /= 2 ; if ( count == node . size ( ) - 1 ) number_of_nodes ++ ; } } return number_of_nodes ; } int main ( ) { int n = 6 , m = 4 ; int x [ m + 5 ] = { 1 , 1 , 4 , 4 } ; int y [ m + 5 ] = { 2 , 3 , 5 , 6 } ; buildGraph ( x , y , n ) ; cout << compute ( n ) << " ▁ weakly ▁ connected ▁ nodes " ; return 0 ; }
Karp 's minimum mean (or average) weight cycle algorithm | C ++ program to find minimum average weight of a cycle in connected and directed graph . ; a struct to represent edges ; vector to store edges ; calculates the shortest path ; initializing all distances as - 1 ; shortest distance from first vertex to in tself consisting of 0 edges ; filling up the dp table ; Returns minimum value of average weight of a cycle in graph . ; array to store the avg values ; Compute average values for all vertices using weights of shortest paths store in dp . ; Find minimum value in avg [ ] ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int V = 4 ; struct edge { int from , weight ; } ; vector < edge > edges [ V ] ; void addedge ( int u , int v , int w ) { edges [ v ] . push_back ( { u , w } ) ; } void shortestpath ( int dp [ ] [ V ] ) { for ( int i = 0 ; i <= V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) dp [ i ] [ j ] = -1 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < edges [ j ] . size ( ) ; k ++ ) { if ( dp [ i - 1 ] [ edges [ j ] [ k ] . from ] != -1 ) { int curr_wt = dp [ i - 1 ] [ edges [ j ] [ k ] . from ] + edges [ j ] [ k ] . weight ; if ( dp [ i ] [ j ] == -1 ) dp [ i ] [ j ] = curr_wt ; else dp [ i ] [ j ] = min ( dp [ i ] [ j ] , curr_wt ) ; } } } } } double minAvgWeight ( ) { int dp [ V + 1 ] [ V ] ; shortestpath ( dp ) ; double avg [ V ] ; for ( int i = 0 ; i < V ; i ++ ) avg [ i ] = -1 ; for ( int i = 0 ; i < V ; i ++ ) { if ( dp [ V ] [ i ] != -1 ) { for ( int j = 0 ; j < V ; j ++ ) if ( dp [ j ] [ i ] != -1 ) avg [ i ] = max ( avg [ i ] , ( ( double ) dp [ V ] [ i ] - dp [ j ] [ i ] ) / ( V - j ) ) ; } } double result = avg [ 0 ] ; for ( int i = 0 ; i < V ; i ++ ) if ( avg [ i ] != -1 && avg [ i ] < result ) result = avg [ i ] ; return result ; } int main ( ) { addedge ( 0 , 1 , 1 ) ; addedge ( 0 , 2 , 10 ) ; addedge ( 1 , 2 , 3 ) ; addedge ( 2 , 3 , 2 ) ; addedge ( 3 , 1 , 0 ) ; addedge ( 3 , 0 , 8 ) ; cout << minAvgWeight ( ) ; return 0 ; }
Frequency of maximum occurring subsequence in given string | C ++ program for the above approach ; Function to find the frequency ; freq stores frequency of each english lowercase character ; dp [ i ] [ j ] stores the count of subsequence with ' a ' + i and ' a ' + j character ; Increment the count of subsequence j and s [ i ] ; Update the frequency array ; For 1 length subsequence ; For 2 length subsequence ; Return the final result ; Driver Code ; Given string str ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; ll findCount ( string s ) { ll freq [ 26 ] ; ll dp [ 26 ] [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ s [ i ] - ' a ' ] += freq [ j ] ; } freq [ s [ i ] - ' a ' ] ++ ; } ll ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = max ( freq [ i ] , ans ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = max ( dp [ i ] [ j ] , ans ) ; } } return ans ; } int main ( ) { string str = " acbab " ; cout << findCount ( str ) ; return 0 ; }
Create an array such that XOR of subarrays of length K is X | C ++ implementation to Create an array in which the XOR of all elements of each contiguous sub - array of length K is X ; Function to construct the array ; Creating a vector of size K , initialised with 0 ; Initialising the first element with the given XOR ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArray ( int N , int K , int X ) { vector < int > ans ( K , 0 ) ; ans [ 0 ] = X ; for ( int i = 0 ; i < N ; ++ i ) { cout << ans [ i % K ] << " ▁ " ; } cout << endl ; } int main ( ) { int N = 5 , K = 2 , X = 4 ; constructArray ( N , K , X ) ; return 0 ; }