@atul

Is the above code's optimization based on the 2nd approach that i have
mentioned above or is it something different..

In case, you are doing something else can u provide us with more
explanation so that it would be easier for everyone to understand the
code ...


On Jan 1, 3:55 pm, atul anand <[email protected]> wrote:
> ok ...
> @Lucifier i have optimized your code further so that there is no need to
> check every element i.e to outer for loop..
> so i have skipped elements if they can never be part of second maxLen
> sequence if exits.
>
> int find_seq(int arr[],int *start,int *end,int k,int n)
> {
> int maxLen=0;
> int min=arr[0];
> int max=arr[0];
> int i,j=0,next,index=-1,p,flag=0;
>
> for(i=1;i<n;i++)
> {
>        flag=0;
>        if(arr[i] > max)
>        max=arr[i];
>        else if(arr[i] < min)
>        min=arr[i];
>        else
> continue;
>
>        if((max - min ) > k)
>        {
>     flag=1;
>        }
>        if(flag==1)
>        {
>      if(maxLen < ( i - j))
>      {
> maxLen=i - j;
> *start=j;
> *end=i-1;
>
>      }
>      index=-1;
>      next=i;
>      for(p=j;p<next;p++)
>      {
>
> if((abs(arr[next] - arr[p])) <= k)
> {
> if(index==-1)
> {
> i=p-1;
> j=i+1;
> index=1;
>
> }
> }
>
> else
> {
> index=-1;
> i=p;
> j=i+1;
>
> }
>
>      }
>      max=arr[j];
>      min=arr[j];
>
>        }
>
> }
>
> if(maxLen < ( i - j ))
> {
>    maxLen=i - j;
>     *start=j;
>     *end=i-1;
>
>
>
>
>
>
>
> }
> return 1;
> }

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to