• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

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

August 6, 2014 by Dhaval Dave

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.

Logic :
1) Create a function
2) Pass initial values one by one to this function and length.
3) What function does ?
Answer : it checks the longest possible consecutive sequence in array
How : pass 1, function will check whether 2 is there is array,
if present increase count and pass 2 to same function.
else return length.
4) get maximum such length and last passed index for which maximum such length is there.
5) Print from max index till the length.

example :
function will return Index, Count as
1-3
2-2
5-4 //max
3-1
6-3
8-1
7-2

PS : To optimize search in hash for sequential search, we can use dynamic programming which may cause extra space. hence here we have not used it. else People are welcome to share Solution with DP

Thanks to Dhaval for suggesting this approach and Article

Code

#include <stdio.h>
#include <stdbool.h>
#define gc getchar_unlocked
#define pc putchar_unlocked
inline int scan(){register int n=0,c=gc();while(c<‘0’||c>’9′)c=gc();while(c<=’9’&&c>=’0’)n=(n<<1)+(n<<3)+c-‘0’,c=gc();return n;}
bool c[100001]={0};

int findLongestsequence(int a, int count){
    int st=a,i,j;
    a++;
    if(c[a]==1){return(findLongestsequence(a,++count));}
    return count;
}//findLongestsequence

int main(void){
    int i=0,j,t,n,st,len,count=1,maxcount=1,maxseq=-1;
    len=scan();
    j=len;
    int a[len];

    while(j–){
    a[i]=scan();
    c[a[i]]=1; //creating hash with value pair same
    i++;
    }
    printf(“Entered String :”);
    for(i=0;i<len;i++){printf(“%d “,a[i]);}
    for(i=0;i<len;i++){
        
    count = findLongestsequence(a[i],count);
    if(count > maxcount ){
    maxcount= count;
    maxseq = a[i] ;
    }
    count=1;
    
    }
    //printf(“nSt and Len : %d & %dn”,maxseq,maxcount);
    printf(“nMaximum Consequitve string “);
    while(maxcount–) printf(“%d “,maxseq++);
    return 0;
    
}

You can find working code at : http://ideone.com/8xODTn

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Amazon Interview On-Campus For Internship – 1

Python List

LeetCode: Binary Tree Maximum Path Sum

Count number of ways to reach a given score in a game

DFS (Depth First Search)

Maximum sum contiguous subarray of an Array

Possible sizes of bus to carry n groups of friends

Find loop in Singly linked list

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

Find position of the only set bit

Implement a generic binary search algorithm for Integer Double String etc

Search element in a matrix with all rows and columns in sorted order

Diagonal Traversal of Binary Tree

Circular Linked List

Linked List V/S Binary Search Tree

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Number of Islands BFS/DFS

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

ADOBE Aptitude C Language Test

Sequence Finder Dynamic Programming

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Print vertical sum of all the axis in the given binary tree

Adobe Interview Questions 8 month Exp

Templates in C++

Find Percentage of Words matching in Two Strings

Given a float number convert it into the string WITHOUT using any inbuilt Function

Implement LRU Cache

Advanced SQL Injection

Python Array String

Binary Tree in Java

Copyright © 2025 · Genesis Framework · WordPress · Log in