Find Maximum Difference between Two Array Elements

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024

ความคิดเห็น • 26

  • @iswariyaravi9134
    @iswariyaravi9134 3 ปีที่แล้ว +1

    Man the most efficient solution!!!

  • @ayushjain9723
    @ayushjain9723 2 ปีที่แล้ว +1

    Nice Explanation Thanks a lot Sir !

  • @SugamMaheshwari
    @SugamMaheshwari 2 ปีที่แล้ว

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

  • @jaisharma8056
    @jaisharma8056 2 ปีที่แล้ว

    what an awesome explaination sir. thank you so much

  • @madhuneellapala4580
    @madhuneellapala4580 ปีที่แล้ว

    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  ปีที่แล้ว

      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.

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

  • @ProgrammingTutorials1M
    @ProgrammingTutorials1M  5 ปีที่แล้ว +1

    Code link is present in the description box.

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

  • @Jyotigupta-vs4mz
    @Jyotigupta-vs4mz ปีที่แล้ว

    brute fore is giving wrong output

  • @edwythefair5215
    @edwythefair5215 3 ปีที่แล้ว

    It helped a lot, thank you !

  • @praveenchouhan6388
    @praveenchouhan6388 4 ปีที่แล้ว

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

  • @sidharthmandal9957
    @sidharthmandal9957 5 ปีที่แล้ว +2

    2nd optimisation code bounced from my head.

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

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