Saturday 4 August 2012

Given a large array of int return the length of the longest increasing(non-necessarily-adjacent) sub-sequence.


#include<iostream.h>
#include<conio.h>

void main()
{
      clrscr();
      int a[]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
      int n=sizeof(a)/sizeof(int);
      int *occ=new int[n];
      for(int i=0;i<n-1;i++)
      {
            int count=0;
            for(int j=i+1;j<n;j++)
                  if(a[j]>a[i])
                        count++;
            occ[i]=count;
      }
      int buf[10];
      int no=-1;
      int max,pos;
      i=0;
      while(i<n)
      {
             int flag=0;
             for(int j=i;j<n;j++)
             {
                  if(a[j]>buf[no] || no==-1)
                  {
                        if(flag==0)
                        {
                              flag=1;
                              max=occ[j];
                              pos=j;
                        }
                        if(occ[j]>max)
                        {
                              max=occ[j];
                              pos=j;
                        }
                  }
             }
             cout<<"\n "<<a[pos]<<"  "<<pos;
             buf[++no]=a[pos];
             i=pos+1;
      }
      getch();
}


No comments:

Post a Comment