Find Second Maximum value in an Array | Animation

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024

Комментарии • 51

  • @shubhamagarwal1434
    @shubhamagarwal1434 2 года назад +3

    Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over RUclips with java implementation and found this. Hats Off To You Man....Excellent Work. GOD BLESS YOU :)

  • @huayang2467
    @huayang2467 Год назад +4

    You make all the dynamic changes so clear with animation slides. Thank you so much, sir! I feel so grateful to have found your videos.

  • @bkishwarchavan7787
    @bkishwarchavan7787 Год назад +2

    I do not understand to appreciate you.....Never seen this type of confidence in DSA while teaching. It means sir how much deep knowledge you have about this subject.......Superb sir no one can teach you .....Awesome such valuable content you are giving us in free of cost salute sir...Thanks for increasing my confidence.

  • @mvs69
    @mvs69 2 года назад +9

    These animation slides make it very easy to understand the logic. Great work!!

  • @TinsaeJembere
    @TinsaeJembere 7 месяцев назад +1

    can't thank u enough really...I was so confused when I try to read books but ur videos are awesome Thanks so much🥰

  • @karentechnologies3990
    @karentechnologies3990 2 года назад +6

    Here's another algorithm to achieve the same goal, it's easier to understand but lower in performance :
    public int myFindSecondMaximum(int[] numbers){
    int maximum = numbers[0];
    int position = 0;
    for (int i=1; i< numbers.length; i++){
    if (numbers[i] > maximum){
    maximum = numbers[i];
    position = i;
    }
    }
    numbers[position] = 0;
    int secondMaximum = numbers[0];
    for (int i=1; i< numbers.length; i++){
    if (numbers[i] > secondMaximum && numbers[i] != maximum){
    secondMaximum = numbers[i];
    }
    }
    return secondMaximum;
    }

    • @7shanky71
      @7shanky71 10 месяцев назад +1

      very right!

  • @thiagobrunoms
    @thiagobrunoms 5 месяцев назад

    For those who want more test cases:
    # Array with unique elements:
    input = [1, 2, 3, 4, 5]
    # Expected output: 4
    # Array with duplicate elements:
    input = [2, 5, 3, 5, 7, 2]
    # Expected output: 5
    # Array with negative numbers:
    input = [-5, -2, -9, -3, -7]
    # Expected output: -3
    # Array with mixed positive and negative numbers:
    input = [10, -5, 3, -8, 7]
    # Expected output: 7
    # Array with repeated highest value:
    input = [2, 5, 9, 5, 7, 9]
    # Expected output: 7
    # Array with only one unique value:
    input = [3, 3, 3, 3, 3]
    # Expected output: No second highest value (some implementations may return an exception)

  • @AhsanAli-vz6mg
    @AhsanAli-vz6mg 3 года назад +2

    Superb explanation

  • @wizardop2100
    @wizardop2100 Год назад +1

    Thanks sir 🙏🏻.It is helpful.

  • @ascar66
    @ascar66 2 года назад

    Thank you sir, nice explanation.

  • @Trending-Media-jk8vp
    @Trending-Media-jk8vp 2 года назад +1

    really very helpful.
    Thanks Sir

  • @yellnitroy2031
    @yellnitroy2031 3 года назад +3

    sir when will the playlist be finish? thank you for your efforts

    • @itsdineshvaryani
      @itsdineshvaryani  3 года назад +4

      It will be a continuation playlist ... in future it will have advanced DSA topics as well ....

    • @yellnitroy2031
      @yellnitroy2031 2 года назад +1

      @@itsdineshvaryani ok sir thank you..

    • @bpriorb
      @bpriorb 2 года назад +1

      @@itsdineshvaryani Outstanding Dinesh. You're the best!

    • @itsdineshvaryani
      @itsdineshvaryani  2 года назад

      @@bpriorb thanks !!!

  • @ajinxRGB
    @ajinxRGB 2 года назад

    just amazing content !! what a beautiful learning experience !!

  • @yasaswynandavareek8399
    @yasaswynandavareek8399 Год назад

    I did the same thing but assigned the max to first element of the array and the second max to the second element of the array and made sure its right or swapped them. Then I started the for loop from the 3rd element and did the similar logic. Is this logic fine as well or will it have any complexity issues?

  • @modinglymods4085
    @modinglymods4085 Год назад

    Can you please check the test case where the second largest element is in the 0th index. I think the above method is going to fail

  • @ERDheerajmann
    @ERDheerajmann Год назад

    Hi, its correct however it fails if all elements of an array are same

    • @yasaswynandavareek8399
      @yasaswynandavareek8399 Год назад

      He already mentioned that the constraint is the array needs to have a second maximum number. There is no second maximum if all the elements are same.

  • @deshansh1219
    @deshansh1219 2 года назад +1

    Not giving correct output while implementing in IDE

  • @indranilchakraborty4965
    @indranilchakraborty4965 2 года назад

    what about sort the array then print second last element?

  • @alfredlotsu9580
    @alfredlotsu9580 6 месяцев назад

    My Solution:
    public static int secondMax(int[] arr) {
    int max = Integer.MIN_VALUE;
    int secMax = Integer.MIN_VALUE;
    for (int i: arr) {
    if (max < i) {
    max = i;
    }
    }
    for (int j:arr) {
    if (secMax

  • @ekansh6267
    @ekansh6267 Год назад

    👌👍👍

  • @addytechie6948
    @addytechie6948 Год назад +1

    Whats wrong with this snippet?
    public int findSecondMaximum(int[] arr) {
    int max = arr[0];
    int secondMax = arr[1];
    for(int i = 2;i < arr.length;i++) {
    if (arr[i] > max) {
    secondMax = max;
    max = arr[i];
    } else if (arr[i] > secondMax) {
    secondMax = arr[i];
    }
    }
    return secondMax;
    }

    • @nyashodhara7290
      @nyashodhara7290 Год назад

      if array is {4,5} your code will return secondMax as 5

    • @addytechie6948
      @addytechie6948 Год назад

      @@nyashodhara7290 can u explain this how plz??

    • @nyashodhara7290
      @nyashodhara7290 Год назад +3

      @@addytechie6948 if array is {4,5}
      max = 4;
      secondMax =5;
      for(int i = 2;i < arr.length;i++) //here i is 2 which is less than 2(array.length) so for loop breaks
      finally it return secondMax as 5.

    • @miasevda4728
      @miasevda4728 2 месяца назад

      ​@@nyashodhara7290
      Sir i think that works if we put
      max = arr[0]
      SecondMax = arr[0]
      And begin the loop for with i=1
      What do you think please ?

  • @imonem2352
    @imonem2352 Год назад

    the algorithm doesn't work if the array starts with 33

  • @NikhilSingh-ss3ho
    @NikhilSingh-ss3ho 2 года назад +1

    For maximum value?

    • @itsdineshvaryani
      @itsdineshvaryani  2 года назад

      just compare adjacent values and return max once loop ends ..

  • @soumyadipmajumder902
    @soumyadipmajumder902 Год назад +1

    Enjoying your course sir💌🤟
    /* Find the second maximum value in an array */
    // method to find the second maximum value
    static int find_second_maximum_value(int[] arr) {
    int max=0;
    int secondMax=0;
    for (int j : arr) {
    if (j > max) {
    secondMax = max;
    max = j;
    } else if (j > secondMax && j != max) {
    secondMax = j;
    }
    }
    return secondMax;
    }
    // method to print the array
    static void printArray(int[] yourArray) {
    for (int x:yourArray) {
    System.out.print(x+"|");
    }
    }
    // main method
    @SuppressWarnings({"static-access"})
    public static void main(String[] args)
    {
    int[] myArray = {44,57,56,25,24,89,84,26,10,49,11,98,100,55,99};
    System.out.println("__Original Array__");
    q6 q = new q6();
    q.printArray(myArray);
    System.out.println();
    System.out.println("Second maximum element in this array is: "+q.find_second_maximum_value(myArray));
    }

    • @_bavanla.io_
      @_bavanla.io_ Год назад

      Thanks for Code. but works only for positive integers. @Dinesh code works for both +ve and -ve.

  • @atassun
    @atassun 3 года назад +3

    pls find the solution
    public static int findSecondMax(int[] input) {
    int firstMax = input[0];
    int secondMax = input[1];
    for (int i = 2; i < input.length; i++) {
    if (input[i] > firstMax) {
    if (firstMax > secondMax) {
    secondMax = firstMax;
    }
    firstMax = input[i];
    } else if (input[i] > secondMax) {
    secondMax = input[i];
    }
    }
    return secondMax;
    }