Associated Course(s):
Geeks Classes DSA
Geeks Classes in Noida

Given a **BST** and a target node **K**. You need to find an integer having minimum** absolute difference** with given target value K.

**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 no of nodes of 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 value of the node with minimum absolute difference 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 maximum

**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:**

17

4

**Explanation:**

**Testcase1:**

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

**Testcase2:**

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

Author: Shubham Joshi 1

may55 | 86 |

ashujack | 84 |

aman19 | 82 |

abbatta7 | 75 |

lakshmi_pandey | 74 |

saumitra13325 | 648 |

ashujack | 591 |

aman19 | 562 |

lakshmi_pandey | 546 |

aroma17 | 535 |

blackshadows | 5249 |

akhayrutdinov | 5111 |

Ibrahim Nash | 5087 |

Quandray | 4354 |

sanjay05 | 3668 |

Login to report an issue on this page.