import
java.util.*;
public
class
GFG {
public
static
void
main(String[] args)
{
int
[] X = {
2
,
2
,
5
,
5
,
5
,
3
,
5
,
5
,
3
,
3
,
2
};
X = Reverse_Array(X);
int
N = X.length;
ArrayList<Integer> set = Set(N, X);
Approach(set, N, X);
}
static
void
Approach(ArrayList<Integer> set,
int
n,
int
[] X)
{
int
max_all = Integer.MIN_VALUE;
for
(
int
i =
0
; i < set.size(); i++) {
int
element = set.get(i);
ArrayList<Integer> adjacent_frequency
=
new
ArrayList<>();
int
leftEnd =
0
;
while
(leftEnd < n) {
if
(X[leftEnd] != element) {
leftEnd++;
}
else
{
int
rightEnd = leftEnd;
while
(rightEnd < n
&& X[rightEnd] == element)
rightEnd = rightEnd +
1
;
adjacent_frequency.add(rightEnd
- leftEnd);
leftEnd = rightEnd +
1
;
}
}
int
max = Integer.MIN_VALUE;
for
(
int
k =
0
; k < adjacent_frequency.size();
k++) {
max = adjacent_frequency.get(k) > max
? adjacent_frequency.get(k)
: max;
}
boolean
flag =
false
;
if
(X[
0
] != element) {
System.out.println(
"Element : "
+ element
+
" Max suffix length : "
+ max);
flag =
true
;
max_all = max > max_all ? max : max_all;
}
else
if
(flag !=
true
) {
for
(
int
j =
1
;
j < adjacent_frequency.size(); j++) {
max = (adjacent_frequency.get(
0
)
+ adjacent_frequency.get(j))
> max
? (adjacent_frequency.get(
0
)
+ adjacent_frequency.get(
j))
: max;
max_all = max > max_all ? max : max_all;
}
System.out.println(
"element : "
+ element
+
" Max suffix length : "
+ max);
}
}
System.out.println(
"Maximum length among all : "
+ max_all);
}
static
int
[] Reverse_Array(
int
[] X)
{
int
start =
0
;
int
end = X.length -
1
;
while
(start < end) {
int
temp = X[start];
X[start] = X[end];
X[end] = temp;
start++;
end--;
}
return
X;
}
static
ArrayList<Integer> Set(
int
n,
int
[] X)
{
ArrayList<Integer> set =
new
ArrayList<>();
for
(
int
i =
0
; i < n; i++) {
if
(set.contains(X[i])) {
continue
;
}
else
{
set.add(X[i]);
}
}
return
(set);
}
}
Stay connected with us on social media platform for instant update click here to join our Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here