Skip to main content
Tweeted twitter.com/StackCodeReview/status/1217235112791769088
Became Hot Network Question
added 222 characters in body; edited tags; edited title
Source Link
L. F.
  • 9.6k
  • 2
  • 26
  • 68

finding Finding the number of longest contigiouscontiguous increasing subsequencesubsequences in a given array

This is different from classic LIS problem. Here I want number of contigiouscontiguous longest increasing subsequences!

For Example: INPUT example: arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }

        OUTPUT: 2
INPUT: arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }
OUTPUT: 2

Explanation: There are 2 longest contigiouscontiguous subsequences -> { 3, 6, 10: }{ 3, 6, 10 } and { 8, 11, 17 }{ 8, 11, 17 }.

Here is a function that I have made to achieve the task.But But is theirthere any optimized way instead of this brute force approach?

#define ll long long int
ll longestIncSeq(vector<ll> temp, ll n) 
{ 
    ll count=0,mymax = -1,flag=0,dummy=0;

    while( (flag==0) && (!(temp.empty())) )
    {
        ll max = 1, len = 1, maxIndex = 0; 

        for (ll i=1; i<n; i++) 
        { 
            if (temp[i] > temp[i-1]) 
                len++; 
            else
            { 
                if (max < len)   
                { 
                    max = len; 
                    maxIndex = i - max; 
                } 
                len = 1;     
            }    
        } 
        if (max < len) 
        { 
            max = len; 
            maxIndex = n - max; 
        }    
        if( max == 1 )
        {
            return count;
        }
        if( (mymax == max) || (dummy==0) )
        {
            count++;
            dummy++;
        }
        else
        {
            flag = 1;
        }
        temp.erase(temp.begin()+maxIndex , temp.begin()+maxIndex+max);

        mymax = max;
        n = temp.size();
    }
    return count;   
}
```

finding the number of longest contigious increasing subsequence in a given array

This is different from classic LIS problem. Here I want number of contigious longest increasing subsequences!

For Example: INPUT : arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }

        OUTPUT: 2

Explanation: There are 2 longest contigious subsequences -> { 3, 6, 10 } and { 8, 11, 17 }

Here is a function that I have made to achieve the task.But is their any optimized way instead of this brute force approach?

#define ll long long int
ll longestIncSeq(vector<ll> temp, ll n) 
{ 
    ll count=0,mymax = -1,flag=0,dummy=0;

    while( (flag==0) && (!(temp.empty())) )
    {
        ll max = 1, len = 1, maxIndex = 0; 

        for (ll i=1; i<n; i++) 
        { 
            if (temp[i] > temp[i-1]) 
                len++; 
            else
            { 
                if (max < len)   
                { 
                    max = len; 
                    maxIndex = i - max; 
                } 
                len = 1;     
            }    
        } 
        if (max < len) 
        { 
            max = len; 
            maxIndex = n - max; 
        }    
        if( max == 1 )
        {
            return count;
        }
        if( (mymax == max) || (dummy==0) )
        {
            count++;
            dummy++;
        }
        else
        {
            flag = 1;
        }
        temp.erase(temp.begin()+maxIndex , temp.begin()+maxIndex+max);

        mymax = max;
        n = temp.size();
    }
    return count;   
}
```

Finding the number of longest contiguous increasing subsequences in a given array

This is different from classic LIS problem. Here I want number of contiguous longest increasing subsequences!

For example:

INPUT: arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }
OUTPUT: 2

Explanation: There are 2 longest contiguous subsequences: { 3, 6, 10 } and { 8, 11, 17 }.

Here is a function that I have made to achieve the task. But is there any optimized way instead of this brute force approach?

#define ll long long int
ll longestIncSeq(vector<ll> temp, ll n) 
{ 
    ll count=0,mymax = -1,flag=0,dummy=0;

    while( (flag==0) && (!(temp.empty())) )
    {
        ll max = 1, len = 1, maxIndex = 0; 

        for (ll i=1; i<n; i++) 
        { 
            if (temp[i] > temp[i-1]) 
                len++; 
            else
            { 
                if (max < len)   
                { 
                    max = len; 
                    maxIndex = i - max; 
                } 
                len = 1;     
            }    
        } 
        if (max < len) 
        { 
            max = len; 
            maxIndex = n - max; 
        }    
        if( max == 1 )
        {
            return count;
        }
        if( (mymax == max) || (dummy==0) )
        {
            count++;
            dummy++;
        }
        else
        {
            flag = 1;
        }
        temp.erase(temp.begin()+maxIndex , temp.begin()+maxIndex+max);

        mymax = max;
        n = temp.size();
    }
    return count;   
}
Source Link

finding the number of longest contigious increasing subsequence in a given array

This is different from classic LIS problem. Here I want number of contigious longest increasing subsequences!

For Example: INPUT : arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }

        OUTPUT: 2

Explanation: There are 2 longest contigious subsequences -> { 3, 6, 10 } and { 8, 11, 17 }

Here is a function that I have made to achieve the task.But is their any optimized way instead of this brute force approach?

#define ll long long int
ll longestIncSeq(vector<ll> temp, ll n) 
{ 
    ll count=0,mymax = -1,flag=0,dummy=0;

    while( (flag==0) && (!(temp.empty())) )
    {
        ll max = 1, len = 1, maxIndex = 0; 

        for (ll i=1; i<n; i++) 
        { 
            if (temp[i] > temp[i-1]) 
                len++; 
            else
            { 
                if (max < len)   
                { 
                    max = len; 
                    maxIndex = i - max; 
                } 
                len = 1;     
            }    
        } 
        if (max < len) 
        { 
            max = len; 
            maxIndex = n - max; 
        }    
        if( max == 1 )
        {
            return count;
        }
        if( (mymax == max) || (dummy==0) )
        {
            count++;
            dummy++;
        }
        else
        {
            flag = 1;
        }
        temp.erase(temp.begin()+maxIndex , temp.begin()+maxIndex+max);

        mymax = max;
        n = temp.size();
    }
    return count;   
}
```