Repository navigation
1.2 Analog Web formated or not
Farrukh Ahmad edited this page Dec 15, 2015
·
1 revision
In this problem there will be an array we need to check whether the array items are increasing and decreasing order or not.
If starting item is follows upword trends then very next element should follow downword trends
[1,2,1,2,1] should return true, since there is strict up and down / down and up trends
[1,2,3,4,3,2] should return false
Solutions:
public static void main(String args[]){
int a[] = {1,2,1,2,3,2};
System.out.println(isSineWeb(a));
}
Wayout 1:
static boolean isSineWeb(int a[]){
if (a.length == 0) return false;
if (a.length == 1) return true;
boolean up = false;
boolean down = false;
if(a[0] > a[1]) { down = true; up = false; }
else { down = false; up = true; }
for(int i=1; i<a.length-1; i++){
if( down && !(a[i] < a[i+1]) ) {
return false;
}
if(up && !(a[i] > a[i+1])) {
return false;
}
if(a[i] > a[i+1]) { down = true; up = false; }
else { down = false; up = true; }
}
return true;
}
Wayout 2:
static boolean isStrictUpDown(int a[]){
if( a.length < 3) return false;
for(int i=0; i<a.length -2; i++){
if( a[i+1] > a[i] && !(a[i+1] > a[i+2])) return false;
else if( a[i+1] < a[i] && !(a[i+1] < a[i+2])) return false;
}
return true;
}