Showing posts with label Search. Show all posts
Showing posts with label Search. Show all posts

Find and print longest consecutive number sequence in a given sequence in O(n)

Ex: Input: 1 2 5 3 6 8 7
    Output: 5 6 7 8 
As given in Above example we need to find Longest Consecutive sequence in O(n)
I have implemented in O(n) considering by creating hash  of data type bool.

Length of the longest substring without repeating characters

Length of the longest substring without repeating characters



Method 1 (Brute Force Method)
We can consider all substrings.
One by one and check for each substring whether it contains all unique characters or not. 

There will be n*(n+1)/2 substrings. 

Get K Max and Delete K Max in stream of incoming integers

Get K Max and Delete K Max in stream of Incoming Integers 

Solution 1 : Using Min Heap

Algorithm :
1) Keep a Min & Max Heap of K size
2) Store first K elements in them.
3) If a new element comes with smaller or larger than root then reorder   heap.

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Given n stairs, how many number of ways can you climb if u use either 1, 2 or 3 at a time?

When you find any difficult problem first step is to reduce it to very small problem with example

Consider 3 steps. you can take only one at a time. Then number of ways are 1: ie {(1,1,1)}
Consider 2 steps. you can take 1 or 2 at a time. Then number of ways are 2: ie {(1,1), (2)}

Implement a generic binary search algorithm for Integer Double String etc

A Simple Binary Search for Integers

BinarySearch(A[0..N-1], value) 
{
   low = 0
   high = N - 1
   while (low <= high) {
       mid = low + ((high - low) / 2)