Found 7197 Articles for C++

Find all combinations that add upto given number using C++

Arnab Chakraborty
Updated on 01-Nov-2019 06:06:46

490 Views

Suppose we have a positive number n. We have to find all combinations of positive numbers, that adds up to that number. Here we want only combinations, not the permutations. For the value n = 4, there will be [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]We will solve this using recursion. We have an array to store combinations, and we will fill that array using recursive approach. Each combination will be stored in increasing order of elements.Example#include using namespace std; void getCombination(int arr[], int index, int num, int decrement) {    if (decrement < 0)       return;    if (decrement == 0){       for (int i = 0; i < index; i++)          cout

Minimum number of power terms with sum equal to n using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:40:40

294 Views

Problem statementGiven two positive integer N and X. The task is to express N as a sum of powers of X (X0 + X1 +…..+ Xn) such that the number of powers of X should be minimum.Print the minimum number of power of N used to make the sum equal to N.If N = 15 and X = 3 then we need 3 powers of ‘3’ as follows −15 = (32 + 31 + 31)AlgorithmUse below formula to calculate final result −1. If x = 1, then answer will be n only (n = 1 + 1 +…. n times)s ... Read More

Minimum number of points to be removed to get remaining points on one side of axis using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:37:14

585 Views

Problem statementWe are given N points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.If given input is {(10, 5), (-2, -5), (13, 8), (-14, 7)} then if we remove (-2, -5) then all remaining points are above X-axis.Hence answer is 1.Algorithm1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of themExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{    int x, ... Read More

Minimum Number of Platforms Required for a Railway Station using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:35:09

313 Views

Problem statementGiven arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.We are given two arrays that represent arrival and departure times of trains that stop.For below input, we need at least 3 platforms −TrainArrival timeDeparture timeTrain-109:0009:15Train-209:3511:45Train-309:4011:05Train-411:0012:00Train-514:3018:15Train-618:0019:00Algorithm1. Sort arrival and departure time arrays in ascending order 2. Trace the number of trains at any time keeping track of trains that haves arrived, but not departedExample#include #include #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; int getPlatformCount(int ... Read More

Minimum number of palindromes required to express N as a sum using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:30:17

210 Views

Problem statementGiven a number N, we have to find the minimum number of palindromes required to express N as a sum of themIf N = 15 then 2 palindromes are required i.e. 8 and 7.Algorithm1. Generate all the palindromes up to N in a sorted fashion 2. Find the size of the smallest subset such that its sum is NExample#include #include #include #include using namespace std; vector table; int createPalindrome(int input, bool isOdd){    int n = input;    int palindrome = input;    if (isOdd)       n /= 10;    while (n > ... Read More

Minimum number of page turns to get to a desired page using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:26:43

571 Views

Problem statementGiven a book of N pages, the task is to calculate the minimum number of page turns to get to a give desired page K.we can either start turning pages from the front side of the book (i.e from page 1) or from the backside of the book (i.e page number N).Each page has two sides, front and back, except the first page, which has only backside and the last page which may only have backside depending on the number of pages of the book.If N = 5 and K = 4 then we have to turn minimum 1 ... Read More

Minimum number of operations required to sum to binary string S using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:24:59

229 Views

Problem statementGiven a binary string str. Find the minimum number of operations required to be performed to create the number represented by str. Only following operations can be performed −Add 2xSubtract 2xIf binary string is “1000” then we have to perform only 1 operation i.e. Add 23If binary string is “101” then we have to perform 2 operations i.e. Add 22 + 20Example#include #include #include using namespace std; int getMinOperations(string s){    reverse(s.begin(), s.end());    int n = s.length();    int result[n + 1][2];    if (s[0] == '0') {       result[0][0] = 0;   ... Read More

Minimum number of operations required to delete all elements of the array using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:21:16

1K+ Views

Problem statementGiven an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. While deleting element following restriction is imposed −Any element from the array can be chosen at random and every element divisible by it can be removed from the arrayIf arr[] = {2, 4, 15, 10, 8, 5, 3} then 3 operation are required to delete all elements −If we choose 2 then it will delete {2, 4, 10, 8}If we choose 5 then it will remove {5, 15}If we choose 3 then it will remove {3}Algorithm1. ... Read More

Minimum number of operations on an array to make all elements 0 using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:18:01

481 Views

Problem statementGiven an array of size N and each element is either 1 or 0. The task is to calculated the minimum number of operations to be performed to convert all elements to zero. One can perform below operations −If an element is 1, You can change its value equal to 0 then −If the next consecutive element is 1, it will automatically get converted to 0If the next consecutive element is already 0, nothing will happen.If arr[] = {1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1} then 4 operation are required to ... Read More

Minimum number of nodes in an AVL Tree with given height using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:15:22

338 Views

Problem statementGiven the height of an AVL tree, the task is to find the minimum number of nodes the tree can have.If height = 0 then AVL tree can have one 1 node If height = 5 then AVL tree can have minimum 20 nodesAlgorithmIn an AVL tree, we have to maintain the height balance property, i.e. a difference in the height of the left and the right subtrees cannot be more than -1, 0 or 1 for each node. Using this property, we can create below recurrence relation −1. If height = 0 then return 1 2. If height ... Read More

Advertisements