Found 7347 Articles for C++

C++ Largest Subset with Sum of Every Pair as Prime

Prateek Jangid
Updated on 25-Nov-2021 09:52:03

104 Views

To find the largest subset from the given array in which the sum of every pair is a prime number. Assuming maximum element is 100000, for example −Input: nums[ ] = { 3, 2, 1, 1 } Output: size = 3, subset = { 2, 1, 1 } Explanation: Subsets can be formed: {3, 2}, {2, 1} and { 2, 1, 1}, In {2, 1, 1} sum of pair (2, 1) is 3 which is prime, and the sum of pairs (1, 1) is 2 which is also a prime number. Input: nums[ ] = {1, 4, 3, 2} ... Read More

C++ Largest Subtree having Equal No of 1's and 0's

Prateek Jangid
Updated on 25-Nov-2021 09:49:47

127 Views

Given a binary tree. Now we are tasked to find the largest subtree having an equal number of 1's and 0's; the tree only contains 0's and 1's.Approach to Find the SolutionIn this approach, we are going to replace all the nodes with values of 0 to -1. Doing this will make our program simpler as we now need to find the largest subtree with a sum equal to 0.ExampleC++ Code for the Above Approach  #include using namespace std; int maxi = -1; struct node { // structure of our tree node     int data;     struct ... Read More

C++ Program to find the Largest Divisible Subset in Array

Prateek Jangid
Updated on 25-Nov-2021 09:48:55

195 Views

This tutorial will discuss a problem where we are given an array of distinct positive integers. We need to find the largest subset such that for every pair larger element is divided by a smaller element, for example −Input: nums[ ] = { 1, 4, 2, 6, 7} Output: 1 2 4 Explanation: All Divisible subsets are: (1, 2, 4), (1, 2, 6), (1, 7), etc We have 2 subsets of length 3 in which all the pairs satisfy the condition. Input: nums[ ] = { 1, 2, 3, 6 } Output: 6 2 1Approach to Find the SolutionThere ... Read More

C++ Program to find the Largest Divisible Pairs Subset

Prateek Jangid
Updated on 25-Nov-2021 09:44:14

152 Views

To solve a problem in which we are given an array consisting of distinct elements. Now our task is to find the subset such that every pair is evenly divisible, i.e, every large element is divisible by every smaller element, for example.Input : arr[] = {10, 5, 3, 15, 20} Output : 3 Explanation: The largest subset is 10, 5, 20. 10 is divisible by 5, and 20 is divisible by 10. Input : arr[] = {18, 1, 3, 6, 13, 17} Output : 4 Explanation: The largest subset is 18, 1, 3, 6, In the subsequence, 3 is ... Read More

Numbers that are Bitwise AND of At Least One Non-Empty Sub-Array using C++

Prateek Jangid
Updated on 25-Nov-2021 09:39:39

100 Views

To solve a problem where we are given an array, and we need to find all possible integers which are bitwise AND of at least one not empty subarray, for example −Input : nums[ ] = { 3, 5, 1, 2, 8 } Output : { 2, 5, 0, 3, 8, 1 } Explanation: 2 is the bitwise AND of subarray {2}, 5 is the bitwise AND of subarray {5}, 0 is the bitwise AND of subarray {1, 2}, {2, 8} and {1, 2, 8}, 3 is the bitwise AND of subarray {3}, 8 is the bitwise AND of subarray ... Read More

C++ Program to find Numbers in a Range with Given Digital Root

Prateek Jangid
Updated on 25-Nov-2021 09:38:25

160 Views

The sum of its digit can find the digital root of a number; if the sum is a single digit, it is a digital root. In this tutorial, we will discuss a problem where we are given a range of numbers and an integer X, and we need to count how many numbers in the range have digital roots as X where X is a single-digit number, for exampleInput: l = 13, r = 25, X = 4 Output: 2 Explanation: Numbers in the range (13, 25) having digit sum 4 are 13 and 22. Input: l = 11, ... Read More

C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k

Prateek Jangid
Updated on 25-Nov-2021 09:35:54

376 Views

To solve a problem in which, given, we are tasked to find the number such that the XOR sum of a given array with that number becomes equal to k, for example.Input: arr[] = {1, 2, 3, 4, 5}, k = 10 Output: 11 Explanation: 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 11 = 10 Input: arr[] = { 12, 23, 34, 56, 78 }, k = 6 Output: 73In this program, we are going to use the property of xor if A^B = C and A^C = B, and we are going to apply this ... Read More

Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++

Prateek Jangid
Updated on 25-Nov-2021 09:31:43

188 Views

To solve a problem in which we are given an array and some queries. Now in each query, we are given a range. Now we need to find a number such that the sum of their xor with x is maximized, for exampleInput : A = {20, 11, 18, 2, 13} Three queries as (L, R) pairs 1 3 3 5 2 4 Output : 2147483629 2147483645 2147483645In this problem, we are going to take a prefix count of 1’s present in the numbers at each position now as we have precalculated our number of ones, so for finding the ... Read More

Find the Number of Ways to Traverse an N-ary Tree using C++

Prateek Jangid
Updated on 25-Nov-2021 09:30:15

149 Views

Given an N-ary tree and we are tasked to find the total number of ways to traverse this tree, for example −For the above tree, our output will be 192.For this problem, we need to have some knowledge about combinatorics. Now in this problem, we simply need to check all the possible combinations for every path and that will give us our answer.Approach to Find the SolutionIn this approach, we simply need to perform a level order traversal and check the number of children each node has and then simply multiply its factorial to the answer.ExampleC++ Code for the Above ... Read More

Find the Number of Ways to Pair People using C++

Prateek Jangid
Updated on 25-Nov-2021 09:20:11

212 Views

To solve a problem in which n − the number of people now each person can either be single or be present in a pair, so we need to find the total number of ways these people can be paired up.Input : 3 Output: 4 Explanation : [ {1}, {2}, {3}, ], [{1, 2}, {3}], [{1}, {2, 3}], [{1, 3}, {2}] these four ways are the only ways we can pa up these 3 people. Input : 6 Output : 76Approach to Find the SolutionIn this approach, we are going to use the formula of Young ... Read More

Advertisements