Sunday, March 11, 2018

Check Number Sequence

Check Number sequence
Send Feedback

You are given S a sequence of n integers i.e. S = s1, s2, ..., sn. Compute if it is possible to split S into two parts : s1, s2, ..., si and si+1, si+2, ….., sn (0 <= i <= n) in such a way that the first part is strictly decreasing while the second is strictly increasing one.

That is, in the sequence if numbers are decreasing, they can start increase at one point. And once number starts increasing, they cannot decrease at any point further.

Sequence made up of only increasing numbers or only decreasing numbers is a valid sequence. So in both the cases, print true.
You just need to print true/false. No need to split the sequence.
Input format :
Line 1 : Integer n
Line 2 : n integers (separated by space)
Output Format :
"true" or "false" (without quotes)
Sample Input 1 :
5
9 8 4 5 6
Sample Output 1 :
true
Sample Input 2 :
3
1 2 3
Sample Output 2 :
true

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// Write your code here
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[]num=new int [n];
int count = 0 , temp=0;
for (int i=0;i<n;i++)
{
num [i] = scan.nextInt();
    }
for(int i = 0 ; i<n-1;i++)
{
    if(count==0)
    {
if(num[i]<num[i+1])
{
count++;
}
    }
else if(count==1)
{
if(num[i]>num[i+1])
{
count++;
}
}
else if(count>1)
{
    break;
}
}
if(count==0||count==1)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}

3 comments:

  1. if two number are a equal then we want console is showing false. so what to do.

    ReplyDelete
  2. just put another else if in the conditions where
    if ( arr[i] == arr[i+1] ){
    System.out.print(false);
    }

    ReplyDelete
  3. put it above the condition where count > 1 has been entered [inside the for loop]

    ReplyDelete