Found 7347 Articles for C++

Sum of subset differences in C++

sudhir sharma
Updated on 05-Aug-2020 08:18:41

171 Views

In this problem, we are given a set S of n number. Our task is to create a program to find the sum of subset difference which is the difference of last and first elements of subset.The formula is, sumSubsetDifference = Σ [last(s) - first(s)] s are subsets of the set S.Let’s take an example to understand the problem, Input −S = {1, 2, 9} n = 3Output −Explanation − All subset are −{1}, last(s) - first(s) = 0 {2}, last(s) - first(s) = 0 {9}, last(s) - first(s) = 0 {1, 2}, last(s) - first(s) = 1 {1, 9}, last(s) ... Read More

Sum of similarities of string with all of its suffixes in C++

sudhir sharma
Updated on 05-Aug-2020 08:17:23

165 Views

In this problem, we are given string str. Our task is to create a program to find the sum of similarities of the string with all of its suffixes.Suffixes of string str are all the strings that are created by eliminating starting characters of the string.Similarities of string str1 and str2 is the length of the longest prefix common to both the string. For example, str1 = ‘abbac’ and str2 = ‘abb’ is 3.While str1 = ‘abca’ and str2 = ‘ca’ is 0. As we count from start.Let’s take an example to understand the problem, Input − str = ‘xyxyx’Output ... Read More

Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++

sudhir sharma
Updated on 05-Aug-2020 08:15:45

376 Views

In this problem, we are given a number n of the series. Our task is to find the sum of series 1^2 + 3^2 + 5^2 +... + (2*n - 1)^2 for the given value of n.Let’s take an example to understand the problem,Input −n = 5Output −84Explanation −sum = 1^2 + 3^2 + 5^2 + 7^2 + 9^2 = 1 + 9 + 25 + 49 = 84A basic approach to solve this problem is by directly applying the formula for the sum of series.Example Live Demo#include using namespace std; int calcSumOfSeries(int n) {    int sum = 0;    for (int i = 1; i

Sum of special triplets having elements from 3 arrays in C++

sudhir sharma
Updated on 05-Aug-2020 08:13:15

111 Views

In this problem, we are given 3 array X, Y, Z. Our task is to create a program to find the Sum of special triplets having elements from 3 arrays.Special Triplet is a special type of triplet that hold the following property −For (a, b, c): a ≤ b and b ≥ c, i.e the middle element of the triplet should be greeter that the other two.And, the value of the triplet is given by the formula −f(a, b, c) = (a+b) * (b+c)To create this triplet we need to use one element from each other the three arrays given.Let’s ... Read More

Sum of smaller elements of nodes in a linked list in C++

sudhir sharma
Updated on 05-Aug-2020 08:09:34

73 Views

In this problem, we are given a linked list with a node consisting of two values and a pointer. Our task is to create a program to find the sum of smaller elements of a node in a linked list.Here, in the linked list we have two elements say X and Y. The program will find a minimum of x and y. The minimum elements from all nodes are added which is the required result.Input −(5, 2)->(7, 9)->(6, 3)->(36, 24)->(19, 26)->nullOutput −55Explanation −Let’s take the minimum of X and Y from each node −node1 - mini = 5 node2 - ... Read More

Block swap algorithm for array rotation in C++

sudhir sharma
Updated on 05-Aug-2020 08:08:00

1K+ Views

The block swap algorithm for array rotation is an efficient algorithm that is used for array rotation. It can do your work in O(n) time complexity.So, in array rotation, we are given an array arr[] of size n and a number k that define the no. of the element to be rotated.Let’s see an example on array rotations −Input  −arr[] = {4, 6, 1, 8, 9, 2}, k = 2 (number of rotations.)Output −{1, 8, 9, 2, 4, 6}Explanation − On rotation, we will shift the one element to the last position and shift the next elements to one position.Element ... Read More

BK Tree Introduction in C++

sudhir sharma
Updated on 05-Aug-2020 08:05:17

228 Views

BK tree or Burkhard tree is a form of a data structure usually used to perform spell checks based on Levenshtein distance. It is also used for string matching Autocorrect feature can be used making this data structure. Let's say we have some words in a dictionary and we need to check some other words for spelling errors. We need to have a collection of words that is close to the given word whose spelling is to be checked. For example, if we have the word “uck” The correct word can be (truck, duck, duck, suck). Therefore spelling mistakes can ... Read More

Bitwise Sieve in C++

sudhir sharma
Updated on 05-Aug-2020 08:02:03

599 Views

In this problem, we are given a number N. Our task is to find all prime numbers smaller than N using Bitwise Sieve.Bitwise sieve is an optimized version of Sieve of Eratosthenes which is used to find all prime numbers smaller than the given number.Let’s take an example to understand the problem, Input − N = 25Output − 2 3 5 7 11 13 17 19 23The bitwise sieve works in the same ways as the normal sieve. It just we will use the bits of integers of represent primes instead of a boolean type. This will reduce the space ... Read More

Bitwise OR of N binary strings in C++

sudhir sharma
Updated on 05-Aug-2020 07:59:26

273 Views

In this problem, we are given an array bin[] of size n of binary strings. Our task is to create a program to find the Bitwise OR (&) of n binary strings.Here, we will take all numbers and find the bitwise AND of them i.e. bin[0] | bin[1] |... bin[n-2] | bin[n]Let’s take an example to understand the problem, Input −bin[] = {“1001”, “11001”, “010101”}Output −011101Explanation − Bitwise OR of all binary string −(1001) | (11001) | (010101) = 011101To solve this problem, We will simply find the string with the largest number of bits (max length string). Then we ... Read More

Bitwise OR (or - ) of a range in C++

sudhir sharma
Updated on 05-Aug-2020 07:57:22

648 Views

In this problem, we are given two integer values a and b. And our task is to find the bitwise OR (|) of range from a to b. This means we will have to find the value of a | a+1 | a+2 | … b-1 | b.Let’s take an example to understand the problem, Input − a = 3 , b = 8Output − 15Explanation − 3 | 4 | 5 | 6 | 7 | 8 = 15To solve the problem, a simple solution is starting from a and find bit-wise OR of all numbers by increasing one ... Read More

Advertisements