Largest & Smallest Numbers In Array - Java Interview Questions - 6

Поділитися
Вставка
  • Опубліковано 9 січ 2025

КОМЕНТАРІ • 53

  • @happytripathi1015
    @happytripathi1015 6 місяців тому +1

    Nice explanation. One more simple method is we can sort the array and the 1st element will be smaller and the last element will be largest
    int a[]= {100,5,9,2,150,122,2,5};
    Arrays.sort(a);//sorting elements
    System.out.println(Arrays.toString(a));
    System.out.println("Largest number is "+a[a.length-1]);
    System.out.println("Smallest number is "+a[0]);

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

    Thanks Naveen,
    Always enjoy watching your videos !!!
    Just to add on above , another way to make it easy:-
    Arrays.sort(numbers);
    System.out.println("Smallest num is :- "+ numbers[0] + " Largest num is :- "+ numbers[numbers.length-1]);

  • @dharmendharjarwani3951
    @dharmendharjarwani3951 5 років тому +3

    thanks sir , you explained very well, each and every debugging point wise, thanks a lot , God Bless You For Free Helping Tutorials .

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

    reallly awesome explanation sir, create more videos on arrays, thank you, keep it up 👍👍🙂🙂🙂

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

    simplest and most meaningful explanation ever

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

    Hi Naveen! Always enjoy watching your videos!!!
    Seems to me this is the easiest way :-))
    List lst = new ArrayList (Arrays.asList(12, 25, -45, 88, -125, 78954));
    System.out.println(Collections.max(lst));
    System.out.println(Collections.min(lst));

    • @Nancy-xd7bp
      @Nancy-xd7bp 5 років тому

      nice

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

      Hey thanks for sharing this.Its really simple.

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

      This works for lists not arrays

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

      @@mouhamadzabaneh2396
      int arr[] = {-10,1,2,5,-88,45,23,999};
      List list = new ArrayList();
      for(int i : arr){
      list.add(i);
      }
      int smallest = Collections.min(list);
      int largest = Collections.max(list);
      System.out.println(largest + " " + smallest);

    • @happytripathi1015
      @happytripathi1015 6 місяців тому

      by sorting the array it will be more simpler to find smallest and largest
      int a[]= {100,5,9,2,150,122,2,5};
      Arrays.sort(a);//sorting elements
      System.out.println(Arrays.toString(a));
      System.out.println("Largest number is "+a[a.length-1]);
      System.out.println("Smallest number is "+a[0]);

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

    Thank-you so much for such a wonderful explanation

  • @artimandale8286
    @artimandale8286 8 місяців тому

    Very well explained Naveen as usual, tons of thank you

  • @saikumarperamsetty
    @saikumarperamsetty 10 місяців тому

    Hey Naveen u can do video for How to Find Duplicates Elements in Normal Int arrays?

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

    Toggle to Debug make sense clear!

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

    Hi Naveen, Thanks for the video. Can you please explain in detail why we put O(n) for Time complexity if we have only one for loop?

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

    Thanks for covering this..It's very famous interview question!! Can you plz prepare some more videos on java programs which are asked these days(Example - Stock Problem where stocks fluctuate whole day and we have to find max profit ).Although, you have covered some imp questions in your java interview questions playlist but can you plz prepare some more..It would be really useful as you explain in very simple way nd it's really easy to understand ur code!

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

      Sure Anshita, I'll try to cover such questions. If you have any specific questions with you, please mail me the same. Thanks!

  • @Ajit-kd9oi
    @Ajit-kd9oi 6 років тому

    Hi Naveen,
    First of all, Many thanks for your effort. It's really appreciable.
    Here's a question I got, I don't know how relevant to post it here . I'm sorry for that. But could you please help me understand the logic of the below question.
    Q. There is an array with 3 strings. Each string is of the format: a+b-c+d.., where a, b, c, d are single-digit integers and the operation symbols can be + , - or *
    The length of the expression can vary between 3 operations to 6 operations.
    Write a program that takes the array of strings as input, evaluates each one of them and returns the string with the smallest value.
    Ex 1: [“1+2+3+4+5”, “3+4-8+4-9-1”, “2+4-5+6”]
    returns “3+4-8+4-9-1”
    Ex 2: [“5+4+9”, “1+4*6-8”, “9+2*1-2”]
    Returns “9+2*1-2”

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

    Thank you so much Naveen, your videos are very useful to me

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

    If we have to sort the same array where negative and positive numbers would be grouped. Would this logic work ?

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

    Hey Naveen
    Awesome Way of explanation.Can you please add some more important programmes from aaray and string which is mostly asked in any interview

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

    Great explanation! Thanks

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

    please make a video on how to check whether a number is prime or not...

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

    Thank you, this was very helpfull

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

    what if you used an array +scanner? like you’re gonna input a value pls reply asap. thank you!

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

      mine always show smallest number is 0

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

    Check against Integer.MinValue instead of array first element

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

    Should they ask the Java Question like this for automation testing for fresher?

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

    For more visit :
    ua-cam.com/video/mUTLg9poUL8/v-deo.html

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

    why the smallest value is always 0 when I input values in an Array?

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

    can you create
    videos for merge sort

  • @Umasalve-tc1hf
    @Umasalve-tc1hf Рік тому

    Hi Naveen,thank you so much for your valuable and great effort. Could you please help me to know that if we write like below code:
    Int op[]={3,4,1,6,7};
    For(int i=0;i

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

    Thank you very much. Question what if you have string and int within the array. How would you solve the same problem?

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

      An array, in the context of Java, is a dynamically-created object that serves as a container to hold constant number of values of the same type.
      You can store only homogeneous i.e of same data type elements in Array either int, String etc.

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

    Why u used Arrays.toString(numbers) here

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

    Very helpful thank you

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

    What if the array is empty ?

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

    int largest = INTEGER.MIN_VALUE;
    int smallest = INTEGER.MAX_VALUE;

  • @spider-maniac300
    @spider-maniac300 10 днів тому

    I can directly use Streams 😁, but in interview we have to write full code

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

    //One more solution
    public class LargestSmallestNum {
    public static void main(String[] args) {
    int[]a={45,8,-78,65,187};
    Seti=new TreeSet();
    for(int k:a){
    i.add(k);
    }
    Object[]x=i.toArray();
    System.out.println("Smallest Num is = " +x[0]);
    System.out.println("Largest Num is = " +x[x.length-1]);
    }
    }

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

    Thank you

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

    how to find 2nd highest number to given array

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

      array.length-2

    • @batman-iw6tx
      @batman-iw6tx 2 роки тому

      int [] numbers= {24,10,88,987,5,66};
      int largest=Integer.MIN_VALUE; //-2147483648
      int secondLargest= Integer.MIN_VALUE;

      for(int i=0;ilargest)
      {
      secondLargest= largest;
      largest= numbers[i];
      }
      else if(numbers[i]>secondLargest && numbers[i]!= largest)
      {
      secondLargest=numbers[i];
      }
      }

      System.out.println("secondLargest : "+ secondLargest);

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

    Nice

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

    thank you sooooo much

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

    There are much better ways to solve this.
    1) If you don't care about 2 loops
    System.out.println("Max: " + Arrays.stream(numbers).max().getAsInt());
    System.out.println("Min: " + Arrays.stream(numbers).min().getAsInt());
    2) If you do care about number of loops
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (int number : numbers) {
    min = Math.min(min, number);
    max = Math.max(max, number);
    }
    System.out.println("Max: " + max);
    System.out.println("Min: " + min);

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

      We don't want to use existing library methods of java. Here interviewer is expecting logic not the direct methods.

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

    thank you sir!