Posts

Largest Rectangular Area in Histogram

Image
Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars.  For simplicity, assume that all bars have same width and the width is 1 unit. SAMPLE INPUT:  7  {6, 2, 5, 4, 5, 1, 6}  SAMPLE OUTPUT :  12 CODE

Print Palindromic Paths

Image
Given a matrix containing lower alphabetical characters only, we need to find all palindromic paths in given matrix. A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell. You cannot go down diagonally.  Input : mat[][] = {"aaab”, "baaa” “abba”}  aaaaaa (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (1, 3) -> (2, 3)   aaaaaa (0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (1, 3) -> (2, 3)   abaaba (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2) -> (2, 3)   Output   {"aaaaaa","aaaaaa","abaaba"}   Order of elements in the output array doesn't matter. CODE

Find the indices with anagram match

Given a String and a pattern find all the indices at which where we find an anagram match.  For example: String : BACDGABCDA  Pattern : ABCD              We can find a match at {0, 5, 6} In other words, if we find 4 consecutive characters from these indices, it contains the characters needed to match the pattern. CODE

Spiral Matrix

Given a 2D Matrix of order n X m , print Kth element in spiral form of matrix. See the following examples.  INPUT  Each test case starts with a number N, the size of the matrix(n*m). The next line contains the n*m numbers. The next number contains kth number.  OUTPUT   Prints the kth element. Constraints: If n or m is less than 1 , return -1;  Example:   Input:   mat[][] = {{1, 2, 3, 4, 5, 6} {7, 8, 9, 10, 11, 12} {13, 14, 15, 16, 17, 18}}  k = 17  Output :  10 CODE

Samosa (Puzzle)

Samosa Bhai got tired of the difficult tasks given by his owner along with meagre salary being paid to him. So he moves to Aloo uncle's village and opens a courier company there. In the village, everyone knows Aloo uncle and Kachori Aunty, so everyone sends their gifts using Samosa Bhai's courier company only. There are N houses in a row in the village. On New Year's Eve, each house sends a gift to all the other houses. The distance between two houses situated at positions x and y in the row is |x - y|. Samosa Bhai's courier company charges dk rupees for each delivery, where d is the distance between the 2 houses. Everyone chose Samosa Bhai's company because of Aloo uncle and Kachori aunty. So he decides to give them the amount of money earned by him modulo 1000000007 (109 + 7). You need to compute this amount.  Input                      The first line of each test case contains 2 integers N and k denoting the number of ...

Find The Ring (PUZZLE)

There is a new magician in town. His trick is known as "Find the Ring". He puts 3 glasses at 3 spots on the table, labeling them as 0, 1 and 2. Now, he hides a ring under one of the glasses. The glasses are opaque and placed upside down, so that the ring is not visible to the audience. Now, he begins to make certain swaps in between adjacent glasses, i.e. the glass at position 0 can be swapped with that at position 1, and the glass at position 2 can be swapped with glass at 1. Assuming that all swaps are done randomly with equal probability of 50%, you have to find index of the glass that is most likely to have the ring, at the end. In case of a tie, choose the one with lower index. Assume that the glass containing the ring was swapped exactly N times. Input: First line contain two space separated integers, index and N. index is index of glass that initially contains the ring and N is the total number of swaps involving the glass with ring. SAMPLE INPUT   3  1 1 0 1 2 ...

Wildcard Pattern Matching (DP)

Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text (not partial text). The wildcard pattern can include the characters ‘?’ and ‘*’ ‘?’ – matches any single character ‘*’ – Matches any sequence of characters (including the empty sequence) Text = "baaabab", Pattern = “*****ba*****ab", output : true Pattern = "baaa?ab", output : true Pattern = "ba*a?", output : true Pattern = "a*ab", output : false Each occurrence of ‘?’ character in wildcard pattern can be replaced with any other character and each occurrence of ‘*’ with a sequence of characters such that the wildcard pattern becomes identical to the input string after replacement. Let’s consider any character in the pattern. Case 1: The character is ‘*’ Here two cases arise We can ignore ‘*’ character and move to next character in the Pattern. ‘*’ cha...