Close

## Searching for an element in array with successive elements atmost 1 distance apart

Given an array of integers where each element has a difference of atmost 1 with it’s neighbors, we need to find the given element. For example, consider the array [1, 2, 2, 1, 2, 3, 2, 1], and the element to be searched is 3, we need to return the index 5. This is just…

## Searching for a word in a grid

Given a two dimensional table of letters, and a word to be searched, design an algorithm to check if it is present in the grid. The word can be formed in any of the 8 possible directions as shown below. Here is how I solved this problem. Write a function which takes the string to…

## Minimum number after removing K digits

Given a number N, and a number K, find the minimum number that can be formed by deleting K digits. The order of the digits should not be changed. For example consider N = 234987, K = 2, we can remove 9,8 and the resulting number is 2347.The solution is based on the following observation,…

## Maximum number of 1s in any row of a boolean matrix

Given a matrix consisting of only 0,1s, with each of it’s row sorted (i.e all 0s appear before 1s). The problem is to efficiently find the maximum number of 1s present in any row. For example consider the following matrix. 0 0 1 10 0 0 10 1 1 10 0 0 0 The maximum…

## Type casting in C++ – Part 1

Type casting frequently arises in real life programming. This post concentrates on type conversion of fundamental data types. Type casting is the process of converting from one data type to another. In C++, type casting is automatic in some situations. These are called implicit conversions. For example char c = ‘A’; int ch_code = c;…

## Right view of the binary tree

Given a binary tree, how do we print the right view of it?For example, consider the simple binary tree below    1 / 2   3 The right view of this is 1, 3. Similarly consider one more example             1          /           2    5        /          3   4  Right view for this tree contains…

## Alternate arrangement of two linked lists

Given two linked lists, we have to merge them into a single list by alternatively taking one node from each list.We should do this by re-arranging the links and should not create duplicate nodes while merging. For example consider the two lists 1 -> 2 -> NULL3 -> 4 -> NULL The result should look…

## Left view of a binary tree

Given a binary tree, how do we print the left view of it?For example, consider the simple binary tree below    1 / 2   3 The left view of this is 1, 2. Similarly consider one more example             1          /           2    3             /             4   5Left view for this tree contains 1, 2,…

## Maximum nesting depth of a paranthesis expression

Given a parenthesis expression, We have to find the maximum nesting depth. For example: the expression ()((()))(()) has a maximum nesting depth of 3. The expression ((()())) has a maximum nesting depth of 3. Let us output -1 for an unbalanced expressions such as (())) or ()( This is a simple problem that can be…

## Longest path in a binary tree

Given a binary tree, how do we find the longest distance between any two nodes in it? Some times this is referred to as the diameter of the tree.For example consider the below binary tree The longest path in this tree is 1 – 2 – 3 – 5 – 8 – 6 – 7.…