Two elements whose sum is closest to zero | GeeksforGeeks

Поділитися
Вставка
  • Опубліковано 1 жов 2024
  • Explanation for the article: www.geeksforgee...
    This video is contributed by Harshit Jain.

КОМЕНТАРІ • 19

  • @haq_se_dkp
    @haq_se_dkp 5 років тому +19

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

  • @sanjaypardeshi1964
    @sanjaypardeshi1964 8 років тому +4

    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

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

  • @niyatisrivastava5407
    @niyatisrivastava5407 3 роки тому

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

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

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

  • @mohammadmujahid8850
    @mohammadmujahid8850 5 років тому

    Please make video with Java and Kotlin

  • @reyou7
    @reyou7 7 років тому

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

  • @radhikabhatia3868
    @radhikabhatia3868 6 років тому +1

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

  • @spicytuna08
    @spicytuna08 6 років тому

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

    • @kaifahmad4131
      @kaifahmad4131 3 роки тому

      Please Study space and time Complexity