Two elements whose sum is closest to zero | GeeksforGeeks
Explanation for the article: www.geeksforgeeks.org/two-elem... This video is contributed by Harshit Jain.
Жүктеу.....
Пікірлер: 19
@haq_se_dkp5 жыл бұрын
The correct and optimal method starts at 8:06, for those who are short of time
@spicytuna086 жыл бұрын
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.
@psaikiran98314 жыл бұрын
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
@swacharahman50844 жыл бұрын
sir can you please explain this with program ..thank you for your solution
@niyatisrivastava54072 жыл бұрын
if sum is minimum then also we need to check l++ and r--, to compare with other min
@mohammadmujahid88505 жыл бұрын
Please make video with Java and Kotlin
@sanjaypardeshi19647 жыл бұрын
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
7 жыл бұрын
You're welcome, Sanjay. We'll try to incorporate your input in our future videos.
@sanjaypardeshi1964
7 жыл бұрын
👍
@reyou77 жыл бұрын
Thanks for sharing this as other videos! You are awesome guys!
@GeeksforGeeksVideos
7 жыл бұрын
Thanks reyou7 for the appreciation!
@UttamKumarSinha12346 жыл бұрын
-10 + (-80) = -90. Is it less close to zero than +5. I presume I did not understand the meaning of closest to zero
@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.
@wecan27293 жыл бұрын
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 { // 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]) { 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; } };
@radhikabhatia38685 жыл бұрын
What if i want those two elements whose sum is equal to 0
Пікірлер: 19
The correct and optimal method starts at 8:06, for those who are short of time
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.
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
sir can you please explain this with program ..thank you for your solution
if sum is minimum then also we need to check l++ and r--, to compare with other min
Please make video with Java and Kotlin
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
7 жыл бұрын
You're welcome, Sanjay. We'll try to incorporate your input in our future videos.
@sanjaypardeshi1964
7 жыл бұрын
👍
Thanks for sharing this as other videos! You are awesome guys!
@GeeksforGeeksVideos
7 жыл бұрын
Thanks reyou7 for the appreciation!
-10 + (-80) = -90. Is it less close to zero than +5. I presume I did not understand the meaning of closest to zero
@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.
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 { // 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]) { 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; } };
What if i want those two elements whose sum is equal to 0
@abhijeetjain2098
5 жыл бұрын
yes it can be found...
how is it that O(nlogn) + O(n) = O(nlogn)?
@kaifahmad4131
3 жыл бұрын
Please Study space and time Complexity