-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathLongestConsecutiveDiffPath.java
More file actions
50 lines (42 loc) · 1.55 KB
/
LongestConsecutiveDiffPath.java
File metadata and controls
50 lines (42 loc) · 1.55 KB
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
package com.thealgorithms.dynamicprogramming;
import java.util.HashMap;
/**
* Problem: Longest Consecutive Difference Path
*
* Given an integer array, find the length of the longest path such that
* the absolute difference between consecutive elements is exactly 1.
*
* Example:
* Input: arr = [3, 4, 2, 1, 2, 3, 4, 5]
* Output: 5
* Explanation: The longest path is [1, 2, 3, 4, 5]
*
* Category: Dynamic Programming with Hashing
*/
public class LongestConsecutiveDiffPath {
/**
* Function to find the longest consecutive difference path length
* @param arr input integer array
* @return length of longest path
*/
public static int longestPath(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
int maxLength = 0;
for (int num : arr) {
int len1 = map.getOrDefault(num - 1, 0);
int len2 = map.getOrDefault(num + 1, 0);
int currentLen = Math.max(len1, len2) + 1;
map.put(num, currentLen);
maxLength = Math.max(maxLength, currentLen);
}
return maxLength;
}
public static void main(String[] args) {
int[] arr = {3, 4, 2, 1, 2, 3, 4, 5};
System.out.println("Longest consecutive difference path length: " + longestPath(arr));
int[] arr2 = {1, 2, 3, 4, 2, 3, 4, 5, 6};
System.out.println("Longest consecutive difference path length: " + longestPath(arr2));
int[] arr3 = {10, 9, 8, 7, 6};
System.out.println("Longest consecutive difference path length: " + longestPath(arr3));
}
}