-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck_Num_Seq.txt
146 lines (125 loc) · 3.45 KB
/
Check_Num_Seq.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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.
Note : We say that x is strictly larger than y when x > y.
So, a strictly increasing sequence can be 1 4 8. However, 1 4 4 is NOT a strictly increasing sequence.
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 and Onwards : 'n' integers on 'n' lines(single integer on each line)
Output Format :
"true" or "false" (without quotes)
Constraints :
0 <= n <= 10^7
Sample Input 1 :
5
9
8
4
5
6
Sample Output 1 :
true
Sample Input 2 :
3
1
2
3
Sample Output 2 :
true
Sample Input 3 :
3
8
7
7
Sample Output 3 :
false
Explanation for Sample Format 3 :
8 7 7 is not strictly decreasing, so output is false.
Sample Input 4 :
6
8
7
6
5
8
2
Sample Output 4 :
false
Explanation for Sample Input 4 :
The series is :
8 7 6 5 8 2
It is strictly decreasing first (8 7 6 5). Then it's strictly increasing (5 8). But then it starts strictly decreasing again (8 2). Therefore, the output for this test case is 'false'
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();int f=0;
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int i=0;
for(i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1]){
f=1;
break;
}
else if(arr[i]<arr[i+1])
{
break;
}
}
for(int j=i+1;j<n-1;j++){
if(f==1){
break;
}
else if(arr[j]>arr[j+1])
{f=1;}
else if(arr[j]==arr[j+1]){
f=1;
break;
}
}
if(f==1)
System.out.println("false");
else
System.out.println("true");
}
}
// MY CODE (RAN PERFECTLY EXCEPT FOR TESTCASE 2 WHILE SUBMISSION)
// import java.util.Scanner;
// public class Main {
// public static void main(String[] args) {
// // Write your code here
// Scanner s = new Scanner(System.in);
// int n = s.nextInt();
// int a = s.nextInt();
// int flag = 0;
// for(int i=0; i<n-1; i++){
// int x = s.nextInt();
// if(a==x){
// flag = 0;
// break;
// }else if(x<a && flag == 0){
// a=x;
// }else if(x>a){
// flag = 1;
// a=x;
// }else if(x<a && flag == 1){
// flag = 0;
// break;
// }
// }
// if(flag == 1){
// System.out.print("true");
// }else if(flag == 0){
// System.out.print("false");
// }
// }
// }