Posts

Showing posts from 2017

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...

Implementation of Medical store Management System

Image
  Code   OutPut

Implementation of Tic-Tac-Toe game

Image
Rules of the Game The game is to be played between two people (in this program between HUMAN and COMPUTER). One of the player chooses ‘O’ and the other ‘X’ to mark their respective cells. The game starts with one of the players and the game ends when one of the players has one whole row/ column/ diagonal filled with his/her respective character (‘O’ or ‘X’). If no one wins, then the game is said to be draw Code Output   

C library function - qsort()

Image
Description               The C library function void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) sorts an array. Parameters base -- This is the pointer to the first element of the array to be sorted. nitems -- This is the number of elements in the array pointed by base. size -- This is the size in bytes of each element in the array. compar -- This is the function that compares two elements. Return Value This function does not return any value Code   #include <stdio.h> #include <stdlib.h> int values [] = { 88 , 56 , 100 , 2 , 25 }; int cmpfunc ( const void * a , const void * b ) { return ( *( int *) a - *( int *) b ); } int main () { int n ; printf ( "Before sorting the list is: \n" ); for ( n = 0 ; n < 5 ; n ++ ) { printf ( "%d " , values [ n ]); } qsort (...

Swapping two nodes

Given a linked list and two characters to swap , WE NEED TO SWAP TWO NODES NOT THE VALUES given. Algorithm step 1- start traversing the linked list step 2- assign the nodes which contain the given values as t1 and t2.respectively step 3- assign the previous nodes before the nodes contain the given values as p1 and p2 step 4-assign p1 next node to t2 step 5-assign p2 next node to t1 step 6-assign t1 next node to t2 next node step 7-at last assign assign t2 next node to p2 step 8-end For example the given linked list be 1->2->3->4->5->null swap 3 and 5 according to the algorithm p1 node will be 2 t1 node will be 3 p2 node will be 4 t2 node will be 5 then while we execute the below code         p1.next=t2;         p2.next=t1;         t1.next=t2.next;         t2.next=p2; the nodes get swapped. output will be 1->2->5->4->3->null Code public class L...

Solution For LRU Stack

The objective is to i mplement a stack using LinkedList. You need to implement push and pop function. When push is called, insert the items in list at the head. When the stack is empty and pop is called return 0 The maximum stack size allowed is 5 Whenever a push is called Case 1 : Value is NOT present in stack: If the stack is full (has 5 items) then remove the oldest pushed item on the stack and push the new item. If the stack is not full, then push the new item Case 2 : Value is already present in stack: Since the item is already present in the stack move it to the top of the stack. Algorithm Step 1 :- Create a class(or structure ,depends on the programming language) with a data members for the creation of linked list. Step 2:- Implement push and pop function using a linked list as we do normally. Step 3:- In push function before pushing the element to stack check the size of the stack. ->if size is less than 5 ,then push the element normally. ->els...

Female co passenger hack

The female co-passenger hack. I came across this trick on the internet. When you need to book a ticket on IRCTC (Indian Railways), book two seats instea of one, and cancel one after some days. One in your name and one in fake Female name to get a girls in the compartment. The IRCTC algorithm works that way.

BODMAS Rule

Easy and simple way to remember  BODMAS  rule !! B  →   B rackets first (parentheses) O   →  O f   (orders i.e. Powers and Square Roots, Cube Roots, etc.) DM  →   D ivision and  M ultiplication (start from left to right) AS   →  A ddition and  S ubtraction (start from left to right) Note: (i) Start Divide/Multiply from left side to right side since they perform equally. (ii) Start Add/Subtract from left side to right side since they perform equally. Steps to simplify the order of operation using BODMAS rule: First part of an equation is start solving inside the 'Brackets'. For Example;  (6 + 4) × 5 First solve inside ‘brackets’ 6 + 4 = 10, then 10 × 5 = 50. Next solve the mathematical 'Of'. For Example;  3 of 4 + 9 First solve ‘of’ 3 × 4 = 12, then 12 + 9 = 21. Next, the part of the equation is to calculate 'Division' and 'Multiplication'. We know that, when divisi...