Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8290883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:56:39+00:00 2026-06-08T12:56:39+00:00

I was trying a problem on SPOJ ,in which we have to simply find

  • 0

I was trying a problem on SPOJ,in which we have to simply find the Length of Longest Increasing Sub-sequence of the given Array A.

I had solved this problem using a dynamic programming O(n^2)algorithm and the solution got accepted..Here is the code,that got Accepted:

void LIS(int *A,int A_Length)
{
    int Seq[MAX];
    for(int i=0;i<A_Length;++i)
    {
        int maxima=0;
        for(int j=0;j<i;++j)
        {
            if(A[i]>A[j])
            {
                maxima=max(Seq[j],maxima);
            }
        }
        Seq[i]=maxima+1;
        //cout<<Seq[i]<<endl;
    }
    cout<<*max_element(Seq,Seq+A_Length)<<endl;
}

But When i tried to solve it using the Second Method (LINK),which is ::

A simple way of finding the longest increasing subsequence is
 to use the Longest Common Subsequence (Dynamic Programming) algorithm.
[1]Make a sorted copy of the sequence A, denoted as B. O(nlog(n)) time.
[2]Use Longest Common Subsequence on with A and B. O(n2) time.

,I got Wrong Answer .

This is my c++ code

//Global Variable
int A[100],B[100];
int DP[100][100];

//This function Finds the Longest common subsequce of Array A[1,2,3...,N] and B[1,2,3...,N]
void LIS(int N)
{

    sort((B+1),(B+1)+N);//STL SORT sort from index 1 to N of Array B.
    int i,j;

    //Base Cases
    for(i=0;i<=N;++i)
        DP[i][0]=0;

    for(j=0;j<=N;++j)
        DP[0][j]=0;

    for(i=1;i<=N;++i)
    {
        for(j=1;j<=N;++j)
        {
            if(A[i]==B[j])
                DP[i][j]=DP[i-1][j-1]+1;
            else
                DP[i][j]=max(DP[i-1][j],DP[i][j-1]);
        }
    }
    printf("%d\n",DP[N][N]);
}
int main()
{
    int N,i;
    scanf("%d",&N);
    for(i=1;i<=N;++i)
    {
        scanf("%d",&A[i]);
        B[i]=A[i];
    }
    LIS(N);

    return 0;
}

I don’t know why i am getting the Wrong Answer.Can You please Help me in Finding the Bug. Or the LIS by LCS Algorithm given in the site is incorrect??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:56:40+00:00Added an answer on June 8, 2026 at 12:56 pm

    The Second Method is correct, but can’t be applied to this problem directly. That’s because the numbers in the sequence are not guaranteed to be unique in this SPOJ problem, and the target is to find a strict increasing subsequence, while Your Second Method’s output is non-decreasing subsequence here. Demonstrating on a simple test case [1,2,2,3] will help you find the difference.

    This solution is also simple: just remove the duplicated elements after sorting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying this problem, http://www.spoj.pl/problems/TWOSQ/ . We have to find the number of
I am trying to solve this SPOJ problem . The question asks to find
I am trying to solve this problem http://www.spoj.pl/problems/PEBBMOV/ . I think I have the
I am trying to solve this problem : https://www.spoj.pl/problems/CERC07S/ I have identified that i
I've have a problem trying to print a given period between two dates. Let
I have a problem trying to transform a given input string to a given
I am trying to solve SPOJ problem 5: find the next largest integer palindrome
I was trying to solve this problem on SPOJ (http://www.spoj.pl/problems/REC/) F(n) = a*F(n-1) +
edit: I was trying to solve a spoj problem. Here is the link to
I was trying to solve this problem - http://www.spoj.pl/problems/LISA/ I thought of Greedy initially,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.