Longest Path in a General Tree Whose Values Form an Arithmetic Progression

Quick Overview

Find the longest path in a rooted tree whose node values form an arithmetic progression with a given common difference, where the path may climb to an ancestor and turn down once. Tests combining results from child subtrees in one bottom-up pass and handling edge cases such as a zero difference and very deep trees.

Longest Path in a General Tree Whose Values Form an Arithmetic Progression

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a tree with `n` nodes numbered `0` to `n - 1` and rooted at node `0`. The list `adj` gives the edges as an adjacency list, `values[i]` is the value stored at node `i`, and `k` is an integer. Find the longest path in the tree whose node values, taken in order along the path, form an arithmetic progression with common difference `k`. A path moves along parent-child edges and never repeats a node. It may climb from a descendant up to an ancestor and then go down into another subtree of that ancestor (at most one change of direction), and it does not have to pass through the root. Return the number of nodes on the longest such path. ### Function Signature ```python def longest_arithmetic_path(adj: list[list[int]], values: list[int], k: int) -> int: ``` ### Rules - `adj` is undirected: `j` appears in `adj[i]` exactly when `i` appears in `adj[j]`. So `adj[i]` lists the parent of node `i` (unless `i` is the root) as well as its children. - A path is valid when it can be read in one of its two directions so that every value equals the previous value plus `k`. Read the other way, the same path steps by `-k`, so `k` and `-k` give the same answer. - When `k` is `0`, every value on the path must be equal. - A single node is a valid path, so the answer is at least `1`. Length counts nodes, not edges. ### Constraints - `1 <= n <= 10^4` - `len(adj) == n`, and the edges form a connected tree rooted at node `0` - `-10^5 <= values[i] <= 10^5` - `-10^5 <= k <= 10^5` ### Examples ```text Input: adj = [[1, 2], [0, 3, 4], [0], [1], [1]], values = [3, 5, 1, 7, 3], k = 2 Output: 4 ``` The root (value 3) has children 1 (value 5) and 2 (value 1), and node 1 has children 3 (value 7) and 4 (value 3). The path 2 -> 0 -> 1 -> 3 reads 1, 3, 5, 7, and no simple path in this tree has more than 4 nodes. ```text Input: adj = [[1, 2, 3], [0], [0], [0]], values = [10, 13, 7, 10], k = 3 Output: 3 ``` The root (value 10) has three leaf children with values 13, 7 and 10. The path 2 -> 0 -> 1 reads 7, 10, 13, and a path in this star can hold at most 3 nodes. ```text Input: adj = [[1, 2], [0, 3], [0], [1]], values = [4, 4, 4, 4], k = 0 Output: 4 ``` With `k = 0` the values must all be equal, and here every value is 4. The longest path in the tree, 3 -> 1 -> 0 -> 2, has 4 nodes.

Overview: Find the longest path in a rooted tree whose node values form an arithmetic progression with a given common difference, where the path may climb to an ancestor and turn down once. Tests combining results from child subtrees in one bottom-up pass and handling edge cases such as a zero difference and very deep trees.

Apple logo
Apple
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a tree with n nodes numbered 0 to n - 1 and rooted at node 0. The list adj gives the edges as an adjacency list, values[i] is the value stored at node i, and k is an integer. Find the longest path in the tree whose node values, taken in order along the path, form an arithmetic progression with common difference k.

A path moves along parent-child edges and never repeats a node. It may climb from a descendant up to an ancestor and then go down into another subtree of that ancestor (at most one change of direction), and it does not have to pass through the root.

Return the number of nodes on the longest such path.

Function Signature

def longest_arithmetic_path(adj: list[list[int]], values: list[int], k: int) -> int:

Rules

  • adj is undirected: j appears in adj[i] exactly when i appears in adj[j] . So adj[i] lists the parent of node i (unless i is the root) as well as its children.
  • A path is valid when it can be read in one of its two directions so that every value equals the previous value plus k . Read the other way, the same path steps by -k , so k and -k give the same answer.
  • When k is 0 , every value on the path must be equal.
  • A single node is a valid path, so the answer is at least 1 . Length counts nodes, not edges.

Constraints

  • 1 <= n <= 10^4
  • len(adj) == n , and the edges form a connected tree rooted at node 0
  • -10^5 <= values[i] <= 10^5
  • -10^5 <= k <= 10^5

Examples

Input: adj = [[1, 2], [0, 3, 4], [0], [1], [1]], values = [3, 5, 1, 7, 3], k = 2
Output: 4

The root (value 3) has children 1 (value 5) and 2 (value 1), and node 1 has children 3 (value 7) and 4 (value 3). The path 2 -> 0 -> 1 -> 3 reads 1, 3, 5, 7, and no simple path in this tree has more than 4 nodes.

Input: adj = [[1, 2, 3], [0], [0], [0]], values = [10, 13, 7, 10], k = 3
Output: 3

The root (value 10) has three leaf children with values 13, 7 and 10. The path 2 -> 0 -> 1 reads 7, 10, 13, and a path in this star can hold at most 3 nodes.

Input: adj = [[1, 2], [0, 3], [0], [1]], values = [4, 4, 4, 4], k = 0
Output: 4

With k = 0 the values must all be equal, and here every value is 4. The longest path in the tree, 3 -> 1 -> 0 -> 2, has 4 nodes.