- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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