codeparrot/xlcost-text-to-code huggingface dataset:
Decimal equivalent of concatenation of absolute difference of floor and rounded | C ++ program for the above approach ; Function to find the decimal equivalent of the new binary array constructed from absolute decimal of floor and the round - off values ; Traverse the givenarray from the end ; Stores the absolute difference between floor and round - off each array element ; If bit / difference is 1 , then calculate the bit by proper power of 2 and add it to result ; Increment the value of power ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDecimal ( float arr [ ] , int N ) { int bit , power = 0 , result = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { bit = abs ( floor ( arr [ i ] ) - round ( arr [ i ] ) ) ; if ( bit ) result += pow ( 2 , power ) ; power ++ ; } cout << result ; } int main ( ) { float arr [ ] = { 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDecimal ( arr , N ) ; return 0 ; }
Calculate money placed in boxes after N days based on given conditions | C ++ program for the above approach ; Function to find the total money placed in boxes after N days ; Stores the total money ; Iterate for N days ; Adding the Week number ; Adding previous amount + 1 ; Return the total amount ; Driver code ; Input ; Function call to find total money placed
#include <iostream> NEW_LINE using namespace std ; int totalMoney ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += i / 7 ; ans += ( i % 7 + 1 ) ; } return ans ; } int main ( ) { int N = 15 ; cout << totalMoney ( N ) ; }
Calculate money placed in boxes after N days based on given conditions | C ++ Program to implement the above approach ; Function to find total money placed in the box ; Number of complete weeks ; Remaining days in the last week ; Driver Code ; Input ; Function call to find the total money placed
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalMoney ( int N ) { int CompWeeks = N / 7 ; int RemDays = N % 7 ; int X = 28 * CompWeeks + 7 * ( CompWeeks * ( CompWeeks - 1 ) / 2 ) ; int Y = RemDays * ( RemDays + 1 ) / 2 + CompWeeks * RemDays ; int cost = X + Y ; cout << cost << ' ' ; } int main ( ) { int N = 15 ; totalMoney ( N ) ; return 0 ; }
Sum of absolute differences of indices of occurrences of each array element | Set 2 | C ++ program for the above approach ; Stores the count of occurrences and previous index of every element ; Constructor ; Function to calculate the sum of absolute differences of indices of occurrences of array element ; Stores the count of elements and their previous indices ; Initialize 2 arrays left [ ] and right [ ] of size N ; Traverse the given array ; If arr [ i ] is present in the Map ; Update left [ i ] to 0 and update the value of arr [ i ] in map ; Otherwise , get the value from the map and update left [ i ] ; Clear the map to calculate right [ ] array ; Traverse the array arr [ ] in reverse ; If arr [ i ] is present in theMap ; Update right [ i ] to 0 and update the value of arr [ i ] in the Map ; Otherwise get the value from the map and update right [ i ] ; Iterate in the range [ 0 , N - 1 ] and print the sum of left [ i ] and right [ i ] as the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct pairr { int count , prevIndex ; pairr ( int countt , int prevIndexx ) { count = countt ; prevIndex = prevIndexx ; } } ; void findSum ( int arr [ ] , int n ) { map < int , pairr * > mapp ; int left [ n ] ; int right [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapp . find ( arr [ i ] ) == mapp . end ( ) ) { left [ i ] = 0 ; mapp [ arr [ i ] ] = new pairr ( 1 , i ) ; } else { pairr * tmp = mapp [ arr [ i ] ] ; left [ i ] = ( tmp -> count ) * ( i - tmp -> prevIndex ) + left [ tmp -> prevIndex ] ; mapp [ arr [ i ] ] = new pairr ( tmp -> count + 1 , i ) ; } } mapp . clear ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( mapp . find ( arr [ i ] ) == mapp . end ( ) ) { right [ i ] = 0 ; mapp [ arr [ i ] ] = new pairr ( 1 , i ) ; } else { pairr * tmp = mapp [ arr [ i ] ] ; right [ i ] = ( tmp -> count ) * ( abs ( i - tmp -> prevIndex ) ) + right [ tmp -> prevIndex ] ; mapp [ arr [ i ] ] = new pairr ( tmp -> count + 1 , i ) ; } } for ( int i = 0 ; i < n ; i ++ ) cout << left [ i ] + right [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 1 , 2 } ; int N = 5 ; findSum ( arr , N ) ; }
Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array | C ++ program for the above approach ; Function to check if a digit x is present in the number N or not ; Convert N to string ; Traverse the string num ; Return first occurrence of the digit x ; Function to remove the character at a given index from the number ; Convert N to string ; Store the resultant string ; Traverse the string num ; If the number becomes empty after deletion , then return - 1 ; Return the number ; Function to check if A can be reduced to B by performing the operations any number of times ; Create a queue ; Push A into the queue ; Hashmap to check if the element is present in the Queue or not ; Set A as visited ; Iterate while the queue is not empty ; Store the front value of the queue and pop it from it ; If top is equal to B , then return true ; Traverse the array , D [ ] ; Divide top by D [ i ] if it is possible and push the result in q ; If D [ i ] is present at the top ; Remove the first occurrence of D [ i ] from the top and store the new number ; Push newElement into the queue q ; Return false if A can not be reduced to B ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPresent ( int n , int x ) { string num = to_string ( n ) ; for ( int i = 0 ; i < num . size ( ) ; i ++ ) { if ( ( num [ i ] - '0' ) == x ) return i ; } return -1 ; } int removeDigit ( int n , int index ) { string num = to_string ( n ) ; string ans = " " ; for ( int i = 0 ; i < num . size ( ) ; i ++ ) { if ( i != index ) ans += num [ i ] ; } if ( ans == " " || ( ans . size ( ) == 1 && ans [ 0 ] == '0' ) ) return -1 ; int x = stoi ( ans ) ; return x ; } bool reduceNtoX ( int a , int b , int d [ ] , int n ) { queue < int > q ; q . push ( a ) ; unordered_map < int , bool > visited ; visited [ a ] = true ; while ( ! q . empty ( ) ) { int top = q . front ( ) ; q . pop ( ) ; if ( top < 0 ) continue ; if ( top == b ) return true ; for ( int i = 0 ; i < n ; i ++ ) { if ( d [ i ] != 0 && top % d [ i ] == 0 && ! visited [ top / d [ i ] ] ) { q . push ( top / d [ i ] ) ; visited [ top / d [ i ] ] = true ; } int index = isPresent ( top , d [ i ] ) ; if ( index != -1 ) { int newElement = removeDigit ( top , index ) ; if ( newElement != -1 && ( ! visited [ newElement ] ) ) { q . push ( newElement ) ; visited [ newElement ] = true ; } } } } return false ; } int main ( ) { int A = 5643 , B = 81 ; int D [ ] = { 3 , 8 , 1 } ; int N = sizeof ( D ) / sizeof ( D [ 0 ] ) ; if ( reduceNtoX ( A , B , D , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Prime triplets consisting of values up to N having difference between two elements equal to the third | C ++ program for the above approach ; Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; Consider all numbers to prime initially ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all tultiples of p as false ; Function to find all prime triplets satisfying the given conditions ; Generate all primes up to N ; Stores the triplets ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime [ 100000 ] ; void SieveOfEratosthenes ( int n ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } void findTriplets ( int N ) { SieveOfEratosthenes ( N ) ; vector < vector < int > > V ; for ( int i = 3 ; i <= N ; i ++ ) { if ( 2 + i <= N && prime [ i ] && prime [ 2 + i ] ) { V . push_back ( { 2 , i , i + 2 } ) ; } } for ( int i = 0 ; i < V . size ( ) ; i ++ ) { cout << V [ i ] [ 0 ] << " ▁ " << V [ i ] [ 1 ] << " ▁ " << V [ i ] [ 2 ] << " STRNEWLINE " ; } } int main ( ) { int N = 8 ; findTriplets ( N ) ; return 0 ; }
Smallest number which is not coprime with any element of an array | C ++ program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to calculate GCD of two numbers ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Print the answer ; Driver Code ; Given array ; Stores the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE #define MAX  50 NEW_LINE bool isPrime ( ll n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( ll i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } void findPrime ( vector < ll > & primes ) { for ( ll i = 2 ; i <= MAX ; i ++ ) { if ( isPrime ( i ) ) primes . push_back ( i ) ; } } ll gcd ( ll a , ll b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } void findMinimumNumber ( ll arr [ ] , ll N ) { vector < ll > primes ; findPrime ( primes ) ; ll ans = INT_MAX ; ll n = primes . size ( ) ; for ( ll i = 1 ; i < ( 1 << n ) ; i ++ ) { ll temp = 1 ; for ( ll j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) { temp *= primes [ j ] ; } } bool check = true ; for ( ll k = 0 ; k < N ; k ++ ) { if ( gcd ( temp , arr [ k ] ) == 1 ) { check = false ; break ; } } if ( check ) ans = min ( ans , temp ) ; } cout << ans ; } int main ( ) { ll arr [ ] = { 3 , 4 , 6 , 7 , 8 , 9 , 10 } ; ll N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinimumNumber ( arr , N ) ; return 0 ; }
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits | C ++ program for the above approach ; Function to check if a number N can be expressed as sum of its digits raised to the power of the count of digits ; Stores the number of digits ; Stores the resultant number ; Return true if both the numbers are same ; Function to precompute and store for all numbers whether they can be expressed ; Mark all the index which are plus perfect number ; If true , then update the value at this index ; Compute prefix sum of the array ; Function to count array elements that can be expressed as the sum of digits raised to the power of count of digits ; Precompute the results ; Traverse the queries ; Print the resultant count ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define R  100005 NEW_LINE int arr [ R ] ; bool canExpress ( int N ) { int temp = N ; int n = 0 ; while ( N != 0 ) { N /= 10 ; n ++ ; } N = temp ; int sum = 0 ; while ( N != 0 ) { sum += pow ( N % 10 , n ) ; N /= 10 ; } return ( sum == temp ) ; } void precompute ( ) { for ( int i = 1 ; i < R ; i ++ ) { if ( canExpress ( i ) ) { arr [ i ] = 1 ; } } for ( int i = 1 ; i < R ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; } } void countNumbers ( int queries [ ] [ 2 ] , int N ) { precompute ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int L1 = queries [ i ] [ 0 ] ; int R1 = queries [ i ] [ 1 ] ; cout << ( arr [ R1 ] - arr [ L1 - 1 ] ) << ' ▁ ' ; } } int main ( ) { int queries [ ] [ 2 ] = { { 1 , 400 } , { 1 , 9 } } ; int N = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; countNumbers ( queries , N ) ; return 0 ; }
Queries to calculate sum of the path from root to a given node in given Binary Tree | C ++ program for the above approach ; Function to find the sum of the path from root to the current node ; Sum of nodes in the path ; Iterate until root is reached ; Update the node value ; Print the resultant sum ; Function to print the path sum for each query ; Traverse the queries ; Driver Code ; arraylist to store integers
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void sumOfNodeInAPath ( int node_value ) { int sum_of_node = 0 ; while ( node_value ) { sum_of_node += node_value ; node_value /= 2 ; } cout << sum_of_node ; return ; } void findSum ( vector < int > Q ) { for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { int node_value = Q [ i ] ; sumOfNodeInAPath ( node_value ) ; cout << " ▁ " ; } } int main ( ) { vector < int > arr = { 1 , 5 , 20 , 100 } ; findSum ( arr ) ; return 0 ; }
Minimum removals required to make frequency of all remaining array elements equal | C ++ program for the above approach ; Function to count the minimum removals required to make frequency of all array elements equal ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDeletions ( int arr [ ] , int N ) { map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } vector < int > v ; for ( auto z : freq ) { v . push_back ( z . second ) ; } sort ( v . begin ( ) , v . end ( ) ) ; int size = v . size ( ) ; int ans = N - ( v [ 0 ] * size ) ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] != v [ i - 1 ] ) { int safe = v [ i ] * ( size - i ) ; ans = min ( ans , N - safe ) ; } } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 2 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minDeletions ( arr , N ) ; }
Check if a string is the typed name of the given name | CPP program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { string vowel = " aeiou " ; for ( int i = 0 ; i < vowel . length ( ) ; ++ i ) if ( vowel [ i ] == c ) return true ; return false ; } bool printRLE ( string str , string typed ) { int n = str . length ( ) , m = typed . length ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != typed [ j ] ) return false ; if ( isVowel ( str [ i ] ) == false ) { j ++ ; continue ; } int count1 = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count1 ++ ; i ++ ; } int count2 = 1 ; while ( j < m - 1 && typed [ j ] == str [ i ] ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) return false ; } return true ; } int main ( ) { string name = " alex " , typed = " aaalaeex " ; if ( printRLE ( name , typed ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Reverse alternate levels of a perfect binary tree | C ++ program to reverse alternate levels of a tree ; Base cases ; Swap subtrees if level is even ; Recur for left and right subtrees ( Note : left of root1 is passed and right of root2 in first call and opposite in second call . ; This function calls preorder ( ) for left and right children of root ; Inorder traversal ( used to print initial andmodified trees ) ; A utility function to create a new node ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { char key ; Node * left , * right ; } ; void preorder ( struct Node * root1 , struct Node * root2 , int lvl ) { if ( root1 == NULL root2 == NULL ) return ; if ( lvl % 2 == 0 ) swap ( root1 -> key , root2 -> key ) ; preorder ( root1 -> left , root2 -> right , lvl + 1 ) ; preorder ( root1 -> right , root2 -> left , lvl + 1 ) ; } void reverseAlternate ( struct Node * root ) { preorder ( root -> left , root -> right , 0 ) ; } void printInorder ( struct Node * root ) { if ( root == NULL ) return ; printInorder ( root -> left ) ; cout << root -> key << " ▁ " ; printInorder ( root -> right ) ; } Node * newNode ( int key ) { Node * temp = new Node ; temp -> left = temp -> right = NULL ; temp -> key = key ; return temp ; } int main ( ) { struct Node * root = newNode ( ' a ' ) ; root -> left = newNode ( ' b ' ) ; root -> right = newNode ( ' c ' ) ; root -> left -> left = newNode ( ' d ' ) ; root -> left -> right = newNode ( ' e ' ) ; root -> right -> left = newNode ( ' f ' ) ; root -> right -> right = newNode ( ' g ' ) ; root -> left -> left -> left = newNode ( ' h ' ) ; root -> left -> left -> right = newNode ( ' i ' ) ; root -> left -> right -> left = newNode ( ' j ' ) ; root -> left -> right -> right = newNode ( ' k ' ) ; root -> right -> left -> left = newNode ( ' l ' ) ; root -> right -> left -> right = newNode ( ' m ' ) ; root -> right -> right -> left = newNode ( ' n ' ) ; root -> right -> right -> right = newNode ( ' o ' ) ; cout << " Inorder ▁ Traversal ▁ of ▁ given ▁ tree STRNEWLINE " ; printInorder ( root ) ; reverseAlternate ( root ) ; cout << " Inorder Traversal of modified tree " ; printInorder ( root ) ; return 0 ; }
Deletion in a Binary Tree | C ++ program to delete element in binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; function to create a new node of tree and return pointer ; Inorder traversal of a binary tree ; function to delete the given deepest node ( d_node ) in binary tree ; Do level order traversal until last node ; function to delete element in binary tree ; Do level order traversal to find deepest node ( temp ) and node to be deleted ( key_node ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { struct Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } ; void inorder ( struct Node * temp ) { if ( ! temp ) return ; inorder ( temp -> left ) ; cout << temp -> key << " ▁ " ; inorder ( temp -> right ) ; } void deletDeepest ( struct Node * root , struct Node * d_node ) { queue < struct Node * > q ; q . push ( root ) ; struct Node * temp ; while ( ! q . empty ( ) ) { temp = q . front ( ) ; q . pop ( ) ; if ( temp == d_node ) { temp = NULL ; delete ( d_node ) ; return ; } if ( temp -> right ) { if ( temp -> right == d_node ) { temp -> right = NULL ; delete ( d_node ) ; return ; } else q . push ( temp -> right ) ; } if ( temp -> left ) { if ( temp -> left == d_node ) { temp -> left = NULL ; delete ( d_node ) ; return ; } else q . push ( temp -> left ) ; } } } Node * deletion ( struct Node * root , int key ) { if ( root == NULL ) return NULL ; if ( root -> left == NULL && root -> right == NULL ) { if ( root -> key == key ) return NULL ; else return root ; } queue < struct Node * > q ; q . push ( root ) ; struct Node * temp ; struct Node * key_node = NULL ; while ( ! q . empty ( ) ) { temp = q . front ( ) ; q . pop ( ) ; if ( temp -> key == key ) key_node = temp ; if ( temp -> left ) q . push ( temp -> left ) ; if ( temp -> right ) q . push ( temp -> right ) ; } if ( key_node != NULL ) { int x = temp -> key ; deletDeepest ( root , temp ) ; key_node -> key = x ; } return root ; } int main ( ) { struct Node * root = newNode ( 10 ) ; root -> left = newNode ( 11 ) ; root -> left -> left = newNode ( 7 ) ; root -> left -> right = newNode ( 12 ) ; root -> right = newNode ( 9 ) ; root -> right -> left = newNode ( 15 ) ; root -> right -> right = newNode ( 8 ) ; cout << " Inorder ▁ traversal ▁ before ▁ deletion ▁ : ▁ " ; inorder ( root ) ; int key = 11 ; root = deletion ( root , key ) ; cout << endl ; cout << " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ; inorder ( root ) ; return 0 ; }
Number of sink nodes in a graph | C ++ program to count number if sink nodes ; Return the number of Sink NOdes . ; Array for marking the non - sink node . ; Marking the non - sink node . ; Counting the sink nodes . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int mark [ n ] ; memset ( mark , 0 , sizeof mark ) ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( ! mark [ i ] ) count ++ ; return count ; } int main ( ) { int n = 4 , m = 2 ; int edgeFrom [ ] = { 2 , 4 } ; int edgeTo [ ] = { 3 , 3 } ; cout << countSink ( n , m , edgeFrom , edgeTo ) << endl ; return 0 ; }
Largest subset of Graph vertices with edges of 2 or more colors | C ++ program to find size of subset of graph vertex such that each vertex has more than 1 color edges ; Number of vertices ; function to calculate max subset size ; set for number of vertices ; loop for deletion of vertex from set ; if subset has only 1 vertex return 0 ; for each vertex iterate and keep removing a vertix while we find a vertex with all edges of same color . ; note down different color values for each vertex ; if only one color is found erase that vertex ( bad vertex ) ; If no vertex was removed in the above loop . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 6 ; int subsetGraph ( int C [ ] [ N ] ) { set < int > vertices ; for ( int i = 0 ; i < N ; ++ i ) vertices . insert ( i ) ; while ( ! vertices . empty ( ) ) { if ( vertices . size ( ) == 1 ) return 1 ; bool someone_removed = false ; for ( int x : vertices ) { set < int > values ; for ( int y : vertices ) if ( y != x ) values . insert ( C [ x ] [ y ] ) ; if ( values . size ( ) == 1 ) { vertices . erase ( x ) ; someone_removed = true ; break ; } } if ( ! someone_removed ) break ; } return ( vertices . size ( ) ) ; } int main ( ) { int C [ ] [ N ] = { { 0 , 9 , 2 , 4 , 7 , 8 } , { 9 , 0 , 9 , 9 , 7 , 9 } , { 2 , 9 , 0 , 3 , 7 , 6 } , { 4 , 9 , 3 , 0 , 7 , 1 } , { 7 , 7 , 7 , 7 , 0 , 7 } , { 8 , 9 , 6 , 1 , 7 , 0 } } ; cout << subsetGraph ( C ) ; return 0 ; }
Count number of edges in an undirected graph | C ++ program to count number of edge in undirected graph ; Adjacency list representation of graph ; add edge to graph ; Returns count of edge in undirected graph ; traverse all vertex ; add all edge that are linked to the current vertex ; The count of edge is always even because in undirected graph every edge is connected twice between two vertices ; driver program to check above function ; making above uhown graph
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Graph { int V ; list < int > * adj ; public : Graph ( int V ) { this -> V = V ; adj = new list < int > [ V ] ; } void addEdge ( int u , int v ) ; int countEdges ( ) ; } ; void Graph :: addEdge ( int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } int Graph :: countEdges ( ) { int sum = 0 ; for ( int i = 0 ; i < V ; i ++ ) sum += adj [ i ] . size ( ) ; return sum / 2 ; } int main ( ) { int V = 9 ; Graph g ( V ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 7 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 1 , 7 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 2 , 8 ) ; g . addEdge ( 2 , 5 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 3 , 5 ) ; g . addEdge ( 4 , 5 ) ; g . addEdge ( 5 , 6 ) ; g . addEdge ( 6 , 7 ) ; g . addEdge ( 6 , 8 ) ; g . addEdge ( 7 , 8 ) ; cout << g . countEdges ( ) << endl ; return 0 ; }