Skip to content

Instantly share code, notes, and snippets.

@anubhavbagri
Last active March 29, 2026 15:41
Show Gist options
  • Select an option

  • Save anubhavbagri/a64ead42bb6923e4833f1f737d6d20d9 to your computer and use it in GitHub Desktop.

Select an option

Save anubhavbagri/a64ead42bb6923e4833f1f737d6d20d9 to your computer and use it in GitHub Desktop.

Process Tree

A hypothetical chain of processes is represented as a tree. Processes are numbered starting at 1, incremented by 1. Every process spawns a number of processes equal to its process number. The first node, processNumber 1, spawns 1 process, the second spawns 2 and so on. Given a process number, find the process number of its parent.

Example

processNumber = 6

From the diagram, the parent of 6 is 3.

graph TD
    1 --> 2
    2 --> 3
    2 --> 4
    3 --> 5
    3 --> 6
    3 --> 7
Loading

Function Description

Complete the findParent function in the editor below.

findParent has the following parameter(s):

  • int processNumber: the process number to query

Returns

int: the process number of the parent

Constraints

  • $2 \leq \text{processNumber} \leq 10^9$

Sample Case 0

Sample Input

STDIN → Function
6processNumber = 6

Sample Output

3

Explanation

Refer to the graph in the problem statement. The child of 1 is 2. Children of 2 are 3 and 4. The children of 3 are 5, 6, and 7. Therefore, the parent process of the given process 6 is 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment