# Minimum concatenation required to get strictly LIS for array with repetitive elements | Set-2

Given an array A[] of size n where there can be repetitive elements in the array. We have to find the minimum concatenation required for sequence A to get strictly Longest Increasing Subsequence. For the array A[] we follow 1 based indexing.

Examples:

Input: A = {2, 1, 2, 4, 3, 5}
Output: 2
Explanation:
We can concatenate A two times as [2, 1, 2, 4, 3, 5, 2, 1, 2, 4, 3, 5] and then output for index 2, 3, 5, 10, 12 which gives sub-sequence as 1 -> 2 -> 3 -> 4 -> 5.

Input: A = {1, 3, 2, 1, 2}
Output: 2
Explanation:
We can concatenate A two times as [1, 3, 2, 1, 2, 1, 3, 2, 1, 2] and then output for index 1, 3, 7 which gives sub-sequence as 1 -> 2 -> 3.

Approach:
To solve the problem mentioned above the very first observation is that a strictly increasing sub-sequence will always have its length equal to the number of unique elements present in sequence A[]. Hence, the maximum length of the subsequence is equal to the count of the distinct elements. To solve the problem follow the steps given below:

• Initialise a variable lets say ans to 1 and partition the sequence in two halves the left subsequence and the right one. Initialise the leftSeq to NULL and copy the original sequence in the rightSeq.
• Traverse in the right subsequence to find the minimum element, represented by variable CurrElement and store its index.
• Now update the left and right subsequence, where the leftSeq is updated with the given sequence up to the index which stores the minimum element in the right subsequence. And the rightSeq to given sequence from the minimum index value until the end.
• Traverse the array to get the next minimum element and update the value for CurrElement. If no such minimum value is there in rightSeq then it has to be in leftSeq. Find the index of that element in the left subsequence and store its index.
• Now again update the value for left and right subsequence where leftSeq = given sequence up to kth index and rightSeq = given sequence from kth index to end. Repeat the process until the array limit is reached.
• Increment the value for ans by 1 and stop when CurrElement is equal to highest element.

Below is the implementation of the above approach:

 `// CPP implementation to Find the minimum  ` `// concatenation required to get strictly  ` `// Longest Increasing Subsequence for the ` `// given array with repetitive elements ` `#include ` `using` `namespace` `std; ` ` `  `int` `LIS(``int` `arr[], ``int` `n) ` `{ ` `    ``// ordered map containing value and  ` `    ``// a vector containing index of  ` `    ``// it's occurrences ` `    ``map<``int``, vector<``int``> > m; ` ` `  `    ``// Mapping index with their values  ` `    ``// in ordered map ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[arr[i]].push_back(i); ` ` `  `    ``// k refers to present minimum index ` `    ``int` `k = n; ` ` `  `    ``// Stores the number of concatenation  ` `    ``// required ` `    ``int` `ans = 0; ` ` `  `    ``// Iterate over map m ` `    ``for` `(``auto` `it = m.begin(); it != m.end();  ` `                                       ``it++) { ` `        ``// it.second refers to the vector ` `        ``// containing all corresponding  ` `        ``// indexes ` ` `  `        ``// it.second.back refers to the  ` `        ``// last element of corresponding vector ` ` `  `        ``if` `(it->second.back() < k) { ` `            ``k = it->second; ` `            ``ans += 1; ` `        ``} ` `        ``else` ` `  `            ``// find the index of next minimum ` `            ``// element in the sequence ` `            ``k = *lower_bound(it->second.begin(), ` `                            ``it->second.end(), k); ` `    ``} ` ` `  `    ``// Return the final answer ` `    ``cout << ans << endl; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 3, 2, 1, 2 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``LIS(arr, n); ` ` `  `    ``return` `0; ` `} `

Output:

```2
```

Time complexity: O(n * log n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8131552,"math_prob":0.96127206,"size":5584,"snap":"2020-24-2020-29","text_gpt3_token_len":1370,"char_repetition_ratio":0.16612904,"word_repetition_ratio":0.03718593,"special_character_ratio":0.2519699,"punctuation_ratio":0.10974456,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9878215,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-16T00:27:26Z\",\"WARC-Record-ID\":\"<urn:uuid:5f8357cf-e5fd-4c18-95a1-6694fc163ab2>\",\"Content-Length\":\"129488\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4b390394-2646-4b3b-8181-8d4aeb03ac0d>\",\"WARC-Concurrent-To\":\"<urn:uuid:a1c46223-dd9e-4ca3-bc8d-9a1468ac9e84>\",\"WARC-IP-Address\":\"168.143.243.236\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/minimum-concatenation-required-to-get-strictly-lis-for-array-with-repetitive-elements-set-2/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:NDR62T35Z3LSVUZLUUP2MKSEERGKXQZF\",\"WARC-Block-Digest\":\"sha1:CJMOOULAJMFKZBB2RBHZL76DN7BS5GHK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593657176116.96_warc_CC-MAIN-20200715230447-20200716020447-00394.warc.gz\"}"}