# Longest increasing sub-sequence formed by concatenating array to itself N times

Given an array **arr[]** of size **N**, the task is to find the length of the longest increasing subsequence in the array formed by the concatenation of the array **arr** to itself **N** times.

**Examples:**

Input:arr[] = {3, 2, 1}, N = 3

Output:3

Explanation:

The array formed by the concatenation: {3, 2, 1, 3, 2, 1, 3, 2, 1}

The longest increasing subsequence that can be formed from this array is of length 3 which is {1, 2, 3}

Input:N = 3, arr[] = {3, 1, 4}

Output:

Explanation:

The array formed by concatenation: {3, 1, 4, 3, 1, 4, 3, 1, 4}

The longest increasing subsequence that can be formed from this array is of length 3 which is {1, 2, 3}

**Naive Approach:**

The basic approach to solve this problem is to create the final array by concatenating the given array to itself N times, and then finding the longest increasing subsequence in it.

**Time Complexity:** O(N^{2})

**Auxilary Space Complexity:** O(N^{2})

**Efficient Approach:**

According to the efficient approach, any element that is present in the longest increasing subsequence can be present only once. It means that the repetition of the elements N times won’t affect the subsequence, but, any element can be chosen anytime. Therefore it would be efficient to find the longest increasing subset in the array of length N, which can be found by finding all the unique elements of the array.

Below is the algorithm for the efficient approach:

- Store the unique elements of the array in a map with (element, count) as the (key, value) pair.
- For each element in the array
- If the current element is not present in the map, then insert it in the map, with count 1.
- Otherwise, increment the count of the array elements in the array.

- Find the length of the map which will be the desired answer.

**For Example:**

Given Array be – {4, 4, 1}

Creating the map of unique elements: {(4, 2), (1, 1)}

Length of the Map = 2

Hence the required longest subsequence = 2

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// longest increasing subsequence ` `// in repeating element of array ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the LCS ` `int` `findLCS(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` ` ` `// Loop to create frequency array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `mp[arr[i]]++; ` ` ` `} ` ` ` ` ` `return` `mp.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `int` `arr[] = { 3, 2, 1 }; ` ` ` `cout << findLCS(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Performance Analysis:**

**Time Complexity:**As in the above approach, there only one loop which takes O(N) time in worst case, Hence the Time Complexity will be**O(N)**.**Space Complexity:**As in the above approach, there one Hash map used which can take O(N) space in worst case, Hence the space complexity will be**O(N)**

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Longest palindrome formed by concatenating and reordering strings of equal length
- Longest Increasing Subsequence using BIT
- Longest Increasing Subsequence | DP-3
- Longest Increasing Odd Even Subsequence
- Construction of Longest Increasing Subsequence (N log N)
- Longest Increasing consecutive subsequence
- C/C++ Program for Longest Increasing Subsequence
- C++ Program for Longest Increasing Subsequence
- Longest Increasing Subsequence Size (N log N)
- Longest Common Increasing Subsequence (LCS + LIS)
- Python program for Longest Increasing Subsequence
- Java Program for Longest Increasing Subsequence
- Printing longest Increasing consecutive subsequence
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.