Found 7347 Articles for C++

Coin Change in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:50:21

4K+ Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 11, the output is 3. This is formed using 5 + 5 + 1 = 11.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Sum Root to Leaf Numbers in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:45:08

338 Views

Suppose we have a binary tree containing digits from 0-9 only, here all root-to-leaf path could represent a number.So if the tree is like −This is representing two paths 21 and 23, so the output will be 21 + 23 = 44.To solve this, we will follow these steps −Create one recursive function called dfs(), this will take root, and num. initially num = 0if the node is not nullnum := num * 10 + value of nodeif node right is not null and node left is not null, then’sum := sum + numnum := num / 10return from the ... Read More

Validate Binary Search Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:38:31

1K+ Views

Suppose we have a binary tree, determine to check whether it is a valid binary search tree (BST) or not. Assume a BST is defined as follows –The left subtree of a node holds only nodes with keys smaller than the node's key.The right subtree of a node holds only nodes with keys larger than the node's key.Both the left and right subtrees must also be binary search trees.So if the tree is like –The output will be true.To solve this, we will follow these steps –Create one recursive function called solve(), this will take root, min and max, the ... Read More

Unique Binary Search Trees in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:31:24

129 Views

Suppose we have an integer n, we have to count all structurally unique binary search trees that store values from 1 to n. So if the input is 3, then the output will be 5, as the trees will be –To solve this, we will follow these steps –create one array of size n + 1dp[0] := 1for i := 1 to nfor j := 0 to i – 1dp[i] := dp[i] + (dp[i – 1 – j] * dp[j])return dp[n]Example(C++)Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; class Solution { ... Read More

Unique Binary Search Trees II in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:21:10

301 Views

Suppose we have an integer n, we have to generate all structurally unique binary search trees that store values from 1 to n. So if the input is 3, then the trees will be −To solve this, we will follow these steps −Define one recursive function called generate(), this will take low and highdefine one tree node called temp.if low > high, then insert null into the temp, and return tempfor i in range low to highleft_subtree := generate(low, i – 1)right_subtree := generate(i + 1, high)current := ifor j in range 0 to size of the left_subtreefor k in ... Read More

deque_insert( ) in C++ in STL

Sunidhi Bansal
Updated on 26-Feb-2020 12:24:12

566 Views

Given is the task to show the functionality of Deque insert( ) function in C++ STLWhat is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the ... Read More

List::clear() in C++ STL

Sunidhi Bansal
Updated on 26-Feb-2020 12:18:36

4K+ Views

In this article we will be discussing the working, syntax and examples of list::clear() function in C++.What is a List in STL?List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.What is clea()?list::clear() ... Read More

List remove() function in C++ STL

Sunidhi Bansal
Updated on 26-Feb-2020 12:15:04

12K+ Views

In this article we will be discussing the working, syntax and examples of remove() function in C++.What is a List in STLList is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.What is remove()remove() ... Read More

deque_rend( ) in C++ in STL

Sunidhi Bansal
Updated on 26-Feb-2020 12:10:39

105 Views

Given is the task to show the functionality of Deque rend( ) function in C++ STLWhat is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the ... Read More

deque_rbegin( ) in C++ in STL

Sunidhi Bansal
Updated on 26-Feb-2020 12:06:59

99 Views

Given is the task to show the functionality of Deque rbegin( ) function in C++ STLWhat is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended queue the ... Read More

Advertisements