Found 7347 Articles for C++

Product of all nodes in a Binary Tree in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:49:54

159 Views

Given with a binary tree containing nodes and the task is to find the product of all the nodes of a given binary tree.In a binary tree, there is a root node which is the master node of all the nodes in a tree. A node contains data part, left pointer which will further create left subdirectory and a right pointer which will help in creating right subdirectory. So to traverse the tree, we can take a temporary pointer that will associate with left pointer to traverse left subdirectory or right pointer to traverse the right sub directory.Input Output Nodes are-: 10, ... Read More

Product of all leaf nodes of binary tree in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:46:42

378 Views

Given with a binary tree containing nodes and the task is to find the product of all the leaf nodes of a given binary tree.Leaf nodes are the end nodes which don’t have any children. In a tree, a node can act as a parent node or child node except the root node which can only be a parent node. So the nodes with right and left pointer as NULL are the leaf nodes.Input Output Leaf nodes are -: 23, 34, 25 Product-: 23*34*25 = 19550ApproachInput the node dataTraverse all the nodes starting from the root node and going to either left ... Read More

Product of nodes at k-th level in a tree represented as string in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:43:54

75 Views

Given with the tree of nodes with data in a string format and the task is to find the product of the nodes at k-th level in a binary tree. Every node of a tree contains three things i.e. data part, left pointer for left subtree and right pointer for right subtree.Level of binary tree starts from number 0 and it can go till ‘n’ which can be any positive number. So, we are given with the level ‘k’ and program must calculate the product of the nodes at given ‘k’ level.In the binary tree, if let’s say we are ... Read More

Probability of A winning the match when individual probabilities of hitting the target given in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:40:54

65 Views

Given with two players let’s say A and B both are trying to get a penalty for winning the match. Given with four integer variables a, b, c, d so the probability of A getting the penalty first is a / b and probability of B getting the penalty first is c / d.The one who scores the penalty first will win the match and as per the given problem statement program must find the probability of A winning the match.Input a = 10, b = 20, c = 30, d = 40Output probability is 0.5333Inputa = 1, b = 2, c ... Read More

Probability of a key K present in array in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:38:44

240 Views

Given with an array of size ’n’ and the task is to find the probability of the given element k if available in an array.Traverse the entire array till ‘n’ which is equals to the number of elements in an array and search for the given element or key ‘k’. If the element is present in an array than calculate its probability else print 0.Input arr[] = { 1, 2, 3, 4, 5, 6} K = 5Output probability of a key 5 in an array is :0.166Input arr[] = { 1, 2, 3, 4, 5, 6, 7 } K = 8Output probability of a ... Read More

Probability of a random pair being the maximum weighted pair in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:27:51

80 Views

Given with two different arrays and the task is to find the probability of the random pair chosen to be the maximum weighted pair.A Pair will contain one element from let’s say array1 and another element form another array let’s say array2. So the program must find the probability of the pair that will contain first element to be the maximum element of array 1 and second element to be the maximum element of array 2 hence forming the maximum weighted pair.Input arr1[] = { 2, 23 } arr2[] = { 10, 3, 8 }Output probability of maximum pair : 0.166667Explanation set of ... Read More

Functions that cannot be overloaded in C++

Sunidhi Bansal
Updated on 13-Aug-2020 06:09:03

1K+ Views

Function overloading is also known as method overloading. Function overloading is the feature provided by the concept of polymorphism which is widely used in object-oriented programming.To achieve function overloading, functions should satisfy these conditions −Return type of functions should be sameName of the functions should be sameParameters can be different in type but should be same in numberExampleint display(int a); int display(float a); // both the functions can be overloaded int display(int a); float display(float b); //both the functions can’t be overloaded as the return type of one function is different from anotherlet’s discuss the functions that can’t overloaded in ... Read More

multimap find( ) in C++ STL

Sunidhi Bansal
Updated on 13-Aug-2020 06:05:07

2K+ Views

In this article we will be discussing the working, syntax and examples of multimap::find() function in C++ STL.What is Multimap in C++ STL?Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.What is multimap::find()?multimap::find( ) an inbuilt function in C++ STL, which is defined in header file. find() searches elements ... Read More

Circular queues-Insertion and deletion operations in C++

Arnab Chakraborty
Updated on 11-Aug-2020 06:34:17

10K+ Views

A queue is an abstract data structure that contains a collection of elements. Queue implements the FIFO mechanism i.e the element that is inserted first is also deleted first.Queue cane be one linear data structure. But it may create some problem if we implement queue using array. Sometimes by using some consecutive insert and delete operation, the front and rear position will change. In that moment, it will look like the queue has no space to insert elements into it. Even if there are some free spaces, that will not be used due to some logical problems. To overcome this ... Read More

Priority Queues in C++

Arnab Chakraborty
Updated on 10-Aug-2020 08:56:47

450 Views

As we know that the queue data structure is First in First Out data structure. The queue has some variations also. These are the Dequeue and the Priority Queue.Here we will see one variation of queue, that is the priority queue. In this structure, each element in the queue has its own priority. When we insert item into queue, we have to assign priority value with it. It will delete the highest priority element at first. To implement priority queue one of the easiest method is using the heap data structure.Let us see one C++ code for priority queue STL. ... Read More

Advertisements