Arnab Chakraborty has Published 4293 Articles

Single-Source Shortest Paths, Arbitrary Weights

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 12:59:46

10K+ Views

The single source shortest path algorithm (for arbitrary weight positive or negative) is also known Bellman-Ford algorithm is used to find minimum distance from source vertex to any other vertex. The main difference between this algorithm with Dijkstra’s algorithm is, in Dijkstra’s algorithm we cannot handle the negative weight, but ... Read More

Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:54:17

388 Views

We have the first N natural numbers. Our task is to get one permutation of them where the absolute difference between every two consecutive elements is > 1. If no such permutation is present, return -1.The approach is simple. We will use the greedy approach. We will arrange all odd ... Read More

Arrangement of words without changing the relative position of vowel and consonants?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:53:20

159 Views

Suppose we have a string with n elements (n < 10). We have to find the number of ways that the string can be arranged without changing the relative position of the vowel and consonants.The approach is simple. We have to count the number of vowels and consonants in the ... Read More

Array element with minimum sum of absolute differences?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:50:00

384 Views

Here we will see one interesting problem. We are taking one array ‘a’ with N elements. We have to find an element x such that |a[0] - x| + |a[1] - x|+ … + |a[n-1] - x| is minimized. Then we have to find the minimized sum.Let the array is: ... Read More

Array elements with prime frequencies?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:45:57

440 Views

Suppose we have one array. we have to count how many of the elements present in the array prime number of times. So if the array is {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, then 1 is present 4 times, 2 is present 3 times, ... Read More

An Interesting Method to Generate Binary Numbers from 1 to n?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:41:31

470 Views

Here we will see one interesting method for generating binary numbers from 1 to n. Here we are using queue. Initially the queue will hold first binary number ‘1’. Now repeatedly delete element from queue, and print it, and append 0 at the end of the front item, and append ... Read More

An interesting solution to get all prime numbers smaller than n?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:40:59

162 Views

Here we will see how to generate all prime numbers that are less than n in an efficient way. In this approach we will use the Wilson’s theorem. According to his theorem if a number k is prime, then ((k - 1)! + 1) mod k will be 0. Let ... Read More

Add elements of given arrays with given constraints?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 09:29:33

156 Views

Here we will see one problem. We will add two array elements and store them into another array. But we will follow some constraints. These constraints are like below −Addition should be stated from 0th index of both of the arraySplit the sum if it is more than one-digit number, ... Read More

Average of ASCII values of characters of a given string?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 07:26:14

869 Views

Here we will see how to count the average of the ASCII values of each character in a given string. Suppose the string is “ABC”. The asci values are 65, 66, 67. So the average of these three is 66.AlgorithmasciiAverage(String)Begin    sum := 0    for each character c in ... Read More

Array range queries for elements with frequency same as value in C Program?

Arnab Chakraborty

Arnab Chakraborty

Updated on 02-Jul-2020 06:56:57

295 Views

Here we will see one interesting problem. We have one array with N elements. We have to perform one query Q as follows −The Q(start, end) indicates that number of times a number ‘p’ is occurred exactly ‘p’ number of times from start to end.So if the array is like: ... Read More

Advertisements