The user inputs n number of datas to be followed in the next line and then it inputs n numbers a1,a2,...,an. These numbers are heights of a some mountains. The set of these numbers is "ACCEPTABLE" if there is only one maxima or minima. for example "1 2 3 2 1", has only one maxima which is 3. Also "1 2 3 4" has one maxima. but "1 10 9 8 7 6 5 6 7" is not acceptable because it has two maxima (10 and 7) or two minima (1 and 5).
In other word, the set is acceptable if and only if it is in one of this forms:
a1<=a2<=a3 ... <= ai > a(i+1) > ... >an
or
a1>=a2>=a3 ... >= ai < a(i+1) < ... < an.
I must submit the answer in a judge system which tests it with unknown test cases. Using any type of Array or Vector is completely prohibited.
My solution is this:
//C code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,temp;
scanf("%d",&n);
if (n==1)
{
int l;
scanf("%d",&l);
printf("Yes");
}
else
{
int a,b;
int last;
int changes =0;
int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
scanf("%d %d",&a,&b);
if (a>b)
{
dec=1;
}
else if (a<b)
{
inc = 1;
}
else
{
inc =1;
dec = 1;
}
last = b;
for (int i =2;i<n;i++)
{
scanf("%d",&temp);
if (temp>last && dec==1)
{
inc = 1;
dec= 0;
changes++;
}
if (temp<last && inc==1)
{
inc =0;
dec=1;
changes++;
}
if (!(inc==1 && dec==1) && temp == last)
{
changes++;
}
last = temp;
last = temp;
}
if (changes <=1)
{
printf("Yes");
}
else
{
printf("No");
}
}
return 0;
}
It gets the right answer for the examples that are in the question but it fails on some unknown test cases. Any idea how to fix this? Can anyone give me a test case that is not solved right in this code?
P.1: I added
if (!(inc==1 && dec==1) && temp == last)
{
changes++;
}
and it accepted one of the failing test cases but still one remains.
P.2:
This is my other algorithm which fails on some test cases but the judge accepts its answers on the failing test cases of the first one:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int inc=0;
int dec=0;
int peak=0;
int valley=0;
int last = -1;
int a;
scanf("%d",&n);
for (int i =0;i<n;i++)
{
if (last!=-1)
{
last =a;
}
scanf("%d",&a);
if (last!=-1)
{
if (a>last)
{
if (!(inc==1))
{
valley++;
inc =1;
dec=0;
}
}
if (a<last)
{
if (!(dec==1))
{
peak++;
dec=1;
inc =0;
}
}
}
last =0;
}
if (valley<=1 && peak<=1)
{
// printf("valley: %d , peak:%d",valley,peak);
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
P.3
New algorithm:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int n,temp;
scanf("%lld",&n);
if (n==1)
{
long long int l;
scanf("%lld",&l);
printf("Yes");
}
else
{
long long int a,b;
long long int last;
long long int changes =0;
int dec =0 , inc =0; //flag: checking if the set is incremental or decremental till now
scanf("%lld %lld",&a,&b);
if (a>b)
{
dec=1;
}
else if (a<b)
{
inc = 1;
}
else
{
inc =1;
dec = 1;
}
last = b;
for (long long int i =2;i<n;i++)
{
scanf("%lld",&temp);
if (temp>last && dec==1)
{
inc = 1;
dec= 0;
changes++;
}
if (temp<last && inc==1)
{
inc =0;
dec=1;
changes++;
}
if (changes>=1 && temp == last)//new change
{
changes+=100;
}//end of new change
last = temp;
}
if (changes <=1)
{
printf("Yes");
}
else
{
printf("No");
}
}
return 0;
}