Find Maximum Difference between Two Array Elements

Поділитися
Вставка
  • Опубліковано 3 жов 2024

КОМЕНТАРІ • 26

  • @iswariyaravi9134
    @iswariyaravi9134 3 роки тому +1

    Man the most efficient solution!!!

  • @ayushjain9723
    @ayushjain9723 2 роки тому +1

    Nice Explanation Thanks a lot Sir !

  • @jaisharma8056
    @jaisharma8056 2 роки тому

    what an awesome explaination sir. thank you so much

  • @ProgrammingTutorials1M
    @ProgrammingTutorials1M  5 років тому +1

    Code link is present in the description box.

  • @SugamMaheshwari
    @SugamMaheshwari 2 роки тому

    Good Solution, just need to handle edge cases like {3,2,1} etc

  • @madhuneellapala4580
    @madhuneellapala4580 2 роки тому

    Good explanation but one concern. If we have an array like {50,9,5,6,13,2} then we are getting wrong answer. Pls check it again (first public static int bruteForce method)

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  2 роки тому

      As per the problem statement it is also mentioned that the larger element appears after the smaller element, please consider this also then check the output.

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

    It helped a lot, thank you !

  • @praveenchouhan6388
    @praveenchouhan6388 4 роки тому

    nice explaination !!!!!thanks a lot!!!!

  • @faktkhandeshi
    @faktkhandeshi 3 роки тому +1

    You can also solve brute force in single iteration like this 😀
    const array = [2, 3, 8, 150, 10, 6, 7, 400];
    max = 0;
    for (let i = 0; i < array.length - 1; i++) {
    const current = array[i];
    const next = array[i + 1];
    max = Math.max(max, next - current);
    console.log(current, next, max);
    }
    console.log(max)

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

      Doesn't work for your example. Your code is right only when we are asked to find the maximum difference between adjacent elements.

  • @Jyotigupta-vs4mz
    @Jyotigupta-vs4mz Рік тому

    brute fore is giving wrong output

  • @sunils.3385
    @sunils.3385 4 роки тому

    int a[] = {7,4,8,2,10,1};
    int maxdif=a[1]-a[0];
    int minele = a[0];

    for (int i = 0; i < a.length; i++) {

    if(a[i]maxdif) {
    maxdif=a[i]-minele;

    }

    }
    System.out.println(maxdif+" "+minele);
    but the output is 8 1
    not 9 1

  • @sidharthmandal9957
    @sidharthmandal9957 5 років тому +2

    2nd optimisation code bounced from my head.

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

    Sir we can sort the array and take the difference between arr[0]-arr[n-1] that will work right?

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

      @@ProgrammingTutorials1M can u upload some pattern printing videos also?
      It will be very helpful

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

      @@rupampaul If you have any specific question in your mind then let me know. I'll definitely upload the videos in that topic.

    • @NitinVerma-uj3yp
      @NitinVerma-uj3yp 4 роки тому

      we can't because of the constraint (j>i)

  • @ivanchan3673
    @ivanchan3673 4 роки тому

    May I ask how can we return an array of length 3, which can show exactly which element minus which element gives us the max difference? In your example, the array would be {5, 13, 8}, because 13-5 = 8 is the max difference. Thank you in advance!

  • @yadnyeshrane7448
    @yadnyeshrane7448 4 роки тому

    sir plz check whether it is right or not
    int main()
    {
    int a[]={7,9,5,6,13,2};
    int diff=a[1]-a[0];
    int res;
    for(int i=0;i