/*22)
* 28/07/11
* input a max of 20 nos
* print longest monotonic increasing and longest monotonic decreasing sequence
*/
import java.io.*;
public class LSequence
{
int ar[];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
LSequence()
{}
LSequence(int n)
{
ar=new int[n];
}
void input()throws IOException
{
System.out.println("enter the no's");
for(int i=0;i< ar.length;i++)
{
ar[i]=Integer.parseInt(in.readLine());
}
}
void display(int n[])
{
for(int i=0;i< n.length;i++)
{
if(i!=n.length-1)
System.out.print(n[i]+",");
else
System.out.print(n[i]);
}
System.out.println();
}
boolean inc_dec(int n,int i)
{
boolean ret=false;
if(n==1 && ar[i]< ar[i+1])
ret=true;
else if(n==2 && ar[i]>ar[i+1])
ret=true;
return ret;
}
int[] MaxSeries(int n)
{
int temp[]=new int[20],i=0,j=0,t[]=new int[0];
while(i< ar.length-1)
{
if(inc_dec(n,i) && i!=ar.length-2)
{
temp[j]=ar[i];
j++;
}
else
{
temp[j]=ar[i];
if(i==ar.length-2 && inc_dec(n,i))
{
j++;
temp[j]=ar[i+1];
}
if(t.length< j+1)
{
t=new int[j+1];
for(int k=0;k< j+1;k++)
t[k]=temp[k];
}
temp=new int[20];
j=0;
}
i++;
}
return t;
}
void main()throws IOException
{
System.out.println("enter n");
int n=Integer.parseInt(in.readLine());
LSequence ob=new LSequence(n);
if(n>20||n< 1)
System.out.println("max value of n is 20 / invalid input");
else
{
ob.input();
System.out.println("\u000cOriginal array:");
ob.display(ob.ar);
System.out.println("longest monotonic increasing sequence:");
int Series1[]=ob.MaxSeries(1);
ob.display(Series1);
System.out.println("longest monotonic decreasing sequence:");
int Series2[]=ob.MaxSeries(2);
ob.display(Series2);
}
}
}
No comments:
Post a Comment