0

I am implementing a simple solution of the sequence to see if it's monotonic or strictly monotonic. I tried different inputs, corner/worst cases, it works, for a better presentation of the problem, the following is the specific statement of the problem.

Determine whether a sequence of number is strictly monotonic or monotonic.

Input:

Contains integer number n (2 ≤ n ≤ 10^9) - number of elements in sequence. There are n elements after it.

Output:

If sequence is strictly monotonic then write "2" else if sequence is juct monotonic then write "1" and "0" in other cases.

My code:

 #include <iostream>
//#include <cmath>

using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, a, b, p, q, e, c; e = 0; q = 0; p = 0;
    cin >> n >> a;
    for (int i = 0; i < n - 1; i++)
    {
        cin >> b;
        if (a > b) { p = 1;
            if (q) { cout << 0; return 0; }
        }
        if (a < b) { q = 1;
            if (p) { cout << 0; return 0; }
        }
        if (a == b) { e = 1; }
        a = b;
    }
    if (e) { cout << 1; }
    else { cout << 2; }
    return 0;
}

My code though doesn't pass my university's ejudge, it gives Time Limit error. What can be wrong with my code? I mean, you have to go through all of n, this code is O(n), and n <= 10^9.

v_head
  • 759
  • 4
  • 13
  • 1
    As far as I can tell your code outputs 0, if the sequence starts with 2,1 (or any first number bigger than second). I.e. it ignores most of the sequence. Is that enough of problem? – Yunnosch Sep 29 '19 at 14:03
  • @Yunnosch can you have a look again. – v_head Oct 02 '19 at 13:37
  • I think you have changed your code fundamentally and are now asking a very different question. If I am right that is considered a "moving target" question and not appreciated. Please undo that change of question. (Editing an existing question is fine if it serves to make it clearer.) If you have another question then please make it a separate question and I will happy to have a look. But this one should stay so that it keeps matching the answer you got. That is of course applicable whether it is an answer by me or anybody else. Remember, this is a Q/A collection, not a chat or forum. – Yunnosch Oct 03 '19 at 05:23

1 Answers1

2

I tried your code.
I had to remove the two freopens, which broke what otherwise seems a MRE.

It compiled successfully.

I entered 4, for 4 inputs, then
2
1

The output is 0.

a) your code ignores everything after the second value
b) your code terminates early
c) for the strictly monotonically decreasing sequence (the non-ignored part) "2,1", it outputs the value for "other cases"

I guess that this does not answer the question in your mind.
But you will have to ask that one, because the one you wrote I consider answered.

In case your question is actually "How can I find out which part of my code causes the error?" I recommend to use a debugger:
How to debug using gdb?
How to debug in Codeblocks?

Or, more generally,
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • This should be a comment. – David G Sep 29 '19 at 17:12
  • @0x499602D2 Nope. It answers the question asked and also the probably meant question. The only question it does NOT answer is "Which code can I hand in to solve the homework assignment?", but that question definitly was not asked. And the question to spot a possibly failing test case is a decent one, even if not explicitly asked like that. – Yunnosch Sep 29 '19 at 19:11