-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRainbow Array
More file actions
101 lines (98 loc) · 2.2 KB
/
Rainbow Array
File metadata and controls
101 lines (98 loc) · 2.2 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
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
Rainbow array
Send Feedback
An array is Rainbow if it has the following structure:
1. First a1 elements equal 1.
2. Next a2 elements equal 2.
3. Next a3 elements equal 3.
4. Next a4 elements equal 4.
5. Next a5 elements equal 5.
6. Next a6 elements equal 6.
7. Next a7 elements equal 7.
8. Next a6 elements equal 6.
9. Next a5 elements equal 5.
10. Next a4 elements equal 4.
11. Next a3 elements equal 3.
12. Next a2 elements equal 2.
13. Next a1 elements equal 1.
(Here a1,a2,a3... are number of elements).
14. ai can be any non-zero positive integer.
15. There are no other elements in array.
Find out if the given array is a Rainbow Array or not.
Input:
The first line of contains an integer N, denoting the number of elements in the given array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of array.
Output:
Output a line containing "yes" or "no" (without quotes) corresponding to the case if the array is rainbow array or not.
Constraints:
7 ≤ N ≤ 100
1 ≤ Ai ≤ 10
Sample Input 1:
19
1 2 3 4 4 5 6 6 6 7 6 6 6 5 4 4 3 2 1
Sample output 1:
Yes
Sample input 2:
12
1 2 3 4 5 6 6 5 4 3 2 1
Sample output 2:
No
(has no elements with value 7 after elements with value 6.)
Sample output 3:
14
1 1 2 3 4 5 6 7 6 5 4 3 2 1
Sample output 3:
No
(On the left we have two occurrences of 1, whereas on the right only one occurrence ).
code in java
*******************************************************
class Solution
{
public static void israinbow (int[]arr)
{
int N = arr.length;
if (N < 13)
{
System.out.println ("no");
return;
}
int start = 0;
int end = N - 1;
boolean isValid = true;
int cur = 0;
while (start != end && start < end)
{
if (arr[start] != arr[end])
{
isValid = false;
break;
}
if (arr[start] < 1 || arr[start] > 7)
{
isValid = false;
break;
}
if (arr[start] != cur)
{
if (arr[start] != cur + 1)
{
isValid = false;
break;
}
else
{
cur = arr[start];
}
}
start++;
end--;
}
if ((arr[start] == 7 || cur == 7) && isValid)
{
System.out.println ("yes");
}
else
{
System.out.println ("no");
}
}
}