Given an array of ints, find the most frequent non-empty subarray in it. If there are more than one such sub-arrays return the longest one/s.
Note: Two subarrays are equal if they contain identical elements and elements are in the same order.
For example: if input = {4,5,6,8,3,1,4,5,6,3,1}
Result: {4,5,6}
private static void longestSubseq(int a[])
{
int len = 1;
int maxLen = 1;
int index = 0;
for (int i = 0; i < a.length-1; ++i)
{
if (a[i+1] == a[i] + 1)
{
len++;
if (len > maxLen)
{
maxLen = len;
index = i+1;
}
}
else
{
len = 1;
}
}
System.out.println(maxLen);
for (int i = index - maxLen+1; i <= index; ++i)
System.out.print(a[i] + " ");
}
int input[] = {4,5,6,8,3,1,4,5,6,3,1};
int aaa[] = {1,2,3,4,4,1,3,2,1,2};
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 9, 7, 8, 9, 1,
2, 7, 8, 9, 1, 2 };
int xa[] = {3};
longestSubseq(xa);