Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Hash
  • Practice Hash
  • MCQs on Hash
  • Hashing Tutorial
  • Hash Function
  • Index Mapping
  • Collision Resolution
  • Open Addressing
  • Separate Chaining
  • Quadratic probing
  • Double Hashing
  • Load Factor and Rehashing
  • Advantage & Disadvantage
Open In App
Next Article:
Length of longest consecutive sequence that can be formed from Array by converting 0s
Next article icon

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

Last Updated : 24 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 arr[] to itself at the end 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, 3, 4} 

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(N2) 
Auxiliary Space: O(N2)

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: 
Algorithm: 

  • 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){
    unordered_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;
}
 
 

Java




// Java implementation to find the
// longest increasing subsequence
// in repeating element of array
import java.util.*;
 
class GFG{
 
// Function to find the LCS
static int findLCS(int arr[], int n)
{
    HashMap<Integer,
            Integer> mp = new HashMap<Integer,
                                      Integer>();
     
    // Loop to create frequency array
    for(int i = 0; i < n; i++)
    {
       if(mp.containsKey(arr[i]))
       {
           mp.put(arr[i], mp.get(arr[i]) + 1);
       }
       else
       {
           mp.put(arr[i], 1);
       }
    }
    return mp.size();
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
    int arr[] = { 3, 2, 1 };
     
    System.out.print(findLCS(arr, n));
}
}
 
// This code is contributed by amal kumar choubey
 
 

Python3




# Python3 implementation to find the
# longest increasing subsequence
# in repeating element of array
 
# Function to find the LCS
def findLCS(arr, n):
     
    mp = {}
 
    # Loop to create frequency array
    for i in range(n):
        if arr[i] in mp:
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1
             
    return len(mp)
 
# Driver code
n = 3
arr = [ 3, 2, 1 ]
 
print(findLCS(arr, n))
 
# This code is contributed by ng24_7
 
 

C#




// C# implementation to find the
// longest increasing subsequence
// in repeating element of array
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the LCS
static int findLCS(int []arr, int n)
{
    Dictionary<int,
               int> mp = new Dictionary<int,
                                        int>();
     
    // Loop to create frequency array
    for(int i = 0; i < n; i++)
    {
       if(mp.ContainsKey(arr[i]))
       {
           mp[arr[i]] = mp[arr[i]] + 1;
       }
       else
       {
           mp.Add(arr[i], 1);
       }
    }
    return mp.Count;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
    int []arr = { 3, 2, 1 };
     
    Console.Write(findLCS(arr, n));
}
}
 
// This code is contributed by amal kumar choubey
 
 

Javascript




<script>
 
// Javascript implementation to find the
// longest increasing subsequence
// in repeating element of array
 
// Function to find the LCS
function findLCS(arr, n)
{
    let mp = new Map();
      
    // Loop to create frequency array
    for(let i = 0; i < n; i++)
    {
       if(mp.has(arr[i]) != 0)
       {
           mp.set(arr[i], mp.get(arr[i]) + 1);
       }
       else
       {
           mp.set(arr[i], 1);
       }
    }
    return mp.size;
}
       
// Driver code
     
      let n = 3;
    let arr = [ 3, 2, 1 ];
      
    document.write(findLCS(arr, n));
                                                                                 
</script>
 
 

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)


Next Article
Length of longest consecutive sequence that can be formed from Array by converting 0s
author
harshitmuhal
Improve
Article Tags :
  • Algorithms
  • Arrays
  • DSA
  • Greedy
  • Hash
  • LIS
  • subsequence
Practice Tags :
  • Algorithms
  • Arrays
  • Greedy
  • Hash

Similar Reads

  • Length of longest consecutive sequence that can be formed from Array by converting 0s
    Given an array arr of N integers, the task is to calculate the length of longest sequence of consecutive integers that can be formed from the array. It is also given that 0's in the array can be converted to any value. Example: Input: N = 7, A = {0, 6, 5, 10, 3, 0, 11}Output: 5Explanation: The maxim
    9 min read
  • Longest increasing sequence by the boundary elements of an Array
    Given an array arr[] of length N with unique elements, the task is to find the length of the longest increasing subsequence that can be formed by the elements from either end of the array.Examples: Input: arr[] = {3, 5, 1, 4, 2} Output: 4 Explanation: The longest sequence is: {2, 3, 4, 5} Pick 2, Se
    8 min read
  • Longest palindromic string possible by concatenating strings from a given array
    Given an array of strings S[] consisting of N distinct strings of length M. The task is to generate the longest possible palindromic string by concatenating some strings from the given array. Examples: Input: N = 4, M = 3, S[] = {"omg", "bbb", "ffd", "gmo"}Output: omgbbbgmoExplanation: Strings "omg"
    8 min read
  • Length of the longest increasing subsequence which does not contain a given sequence as Subarray
    Given two arrays arr[] and arr1[] of lengths N and M respectively, the task is to find the longest increasing subsequence of array arr[] such that it does not contain array arr1[] as subarray. Examples: Input: arr[] = {5, 3, 9, 3, 4, 7}, arr1[] = {3, 3, 7}Output: 4Explanation: Required longest incre
    14 min read
  • Length of the longest increasing subsequence such that no two adjacent elements are coprime
    Given an array arr[]. The task is to find the length of the longest Subsequence from the given array such that the sequence is strictly increasing and no two adjacent elements are coprime. Note: The elements in the given array are strictly increasing in order (1 <= a[i] <= 105) Examples: Input
    10 min read
  • Longest Increasing Subsequence having sum value atmost K
    Given an integer array arr[] of size N and an integer K. The task is to find the length of the longest subsequence whose sum is less than or equal to K. Example: Input: arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15} K = 40 Output: 5 Explanation: If we select subsequence {0, 1, 3, 7,
    5 min read
  • Longest palindrome formed by concatenating and reordering strings of equal length
    Given an array arr[] consisting of N strings of equal length M, the task is to create the longest palindrome by concatenating the strings. Reordering and discarding some strings from the given set of strings can also be done.Examples: Input: N = 3, arr[] = { "tab", "one", "bat" }, M = 3 Output: tabb
    9 min read
  • Minimum concatenation required to get strictly LIS for the given array
    Given an array A[] of size n where there are only unique elements in the array. We have to find the minimum concatenation required for sequence A to get strictly Longest Increasing Subsequence. For array A[] we follow 1 based indexing. Examples: Input: A = {1, 3, 2} Output: 2 Explanation: We can con
    6 min read
  • Longest Increasing Subsequence(LIS) using two arrays
    Given two arrays A[] and B[] of size N. Your Task is to find Longest Increasing Subsequence(LIS) in another array C[] such that array C[] is formed by following rules: C[i] = A[i] or C[i] = B[i] for all i from 1 to N.Examples: Input: A[] = {2, 3, 1}, B[] = {1, 2, 1}Output: 2Explanation: Form C[] as
    9 min read
  • Longest subsequence such that difference between adjacents is one
    Given an array arr[] of size n, the task is to find the longest subsequence such that the absolute difference between adjacent elements is 1. Examples: Input: arr[] = [10, 9, 4, 5, 4, 8, 6]Output: 3Explanation: The three possible subsequences of length 3 are [10, 9, 8], [4, 5, 4], and [4, 5, 6], whe
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences