Found 7197 Articles for C++

Minimum insertions to make a Co-prime array in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:47:05

193 Views

In this section we will see another interesting problem. Suppose we have an array of N elements. We have to find minimum number of intersection points to make this array as co-prime array. In the co-prime array gcd of every two consecutive elements is 1. We have to print the array also.Suppose we have elements like {5, 10, 20}. This is not co-prime array. Now by inserting 1 between 5, 10 and 10, 20, it will be co-prime array. So the array will be like {5, 1, 10, 1, 20}AlgorithmmakeCoPrime(arr, n): begin    count := 0    for i in ... Read More

Minimum Initial Energy Required To Cross Street in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:40:05

447 Views

Suppose we have an array where positive and negative numbers are stored. The array is representing the checkpoint from one end to another end of the streets. The positive and negative values are representing the energy at the checkpoints. The positive values can increase energy, and negative number decreases energy. We have to find the initial energy level to cross the street, such that energy level never becomes 0 or less than 0.Suppose we have an array A = {4, -6, 2, 3}. Let the initial energy is 0. So after reaching at first check point, the energy is 4. ... Read More

Maximum Length Chain of Pairs in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:32:39

189 Views

There is a chain of pairs is given. In each pair, there are two integers and the first integer is always smaller, and second one is greater, the same rule can also be applied for the chain construction. A pair (x, y) can be added after a pair (p, q), only if q < x.To solve this problem, at first we have to sort given pairs in increasing order of first element. After that we will compare the second element of a pair, with the first element of next pair.Input − A chain of number pairs. {(5, 24), (15, 25), ... Read More

Matrix Chain Multiplication (A O(N^3) Solution) in C++

Arnab Chakraborty
Updated on 02-Dec-2024 11:54:42

3K+ Views

If a chain of matrices is given, we have to find a minimum number of correct sequences of matrices to multiply. We know that the matrix multiplication is associative, so for four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, and A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.ExampleIn the given input there is an array say arr, which contains arr[] = {1, 2, 3, 4}. It means the matrices are of the order (1 x 2), (2 x 3), (3 x 4). Input − The ... Read More

Tribonacci Word in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:17:14

220 Views

The Tribonacci Word is a sequence of digits. This is similar to the Fibonacci Words. Tribonacci Word is constructed by repeated concatenation of three previous stringsT(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few strings to start, are {1, 12, 1213} So the next one will be 1213 + 12 + 1 = 1213121Algorithmtribonacci_word(n): Begin    first := 1, second := 12, third := 1213    print first, second, third    for i in range 3 to n, do       temp := third       third := third + second + ... Read More

Tribonacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:13:29

2K+ Views

Here we will see how to generate the Tribonacci numbers using C++. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}Algorithmtribonacci(n): Begin    first := 0, second := 1, third := 1    print first, second, third    for i in range n – 3, do       next := first + ... Read More

Tetranacci Numbers in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:10:21

198 Views

Here we will see how to generate the Tetranacci numbers using C++. The Tetranacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding four previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3) + T(n - 4)The first few numbers to start, are {0, 1, 1, 2}Algorithmtetranacci(n): Begin    first := 0, second := 1, third := 1, fourth := 2    print first, second, third, fourth    for i in range n – ... Read More

Sort the numbers according to their sum of digits in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:05:21

424 Views

In this section we will see how to sort numbers according to their sum of digits. So if a number has lesser sum of digits, then that will be placed at first, then next number will be placed with larger sum of digits.data = {14, 129, 501, 23, 0, 145}after sorting, they will be −data = {0, 14, 23, 501, 145, 129}Here we will create our own comparison logic to sort them. That comparison logic will be used in the sort function in C++ STL.Algorithmcompare(num1, num2): Begin    if sum of digits of num1 < sum of digits of num2, ... Read More

Sort an array of strings according to string lengths in C++

Arnab Chakraborty
Updated on 25-Sep-2019 11:51:12

1K+ Views

Here we will see how to sort a list of strings based on their lengths. So if a string has less number of characters, then that will be placed first, then other longer strings will be placed. Suppose the strings arestr_list = {“Hello”, “ABC”, “Programming”, “Length”, “Population”}after sorting, they will be −str_list = {“ABC”, “Hello”, “Length”, “Population”, “Programming”}Here we will create our own comparison logic to sort them. That comparison logic will be used in the sort function in C++ STL.Algorithmcompare(str1, str2): Begin    if length of str1 < length of str2, then       return 1    return ... Read More

Sort an array according to the order defined by another array in C++

Arnab Chakraborty
Updated on 25-Sep-2019 11:47:56

332 Views

In this section we will see another sorting problem. Suppose we have two arrays A1 and A2. We have to sort A1 in such a way that the relative order among the elements will be same as those are in A2. If some elements are not present in A2, then they will be appended after the sorted elements. Suppose A1 and A2 are the following −A1 = {2, 1, 2, 1, 7, 5, 9, 3, 8, 6, 8} A2 = {2, 1, 8, 3}After the sorting A1 will be like below −A1 = {2, 2, 1, 1, 8, 8, 3, ... Read More

Advertisements