Maximum length subsequence

4.3

3 votes
Implementation, Sorting, Algorithms, 2D dynamic programming
Problem

You are given an array A consisting of N integers. A subsequence of the array is called good if every pair of elements in the subsequence have an absolute difference of at most 10.

Task

Determine the maximum possible length of good subsequence.

Example

Assumptions

  • N = 8
  • A = [1, 9, 14, 2, 17, 14, 5, 18]

Approach

  • Subsequence: [9,14,17,14,18] is good which is the maximum length subsequence.
  • Hence the output is 5.

Function description

Complete the function solve() which takes the following 2 parameters and returns an integer as the required answer:

  • N: Represents an integer denoting the size of the array
  • A: Represents an array of N integers

Input format

Note: This is the input format you must use to provide custom input (available above the Compile and Test button). 

  • The first line contains an integer N.
  • The second line contains an array A of N integers.

Output format

Print the maximum possible length of good subsequence.

Constraints

1N1051Ai109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Given

  • N = 10
  • A = [4, 5, 10, 101, 2, 129, 131, 130, 118, 14]

Approach

  • Subsequence: [4, 5, 10, 14] is good which is the maximum length subsequence.
  • So the output is 4.
Editor Image

?