codeparrot/xlcost-text-to-code huggingface dataset:
Check if removing an edge can divide a Binary Tree in two halves | C ++ program to check if there exist an edge whose removal creates two trees of same size ; utility function to create a new node ; To calculate size of tree with given root ; This function returns size of tree rooted with given root . It also set " res " as true if there is an edge whose removal divides tree in two halves . n is size of tree ; Base case ; Compute sizes of left and right children ; If required property is true for current node set " res " as true ; Return size ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Initialize result and recursively check all nodes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int x ) { struct Node * temp = new Node ; temp -> data = x ; temp -> left = temp -> right = NULL ; return temp ; } ; int count ( Node * root ) { if ( root == NULL ) return 0 ; return count ( root -> left ) + count ( root -> right ) + 1 ; } int checkRec ( Node * root , int n , bool & res ) { if ( root == NULL ) return 0 ; int c = checkRec ( root -> left , n , res ) + 1 + checkRec ( root -> right , n , res ) ; if ( c == n - c ) res = true ; return c ; } bool check ( Node * root ) { int n = count ( root ) ; bool res = false ; checkRec ( root , n , res ) ; return res ; } int main ( ) { struct Node * root = newNode ( 5 ) ; root -> left = newNode ( 1 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( 3 ) ; root -> right -> left = newNode ( 7 ) ; root -> right -> right = newNode ( 4 ) ; check ( root ) ? printf ( " YES " ) : printf ( " NO " ) ; return 0 ; }
Distance of nearest cell having 1 in a binary matrix | C ++ program to find distance of nearest cell having 1 in a binary matrix . ; Print the distance of nearest cell having 1 for each cell . ; Initialize the answer matrix with INT_MAX . ; For each cell ; Traversing the whole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  3 NEW_LINE #define M  4 NEW_LINE using namespace std ; void printDistance ( int mat [ N ] [ M ] ) { int ans [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) ans [ i ] [ j ] = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) for ( int l = 0 ; l < M ; l ++ ) { if ( mat [ k ] [ l ] == 1 ) ans [ i ] [ j ] = min ( ans [ i ] [ j ] , abs ( i - k ) + abs ( j - l ) ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) cout << ans [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int mat [ N ] [ M ] = { 0 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 } ; printDistance ( mat ) ; return 0 ; }
Distance of nearest cell having 1 in a binary matrix | C ++ program to find distance of nearest cell having 1 in a binary matrix . ; Making a class of graph with bfs function . ; Function to create graph with N * M nodes considering each cell as a node and each boundary as an edge . ; A number to be assigned to a cell ; If last row , then add edge on right side . ; If not bottom right cell . ; If last column , then add edge toward down . ; Else makes an edge in all four directions . ; BFS function to find minimum distance ; Printing the solution . ; Find minimum distance ; Creating a graph with nodes values assigned from 1 to N x M and matrix adjacent . ; To store minimum distance ; To mark each node as visited or not in BFS ; Initialising the value of distance and visit . ; Inserting nodes whose value in matrix is 1 in the queue . ; Calling for Bfs with given Queue . ; Printing the solution . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  500 NEW_LINE #define N  3 NEW_LINE #define M  4 NEW_LINE using namespace std ; class graph { private : vector < int > g [ MAX ] ; int n , m ; public : graph ( int a , int b ) { n = a ; m = b ; } void createGraph ( ) { int k = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( i == n ) { if ( j != m ) { g [ k ] . push_back ( k + 1 ) ; g [ k + 1 ] . push_back ( k ) ; } } else if ( j == m ) { g [ k ] . push_back ( k + m ) ; g [ k + m ] . push_back ( k ) ; } else { g [ k ] . push_back ( k + 1 ) ; g [ k + 1 ] . push_back ( k ) ; g [ k ] . push_back ( k + m ) ; g [ k + m ] . push_back ( k ) ; } k ++ ; } } } void bfs ( bool visit [ ] , int dist [ ] , queue < int > q ) { while ( ! q . empty ( ) ) { int temp = q . front ( ) ; q . pop ( ) ; for ( int i = 0 ; i < g [ temp ] . size ( ) ; i ++ ) { if ( visit [ g [ temp ] [ i ] ] != 1 ) { dist [ g [ temp ] [ i ] ] = min ( dist [ g [ temp ] [ i ] ] , dist [ temp ] + 1 ) ; q . push ( g [ temp ] [ i ] ) ; visit [ g [ temp ] [ i ] ] = 1 ; } } } } void print ( int dist [ ] ) { for ( int i = 1 , c = 1 ; i <= n * m ; i ++ , c ++ ) { cout << dist [ i ] << " ▁ " ; if ( c % m == 0 ) cout << endl ; } } } ; void findMinDistance ( bool mat [ N ] [ M ] ) { graph g1 ( N , M ) ; g1 . createGraph ( ) ; int dist [ MAX ] ; bool visit [ MAX ] = { 0 } ; for ( int i = 1 ; i <= M * N ; i ++ ) { dist [ i ] = INT_MAX ; visit [ i ] = 0 ; } int k = 1 ; queue < int > q ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { dist [ k ] = 0 ; visit [ k ] = 1 ; q . push ( k ) ; } k ++ ; } } g1 . bfs ( visit , dist , q ) ; g1 . print ( dist ) ; } int main ( ) { bool mat [ N ] [ M ] = { 0 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 } ; findMinDistance ( mat ) ; return 0 ; }
First negative integer in every window of size k | C ++ implementation to find the first negative integer in every window of size k ; function to find the first negative integer in every window of size k ; flag to check whether window contains a negative integer or not ; Loop for each subarray ( window ) of size k ; traverse through the current window ; if a negative integer is found , then it is the first negative integer for current window . Print it , set the flag and break ; if the current window does not contain a negative integer ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { bool flag ; for ( int i = 0 ; i < ( n - k + 1 ) ; i ++ ) { flag = false ; for ( int j = 0 ; j < k ; j ++ ) { if ( arr [ i + j ] < 0 ) { cout << arr [ i + j ] << " ▁ " ; flag = true ; break ; } } if ( ! flag ) cout << "0" << " ▁ " ; } } int main ( ) { int arr [ ] = { 12 , -1 , -7 , 8 , -15 , 30 , 16 , 28 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; return 0 ; }
First negative integer in every window of size k | C ++ implementation to find the first negative integer in every window of size k ; function to find the first negative integer in every window of size k ; A Double Ended Queue , Di that will store indexes of useful array elements for the current window of size k . The useful elements are all negative integers . ; Process first k ( or first window ) elements of array ; Add current element at the rear of Di if it is a negative integer ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; if Di is not empty then the element at the front of the queue is the first negative integer of the previous window ; else the window does not have a negative integer ; Remove the elements which are out of this window ; Remove from front of queue ; Add current element at the rear of Di if it is a negative integer ; Print the first negative integer of last window ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { deque < int > Di ; int i ; for ( i = 0 ; i < k ; i ++ ) if ( arr [ i ] < 0 ) Di . push_back ( i ) ; for ( ; i < n ; i ++ ) { if ( ! Di . empty ( ) ) cout << arr [ Di . front ( ) ] << " ▁ " ; else cout << "0" << " ▁ " ; while ( ( ! Di . empty ( ) ) && Di . front ( ) < ( i - k + 1 ) ) Di . pop_front ( ) ; if ( arr [ i ] < 0 ) Di . push_back ( i ) ; } if ( ! Di . empty ( ) ) cout << arr [ Di . front ( ) ] << " ▁ " ; else cout << "0" << " ▁ " ; } int main ( ) { int arr [ ] = { 12 , -1 , -7 , 8 , -15 , 30 , 16 , 28 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; return 0 ; }
First negative integer in every window of size k | C ++ code for First negative integer in every window of size k ; skip out of window and positive elements ; check if a negative element is found , otherwise use 0 ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printFirstNegativeInteger ( int arr [ ] , int k , int n ) { int firstNegativeIndex = 0 ; int firstNegativeElement ; for ( int i = k - 1 ; i < n ; i ++ ) { while ( ( firstNegativeIndex < i ) && ( firstNegativeIndex <= i - k arr [ firstNegativeIndex ] > 0 ) ) { firstNegativeIndex ++ ; } if ( arr [ firstNegativeIndex ] < 0 ) { firstNegativeElement = arr [ firstNegativeIndex ] ; } else { firstNegativeElement = 0 ; } cout << firstNegativeElement << " ▁ " ; } } int main ( ) { int arr [ ] = { 12 , -1 , -7 , 8 , -15 , 30 , 16 , 28 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printFirstNegativeInteger ( arr , k , n ) ; }
Check if all levels of two trees are anagrams or not | Iterative program to check if two trees are level by level anagram . ; A Binary Tree Node ; Returns true if trees with root1 and root2 are level by level anagram , else returns false . ; Base Cases ; start level order traversal of two trees using two queues . ; n1 ( queue size ) indicates number of Nodes at current level in first tree and n2 indicates number of nodes in current level of second tree . ; If n1 and n2 are different ; If level order traversal is over ; Dequeue all Nodes of current level and Enqueue all Nodes of next level ; Check if nodes of current levels are anagrams or not . ; Utility function to create a new tree Node ; Driver program to test above functions ; Constructing both the trees .
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { struct Node * left , * right ; int data ; } ; bool areAnagrams ( Node * root1 , Node * root2 ) { if ( root1 == NULL && root2 == NULL ) return true ; if ( root1 == NULL root2 == NULL ) return false ; queue < Node * > q1 , q2 ; q1 . push ( root1 ) ; q2 . push ( root2 ) ; while ( 1 ) { int n1 = q1 . size ( ) , n2 = q2 . size ( ) ; if ( n1 != n2 ) return false ; if ( n1 == 0 ) break ; vector < int > curr_level1 , curr_level2 ; while ( n1 > 0 ) { Node * node1 = q1 . front ( ) ; q1 . pop ( ) ; if ( node1 -> left != NULL ) q1 . push ( node1 -> left ) ; if ( node1 -> right != NULL ) q1 . push ( node1 -> right ) ; n1 -- ; Node * node2 = q2 . front ( ) ; q2 . pop ( ) ; if ( node2 -> left != NULL ) q2 . push ( node2 -> left ) ; if ( node2 -> right != NULL ) q2 . push ( node2 -> right ) ; curr_level1 . push_back ( node1 -> data ) ; curr_level2 . push_back ( node2 -> data ) ; } sort ( curr_level1 . begin ( ) , curr_level1 . end ( ) ) ; sort ( curr_level2 . begin ( ) , curr_level2 . end ( ) ) ; if ( curr_level1 != curr_level2 ) return false ; } return true ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { struct Node * root1 = newNode ( 1 ) ; root1 -> left = newNode ( 3 ) ; root1 -> right = newNode ( 2 ) ; root1 -> right -> left = newNode ( 5 ) ; root1 -> right -> right = newNode ( 4 ) ; struct Node * root2 = newNode ( 1 ) ; root2 -> left = newNode ( 2 ) ; root2 -> right = newNode ( 3 ) ; root2 -> left -> left = newNode ( 4 ) ; root2 -> left -> right = newNode ( 5 ) ; areAnagrams ( root1 , root2 ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Check if given Preorder , Inorder and Postorder traversals are of same tree | C ++ program to check if all three given traversals are of the same tree ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; Recursive function to construct binary tree of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for  cases where inorder and preorder do not form a  tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; function to compare Postorder traversal on constructed tree and given Postorder ; first recur on left child ; now recur on right child ; Compare if data at current index in both Postorder traversals are same ; Driver program to test above functions ; build tree from given Inorder and Preorder traversals ; compare postorder traversal on constructed tree with given Postorder traversal ; If both postorder traversals are same
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int search ( int arr [ ] , int strt , int end , int value ) { for ( int i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } } Node * buildTree ( int in [ ] , int pre [ ] , int inStrt , int inEnd ) { static int preIndex = 0 ; if ( inStrt > inEnd ) return NULL ; Node * tNode = newNode ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( in , inStrt , inEnd , tNode -> data ) ; tNode -> left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode -> right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } int checkPostorder ( Node * node , int postOrder [ ] , int index ) { if ( node == NULL ) return index ; index = checkPostorder ( node -> left , postOrder , index ) ; index = checkPostorder ( node -> right , postOrder , index ) ; if ( node -> data == postOrder [ index ] ) index ++ ; else return -1 ; return index ; } int main ( ) { int inOrder [ ] = { 4 , 2 , 5 , 1 , 3 } ; int preOrder [ ] = { 1 , 2 , 4 , 5 , 3 } ; int postOrder [ ] = { 4 , 5 , 2 , 3 , 1 } ; int len = sizeof ( inOrder ) / sizeof ( inOrder [ 0 ] ) ; Node * root = buildTree ( inOrder , preOrder , 0 , len - 1 ) ; int index = checkPostorder ( root , postOrder , 0 ) ; if ( index == len ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if X can give change to every person in the Queue | C ++ program to check whether X can give change to every person in the Queue ; Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; Driver Code ; queue of customers with available notes . ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isChangeable ( int notes [ ] , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } else { if ( fiveCount > 0 && tenCount > 0 ) { fiveCount -- ; tenCount -- ; } else if ( fiveCount >= 3 ) { fiveCount -= 3 ; } else return 0 ; } } return 1 ; } int main ( ) { int a [ ] = { 5 , 5 , 5 , 10 , 20 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isChangeable ( a , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Index Mapping ( or Trivial Hashing ) with negatives allowed | CPP program to implement direct index mapping with negative values allowed . ; Since array is global , it is initialized as 0. ; searching if X is Present in the given array or not . ; if X is negative take the absolute value of X . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000 NEW_LINE bool has [ MAX + 1 ] [ 2 ] ; bool search ( int X ) { if ( X >= 0 ) { if ( has [ X ] [ 0 ] == 1 ) return true ; else return false ; } X = abs ( X ) ; if ( has [ X ] [ 1 ] == 1 ) return true ; return false ; } void insert ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) has [ a [ i ] ] [ 0 ] = 1 ; else has [ abs ( a [ i ] ) ] [ 1 ] = 1 ; } } int main ( ) { int a [ ] = { -1 , 9 , -5 , -8 , -5 , -2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; insert ( a , n ) ; int X = -5 ; if ( search ( X ) == true ) cout << " Present " ; else cout << " Not ▁ Present " ; return 0 ; }
Given level order traversal of a Binary Tree , check if the Tree is a Min | C ++ program to check if a given tree is Binary Heap or not ; Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMinHeap ( int level [ ] , int n ) { for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) return false ; if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) return false ; } } return true ; } int main ( ) { int level [ ] = { 10 , 15 , 14 , 25 , 30 } ; int n = sizeof ( level ) / sizeof ( level [ 0 ] ) ; if ( isMinHeap ( level , n ) ) cout << " True " ; else cout << " False " ; return 0 ; }
Minimum delete operations to make all elements of array same | C ++ program to find minimum number of deletes required to make all elements same . ; Function to get minimum number of elements to be deleted from array to make array elements equal ; Create an hash map and store frequencies of all array elements in it using element as key and frequency as value ; Find maximum frequency among all frequencies . ; To minimize delete operations , we remove all elements but the most frequent element . ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDelete ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int max_freq = INT_MIN ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) max_freq = max ( max_freq , itr -> second ) ; return n - max_freq ; } int main ( ) { int arr [ ] = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minDelete ( arr , n ) ; return 0 ; }
Minimum operation to make all elements equal in array | CPP program to find the minimum number of operations required to make all elements of array equal ; function for min operation ; Insert all elements in hash . ; find the max frequency ; return result ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 ; for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ; return ( n - max_count ) ; } int main ( ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperation ( arr , n ) ; return 0 ; }
Maximum distance between two occurrences of same element in array | C ++ program to find maximum distance between two same occurrences of a number . ; Function to find maximum distance between equal elements ; Used to store element to first index mapping ; Traverse elements and find maximum distance between same occurrences with the help of map . ; If this is first occurrence of element , insert its index in map ; Else update max distance ; Driver program to run the case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDistance ( int arr [ ] , int n ) { unordered_map < int , int > mp ; int max_dist = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ; else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ; } return max_dist ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxDistance ( arr , n ) ; return 0 ; }
Check if a given array contains duplicate elements within k distance from each other | C ++ program to Check if a given array contains duplicate elements within k distance from each other ; Creates an empty hashset ; Traverse the input array ; If already present n hash , then we found a duplicate within k distance ; Add this item to hashset ; Remove the k + 1 distant item ; Driver method to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) { unordered_set < int > myset ; for ( int i = 0 ; i < n ; i ++ ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ; myset . insert ( arr [ i ] ) ; if ( i >= k ) myset . erase ( arr [ i - k ] ) ; } return false ; } int main ( ) { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkDuplicatesWithinK ( arr , n , 3 ) ) cout << " Yes " ; else cout << " No " ; }
Find duplicates in a given array when elements are not limited to a range | C ++ implementation of the above approach ; Function to find the Duplicates , if duplicate occurs 2 times or more than 2 times in array so , it will print duplicate value only once at output ; Initialize ifPresent as false ; ArrayList to store the output ; Checking if element is present in the ArrayList or not if present then break ; If element is not present in the ArrayList then add it to ArrayList and make ifPresent at true ; If duplicates is present then print ArrayList ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDuplicates ( int arr [ ] , int len ) { bool ifPresent = false ; vector < int > al ; for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { auto it = std :: find ( al . begin ( ) , al . end ( ) , arr [ i ] ) ; if ( it != al . end ( ) ) { break ; } else { al . push_back ( arr [ i ] ) ; ifPresent = true ; } } } } if ( ifPresent == true ) { cout << " [ " << al [ 0 ] << " , ▁ " ; for ( int i = 1 ; i < al . size ( ) - 1 ; i ++ ) { cout << al [ i ] << " , ▁ " ; } cout << al [ al . size ( ) - 1 ] << " ] " ; } else { cout << " No ▁ duplicates ▁ present ▁ in ▁ arrays " ; } } int main ( ) { int arr [ ] = { 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDuplicates ( arr , n ) ; return 0 ; }