-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question_30.java
65 lines (51 loc) · 1.71 KB
/
Question_30.java
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
/**Question 30:-
*
* You are given an integer array nums and an integer k.
Find the longest subsequence of nums that meets the following requirements:
The subsequence is strictly increasing and
The difference between adjacent elements in the subsequence is at most k.
Return the length of the longest subsequence that meets the requirements.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,2,1,4,3,4,5,8,15], k = 3
Output: 5
Explanation:
The longest subsequence that meets the requirements is [1,3,4,5,8].
The subsequence has a length of 5, so we return 5.
Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.
*/
import java.util.*;
public class Question_30{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int arrLength = scan.nextInt();
int nums[] = new int[arrLength];
int k ;
for (int i=0; i<arrLength; i++) {
nums[i] = scan.nextInt();
}
k = scan.nextInt();
System.out.println( max_subseq(k, nums));
}
public static int max_subseq(int k, int nums[]){
int max = 0;
int temp = 1;
for(int i=0; i<nums.length-1; i++){
/**
* condition for strictly increasing and The difference between adjacent elements in the subsequence is at most k.
*/
if(nums[i+1]-nums[i] <= k && nums[i+1]-nums[i] > 0){
temp++;
// if temp value if more then one only then program will change the value
if(max < temp){
if(temp > 1){
max = temp;
}
}
}else{
temp = 1;
}
}
return max;
}
}