Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5594 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 783 |

@Manikanta punnam | 605 |

@sriramgoparaju99 | 594 |

@rdakka | 538 |

@praveenbgp6 | 529 |

@prankursharma31 | 518 |

@yashkaril4 | 517 |

@purohitmn02 | 467 |

@sonamnigam1999 | 443 |

@sonamkumari63928 | 441 |

@shubhamstudent5 | 433 |

Complete Leaderboard |

Given an array **Arr** of **N** numbers and another number **target**, find three integers in the array such that the sum is closest to target. Return the sum of the three integers.

**Example 1:**

**Input:
**N = 6, target = 2
A[] = {-7,9,8,3,1,1}
**Output: **2**
Explanation: **There is one triplet with sum
2 in the array. Triplet elements are -7,8,
1 whose sum is 2.

**Example 2:**

**Input:
**N = 4, target = 13
A[] = {5,2,7,5}
**Output: **14**
Explanation: **There is one triplet with sum
12 and other with sum 14 in the array.
Triplet elements are 5, 2, 5 and 2, 7, 5
respectively. Since abs(13-12) ==
abs(13-14) maximum triplet sum will be
preferred i.e 14.

**Your Task:**

Complete **threeSumClosest()** function and return the expected answer.

**NOTE: **If their exists more than one answer then return the maximum sum.

**Expected Time Complexity: **O(N*N).

**Expected Auxiliary Space: **O(1).

**Constraints:**

1 ≤ N ≤ 10^{3}

-10^{5} ≤ A[i] ≤ 10^{5}

1 ≤ target ≤ 10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Three Sum Closest

...