Found 7347 Articles for C++

Fitting Shelves Problem in C++

sudhir sharma
Updated on 31-Jan-2022 12:22:51

248 Views

In this problem, we are given three integer values W, n, m denoting the length of wall W, size of shelves n, and m. Our task is To Create a Program to solve the Fitting Shelves Problem.We need to find a way to fit shelves in such a way that the space left after fitting shelves is minimized. A secondary constrain while solving is the cost of making, the larger shelves are more cost-effective so, we need to give them a priority.The output should be in the following form, Number of n size shelves number of m size shelves space ... Read More

Find the largest twins in given range in C++

sudhir sharma
Updated on 28-Jan-2022 11:43:13

138 Views

In this problem, we are given two values lValue and hValue. Our task is to find the largest twins in given range.Two numbers are said to be twin numbers if both of them are prime numbers and the difference between them is 2.Let's take an example to understand the problem, Input : lValue = 65, rValue = 100 Output : 71, 73Solution ApproachA simple solution to the problem is by looping from rValue - 2 to lValue and checking each pair of i and (i+2) for twins and print the first occurred twin.Another Approach is by finding all prime numbers ... Read More

Find the largest three elements in an array in C++

sudhir sharma
Updated on 28-Jan-2022 11:38:04

8K+ Views

In this problem, we are given an arr[] consisting of N unsorted elements. Our task is to find the largest three elements in an array.Let's take an example to understand the problem, Input : arr[] = {7, 3, 9, 12, 1} Output : 12, 9, 7Solution ApproachWe basically need to find three largest elements of the array and print them. This can be done is multiple ways, Method 1For the largest three elements, we will create three elements holding their values, max, max2 and max3 and set these values to arr[0].Then we will loop form i -> 1 to n-1 ... Read More

Find the largest pair sum in an unsorted array in C++

sudhir sharma
Updated on 28-Jan-2022 11:32:16

832 Views

In this problem, we are given an arr[] consisting of N unsorted elements. Our task is to find the largest pair sum in an unsorted array.We will find a pair whose sum is the maximum.Let's take an example to understand the problem, Input : arr[] = {7, 3, 9, 12, 1} Output : 21Explanation −Pair with largest sum, (9, 12). Sum = 21 Solution ApproachA simple solution to the problem is by making a pair of maximum and second maximum elements of the array.For this we will initialise the max and secondMax elements of the array with the first and ... Read More

Find the largest number with n set and m unset bits in C++

sudhir sharma
Updated on 28-Jan-2022 11:28:26

120 Views

In this problem, we are given two integer values, n and m. Our task is to find the largest number with n set and m unset bits in the binary representation of the number.Let's take an example to understand the problemInput : n = 3, m = 1 Output : 14Explanation −Largest number will have 3 set bits and then 1 unset bit. (1110)2 = 14Solution ApproachA simple solution to the problem is by finding the number consisting of (n+m) set bits. From this number, toggle off the m bits from the end (LSB). To create a number with (n+m) ... Read More

Find the Largest number with given number of digits and sum of digits in C++

sudhir sharma
Updated on 28-Jan-2022 11:21:10

1K+ Views

In this problem, we are given two integer values, N denoting the count of digits of a number and sum denoting the sum of digits of the number. Our task is to find the largest number with given number of digits and sum of digits.Let's take an example to understand the problem, Input : N = 3, sum = 15 Output : 960Solution ApproachA simple approach to solve the problem is by traversing all N digit numbers from the largest to smallest. The find the digit sum numbers, if it's equal to sum return the number.ExampleProgram to illustrate the working ... Read More

Find the largest node in Doubly linked list in C++

sudhir sharma
Updated on 28-Jan-2022 11:13:04

246 Views

In this problem, we are given a doubly linked list LL. Our task is to find the largest node in Doubly Linked List.Let's take an example to understand the problem, Input : linked-list = 5 -> 2 -> 9 -> 8 -> 1 -> 3 Output : 9Solution ApproachA simple approach to solve the problem is by traversing the linked-list and if the data value of max is greater than maxVal's data. After traversing the linked-list, we will return the macVal nodes data.ExampleProgram to illustrate the working of our solution#include using namespace std; struct Node{    int data;   ... Read More

Find the largest multiple of 2, 3 and 5 in C++

sudhir sharma
Updated on 28-Jan-2022 11:06:27

143 Views

In this problem, we are given an array arr[] of size N consisting of single digits only. Our task is to find the largest multiple of 2, 3 and 5.Let's take an example to understand the problem, Input : arr[] = {1, 0, 5, 2} Output : 510Explanation −The number 510 is divisible by all 2, 3, 5. Solution ApproachA simple solution to the problem is by checking for basic divisibility of the number created.So, if the number needs to be divisible by 2 and 5 i.e. it is divisible by 10. For creating a number divisible by 10, the ... Read More

Find the largest good number in the divisors of given number N in C++

sudhir sharma
Updated on 28-Jan-2022 10:58:00

177 Views

In this problem, we are given a number N. Our task is to find the largest good number in the divisors of given number N.A good number is a number in which every digit is larger than the sum of digits of its right (all less significant bits than it). For example, 732 is a good number, 7> 3+2 and 3>2.Let's take an example to understand the problem, Input : N = 15 Output : 15Explanation −Divisors of 15 : 1, 3, 5, 15. Solution ApproachA simple solution to the problem is by finding all the divisors of N. And ... Read More

Find the largest BST subtree in a given Binary Tree - Set 1 in C++

sudhir sharma
Updated on 28-Jan-2022 10:52:08

188 Views

In this problem, we are given a binary tree BT. Our task is to find the largest BST subtree in a given Binary Tree.Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children.Binary Search Tree (BST)is a tree in which all the nodes follow the below-mentioned properties −The value of the key of the left sub-tree is less than the value of its parent (root) node's key.The value of the key of the right subtree is greater than or equal to ... Read More

Advertisements