Maximum difference between two elements (larger element appears after smaller) | GeeksforGeeks

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

КОМЕНТАРІ • 28

  • @SamyakJain74
    @SamyakJain74 5 років тому +38

    Instead of going through the code line by line , i think you should focus on dry running the solution on any example. Will make things more clear. Everyone knows what a single piece of line is doing , no need to explain that.

  • @pulkitgupta1946
    @pulkitgupta1946 5 років тому +6

    You are just reading the code line by line. Please explain it more efficiently.

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

    u shud focus on the approach more and on the implementation less, going thru the snap of a code snippet is not going to HELP the viewers, viewers need to visualize the flow, implementation is what any one can do once the flow is clear!!!

  • @codestorywithMIK
    @codestorywithMIK 7 років тому +3

    The tricky solution was awesome. Thanks GfG and thanks a lot Harshit

  • @infotainment7123
    @infotainment7123 4 роки тому +3

    how are you sure that arr1-arr0 will always return a postive integer

  • @karamany9870
    @karamany9870 Рік тому

    I got this problem in an interview for an internship at a startup

  • @AnkitSharma-gt1dr
    @AnkitSharma-gt1dr 6 років тому +1

    Another approach
    arr=[5,84,8,8,1,6,90]
    max=arr[-1]
    diff=0
    for i in range(-2,-len(arr)-1,-1):
    if arr[i]diff:
    diff=max-arr[i]
    else:
    max=arr[i]
    print(diff)

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

    roughly explain the analysis of each line

  • @Relaxingmusic-tw4qn
    @Relaxingmusic-tw4qn 4 роки тому +2

    zzz every 1 here knows English , you just read whats written

  • @saurabhgoel7021
    @saurabhgoel7021 6 років тому +2

    Another O(n) solution with O(1) space could be
    Start from rightmost element and obtain the max element for any element at index i (from i+1 to nth element )
    And side by side updating the max difference found so far with only case that maximum element found at the right of any element must be greater than the element
    This requires n operations without using any extra space

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

      But bro this could take n2. See this test case 5 4 3 2 1

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

      @@akshatjain6854 but I think it's worst case. Avg case would still be o(N).

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

    We can read ourselves..go through hand simulation

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

    What will be output for these inputs
    Ip : 1 2 90 10 -110
    I want a program to consider my negative value too

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

      10-1 =9

    • @shubhamraghav562
      @shubhamraghav562 2 місяці тому

      89 because 90 will be the greatest element in the array and the smaller element in the array before 90 is 1 so 90-1=89.

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

    How to be able to think faster and get quicker answer? please help

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

    not clear

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

    yet another cool video from G4G!

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

    Will it work for below input array?
    int arr[] = { 6, 7, 9, 5, 6, 3, 2 };
    Expected Output: 2 => (9 - 7), However, this code will provide output as 3?
    I have written below code which will take care for above input. You may have to correct geeksforgeek as applicable?
    #include
    #include
    #include
    #include
    using namespace std;
    void printArray(vector a)
    {
    for(vector::iterator itr=a.begin(); itr!=a.end(); itr++)
    {
    cout

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

      Your expected output is incorrect. Either you didn't understand the question properly or you just made an innocent mistake.
      The expected output is 3 (6-9) They need not be adjacent elements.

  • @pragatikumari306
    @pragatikumari306 4 роки тому +1

    Worst explanation ...just reading the code doesn't help much ... please understand the logic don't read