Associated Course(s):
DSA-Self Paced with Doubt Assistance
Placement 100

Given a **BST** with **N** nodes and a target node **K**. The task is to find an integer having minimum** absolute difference** with given target value K and return this difference.

**Input Format:**

The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of three lines. First line of each test case contains an integer N denoting the number of nodes in the BST . Second line of each test case consists of 'N' space separated integers denoting the elements of the BST. These elements are inserted into BST in the given order.The last line of each test case contains an integer k as specified in problem statement.

**Output Format:**

The output for each test case will be the minimum absolute difference of a node with given target value K.

**Your Task:**

This is a function problem. You don't have to take any input. Just complete the **function maxDiff() **that takes **node and K **as parameter and returns the minimum difference.

**Constraints:**

1 <= T <= 100

1 <= N <= 200

**Example:**

**Input:**

2

9

9 4 3 6 5 7 17 22 20

18

9

9 4 3 6 5 7 17 22 20

4

**Output:**

1

0

**Explanation:**

**Testcase1:**

K=18. The node that has value nearest to K is 17 so it's difference with k is the answer.

**Testcase2:**

K=4. The node that has value nearest to K is 4 so it's difference with k is the answer.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

RatneshTiwari3 | 24 |

AnmolPassi | 20 |

Swords | 20 |

baranwalsuraj96 | 17 |

tarun711 | 16 |

Bishnu Dev Panda | 727 |

yk12 | 512 |

DevMittal | 472 |

okayboss | 458 |

VijethaShastri | 431 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4921 |

Quandray | 4547 |

Login to report an issue on this page.