Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts

K’th Largest Element in BST when modification to BST is not allowed

Given a Binary Search Tree (BST) and a positive integer k, find the k’th largest element in the Binary Search Tree.

      [20]
      /  \     
    [8]  [22] 
    /  \
  [5]  [10]
       /  \
     [9]  [12]

For example, in the following BST, if
if k = 3, then output should be 12
if k = 5, then output should be 9.

Practo Hiring Experience

Started from Written Test on HackeEarth

1) http://www.hackerearth.com/problem/algorithm/divide-to-three-33/
Code Solution Click : http://ideone.com/7M1o9j
2) http://www.hackerearth.com/problem/algorithm/good-times-vs-bad-times-3/
Code Solution Click : http://ideone.com/xj7o49

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Example : 00001111
Output : 4

Method 1 : in O(n) we can do it . with sequential search.

Method 2 : Binary search.
Yes binary search is useful, as data is sorted.
Just we need to change its conditions for 0 and 1.
lets have a look at Function firstOccurance.

Generate next palindrome number

Several Companies like MS, Amazon and Flipkart has started asking this question to generate next number which is palindrome.

Say K is given, Generate N such that N is palindrome and N > K.

How Radix sort works

Radix sort is simple and unique sorting method.
It sorts number with LSD and MSD ( Lease Significant Digit to Most Significant Digit)

Radix Sort puts the elements in order by comparing the digits of the numbers. I will explain with an example.