INCREASING DECREASING SEQUENCE

Could you please help me check as to where I am going wrong in the following code (also it would be nice if you could tell how to generate link for my code so next time I wont have to paste the whole thing :slight_smile: ):

#include
using namespace std;
int main() {
long n,i=0;
cin>>n;
long long a[n],prev=0;
bool isinc=1,ans=0;
for(;i<n;i++){
cin>>a[i];
}
i=0;
while(i>n){
if(isinc){
if(prev>a[i])
isinc=0;
}
if(!isinc){
if(prev<a[i]){
cout<<“false”;
ans=1;
}
}
prev=a[i];
i++;
}
if(!ans){
cout<<“true”;
}

return 0;

}

Hello @Kritesha_Dhanda,

Steps:

  1. Paste your code on Coding Clocks Online IDE https://ide.codingblocks.com/.
  2. Save your code there.
  3. Share the URL generated.

Now, coming back to your program:
There are few cases that you have missed, that can cause wrong errors:

  1. strictly increasing eg: 5 1 2 3 4 5
  2. strictly decreasing eg: 5 5 4 3 2 1
  3. strictly decreasing then strictly increasing eg: 5 3 2 1 2 3
  4. strictly increasing then strictly decreasing eg: 5 1 2 3 2 1

for 1., 2., 3. case you should print true and
for case 4. and rest, it should print false.
Note: strictly words means equal numbers at adjacent positions would not be accepted. eg: 5 3 2 2 1 0 (false)

Run your code for the above mentioned testcases and check it is producing the desired output or not.
Modify your code as required.
In case you are stuck, feel free to send the modified code.

Hope, this would help.

Sir the second test case still doesnt pass cud u pl check the code :

Correct your code for following:

You have missed an important part:
5
5 5 3 2 1
Why your code is printing true?
You have to check for equality of adjacent elements also.
Print false, if this condition satisfies.

To solve this problem you can use two for loops:

I have modified your code:

Hope, this would help.
Give a like, if you are satisfied.

1 Like

Thank you very much!

Please, mark this doubt as resolved if you don’t have any query regarding the same.

Is it necessary to use arrays?

Hello @idontknowhowtocode,

Nope, you can use any vector also.
BTW, the need is of a linear data structure.