• 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

Longest Increasing Subsequence

December 22, 2014 by Dhaval Dave

The longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.For example, length of LIS for { 3,2,6,4,5,1 } is 2 and LIS is {2,4,5}.

Method 1) Brute Force Approch

for (i=N; i>0 ; –i){
   Find a SubSequence of length i
   if any increasing subsequence
       Break;
}

But finding all subsequence of length i will give Time Complexity in Exponential.
as all subsequences will be N! / i! * (N-i)!

Lets think directly in DP way…

Method 2) Dynamic Programming way.

Take Example D := { 3, 2 , 6 , 4 , 5 , 1 }    We are defining vector L of vector <Numbers> such that
L[i] = A vector, Longest Increasing sequence of D that ends with D[i].

L[0] = 3.
L[1] = 2 ( because 3 > 2, and we need to find increasing sub sequence )
L[2] = 2,6
L[3] = 2,4
L[4] = 2,4,5
L[5] = 1

say L[2] = 2,6 is ‘2’ + ‘6’  where 2 is so far found max LIS, and ‘6’ is current element

So formula
L[i] is  maximum of all L[j] where j<i and D[j]<D[i] + ‘D[i]’

L[i] = MAX(L[j], where j<i, D[j]<D[i]) + D[i]

So now We have working code to print all Increasing SubSequence
Edit it your way to find larget increasing subsequence

Code :

#include <iostream>
using namespace std;
#include <iostream>
#include <vector>


void prt(vector<int>& arr, string msg = “”) {
cout << msg << ” “;
for  (auto i: arr) {
cout << i << ” “;
}
cout << endl;
}


void calc_LIS(vector<int>& D) {
vector< vector<int> > L(D.size());  // The longest increasing subsequence ends with D[i]

   L[0].push_back(D[0]);

for (int i=1; i<D.size(); i++) {
for(int j=0; j<i; j++) {
if ( (D[j] < D[i]) && ( L[i].size() < L[j].size() ) ) {
L[i] = L[j];  
}         
}
      L[i].push_back(D[i]);
}

for (auto x: L) {
prt(x);
}

}

int main() {
int a[] = {3, 2, 6, 4, 5, 1};
vector<int> arr(a, a + sizeof(a)/sizeof(a[0]));



//prt(arr, “Data In:”);
calc_LIS(arr);

return 0;
}

Working Code at : http://ideone.com/lOrktE

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Hacker Earth Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array, Dynamic Programming

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

SAP Off Campus Hiring_ March 2015 Verbal Skills

Password Predictor

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

Singly linked list

Generic Object Oriented Stack with Template

Word Break Problem

Maximum of all subarrays of size k

Linked List V/S Binary Search Tree

Given a string, find the first character which is non-repetitive

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

Edit Distance ( Dynamic Programming )

Implement LRU Cache

Urban Ladder Written Test.

Print Power Set of a Set

LeetCode : Word Search

Length of the longest substring without repeating characters

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

CodeChef’ RRCOPY

Reliance Jio Software Developer Interview Experience

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

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Trapping Rain Water

TicTacToe Game As Asked in Flipkart

Coin Collection Dynamic Programming

Printing intermediate Integers between one element & next element of array

Find Percentage of Words matching in Two Strings

Amazon Interview On-Campus For Internship – 1

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

25 horses 5 tracks Find 3 fastest puzzle

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Copyright © 2025 · Genesis Framework · WordPress · Log in