import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { /* Examples a. 4 6 5 3 3 1 6 5 4 3 3 1 ___________ 6 5, 5 4, 4 3 3 3 3 1 b. 1 2 2 3 1 2 Pseudo sort arr sum count maxCount = 0; while index is bigger than -1 if(element at index differs from start element by more than 1) { get value of previous index get first occurence index of that value set index to that if(sum > maxSum){ maxCount = count maxSum } } increase sum increase count if next element has difference of 1 and nextStartIndex hasn't been set start iteration from nextStartIndex */ //6 5 4 3 3 1 //1 2 2 3 1 2 //1 1 2 2 2 3 static int maxSum(int[] arr){ Arrays.sort(arr); int maxCount = -1; int count = 0; boolean nextIndexNotSet = true; int nextIndex = -1; int index = arr.length-1; int value = arr[index]; while(index > -1){ if(value != arr[index] && nextIndexNotSet){ nextIndex = index; nextIndexNotSet = false; } if(value - arr[index] < 2){ count++; index--; }else{ if(count > maxCount){ maxCount = count; } index = nextIndex; count = 0; value = arr[index]; nextIndexNotSet = true; } } if(count > maxCount){ maxCount = count; } return maxCount; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int a_i=0; a_i < n; a_i++){ a[a_i] = in.nextInt(); } System.out.println(maxSum(a)); } }