Increasing Decreasing Sequence

wrong answer on test cases 4/5;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=scn.nextInt();

}
int k;
if(n%2==0){
 k=n/2;
}else {
k=(n/2)+1;
}

System.out.println(checkSequence(arr,k));

}
public static boolean checkSequence(int[] arr,int k){
	boolean flag;
	boolean flag1=true;
	boolean flag2=true;
	for(int i=k;i>=0;i--){
		if((i-1>=0)&&arr[i]<arr[i-1]){
			flag1=false;
		//	System.out.println(flag1);
		}
	}for(int i=k;i<arr.length;i++){
		if((i+1<arr.length)&&arr[i]>arr[i+1]){
			flag2=false;
			//System.out.println(flag2);
		}
	}if(flag1==true&& flag2==true){
		flag=true;
		return true;

	}else{
	//	System.out.println(flag1);
	//	System.out.println(flag2);
		return false;
	}
	
}

}
second solution this is failing more testcases
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=scn.nextInt();

}
int k;
if(n%2==0){
 k=n/2;
}else {
k=(n/2)+1;
}

System.out.println(checkSequence(arr,k));

}
public static boolean checkSequence(int[] arr,int k){
	boolean flag;
	boolean flag1=true;
	boolean flag2=true;
	for(int i=k;i>=0;i--){
		if((i-1>=0)&&arr[i]<arr[i-1]){
			flag1=false;
		//	System.out.println(flag1);
		}
	}for(int i=k;i<arr.length;i++){
		if((i+1<arr.length)&&arr[i]>arr[i+1]){
			flag2=false;
			//System.out.println(flag2);
		}
	}if(flag1==true&& flag2==true){
		flag=true;
		return true;

	}else{
	//	System.out.println(flag1);
	//	System.out.println(flag2);
		return false;
	}
	
}

}

your code seems great. It does not cover one case i.e. when two consecutive numbers are same in the sequence. For example if your test case is like :

5
2
2
2
3
4

Then the output should be false. But your code returns true ! So kindly cover that case as well.

I hope this resolves your doubt !
Happy Coding :slight_smile:

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=scn.nextInt();

}
boolean flag=false;
for(int k=1; k<n-1;k++) {
flag=checkSequence(arr,k);
if(flag==true) {
break;
}
}
System.out.println(flag);
}
public static boolean checkSequence(int[] arr,int k){
boolean flag1=true;
boolean flag2=true;
for(int i=k;i>=1;i–){
if(arr[i]<=arr[i-1]){
flag1=false;
}
}
for(int i=k+1;i<arr.length-1;i++){
if(arr[i]<=arr[i+1]){
flag2=false;
}
}
if(flag1==true&& flag2==true){
return true;

} else {
return false;
}

}
}

still some test cases are giving wrong answers

please save your code on ide.codingblocks.com and share

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int [] arr=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=scn.nextInt();

}
boolean flag=false;
for(int k=1; k<n-1;k++) {
flag=checkSequence(arr,k);
if(flag==true) {
break;
}
}
System.out.println(flag);
}
public static boolean checkSequence(int[] arr,int k){
boolean flag1=true;
boolean flag2=true;
for(int i=k;i>=1;i–){
if(arr[i]<=arr[i-1]){
flag1=false;
}
}
for(int i=k+1;i<arr.length-1;i++){
if(arr[i]<=arr[i+1]){
flag2=false;
}
}
if(flag1==true&& flag2==true){
return true;

} else {
return false;
}

}
}

please go to ide.codingblocks.com copy your code there go to file->save and then share the url

Suggested Approach:

  1. First we assume that our sequence is decreasing only. We also assume that our sequence is initially valid and only try to disprove it during the course.
  2. We compare our current element with the previous element and based on that comparison make our decision.
  3. If the curr element is equal to the prev element , we see that the sequence is neither strictly increasing or decreasing so we mark our sequence as invalid.
  4. If the curr element is greater than the prev element, then we realise that the sequence has started increasing and we mark it so using a flag variable “goingUp” which we had initially marked as false based on our assumption that the sequence is decreasing.
  5. If the curr element is less than the previous element and the current sequence is also proceeding as decreasing , we are to do nothing. However if the current sequence is increasing and we get a pair for a decreasing sequence then we mark our sequence as invalid since we cannot have a decreasing part after the increasing sequence.
  6. After each iteration , store the value of the curr element into the prev element so that we can compare it with the next input element.

ok THANK YOUU SO MUCH!!

Please mark your doubt as resolved and rate as well :slight_smile:

     Okie
          DONE!!! :blush:
1 Like