Max length biotonic sub array sl


it is giving wrong output

Hello @chahatkumar,

The logic you are using is correct.
But, the output is incorrect.

Example:
1
6
12 2 78 90 45 23
Expected Output:
5
Your Code is printing:
52

52?.. Isn’t it a big number?

Actually, these are two different outputs.
To understand this, i would suggest you to dry run your code on above mentioned example.

What is happening is,

  1. for i=0, the value of ans becomes 2.

  2. for i=1, the first (i==n-1) is satisfying. Thus, your code is printing 5 and then breaking out of the for loop.
    Now, the main problem occurs.

    When you break out of for loop. There is a statement printing value of ans i.e. 2.
    Therefore, output is 52.

Solution:
Is there a need of those two if conditions checking for i== n-1? Think.
Is there a need to break out of for loop? Think.

Hope, this would help.
If you still face problem, let me know.
Give a like, if you are satisfied.

1 Like

okay thanks
at line no 22, will the break take the control out of while loop or for loop ?

It will break out of while loop.

In case of nested loop, break and continue statements actions/operates on the immediate loop, they are part of.

Mark it as resolved, if your doubts are solved.
Let me know in case you face any issue.

1 Like

why will it print for i=0 ?
the cout statement is outside for loop
wont the for loop continue for i=1,rather than printing the i=0 case ??

Because you are iterating for loop for j=0 to j<n;
and then assigning the value of j to i.

Now, for i=j=0;

  1. program will enter the first while loop as 0<n-1,
  2. Then it will enter else statement and break out of while loop as a[0]>a[0+1],
  3. It will skip next if condition as i!=n-1,
    4.Now, it will enter the next while loop.
  4. satisfies the if condition and will incremented to i=1,
  5. Now, it will never break out of loop as there is no break statement.

Please, before asking for doubts first make a dry run on your code.

okay i made the changes still the output is incorrect
as i made a dry run

  1. for j=0, i=0; now program breaks out of while loop after the break statement
    2)it will skip the next if condition as i != n-1
  2. it enters while loop and i increments to 1
  3. for i=1; if condition is not satisfied so it breaks out of while loop
  4. it calculates sum as 2 (i-j+1)
  5. ans becomes 2
  6. now j gets incremented to 1 and for loop starts again
  7. sum becomes 5 , hence ans is now 5,
  8. now for loops interates for j=2,3,4,5
  9. but still ans remains 5 ( the max value )

now , why is 15 showing up in the output :sob: :cry:

No need to worry about these errors.
You’ll learn about different types of error you should avoid while writing a code.
These mistakes will help you to debug your codes in future.

BTW send me your modified code. I’ll let you know your mistakes.

1 Like

pls tell the logical error associated with the above code

There are following mistakes:

  1. You are repeating the same mistake i have explained in the point 2 of my first reply “if(i==n-1)”.
    This will print two outputs.

  2. In biotonic subarray you can consider same values at consecutive positions as a part of this subarray.

I have modified your code for above mistakes.
You can check it here.

Hope, i have cleared you doubts.

okay thanks a lot :smiley: