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_dkp
    @haq_se_dkp5 жыл бұрын

    The correct and optimal method starts at 8:06, for those who are short of time

  • @spicytuna08
    @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.

  • @psaikiran9831
    @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

  • @swacharahman5084
    @swacharahman50844 жыл бұрын

    sir can you please explain this with program ..thank you for your solution

  • @niyatisrivastava5407
    @niyatisrivastava54072 жыл бұрын

    if sum is minimum then also we need to check l++ and r--, to compare with other min

  • @mohammadmujahid8850
    @mohammadmujahid88505 жыл бұрын

    Please make video with Java and Kotlin

  • @sanjaypardeshi1964
    @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

    @GeeksforGeeksVideos

    7 жыл бұрын

    You're welcome, Sanjay. We'll try to incorporate your input in our future videos.

  • @sanjaypardeshi1964

    @sanjaypardeshi1964

    7 жыл бұрын

    👍

  • @reyou7
    @reyou77 жыл бұрын

    Thanks for sharing this as other videos! You are awesome guys!

  • @GeeksforGeeksVideos

    @GeeksforGeeksVideos

    7 жыл бұрын

    Thanks reyou7 for the appreciation!

  • @UttamKumarSinha1234
    @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

    @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.

  • @wecan2729
    @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; } };

  • @radhikabhatia3868
    @radhikabhatia38685 жыл бұрын

    What if i want those two elements whose sum is equal to 0

  • @abhijeetjain2098

    @abhijeetjain2098

    5 жыл бұрын

    yes it can be found...

  • @spicytuna08
    @spicytuna086 жыл бұрын

    how is it that O(nlogn) + O(n) = O(nlogn)?

  • @kaifahmad4131

    @kaifahmad4131

    3 жыл бұрын

    Please Study space and time Complexity

Келесі