Server Side Programming Articles - Page 1691 of 2650

Find all reachable nodes from every node present in a given set in C++

Arnab Chakraborty
Updated on 28-Aug-2020 09:18:13

655 Views

Suppose we have one undirected graph and a set of vertices; we have to find all reachable nodes from every vertex present in the given set.So, if the input is likethen the output will be [1, 2, 3] and [4, 5] as these are two connected components.To solve this, we will follow these steps −nodes := number of nodes in the graphDefine an array visited of size: nodes+1. And fill with 0Define one map mcomp_sum := 0for initialize i := 0, when i < n, update (increase i by 1), do −u := arr[i]if visited[u] is false, then −(increase comp_sum ... Read More

Find all palindromic sub-strings of a given string - Set 2 in Python

Arnab Chakraborty
Updated on 28-Aug-2020 09:09:34

142 Views

Suppose we have a string; we have to find all the palindromic sub-strings from that string. Here aa and aa are considered as two sub-strings, not one.So, if the input is like redivider, then the output will be ['r', 'e', 'd', 'i', 'v', 'ivi', 'divid', 'edivide', 'redivider', 'i', 'd', 'e', 'r']To solve this, we will follow these steps −v := a new listpos := 0.0while pos < size of s, dorad := pos - (pos as integer)while (pos + rad) < size of s and (pos - rad) >= 0 and (s[integer of (pos - rad)] is same as s[integer ... Read More

Find all good indices in the given Array in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:59:25

560 Views

Suppose we have an array A of numbers, we have to find all indices of this array so that after deleting the ith element from the array, the array will be a good array. We have to keep in mind that −Good array is an array with an element that equals to the sum of all other elements.1-based indexing will be used here.So, if the input is like [10, 4, 6, 2], then the output will be [1, 4] as when we remove A[1], the array will look like [4, 6, 2] and it is good, as 6 = 4+2. ... Read More

Find all distinct palindromic sub-strings of a given String in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:55:03

421 Views

Suppose we have a string with lowercase ASCII characters, we have to find all distinct continuous palindromic substrings of it.So, if the input is like "bddaaa", then the output will be [a, aa, aaa, b, d, dd]To solve this, we will follow these steps −m := a new mapn := size of smatrix := make two rows of n number of 0ss := "@" concatenate s concatenate "#"for j in range 0 to 1, dotemp := 0matrix[j, 0] := 0i := 1while i

Find a string such that every character is lexicographically greater than its immediate next character in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:46:39

228 Views

Suppose we have a number n; we have to check a lower case stringof length n+1 so that the character at any position should be lexicographically bigger than its immediate next character.So, if the input is like 15, then the output will be ponmlkjihgfedcba.To solve this, we will follow these steps −temp_str := blank stringextra := n mod 26if extra >= 1, thenfor i in range 26 -(extra + 1) to 25, dotemp_str := temp_str + str[i]count := n / 26 (integer division)for i in range 1 to count + 1, dofor j in range 0 to 25, dotemp_str := ... Read More

Find a sorted subsequence of size 3 in linear time in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:38:31

207 Views

Suppose we have an array with N numbers, we have to check whether the 3 elements such that b[i]< b[j] < b[k] and i < j < k in linear (O(n)) time. If there are multiple such triplets, then print any one of them.So, if the input is like [13, 12, 11, 6, 7, 3, 31], then the output will be [6, 7, 31]To solve this, we will follow these steps −n := size of Amaximum := n-1, minimum := 0smaller := an array of size 1000, and fill with 0smaller[0] := -1for i in range 1 to n, doif ... Read More

Find a positive number M such that gcd(N^M,N&M) is maximum in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:32:45

157 Views

Suppose we have a number N, we have to find a positive number M such that gcd(N^M, N&M) is as large as possible and m < n. We will also return the largest gcd thus obtained.So, if the input is like 20, then the output will be 31To solve this, we will follow these steps −if bit_count(n) is same as 0, thenfor i in range 2 to int(square root of (n)) + 1, doif n mod i is same as 0, thenreturn int(n / i)otherwise, val := 0p :=dupn := nwhile n is non-zero, doif (n AND 1) is same ... Read More

Find a permutation that causes worst case of Merge Sort in C

Arnab Chakraborty
Updated on 23-Jul-2020 08:33:31

319 Views

ConceptWith respect of a given set of elements, determine which permutation of these elements would result in worst case of Merge Sort?We know, asymptotically, merge sort always consumes O(n log n) time, but the cases that need more comparisons generally consume more time in practice. Now we basically require determining a permutation of input elements that would lead to largest number of comparisons when sorted implementing a typical Merge Sort algorithm.Example Consider the below set of elements as Sorted array 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26Resultant input array that will result ... Read More

Find a pair with given sum in a Balanced BST in Java

Arnab Chakraborty
Updated on 23-Jul-2020 08:32:23

236 Views

ConceptWith respect of a given Balanced Binary Search Tree and a target sum, we write a function that returns true if there is a pair with sum equals to target sum, otherwise return false. In this case, expected time complexity is O(n) and only O(Logn) extra space can beimplemented. Here, any modification to Binary Search Tree is not permitted.We have to note that height of a Balanced BST is always O(Logn).ExampleMethodAccording to the Brute Force Solution, we consider each pair in BST and verify whether the sum equals to X. The time complexity of this solution will be O(n^2).Now a ... Read More

Find a number which give minimum sum when XOR with every number of array of integer in C++

Arnab Chakraborty
Updated on 23-Jul-2020 08:25:24

282 Views

ConceptWith respect of given array Arr[] of non-negative integers, the task is to determine an integer X such that (Arr[0] XOR X) + (Arr[1] XOR X) + … + Arr[n – 1] XOR X is minimum possible.Input Arr[] = {3, 4, 5, 6, 7}Output X = 7, Sum = 10ApproachSo we will verify ‘i’th bit of every number of array in binary representation and consider and count those numbers containing that ‘i’th bit set to ‘1’ because these set bits will contribute to maximize the sum instead of minimize. As a result of this, we have to build this set ‘i’th bit ... Read More

Advertisements