# C++ Program to get length of longest subsequence in n times copied sequence

Suppose we have an array A with n elements. We can make another array consisting of n copies of the old array, added elements back-to-back. We have to find the the length of the new array's longest increasing subsequence? We know that a sequence p is a subsequence of an array b if p can be obtained from b by removing zero or more elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order.

## Problem Category

An array in the data structure is a finite collection of elements of a specific type. Arrays are used to store elements of the same type in consecutive memory locations. An array is assigned a particular name and it is referenced through that name in various programming languages. To access the elements of an array, indexing is required. We use the terminology 'name[i]' to access a particular element residing in position 'i' in the array 'name'. Various data structures such as stacks, queues, heaps, priority queues can be implemented using arrays. Operations on arrays include insertion, deletion, updating, traversal, searching, and sorting operations. Visit the link below for further reading.

https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm

So, if the input of our problem is like A = [3, 1, 4, 1, 5, 9], then the output will be 5, because the possible subsequence will be [1, 3, 4, 5, 9].

## Steps

To solve this, we will follow these steps −

Define one set s
for initialize i := 0, when i < size of A, update (increase i by 1), do:
insert A[i] into s
return size of s

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
set<int> s;
for (int i = 0; i < A.size(); i++){
s.insert(A[i]);
}
return s.size();
}
int main(){
vector<int> A = { 3, 1, 4, 1, 5, 9 };
cout << solve(A) << endl;
}

## Input

{ 3, 1, 4, 1, 5, 9 }

## Output

5

Updated on: 08-Apr-2022

117 Views