Saturday, August 30, 2014

Friday, August 29, 2014

Adobe Interview: Find next larger element in the right side in an array

Write a program to replace each element of an array with a number present on the right side of the element such that the number is least greater than the element. If there is no greater number replace it with -1 

e.g : 8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28 
ans : 18, 63, 80, 25, 32, 43, 80, 93, 80, 25, 93, -1, 28, -1, -1 



Idea here is to create a BST with the elements in array starting from rightmost element in the array and keep track of left turn.
The last left turn node is the answer.

Here is the link:

http://ideone.com/UCfr8J

Monday, August 25, 2014

Optym Online Test: Shortest Palindrom

Given a string, Find minimum no of characters to be inserted to convert it to palindrome.
Input: ab
         aa
         abcd

Output:
     1 .  insertion to make it bab
     0   insertion to make it aa
     3   insertions to make it dcbabcd

Optym Online Test: Minimize the moves

You have a sequence of d[0] , d[1], d[2] , d[3] ,..,d[n]. In each move you are allowed to increase any d[i] by 1 or 2 or 5  i:0 to n .What is the minimum number of moves required to transform the sequence to permutation of [1,2,3,..,n] if it's possible else return -1. 
Constraint: 1<=n<=1000

Input:
First Line will have no of elemnts
Second Line on. elements will be present
Ex: 5
  1
1
3
2
1

Output:
As no of elements are 5, all permutation can be {1, 2, 3, 4, 5} .
To convert input to get all possible permutation i.e {1, 2, 5, 4, 3},
no of moves will be 4.

Hence output would be 4.

Optym Online Test: Complement of a number

Write a program to find complement of a number.

Ex: Input: 50
      Output: 13
 
   Input: 100
   Output: 27