Found 26504 Articles for Server Side Programming

Print all palindromic paths from top left to bottom right in a matrix in C++

sudhir sharma
Updated on 14-Jul-2020 07:36:48

220 Views

In this problem, we are given a matix which contains aplhabets (lowercase only) and we have to print all palidromic paths in the given matrix from top left to bottom right of the matrix.Allowed moves in this problem are right and down. Diagonal moves are not allowed.Let’s take an example to understand the problem −Input: matrix[][] ={    {"xxxy",    "yxxx",    "xyyx"} Output: xxxxxx , xxxxxx , xyxxyxExplainationLets see all valid moves from top left to bottom right using the position wrt to ith position.i00 -> i01 -> i02 -> i03 -> i13 -> i23 = xxxyxx i00 -> ... Read More

Prime Arrangements in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:57:12

326 Views

We have to find the number of permutations of 1 to n, so the prime numbers are placed at prime indices. The answers may be large, return the answer modulo 10^9 + 7. So if n = 5, then output will be 12. So there will be 12 permutations. one possible permutation will be [1, 2, 5, 4, 3], one invalid permutation is [5, 2, 3, 4, 1] because 5 is placed at index 1, that is not prime.To solve this, we will follow these steps −Define one method called getNum, as follows −prime := list of all primes from ... Read More

Print all paths from a given source to a destination using BFS in C++

sudhir sharma
Updated on 14-Jul-2020 07:28:01

1K+ Views

In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph using Breadth first Search (BFS).Directed graph is a graph in with edges that are directed from vertex a to b.Let’s take an example to understand the problem -Source = K destination = POutputK -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> PHere, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.To ... Read More

Day of the Year in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:54:48

3K+ Views

Suppose, we have a date in the format “YYYY-MM-DD”. We have to return the day number of the year. So if the date is “2019-02-10”, then this is 41st day of the year.To solve this, we will follow these steps −Suppose D is an array of day count like [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]Convert the date into list of year, month and dayif the year is leap year then set date D[2] = 29Add up the day count up to the month mm – 1. and day count after that.ExampleLet us see ... Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:45

317 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Print all paths from top left to bottom right in a matrix with four moves allowed in C++

sudhir sharma
Updated on 16-Jan-2020 12:39:11

265 Views

In this problem, we are given an mXn 2D matrix and we have to print all possible paths from top left to bottom right of matrix. For traversal, we can move in all four directions i.e. left, right, top, bottom.Thought the moves right and top are rarely used but these can be beneficial sometimes.Let’s take an example to understand the topic better :Input:1 3 5 2 8 9Output:1 -> 3 -> 5 -> 9 1 -> 3 -> 8 -> 9 1 -> 2 -> 8 -> 9To solve this problem, we will move from one cell to other ... Read More

Relative Sort Array in Python

Arnab Chakraborty
Updated on 18-May-2020 05:54:15

758 Views

Suppose we have two arrays arr1 and arr2, the elements of arr2 are unique, and all elements in arr2 are also present in arr1. We have to sort the elements of arr1 in such a way that the relative ordering of items in arr1 are the same as in arr2. If there are some elements that are not present in arr2, they should be placed at the end of arr1 in ascending order. So if the arr1 is like [2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], and arr2 is like [2, 1, 4, 3, 9, 6], ... Read More

Distribute Candies to People in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:45:56

1K+ Views

Suppose we want to distribute some number of candies to a row of n people in the following way −We then give 1 candy to the first people, 2 candies to the second people, and so on until we give n candies to the last people.After that, we go back to the start of the row again, give n + 1 candies to the first people, n + 2 candies to the second people, and so on until we give 2 * n candies to the last people.We will repeat this process until we run out of candies. The last ... Read More

Print all permutations in sorted (lexicographic) order in C++

sudhir sharma
Updated on 16-Jan-2020 12:29:24

541 Views

In this problem, we are given a string of length n and we have to print all permutations of the characters of the string in sorted order.Let’s take an example to understand the problem :Input: ‘XYZ’Output: XYZ, XZY, YXZ, YZX, ZXY, ZYX.Here we have to print all permutations in lexicographical order (alphabetically increasing order).To solve this problem, we have to first sort the array in alphabetically increasing order, the sorted array is the first element of the permutation. And then generate the next higher order permutation of the string.The below code will make the solution more clear to you :Example Live ... Read More

Occurrences After Bigram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 17:45:32

172 Views

Suppose there are words given. These are first and second, consider occurrences in some text of the form "first second third", here second comes immediately after the first, and third comes immediately after the second.For each such cases, add "third" into the answer, and show the answer. So if the text is like “lina is a good girl she is a good singer”, first = “a”, second = “good”, the answer will be [girl, singer]To solve this, we will follow these steps −text := split the string by spacesres is an empty listfor i := 0 to size of text ... Read More

Advertisements