codeparrot/xlcost-text-to-code huggingface dataset:
Check if leaf traversal of two Binary Trees is same ? | C ++ code to check if leaf traversals of two Binary Trees are same or not . ; Binary Tree Node ; Returns new Node with data as input to below function . ; checks if a given node is leaf or not . ; iterative function . returns true if leaf traversals are same , else false . ; Create empty stacks . These stacks are going to be used for iterative traversals . ; loop until either of stacks are non - empty . ; this means one of the stacks has extra leaves , hence return false . ; Push right and left children of temp1 . Note that right child is inserted before left ; same for tree2 ; If one is null and other is not , then return false ; If both are not null and data is not same return false ; all leaves are matched ; Driver Code ; Let us create trees in above example 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left ; Node * right ; } ; Node * newNode ( int d ) { Node * temp = new Node ; temp -> data = d ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } bool isLeaf ( Node * root ) { if ( root == NULL ) return false ; if ( ! root -> left && ! root -> right ) return true ; return false ; } bool isSame ( Node * root1 , Node * root2 ) { stack < Node * > s1 ; stack < Node * > s2 ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { if ( s1 . empty ( ) || s2 . empty ( ) ) return false ; Node * temp1 = s1 . top ( ) ; s1 . pop ( ) ; while ( temp1 != NULL && ! isLeaf ( temp1 ) ) { if ( temp1 -> right ) s1 . push ( temp1 -> right ) ; if ( temp1 -> left ) s1 . push ( temp1 -> left ) ; temp1 = s1 . top ( ) ; s1 . pop ( ) ; } Node * temp2 = s2 . top ( ) ; s2 . pop ( ) ; while ( temp2 != NULL && ! isLeaf ( temp2 ) ) { if ( temp2 -> right ) s2 . push ( temp2 -> right ) ; if ( temp2 -> left ) s2 . push ( temp2 -> left ) ; temp2 = s2 . top ( ) ; s2 . pop ( ) ; } if ( ! temp1 && temp2 ) return false ; if ( temp1 && ! temp2 ) return false ; if ( temp1 && temp2 ) { return temp1 -> data == temp2 -> data ; } } return true ; } int main ( ) { Node * root1 = newNode ( 1 ) ; root1 -> left = newNode ( 2 ) ; root1 -> right = newNode ( 3 ) ; root1 -> left -> left = newNode ( 4 ) ; root1 -> right -> left = newNode ( 6 ) ; root1 -> right -> right = newNode ( 7 ) ; Node * root2 = newNode ( 0 ) ; root2 -> left = newNode ( 1 ) ; root2 -> right = newNode ( 5 ) ; root2 -> left -> right = newNode ( 4 ) ; root2 -> right -> left = newNode ( 6 ) ; root2 -> right -> right = newNode ( 7 ) ; if ( isSame ( root1 , root2 ) ) cout << " Same " ; else cout << " Not ▁ Same " ; return 0 ; }
Most frequent element in an array | CPP program to find the most frequent element in an array . ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mostFrequent ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int max_count = 1 , res = arr [ 0 ] , curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } int main ( ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << mostFrequent ( arr , n ) ; return 0 ; }
Most frequent element in an array | CPP program to find the most frequent element in an array . ; Insert all elements in hash . ; find the max frequency ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mostFrequent ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 , res = -1 ; for ( auto i : hash ) { if ( max_count < i . second ) { res = i . first ; max_count = i . second ; } } return res ; } int main ( ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << mostFrequent ( arr , n ) ; return 0 ; }
Smallest subarray with all occurrences of a most frequent element | C ++ implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void smallestSubsegment ( int a [ ] , int n ) { unordered_map < int , int > left ; unordered_map < int , int > count ; int mx = 0 ; int mn , strindex ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count [ x ] == 0 ) { left [ x ] = i ; count [ x ] = 1 ; } else count [ x ] ++ ; if ( count [ x ] > mx ) { mx = count [ x ] ; mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } else if ( count [ x ] == mx && i - left [ x ] + 1 < mn ) { mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int A [ ] = { 1 , 2 , 2 , 2 , 1 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; smallestSubsegment ( A , n ) ; return 0 ; }
Given an array of pairs , find all symmetric pairs in it | A C ++ program to find all symmetric pairs in a given array of pairs ; Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSymPairs ( int arr [ ] [ 2 ] , int row ) { unordered_map < int , int > hM ; for ( int i = 0 ; i < row ; i ++ ) { int first = arr [ i ] [ 0 ] ; int sec = arr [ i ] [ 1 ] ; if ( hM . find ( sec ) != hM . end ( ) && hM [ sec ] == first ) cout << " ( " << sec << " , ▁ " << first << " ) " << endl ; else hM [ first ] = sec ; } } int main ( ) { int arr [ 5 ] [ 2 ] ; arr [ 0 ] [ 0 ] = 11 ; arr [ 0 ] [ 1 ] = 20 ; arr [ 1 ] [ 0 ] = 30 ; arr [ 1 ] [ 1 ] = 40 ; arr [ 2 ] [ 0 ] = 5 ; arr [ 2 ] [ 1 ] = 10 ; arr [ 3 ] [ 0 ] = 40 ; arr [ 3 ] [ 1 ] = 30 ; arr [ 4 ] [ 0 ] = 10 ; arr [ 4 ] [ 1 ] = 5 ; findSymPairs ( arr , 5 ) ; }
Find any one of the multiple repeating elements in read only array | C ++ program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver Program ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRepeatingNumber ( const int arr [ ] , int n ) { int sq = sqrt ( n ) ; int range = ( n / sq ) + 1 ; int count [ range ] = { 0 } ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } unordered_map < int , int > m ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m [ arr [ i ] ] ++ ; if ( m [ arr [ i ] ] > 1 ) return arr [ i ] ; } } return -1 ; } int main ( ) { const int arr [ ] = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; cout << " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in " " ▁ the ▁ array ▁ is : ▁ " << findRepeatingNumber ( arr , n ) << endl ; }
Find top three repeated in array | C ++ Program to Find the top three repeated numbers ; Function to print top three repeated numbers ; There should be atleast two elements ; Count Frequency of each element ; Initialize first value of each variable of Pair type is INT_MIN ; If frequency of current element is not zero and greater than frequency of first largest element ; Update second and third largest ; Modify values of x Number ; If frequency of current element is not zero and frequency of current element is less than frequency of first largest element , but greater than y element ; Modify values of third largest ; Modify values of second largest ; If frequency of current element is not zero and frequency of current element is less than frequency of first element and second largest , but greater than third largest . ; Modify values of z Number ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void top3Repeated ( int arr [ ] , int n ) { if ( n < 3 ) { cout << " Invalid ▁ Input " ; return ; } unordered_map < int , int > fre ; for ( int i = 0 ; i < n ; i ++ ) fre [ arr [ i ] ] ++ ; pair < int , int > x , y , z ; x . first = y . first = z . first = INT_MIN ; for ( auto curr : fre ) { if ( curr . second > x . first ) { z = y ; y = x ; x . first = curr . second ; x . second = curr . first ; } else if ( curr . second > y . first ) { z = y ; y . first = curr . second ; y . second = curr . first ; } else if ( curr . second > z . first ) { z . first = curr . second ; z . second = curr . first ; } } cout << " Three ▁ largest ▁ elements ▁ are ▁ " << x . second << " ▁ " << y . second << " ▁ " << z . second ; } int main ( ) { int arr [ ] = { 3 , 4 , 2 , 3 , 16 , 3 , 15 , 16 , 15 , 15 , 16 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; top3Repeated ( arr , n ) ; return 0 ; }
Group multiple occurrence of array elements ordered by first occurrence | A simple C ++ program to group multiple occurrences of individual array elements ; A simple method to group all occurrences of individual elements ; Initialize all elements as not visited ; Traverse all elements ; Check if this is first occurrence ; If yes , print it and all subsequent occurrences ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void groupElements ( int arr [ ] , int n ) { bool * visited = new bool [ n ] ; for ( int i = 0 ; i < n ; i ++ ) visited [ i ] = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { cout << arr [ i ] << " ▁ " ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { cout << arr [ i ] << " ▁ " ; visited [ j ] = true ; } } } } delete [ ] visited ; } int main ( ) { int arr [ ] = { 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; groupElements ( arr , n ) ; return 0 ; }
How to check if two given sets are disjoint ? | A Simple C ++ program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Take every element of set1 [ ] and search it in set2 ; If no element of set1 is present in set2 ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areDisjoint ( int set1 [ ] , int set2 [ ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( set1 [ i ] == set2 [ j ] ) return false ; return true ; } int main ( ) { int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; int m = sizeof ( set1 ) / sizeof ( set1 [ 0 ] ) ; int n = sizeof ( set2 ) / sizeof ( set2 [ 0 ] ) ; areDisjoint ( set1 , set2 , m , n ) ? cout << " Yes " : cout << " ▁ No " ; return 0 ; }
How to check if two given sets are disjoint ? | A Simple C ++ program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areDisjoint ( int set1 [ ] , int set2 [ ] , int m , int n ) { sort ( set1 , set1 + m ) ; sort ( set2 , set2 + n ) ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set2 [ j ] < set1 [ i ] ) j ++ ; else return false ; } return true ; } int main ( ) { int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; int m = sizeof ( set1 ) / sizeof ( set1 [ 0 ] ) ; int n = sizeof ( set2 ) / sizeof ( set2 [ 0 ] ) ; areDisjoint ( set1 , set2 , m , n ) ? cout << " Yes " : cout << " ▁ No " ; return 0 ; }
How to check if two given sets are disjoint ? | C ++ program to check if two sets are distinct or not ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the first set and store its elements in hash ; Traverse the second set and check if any element of it is already in hash or not . ; Driver method to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areDisjoint ( int set1 [ ] , int set2 [ ] , int n1 , int n2 ) { set < int > myset ; for ( int i = 0 ; i < n1 ; i ++ ) myset . insert ( set1 [ i ] ) ; for ( int i = 0 ; i < n2 ; i ++ ) if ( myset . find ( set2 [ i ] ) != myset . end ( ) ) return false ; return true ; } int main ( ) { int set1 [ ] = { 10 , 5 , 3 , 4 , 6 } ; int set2 [ ] = { 8 , 7 , 9 , 3 } ; int n1 = sizeof ( set1 ) / sizeof ( set1 [ 0 ] ) ; int n2 = sizeof ( set2 ) / sizeof ( set2 [ 0 ] ) ; if ( areDisjoint ( set1 , set2 , n1 , n2 ) ) cout << " Yes " ; else cout << " No " ; }
Non | CPP program to find Non - overlapping sum ; function for calculating Non - overlapping sum of two array ; Insert elements of both arrays ; calculate non - overlapped sum ; driver code ; size of array ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int A [ ] , int B [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) { hash [ A [ i ] ] ++ ; hash [ B [ i ] ] ++ ; } int sum = 0 ; for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ; return sum ; } int main ( ) { int A [ ] = { 5 , 4 , 9 , 2 , 3 } ; int B [ ] = { 2 , 8 , 7 , 6 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findSum ( A , B , n ) ; return 0 ; }
Find elements which are present in first array and not in second | C ++ simple program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissing ( int a [ ] , int b [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) cout << a [ i ] << " ▁ " ; } } int main ( ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 1 ] ) ; findMissing ( a , b , n , m ) ; return 0 ; }
Find elements which are present in first array and not in second | C ++ efficient program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Store all elements of second array in a hash table ; Print all elements of first array that are not present in hash table ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissing ( int a [ ] , int b [ ] , int n , int m ) { unordered_set < int > s ; for ( int i = 0 ; i < m ; i ++ ) s . insert ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s . find ( a [ i ] ) == s . end ( ) ) cout << a [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 1 ] ) ; findMissing ( a , b , n , m ) ; return 0 ; }
Check if two arrays are equal or not | C ++ program to find given two array are equal or not ; Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { if ( n != m ) return false ; sort ( arr1 , arr1 + n ) ; sort ( arr2 , arr2 + m ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } int main ( ) { int arr1 [ ] = { 3 , 5 , 2 , 5 , 2 } ; int arr2 [ ] = { 2 , 3 , 5 , 5 , 2 } ; int n = sizeof ( arr1 ) / sizeof ( int ) ; int m = sizeof ( arr2 ) / sizeof ( int ) ; if ( areEqual ( arr1 , arr2 , n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Pair with given sum and maximum shortest distance from end | C ++ code to find maximum shortest distance from endpoints ; function to find maximum shortest distance ; stores the shortest distance of every element in original array . ; shortest distance from ends ; if duplicates are found , b [ x ] is replaced with minimum of the previous and current position 's  shortest distance ; similar elements ignore them cause we need distinct elements ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_maximum ( int a [ ] , int n , int k ) { unordered_map < int , int > b ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = min ( 1 + i , n - i ) ; if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ; else b [ x ] = min ( d , b [ x ] ) ; } int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . find ( k - x ) != b . end ( ) ) ans = min ( max ( b [ x ] , b [ k - x ] ) , ans ) ; } return ans ; } int main ( ) { int a [ ] = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << find_maximum ( a , n , K ) << endl ; return 0 ; }
Pair with given product | Set 1 ( Find if any pair exists ) | A simple C ++ program to find if there is a pair with given product . ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isProduct ( int arr [ ] , int n , int x ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; i < n ; i ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } int main ( ) { int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isProduct ( arr , n , x ) ? cout << " Yesn " : cout << " Non " ; x = 190 ; isProduct ( arr , n , x ) ? cout << " Yesn " : cout << " Non " ; return 0 ; }
Pair with given product | Set 1 ( Find if any pair exists ) | C ++ program to find if there is a pair with given product . ; Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Create an empty set and insert first element into it ; Traverse remaining elements ; 0 case must be handles explicitly as x % 0 is undefined behaviour in C ++ ; x / arr [ i ] exists in hash , then we found a pair ; Insert arr [ i ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isProduct ( int arr [ ] , int n , int x ) { if ( n < 2 ) return false ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( s . find ( x / arr [ i ] ) != s . end ( ) ) return true ; s . insert ( arr [ i ] ) ; } } return false ; } int main ( ) { int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isProduct ( arr , n , x ) ? cout << " Yes STRNEWLINE " : cout << " Non " ; x = 190 ; isProduct ( arr , n , x ) ? cout << " Yes STRNEWLINE " : cout << " Non " ; return 0 ; }
Check whether a given binary tree is perfect or not | C ++ program to check whether a given Binary Tree is Perfect or not ; Tree node structure ; Returns depth of leftmost leaf . ; This function tests if a binary tree is perfect or not . It basically checks for two things : 1 ) All leaves are at same level 2 ) All internal nodes have two children ; An empty tree is perfect ; If leaf node , then its depth must be same as depth of all other leaves . ; If internal node and one child is empty ; Left and right subtrees must be perfect . ; Wrapper over isPerfectRec ( ) ; Helper function that allocates a new node with the given key and NULL left and right pointer . ; Driver Program
#include <bits/stdc++.h> NEW_LINE struct Node { int key ; struct Node * left , * right ; } ; int findADepth ( Node * node ) { int d = 0 ; while ( node != NULL ) { d ++ ; node = node -> left ; } return d ; } bool isPerfectRec ( struct Node * root , int d , int level = 0 ) { if ( root == NULL ) return true ; if ( root -> left == NULL && root -> right == NULL ) return ( d == level + 1 ) ; if ( root -> left == NULL root -> right == NULL ) return false ; return isPerfectRec ( root -> left , d , level + 1 ) && isPerfectRec ( root -> right , d , level + 1 ) ; } bool isPerfect ( Node * root ) { int d = findADepth ( root ) ; return isPerfectRec ( root , d ) ; } struct Node * newNode ( int k ) { struct Node * node = new Node ; node -> key = k ; node -> right = node -> left = NULL ; return node ; } int main ( ) { struct Node * root = NULL ; root = newNode ( 10 ) ; root -> left = newNode ( 20 ) ; root -> right = newNode ( 30 ) ; root -> left -> left = newNode ( 40 ) ; root -> left -> right = newNode ( 50 ) ; root -> right -> left = newNode ( 60 ) ; root -> right -> right = newNode ( 70 ) ; if ( isPerfect ( root ) ) printf ( " Yes STRNEWLINE " ) ; else printf ( " No STRNEWLINE " ) ; return ( 0 ) ; }
Find pair with greatest product in array | C ++ program to find a pair with product in given array . ; Function to find greatest number that us ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findGreatest ( int arr [ ] , int n ) { int result = -1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = max ( result , arr [ i ] ) ; return result ; } int main ( ) { int arr [ ] = { 30 , 10 , 9 , 3 , 35 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGreatest ( arr , n ) ; return 0 ; }
Find pair with greatest product in array | C ++ program to find the largest product number ; Function to find greatest number ; Store occurrences of all elements in hash array ; Sort the array and traverse all elements from end . ; For every element , check if there is another element which divides it . ; Check if the result value exists in array or not if yes the return arr [ i ] ; To handle the case like arr [ i ] = 4 and arr [ j ] = 2 ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findGreatest ( int arr [ ] , int n ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; sort ( arr , arr + n ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m [ result ] > 0 ) return arr [ i ] ; else if ( result == arr [ j ] && m [ result ] > 1 ) return arr [ i ] ; } } } return -1 ; } int main ( ) { int arr [ ] = { 17 , 2 , 1 , 15 , 30 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findGreatest ( arr , n ) ; return 0 ; }