Two elements whose sum is closest to zero | GeeksforGeeks

  Рет қаралды 27,531

GeeksforGeeks

GeeksforGeeks

8 жыл бұрын

Explanation for the article: www.geeksforgeeks.org/two-elem...
This video is contributed by Harshit Jain.

Пікірлер: 19
@haq_se_dkp
@haq_se_dkp 5 жыл бұрын
The correct and optimal method starts at 8:06, for those who are short of time
@spicytuna08
@spicytuna08 6 жыл бұрын
thx. isn't safe to start the l index with 1 since min_sum was already evaluated using [0] and [1] indices before the loop began.
@psaikiran9831
@psaikiran9831 4 жыл бұрын
But if quick sort takes O(n^2) in worst case, how can we assume this as an optimal solution? Merge Sort is my choice
@swacharahman5084
@swacharahman5084 4 жыл бұрын
sir can you please explain this with program ..thank you for your solution
@niyatisrivastava5407
@niyatisrivastava5407 2 жыл бұрын
if sum is minimum then also we need to check l++ and r--, to compare with other min
@sanjaypardeshi1964
@sanjaypardeshi1964 7 жыл бұрын
Thanks for the solution but please try to explain program with the example. I have not understood program, so now I will write program and will try to understand it
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
You're welcome, Sanjay. We'll try to incorporate your input in our future videos.
@sanjaypardeshi1964
@sanjaypardeshi1964 7 жыл бұрын
👍
@reyou7
@reyou7 7 жыл бұрын
Thanks for sharing this as other videos! You are awesome guys!
@GeeksforGeeksVideos
@GeeksforGeeksVideos 7 жыл бұрын
Thanks reyou7 for the appreciation!
@mohammadmujahid8850
@mohammadmujahid8850 5 жыл бұрын
Please make video with Java and Kotlin
@wecan2729
@wecan2729 3 жыл бұрын
class Solution { public: int closestToZero(int arr[], int n) { sort (arr, arr + n); // sorting the array int i = 0, j = n - 1; int sum = arr[i] + arr[j]; // initializing sum int diff = abs (sum); // initializing the result while (i < j) { // if we have zero sum, there's no result better. Hence, we return if (arr[i] + arr[j] == 0) return 0; // if we get a better result, we update the difference if (abs (arr[i] + arr[j]) < abs (diff)) { diff = (arr[i] + arr[j]); sum = arr[i] + arr[j]; } else if(abs (arr[i] + arr[j]) == abs (diff)) { sum=max(sum,arr[i]+arr[j]); } // if the current sum is greater than zero, we search for a smaller sum if (arr[i] + arr[j] > 0) j--; // else, we search for a larger sum else i++; } return sum; } };
@UttamKumarSinha1234
@UttamKumarSinha1234 6 жыл бұрын
-10 + (-80) = -90. Is it less close to zero than +5. I presume I did not understand the meaning of closest to zero
@spicytuna08
@spicytuna08 6 жыл бұрын
take absolute value. absolute value is making every non negative number to positive, if you take abs value of -90, it becomes +90.
@radhikabhatia3868
@radhikabhatia3868 5 жыл бұрын
What if i want those two elements whose sum is equal to 0
@abhijeetjain2098
@abhijeetjain2098 5 жыл бұрын
yes it can be found...
@spicytuna08
@spicytuna08 6 жыл бұрын
how is it that O(nlogn) + O(n) = O(nlogn)?
@kaifahmad4131
@kaifahmad4131 3 жыл бұрын
Please Study space and time Complexity
Convert array into Zig-Zag fashion | GeeksforGeeks
8:53
GeeksforGeeks
Рет қаралды 43 М.
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 4,9 МЛН
Я обещал подарить ему самокат!
01:00
Vlad Samokatchik
Рет қаралды 9 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 10 МЛН
Find the Number Occurring Odd Number of Times | GeeksforGeeks
6:28
GeeksforGeeks
Рет қаралды 46 М.
Count minimum steps to get the given desired array | GeeksforGeeks
8:43
Blue Sky talk with Ms. Sudha Murty
58:49
Blue Sky
Рет қаралды 720 М.
Vegetarian Food in Japan I Top 10 must try Vegetarian Dishes of Japan
8:27
Two Elements Whose Sum Is Closest To Zero in Java Video 6
21:49
learnby bhanu
Рет қаралды 811
Find Pairs in Array with Given Sum | Programming Tutorials
5:41
Programming Tutorials
Рет қаралды 65 М.
Majority Element | GeeksforGeeks
12:30
GeeksforGeeks
Рет қаралды 54 М.
Largest number formed from an array
8:33
Techdose
Рет қаралды 117 М.
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 4,9 МЛН