codeparrot/xlcost-text-to-code huggingface dataset:
Maximum difference between node and its ancestor in Binary Tree | C ++ program to find maximum difference between node and its ancestor ; A binary tree node has key , pointer to left child and a pointer to right child ; To create a newNode of tree and return pointer ; Recursive function to calculate maximum ancestor - node difference in binary tree . It updates value at ' res ' to store the result . The returned value of this function is minimum value in subtree rooted with ' t ' ; Returning Maximum int value if node is not there ( one child case ) ; If leaf node then just return node 's value  ; Recursively calling left and right subtree for minimum value ; Updating res if ( node value - minimum value from subtree ) is bigger than res ; Returning minimum value got so far ; This function mainly calls maxDiffUtil ( ) ; Initialising result with minimum int value ; Helper function to print inorder traversal of binary tree ; Driver program to test above functions ; Making above given diagram 's binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int maxDiffUtil ( Node * t , int * res ) { if ( t == NULL ) return INT_MAX ; if ( t -> left == NULL && t -> right == NULL ) return t -> key ; int val = min ( maxDiffUtil ( t -> left , res ) , maxDiffUtil ( t -> right , res ) ) ; * res = max ( * res , t -> key - val ) ; return min ( val , t -> key ) ; } int maxDiff ( Node * root ) { int res = INT_MIN ; maxDiffUtil ( root , & res ) ; return res ; } void inorder ( Node * root ) { if ( root ) { inorder ( root -> left ) ; printf ( " % d ▁ " , root -> key ) ; inorder ( root -> right ) ; } } int main ( ) { Node * root ; root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; root -> right = newNode ( 10 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; printf ( " Maximum ▁ difference ▁ between ▁ a ▁ node ▁ and " " ▁ its ▁ ancestor ▁ is ▁ : ▁ % d STRNEWLINE " , maxDiff ( root ) ) ; }
Query for ancestor | C / C ++ program to query whether two node has ancestor - descendant relationship or not ; Utility dfs method to assign in and out time to each node ; assign In - time to node u ; call dfs over all neighbors except parent ; assign Out - time to node u ; method to preprocess all nodes for assigning time ; construct array of vector data structure for tree ; call dfs method from root ; method returns " yes " if u is a ancestor node of v ; Driver code to test abovea methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( vector < int > g [ ] , int u , int parent , int timeIn [ ] , int timeOut [ ] , int & cnt ) { timeIn [ u ] = cnt ++ ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] [ i ] ; if ( v != parent ) dfs ( g , v , u , timeIn , timeOut , cnt ) ; } timeOut [ u ] = cnt ++ ; } void preProcess ( int edges [ ] [ 2 ] , int V , int timeIn [ ] , int timeOut [ ] ) { vector < int > g [ V ] ; for ( int i = 0 ; i < V - 1 ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; g [ u ] . push_back ( v ) ; g [ v ] . push_back ( u ) ; } int cnt = 0 ; dfs ( g , 0 , -1 , timeIn , timeOut , cnt ) ; } string isAncestor ( int u , int v , int timeIn [ ] , int timeOut [ ] ) { bool b = ( timeIn [ u ] <= timeIn [ v ] && timeOut [ v ] <= timeOut [ u ] ) ; return ( b ? " yes " : " no " ) ; } int main ( ) { int edges [ ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 5 } , { 4 , 6 } , { 5 , 7 } } ; int E = sizeof ( edges ) / sizeof ( edges [ 0 ] ) ; int V = E + 1 ; int timeIn [ V ] , timeOut [ V ] ; preProcess ( edges , V , timeIn , timeOut ) ; int u = 1 ; int v = 6 ; cout << isAncestor ( u , v , timeIn , timeOut ) << endl ; u = 1 ; v = 7 ; cout << isAncestor ( u , v , timeIn , timeOut ) << endl ; return 0 ; }
Print path from root to a given node in a binary tree | C ++ implementation to print the path from root to a given node in a binary tree ; structure of a node of binary tree ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Returns true if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is NULL there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ' ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ▁ ' arr 'and then return false  ; function to print the path from root to the given node if the node lies in the binary tree ; vector to store the path ; if required node ' x ' is present then print the path ; ' x ' is not present in the binary tree ; Driver program to test above ; binary tree formation
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; struct Node * getNode ( int data ) { struct Node * newNode = new Node ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } bool hasPath ( Node * root , vector < int > & arr , int x ) { if ( ! root ) return false ; arr . push_back ( root -> data ) ; if ( root -> data == x ) return true ; if ( hasPath ( root -> left , arr , x ) || hasPath ( root -> right , arr , x ) ) return true ; arr . pop_back ( ) ; return false ; } void printPath ( Node * root , int x ) { vector < int > arr ; if ( hasPath ( root , arr , x ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) cout << arr [ i ] << " - > " ; cout << arr [ arr . size ( ) - 1 ] ; } else cout << " No ▁ Path " ; } int main ( ) { struct Node * root = getNode ( 1 ) ; root -> left = getNode ( 2 ) ; root -> right = getNode ( 3 ) ; root -> left -> left = getNode ( 4 ) ; root -> left -> right = getNode ( 5 ) ; root -> right -> left = getNode ( 6 ) ; root -> right -> right = getNode ( 7 ) ; int x = 5 ; printPath ( root , x ) ; return 0 ; }
Print Ancestors of a given node in Binary Tree | C ++ program to print ancestors of given node ; A binary tree node has data , pointer to left child and a pointer to right child ; If target is present in tree , then prints the ancestors and returns true , otherwise returns false . ; base cases ; If target is present in either left or right subtree of this node , then print this node ; Else return false ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Construct the following binary tree 1 / \ 2 3 / \ 4 5 / 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; bool printAncestors ( struct node * root , int target ) { if ( root == NULL ) return false ; if ( root -> data == target ) return true ; if ( printAncestors ( root -> left , target ) || printAncestors ( root -> right , target ) ) { cout << root -> data << " ▁ " ; return true ; } return false ; } struct node * newnode ( int data ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int main ( ) { struct node * root = newnode ( 1 ) ; root -> left = newnode ( 2 ) ; root -> right = newnode ( 3 ) ; root -> left -> left = newnode ( 4 ) ; root -> left -> right = newnode ( 5 ) ; root -> left -> left -> left = newnode ( 7 ) ; printAncestors ( root , 7 ) ; getchar ( ) ; return 0 ; }
Kth ancestor of a node in binary tree | Set 2 | C ++ program to calculate Kth ancestor of given node ; A Binary Tree Node ; temporary node to keep track of Node returned from previous recursive call during backtrack ; recursive function to calculate Kth ancestor ; Base case ; print the kth ancestor ; return NULL to stop further backtracking ; return current node to previous call ; Utility function to create a new tree node ; Driver program to test above functions ; print kth ancestor of given node ; check if parent is not NULL , it means there is no Kth ancestor of the node
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * temp = NULL ; Node * kthAncestorDFS ( Node * root , int node , int & k ) { if ( ! root ) return NULL ; if ( root -> data == node || ( temp = kthAncestorDFS ( root -> left , node , k ) ) || ( temp = kthAncestorDFS ( root -> right , node , k ) ) ) { if ( k > 0 ) k -- ; else if ( k == 0 ) { cout << " Kth ▁ ancestor ▁ is : ▁ " << root -> data ; return NULL ; } return root ; } } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; int k = 2 ; int node = 5 ; Node * parent = kthAncestorDFS ( root , node , k ) ; if ( parent ) cout << " - 1" ; return 0 ; }
Succinct Encoding of Binary Tree | C ++ program to demonstrate Succinct Tree Encoding and decoding ; A Binary Tree Node ; Utility function to create new Node ; This function fills lists ' struc ' and ' data ' . ' struc ' list stores structure information . ' data ' list stores tree data ; If root is NULL , put 0 in structure array and return ; Else place 1 in structure array , key in ' data ' array and recur for left and right children ; Constructs tree from ' struc ' and ' data ' ; Remove one item from structure list ; If removed bit is 1 , ; remove an item from data list ; Create a tree node with the removed data ; And recur to create left and right subtrees ; A utility function to print tree ; Driver program ; Let us construct the Tree shown in the above figure ; Structure iterator ; Data iterator
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void EncodeSuccinct ( Node * root , list < bool > & struc , list < int > & data ) { if ( root == NULL ) { struc . push_back ( 0 ) ; return ; } struc . push_back ( 1 ) ; data . push_back ( root -> key ) ; EncodeSuccinct ( root -> left , struc , data ) ; EncodeSuccinct ( root -> right , struc , data ) ; } Node * DecodeSuccinct ( list < bool > & struc , list < int > & data ) { if ( struc . size ( ) <= 0 ) return NULL ; bool b = struc . front ( ) ; struc . pop_front ( ) ; if ( b == 1 ) { int key = data . front ( ) ; data . pop_front ( ) ; Node * root = newNode ( key ) ; root -> left = DecodeSuccinct ( struc , data ) ; root -> right = DecodeSuccinct ( struc , data ) ; return root ; } return NULL ; } void preorder ( Node * root ) { if ( root ) { cout << " key : ▁ " << root -> key ; if ( root -> left ) cout << " ▁ | ▁ left ▁ child : ▁ " << root -> left -> key ; if ( root -> right ) cout << " ▁ | ▁ right ▁ child : ▁ " << root -> right -> key ; cout << endl ; preorder ( root -> left ) ; preorder ( root -> right ) ; } } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 20 ) ; root -> right = newNode ( 30 ) ; root -> left -> left = newNode ( 40 ) ; root -> left -> right = newNode ( 50 ) ; root -> right -> right = newNode ( 70 ) ; cout << " Given ▁ Tree STRNEWLINE " ; preorder ( root ) ; list < bool > struc ; list < int > data ; EncodeSuccinct ( root , struc , data ) ; cout << " Encoded Tree " cout << " Structure ▁ List STRNEWLINE " ; list < bool > :: iterator si ; for ( si = struc . begin ( ) ; si != struc . end ( ) ; ++ si ) cout << * si << " ▁ " ; cout << " Data List " list < int > :: iterator di ; for ( di = data . begin ( ) ; di != data . end ( ) ; ++ di ) cout << * di << " ▁ " ; Node * newroot = DecodeSuccinct ( struc , data ) ; cout << " Preorder traversal of decoded tree " ; preorder ( newroot ) ; return 0 ; }
Binary Indexed Tree : Range Updates and Point Queries | C ++ program to demonstrate Range Update and Point Queries Without using BIT ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver program to test above function ; Find the element at Index 4 ; Find the element at Index 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; void update ( int arr [ ] , int l , int r , int val ) { arr [ l ] += val ; arr [ r + 1 ] -= val ; } int getElement ( int arr [ ] , int i ) { int res = 0 ; for ( int j = 0 ; j <= i ; j ++ ) res += arr [ j ] ; return res ; } int main ( ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int l = 2 , r = 4 , val = 2 ; update ( arr , l , r , val ) ; int index = 4 ; cout << " Element ▁ at ▁ index ▁ " << index << " ▁ is ▁ " << getElement ( arr , index ) << endl ; l = 0 , r = 3 , val = 4 ; update ( arr , l , r , val ) ; index = 3 ; cout << " Element ▁ at ▁ index ▁ " << index << " ▁ is ▁ " << getElement ( arr , index ) << endl ; return 0 ; }
Binary Indexed Tree : Range Updates and Point Queries | C ++ code to demonstrate Range Update and Point Queries on a Binary Index Tree ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Store the actual values in BITree [ ] using update ( ) ; SERVES THE PURPOSE OF getElement ( ) Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Iniialize result ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Return the sum ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Increase value at ' l ' by ' val ' ; Decrease value at ' r + 1' by ' val ' ; Driver program to test above function ; Add 2 to all the element from [ 2 , 4 ] ; Find the element at Index 4 ; Add 2 to all the element from [ 0 , 3 ] ; Find the element at Index 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } int * constructBITree ( int arr [ ] , int n ) { int * BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } int getSum ( int BITree [ ] , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } void update ( int BITree [ ] , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; } int main ( ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int * BITree = constructBITree ( arr , n ) ; int l = 2 , r = 4 , val = 2 ; update ( BITree , l , r , n , val ) ; int index = 4 ; cout << " Element ▁ at ▁ index ▁ " << index << " ▁ is ▁ " << getSum ( BITree , index ) << " STRNEWLINE " ; l = 0 , r = 3 , val = 4 ; update ( BITree , l , r , n , val ) ; index = 3 ; cout << " Element ▁ at ▁ index ▁ " << index << " ▁ is ▁ " << getSum ( BITree , index ) << " STRNEWLINE " ; return 0 ; }
Print Postorder traversal from given Inorder and Preorder traversals | C ++ program to print Postorder traversal from given Inorder and Preorder traversals . ; Find index of next item in preorder traversal in inorder . ; traverse left tree ; traverse right tree ; print root node at the end of traversal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int preIndex = 0 ; void printPost ( int in [ ] , int pre [ ] , int inStrt , int inEnd , map < int , int > hm ) { if ( inStrt > inEnd ) return ; int inIndex = hm [ pre [ preIndex ++ ] ] ; printPost ( in , pre , inStrt , inIndex - 1 , hm ) ; printPost ( in , pre , inIndex + 1 , inEnd , hm ) ; cout << in [ inIndex ] << " ▁ " ; } void printPostMain ( int in [ ] , int pre [ ] , int n ) { map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ in [ i ] ] = i ; printPost ( in , pre , 0 , n - 1 , hm ) ; } int main ( ) { int in [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = sizeof ( pre ) / sizeof ( pre [ 0 ] ) ; printPostMain ( in , pre , n ) ; return 0 ; }
Coordinates of the last cell in a Matrix on which performing given operations exits from the Matrix | CPP program for the above approach ; Function to check if the indices ( i , j ) are valid indices in a Matrix or not ; Cases for invalid cells ; Return true if valid ; Function to find indices of cells of a matrix from which traversal leads to out of the matrix ; Starting from cell ( 0 , 0 ) , traverse in right direction ; Stores direction changes ; Iterate until the current cell exceeds beyond the matrix ; Current index ; If the current cell is 1 ; Update arr [ i ] [ j ] = 0 ; Update indices according to the direction ; Otherwise ; Update indices according to the direction ; The exit cooridnates ; Driver Code ; Number of rows ; Number of columns ; Given matrix arr [ ] [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool issafe ( int m , int n , int i , int j ) { if ( i < 0 ) return false ; if ( j < 0 ) return false ; if ( i >= m ) return false ; if ( j >= n ) return false ; return true ; } pair < int , int > endpoints ( vector < vector < int > > arr , int m , int n ) { int i = 0 ; int j = 0 ; int current_i = 0 ; int current_j = 0 ; char current_d = ' r ' ; map < char , char > rcd = { { ' l ' , ' u ' } , { ' u ' , ' r ' } , { ' r ' , ' d ' } , { ' d ' , ' l ' } } ; while ( issafe ( m , n , i , j ) ) { current_i = i ; current_j = j ; if ( arr [ i ] [ j ] == 1 ) { char move_in = rcd [ current_d ] ; arr [ i ] [ j ] = 0 ; if ( move_in == ' u ' ) i -= 1 ; else if ( move_in == ' d ' ) i += 1 ; else if ( move_in == ' l ' ) j -= 1 ; else if ( move_in == ' r ' ) j += 1 ; current_d = move_in ; } else { if ( current_d == ' u ' ) i -= 1 ; else if ( current_d == ' d ' ) i += 1 ; else if ( current_d == ' l ' ) j -= 1 ; else if ( current_d == ' r ' ) j += 1 ; } } return { current_i , current_j } ; } int main ( ) { int M = 3 ; int N = 5 ; vector < vector < int > > arr { { 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 1 , 1 , 1 , 0 , 0 } } ; pair < int , int > p = endpoints ( arr , M , N ) ; cout << " ( " << p . first << " , ▁ " << p . second << " ) " << endl ; }
Check if a number can be represented as sum of two positive perfect cubes | C ++ program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; Stores the perfect cubes of first N natural numbers ; Traverse the map ; Stores first number ; Stores second number ; Search the pair for the first number to obtain sum N from the Map ; If N cannot be represented as sum of two positive perfect cubes ; Driver Code ; Function call to check if N can be represented as sum of two perfect cubes or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfTwoPerfectCubes ( int N ) { map < int , int > cubes ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes [ i * i * i ] = i ; map < int , int > :: iterator itr ; for ( itr = cubes . begin ( ) ; itr != cubes . end ( ) ; itr ++ ) { int firstNumber = itr -> first ; int secondNumber = N - itr -> first ; if ( cubes . find ( secondNumber ) != cubes . end ( ) ) { cout << " True " ; return ; } } cout << " False " ; } int main ( ) { int N = 28 ; sumOfTwoPerfectCubes ( N ) ; return 0 ; }
Count subsets consisting of each element as a factor of the next element in that subset | C ++ program for the above approach ; Function to find number of subsets satisfying the given condition ; Stores number of required sets ; Stores maximum element of arr [ ] that defines the size of sieve ; Iterate through the arr [ ] ; If current element > maxE , then update maxE ; Declare an array sieve of size N + 1 ; Mark all elements corresponding in the array , by one as there will always exists a singleton set ; Iterate from range [ 1 , N ] ; If element is present in array ; Traverse through all its multiples <= n ; Update them if they are present in array ; Iterate from the range [ 1 , N ] ; Update the value of cnt ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define mod  1000000007 NEW_LINE using namespace std ; void countSets ( int * arr , int n ) { int cnt = 0 ; int maxE = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( maxE < arr [ i ] ) maxE = arr [ i ] ; } int * sieve = new int [ maxE + 1 ] ; for ( int i = 0 ; i <= maxE ; i ++ ) sieve [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) sieve [ arr [ i ] ] = 1 ; for ( int i = 1 ; i <= maxE ; i ++ ) { if ( sieve [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxE ; j += i ) { if ( sieve [ j ] != 0 ) sieve [ j ] = ( sieve [ j ] + sieve [ i ] ) % mod ; } } } for ( int i = 0 ; i <= maxE ; i ++ ) cnt = ( cnt % mod + sieve [ i ] % mod ) % mod ; delete [ ] sieve ; cout << cnt % mod ; } int main ( ) { int arr [ ] = { 16 , 18 , 6 , 7 , 2 , 19 , 20 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSets ( arr , N ) ; return 0 ; }
Count distinct prime factors for each element of an array | C ++ program for the above approach ; Stores smallest prime factor for every number ; Stores distinct prime factors ; Function to find the smallest prime factor of every number ; Mark the smallest prime factor of every number to itself ; Separately mark all the smallest prime factor of every even number to be 2 ; If i is prime ; Mark spf for all numbers divisible by i ; Mark spf [ j ] if it is not previously marked ; Function to find the distinct prime factors ; Push all distinct of x prime factor in v [ x ] ; Pushback into v [ i ] ; Increment the idx ; Update x = ( x / spf [ x ] ) ; Function to get the distinct factor count of arr [ ] ; Precompute the smallest Prime Factors ; For distinct prime factors Fill the v [ ] vector ; Count of Distinct Prime Factors of each array element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int spf [ MAX ] ; vector < int > v [ MAX ] ; void sieve ( ) { for ( int i = 1 ; i < MAX ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAX ; i = i + 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) spf [ j ] = i ; } } } void DistPrime ( ) { for ( int i = 1 ; i < MAX ; i ++ ) { int idx = 1 ; int x = i ; if ( x != 1 ) v [ i ] . push_back ( spf [ x ] ) ; x = x / spf [ x ] ; while ( x != 1 ) { if ( v [ i ] [ idx - 1 ] != spf [ x ] ) { v [ i ] . push_back ( spf [ x ] ) ; idx += 1 ; } x = x / spf [ x ] ; } } } void getFactorCount ( int arr [ ] , int N ) { sieve ( ) ; DistPrime ( ) ; for ( int i = 0 ; i < N ; i ++ ) { cout << ( int ) v [ arr [ i ] ] . size ( ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 6 , 9 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getFactorCount ( arr , N ) ; return 0 ; }