Found 7197 Articles for C++

Program to check bitnoicity of an array in C++

Sunidhi Bansal
Updated on 13-Aug-2020 07:03:01

470 Views

Given an array arr[N] of N integers, the task is to check whether the given array is bitonic or not. If the given array is bitonic then print “Yes its a bitonic array”, else print “No its not a bitonic array”.A Bitonic array is when the array is in strictly increasing order first and then in strictly decreasing order.Like this array arr[] = {1, 2, 3, 4, 2, -1, -5} is a bitonic array, because till 4 it is in strictly increasing order and after 4 it is in strictly decreasing order.Input arr[] = {1, 3, 5, 4, 2, 0}Output Yes its ... Read More

Program for average of an array(Iterative and Recursive) in C++

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

2K+ Views

Given an array of N integers arr[N], the task is to find the average of arr[N]. To achieve the result we can either use iterative approach or the recursive approach. We will be showing both in the given solution.Average of an array will be sum of all the elements of an array divided by the number of elements.Iterative methodIn iterative approach, we use loops like for-loop, while-loop or do-while loop which executes the statements till the condition holds true which means 1.Let’s take an example and then discuss how it can be obtained using iterative approach.Input arr[] = {1, 2, 4, ... Read More

Program to add two binary strings in C++

Nishu Kumari
Updated on 23-Jul-2025 18:01:13

12K+ Views

In this problem, we are given two binary strings, and we need to find their sum and return the result as a binary string. A binary string is a string that contains only the characters '0' and '1', where 0 and 1 are binary numbers. While adding two binary numbers, we follow the binary addition rules given below - 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 = 0 with a carry of 1 ... Read More

Product of all prime nodes in a Singly Linked List in C++

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

307 Views

Given with n nodes and the task is to print the product of all the prime nodes in a linked list. Prime nodes are the ones that will have prime values as their count locations.Input 10 20 30 40 50Output 4, 00, 000Explanation − 10 is at index value 1 which is non-prime so it will be skipped. Moving to 20 with index value 2 which is a prime number so it will be considered. Similarly, 40 and 50 are at prime index locations.Product − 20*40*50 = 4, 00, 000In the above diagram, red coloured nodes represents the prime nodesApproach used below ... Read More

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

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

257 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

495 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

137 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

125 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

315 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

164 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

Advertisements