Found 7347 Articles for C++

Check for Integer Overflow in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:50:57

276 Views

The only safe way is to check for overflow before it occurs. There are some hacky ways of checking for integer overflow though. So if you're aiming for detecting overflow in unsigned int addition, you can check if the result is actually lesser than either values added. So for example, Exampleunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively "value < y" should also workThis is because if x and y are both unsigned ints, if added and they overflow, their values can't be greater than either of them as ... Read More

Fleury’s Algorithm for printing Eulerian Path or Circuit in C++

Arnab Chakraborty
Updated on 25-Sep-2019 14:53:48

2K+ Views

Fleury’s Algorithm is used to display the Euler path or Euler circuit from a given graph. In this algorithm, starting from one edge, it tries to move other adjacent vertices by removing the previous vertices. Using this trick, the graph becomes simpler in each step to find the Euler path or circuit.We have to check some rules to get the path or circuit −The graph must be a Euler Graph.When there are two edges, one is bridge, another one is non-bridge, we have to choose non-bridge at first.sChoosing of starting vertex is also tricky, we cannot use any vertex as ... Read More

Find One’s Complement of an Integer in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:34:21

2K+ Views

In this section, we will see how to find the 1’s complete of an integer. We can use the complement operator to do this task very fast, but it will make 32bit complemented value (4-bype integer). Here we want complement of n bit numbers.Suppose we have a number say 22. The binary equivalent is 10110. The complemented value is 01001 which is same as 9. Now the question comes, how to find this value? At first we have to find number of bits of the given number. Suppose the count is c (here c = 5 for 22). We have ... Read More

Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:14:18

269 Views

Here we will see another problem, where one string and another integer value say k is given. We have to find the number of substrings of length k, whose sum of ASCII values of characters is divisible by k.Suppose a string is “BCGABC”. And the value of k is 3. Here string BCG has ASCII sum 300, ABC has ASCII sum 294, both are divisible by k = 3.The approach is simple. At first we have to find the ASCII value of characters of first substring, whose length is k. We have to use the sliding window technique and subtract ... Read More

Find Cube Pairs - (A n^(2/3) Solution) in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:09:18

84 Views

A number is given. We have to find two pairs, that can represent the number as sum of two cubes. So we have to find two pairs (a, b) and (c, d) such that the given number n can be expressed as n = a3 + b3 = c3 + d3The idea is simple. Here every number a, b, c and d are all less than n1/3. For every distinct pair (x, y) formed by number less than n1/3, if their sum (x3 + y3) is equal to the given number, we store them into hash table with the sum ... Read More

Find any pair with given GCD and LCM in C++

Arnab Chakraborty
Updated on 25-Sep-2019 12:56:42

189 Views

In this section we will see how to get number of pairs using the given GCD and LCM values. Suppose the GCD and LCM values are 2 and 12. Now the possible pairs of numbers are (2, 12), (4, 6), (6, 4) and (12, 2). So our program will find the count of pairs. That is 4.Let us see the algorithm to understand what will be the technique to solve this problem.AlgorithmcountPairs(gcd, lcm): Begin    if lcm is nit divisible by gcd, then       return 0    temp := lcm/gcd    c := primeFactorCount(temp)    res := shift ... Read More

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

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

115 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

353 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

118 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 25-Sep-2019 12:22:56

2K+ Views

If a chain of matrices is given, we have to find minimum number of correct sequence of matrices to multiply.We know that the matrix multiplication is associative, so four matrices ABCD, we can multiply A(BCD), (AB)(CD), (ABC)D, A(BC)D, in these sequences. Like these sequences, our task is to find which ordering is efficient to multiply.In 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 orders of the input matrices. {1, 2, 3, 4}. ... Read More

Advertisements