Given an array of n positive integers. We need to find the largest increasing sequence of consecutive positive integers.
Examples:
Input : arr[] = {5, 7, 6, 7, 8}
Output : Size of LIS = 4
LIS = 5, 6, 7, 8
Input : arr[] = {5, 7, 8, 7, 5}
Output : Size of LIS = 2
LIS = 7, 8
This problem can be solved easily by the concept of LIS where each next greater element differ from earlier one by 1. But this will take O(n^2) time complexity.
With the use of hashing we can finding the size of longest increasing sequence with consecutive integers in time complexity of O(n).
We create a hash table.. Now for each element arr[i], we perform hash[arr[i]] = hash[arr[i] – 1] + 1. So, for every element we know longest consecutive increasing subsequence ending with it. Finally we return maximum value from hash table.
C++
#include <bits/stdc++.h>
using namespace std;
int findLIS(int A[], int n)
{
unordered_map<int, int> hash;
int LIS_size = 1;
int LIS_index = 0;
hash[A[0]] = 1;
for (int i = 1; i < n; i++) {
hash[A[i]] = hash[A[i] - 1] + 1;
if (LIS_size < hash[A[i]]) {
LIS_size = hash[A[i]];
LIS_index = A[i];
}
}
cout << "LIS_size = " << LIS_size << "\n";
cout << "LIS : ";
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index) {
cout << start << " ";
start++;
}
}
int main()
{
int A[] = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = sizeof(A) / sizeof(A[0]);
findLIS(A, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findLIS(int A[], int n)
{
Map<Integer, Integer> hash = new HashMap<Integer, Integer>();
int LIS_size = 1;
int LIS_index = 0;
hash.put(A[0], 1);
for (int i = 1; i < n; i++)
{
hash.put(A[i], hash.get(A[i] - 1)==null? 1:hash.get(A[i] - 1)+1);
if (LIS_size < hash.get(A[i]))
{
LIS_size = hash.get(A[i]);
LIS_index = A[i];
}
}
System.out.println("LIS_size = " + LIS_size);
System.out.print("LIS : ");
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index)
{
System.out.print(start + " ");
start++;
}
}
public static void main(String[] args)
{
int A[] = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = A.length;
findLIS(A, n);
}
}
|
Python3
def findLIS(A, n):
hash = dict()
LIS_size, LIS_index = 1, 0
hash[A[0]] = 1
for i in range(1, n):
if A[i] - 1 not in hash:
hash[A[i] - 1] = 0
hash[A[i]] = hash[A[i] - 1] + 1
if LIS_size < hash[A[i]]:
LIS_size = hash[A[i]]
LIS_index = A[i]
print("LIS_size =", LIS_size)
print("LIS : ", end = "")
start = LIS_index - LIS_size + 1
while start <= LIS_index:
print(start, end = " ")
start += 1
if __name__ == "__main__":
A = [ 2, 5, 3, 7, 4, 8, 5, 13, 6 ]
n = len(A)
findLIS(A, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findLIS(int []A, int n)
{
Dictionary<int,int> hash = new Dictionary<int,int>();
int LIS_size = 1;
int LIS_index = 0;
hash.Add(A[0], 1);
for (int i = 1; i < n; i++)
{
if(hash.ContainsKey(A[i]-1))
{
var val = hash[A[i]-1];
hash.Remove(A[i]);
hash.Add(A[i], val + 1);
}
else
{
hash.Add(A[i], 1);
}
if (LIS_size < hash[A[i]])
{
LIS_size = hash[A[i]];
LIS_index = A[i];
}
}
Console.WriteLine("LIS_size = " + LIS_size);
Console.Write("LIS : ");
int start = LIS_index - LIS_size + 1;
while (start <= LIS_index)
{
Console.Write(start + " ");
start++;
}
}
public static void Main(String[] args)
{
int []A = { 2, 5, 3, 7, 4, 8, 5, 13, 6 };
int n = A.Length;
findLIS(A, n);
}
}
|
Javascript
<script>
function findLIS(A, n) {
let hash = new Map();
let LIS_size = 1;
let LIS_index = 0;
hash.set(A[0], 1);
for (let i = 1; i < n; i++) {
hash.set(A[i], hash.get(A[i] - 1) == null ?
1 : hash.get(A[i] - 1) + 1);
if (LIS_size < hash.get(A[i])) {
LIS_size = hash.get(A[i]);
LIS_index = A[i];
}
}
document.write("LIS_size = " + LIS_size + "<br>");
document.write("LIS : ");
let start = LIS_index - LIS_size + 1;
while (start <= LIS_index) {
document.write(start + " ");
start++;
}
}
let A = [2, 5, 3, 7, 4, 8, 5, 13, 6];
let n = A.length;
findLIS(A, n);
</script>
|
Output: LIS_size = 5
LIS : 2 3 4 5 6