-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4.java
More file actions
56 lines (51 loc) · 1.66 KB
/
q4.java
File metadata and controls
56 lines (51 loc) · 1.66 KB
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
/*Given an array of ints, return true if the sequence of numbers 1, 2, 3 appears in the
array somewhere.
array123([1, 1, 2, 3, 1]) → true
array123([1, 1, 2, 4, 1]) → false
array123([1, 1, 2, 1, 2, 3]) → true
*/
// Id - 21CE002 Andrew
import java.util.*;
public class q4 {
static public boolean arr123(int[] arr) {
boolean flag = false;
for (int i = 0; i < arr.length - 2; i++) {
// if (arr[i] != 1 && arr[i + 1] != 2 && arr[i + 2] != 3) {
// continue;
// }
if (arr[i] == 1 && arr[i + 1] == 2 && arr[i + 2] == 3) {
flag = true;
}
}
return flag;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ArrayList<Integer> arr = new ArrayList<Integer>();
System.out.println("Enter length of array: ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the array");
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
boolean flag = arr123(arr);
System.out.println(flag);
// Appending new elements at
// the end of the list
// String s = arr.toString();
// if(s.contains("123"))
// {
// System.out.println("true");
// }
// for (int i = 0; i < n - 2; i++) {
// if (arr[i] != 1 && arr[i + 1] != 2 && arr[i + 2] != 3) {
// System.out.println("false");
// //break;
// }
// else if(arr[i] == 1 && arr[i + 1] == 2 && arr[i + 2] == 3)
// {System.out.println("true");
// break;
// }
// }
}
}