Found 7347 Articles for C++

Perfect Squares in C++

Arnab Chakraborty
Updated on 04-May-2020 09:08:12

412 Views

Suppose we have a positive integer n, find the least number of perfect square numbers whose sum is n. So if the number is 13, then the output is 2, as the numbers are 13 = 9 + 4To solve this, we will follow these steps −create one table for dynamic programming, of length n + 1, and fill it with infinitydp[0] := 0for i := 1, when i*i dp(n+1,INF);       dp[0] = 0;       for(int i =1;i*i

Count Complete Tree Nodes in C++

Arnab Chakraborty
Updated on 04-May-2020 09:02:45

801 Views

Suppose we have a complete binary tree, we have to count the number of nodes. So if the tree is like −So the output will be 6.To solve this, we will follow these stepsThis will use the recursive approach. This method, countNodes() is taking the root as argument.hr := 0 and hl := 0create two nodes l and r as rootwhile l is not emptyincrease hl by 1l := left of lwhile r is not emptyr := right of rincrease hr by 1if hl = hr, then return (2 ^ hl) – 1return 1 + countNodes(left of root) + countNodes(right ... Read More

Combination Sum IIII in C++

Arnab Chakraborty
Updated on 04-May-2020 09:01:23

98 Views

Consider we have to generate all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used. Each combination should be a unique set of numbers. All numbers should be positive, and the solution must not contain duplicate combinations. So if k = 3 and n = 9, then the possible combinations are [[1, 2, 6], [1, 3, 5], [2, 3, 4]]To solve this, we will follow these steps −Suppose we will solve this using forming a method called solve. This will be recursive method, this will take ... Read More

House Robber II in C++

Arnab Chakraborty
Updated on 04-May-2020 08:59:30

683 Views

Consider, you are a professional robber. And you are planning to rob houses along a street. Each house has a certain amount of money stored. All houses are arranged in a circle. That means the first house is the neighbor of the last house. We have to keep in mind that the adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. So if we have a list of integers representing the amount of money of each house, determine the maximum amount of money you can ... Read More

Binary Tree Right Side View in C++

Arnab Chakraborty
Updated on 04-May-2020 08:56:19

142 Views

Suppose we have a binary tree, if we see the tree from right side, then we can see some elements of it. we have to display those elements. So if the tree is like −To solve this, we will follow these steps −We will create one helping method for dfs. This will take tree_node, an array to hold answers, and level. The level is initially 0. The dfs will work like below −if node is null, then returnif level = length of the answer array, then insert value of node into the ans arraydfs(right of the node, ans, level + ... Read More

Binary Search Tree Iterator in C++

Arnab Chakraborty
Updated on 04-May-2020 08:55:04

2K+ Views

Suppose we want to make one iterator for binary tree. There will be two methods. The next() method to return the next element, and hasNext() method to return Boolean value, that will indicate that the next element is present or not. So if the tree is like −And the sequence of function calls are [next(), next(), hasNext(), next(), hasNext(), next(), hasNext(), next(), hasNext()]. The output will be [3, 7, true, 9, true, 15, true, 20, false]To solve this, we will follow these steps −There are two methods next and hasNext, The next() method will be like −curr := stack top ... Read More

Count numbers from 1 to n that have 4 as a digit in C++

Ayush Gupta
Updated on 05-Feb-2020 07:57:50

338 Views

In this tutorial, we will be discussing a program to find the numbers from 1 to n that have 4 as a digit.For this we will be provided with a number n. Our task is to count all the numbers which have 4 as one of their digits and print it out.Example Live Demo#include using namespace std; bool has4(int x); //returning sum of digits in the given numbers int get_4(int n){    int result = 0;    //calculating the sum of each digit    for (int x=1; x

Count number of even and odd elements in an array in C++

Ayush Gupta
Updated on 05-Feb-2020 07:53:52

523 Views

In this tutorial, we will be discussing a program to find the number of even and odd elements in an array.For this we will be provided with an array. Our task is to calculate the number of even and odd elements in the given array.Example Live Demo#include using namespace std; void CountingEvenOdd(int arr[], int arr_size){    int even_count = 0;    int odd_count = 0;    //looping through the elements    for(int i = 0 ; i < arr_size ; i++) {       //checking if the number is odd       if (arr[i]%2 != 0)          odd_count ++ ;       else          even_count ++ ;    }    cout

Count all increasing subsequences in C++

Ayush Gupta
Updated on 05-Feb-2020 07:49:01

397 Views

In this tutorial, we will be discussing a program to find the number of increasing sequences.For this we will be provided with an array containing digits 0 to 9. Our task is to count all the sequences present in the array such that the next element is greater than the previous element.Example Live Demo#include using namespace std; //counting the possible subsequences int count_sequence(int arr[], int n){    int count[10] = {0};    //scanning each digit    for (int i=0; i=0; j--)          count[arr[i]] += count[j];       count[arr[i]]++;    }    //getting all the possible subsequences    int result = 0;    for (int i=0; i

Count all elements in the array which appears at least K times after their first occurrence in C++

Ayush Gupta
Updated on 05-Feb-2020 07:45:38

194 Views

In this tutorial, we will be discussing a program to find the number of elements in the array which appears at least K times after their first occurrence.For this we will be provided with an integer array and a value k. Our task is to count all the elements occurring k times among the elements after the element in consideration.Example Live Demo#include #include using namespace std; //returning the count of elements int calc_count(int n, int arr[], int k){    int cnt, ans = 0;    //avoiding duplicates    map hash;    for (int i = 0; i < n; ... Read More

Advertisements