Server Side Programming Articles - Page 2172 of 2650

Check if a binary string has a 0 between 1s or not in C++

Arnab Chakraborty
Updated on 25-Sep-2019 13:56:06

191 Views

Here we will see one interesting problem. We have to check whether a string has 0 in between a 1s or not. If not, then the string is valid, otherwise invalid.Suppose there are three strings −100011110100000111110001111101111From these three strings, only B is valid, because there is no 0 inside the stream of 1sTo solve this problem, we will find the index of first 1 present in the string, and also find the index of the last 1. Then we will check, is there any 0 from these two indices, if so, then return false, otherwise true (as valid)Example Live Demo#include ... Read More

Check for Integer Overflow in C++

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

386 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

Python Program for Difference between sums of odd and even digits

Pavitra
Updated on 25-Sep-2019 13:20:27

622 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.To reduce the computational time we use the concept of mental mathematics .The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the ... Read More

Python Program for cube sum of first n natural numbers

Pavitra
Updated on 25-Sep-2019 13:16:16

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an input n, we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.Here we will discuss two approach to reach the solution of the problem statement −Brute-force approach using loops.Mathematical solution of sum of n numbers.Approach 1 −Computing sum of each term by adding by iterating over the numbersExample Live Demodef sumOfSeries(n):    sum = 0    for i in range(1, n+1):       sum +=i*i*i    return sum # ... Read More

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

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

3K+ 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

Python Program for compound interest

Pavitra
Updated on 25-Sep-2019 13:13:09

587 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −We are given with three input values i.e principle, rate & time and we need to compute compound interest.The code given below shows the process of computation of compound interest.The formula used here isCompound Interest = P(1 + R/100)rWhere, P is the principal amountR is the rate andT is the time spanThe implementation is given belowExample Live Demodef compound_interest(principle, rate, time):    CI = principle * (pow((1 + rate / 100), time))    print("Compound interest : ", CI) # main compound_interest(10000, 7.78, 2)OutputCompound ... 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

365 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

135 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

Python Program for Bubble Sort

Pavitra
Updated on 25-Sep-2019 12:59:03

2K+ Views

In this article, we will learn about the implementation of bubble sort sorting technique.The figure shown below illustrates the working of this algorithm −ApproachStarting with the first element(index = 0), compare the current element with the next element of the array.If the current element is greater than the next element of the array, swap them.If the current element is less than the next element, move to the next element.Repeat Step 1.Now let’s see the implementation below −Exampledef bubbleSort(ar):    n = len(arr)    # Traverse through all array elements    for i in range(n):    # Last i elements are ... Read More

Advertisements