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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 พ.ย. 2017
  • Largest & Smallest Numbers In Array in Java:
    Here is the logic:
    A ‘for loop’ is used which goes from 1 to the array length. Within this loop the largest and the smallest value is detected and initialized to the smallest and largest value uisng if()
    When …. numbers[i] is greater than largetst
    largetst = numbers[i];
    when numbers[i] greater than smallest
    smallest = numbers[I];
    ======================================================
    Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
    th-cam.com/users/Naveen%20Au...
    Follow me on my Facebook Page:
    / naveenqtpexpert
    Let's join our Automation community for some amazing knowledge sharing and group discussion:
    t.me/joinchat/COJqZQ4enmEt4JA...
    ~-~~-~~~-~~-~
    Follow my Site/Blog: www.naveenautomationlabs.com
    ========================================================
    Please watch: "Selenium & Automation Interview Preparation - By Naveen AutomationLabs"
    • Selenium & Automation ...
    ~-~~-~~~-~~-~ Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
    th-cam.com/users/Naveen%20Au...
    Follow me on my Facebook Page:
    / naveenqtpexpert
    Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
    t.me/joinchat/COJqZUPB02r5sB7...
    Paid courses (Recorded) videos:
    Java & Selenium Course: www.naveenautomationlabs.com/p...
    API Course: www.naveenautomationlabs.com/p... ➡️Get Our Courses✔️
    📗 Get My Paid Courses at
    Paid courses (Recorded) videos:
    Java & Selenium Course: www.naveenautomationlabs.com/p...
    API Course: www.naveenautomationlabs.com/p...
    -------------------------------
    ✔️SOCIAL NETWORKS
    Facebook: / naveenqtpexpert
    Twitter: / naveenkhunteta
    Blog: www.naveenautomationlabs.com
    --------------------------------
    Support My Channel✔️Or Buy Me A Coffee
    Paypal: paypal.me/naveenkhunteta
    Google Pay: naveenanimation20@gmail.com
    --------------------------------
    ✔️Thanks for watching!
    देखने के लिए धन्यवाद
    Благодаря за гледането
    感谢您观看
    Merci d'avoir regardé
    Grazie per la visione
    Gracias por ver
    شكرا للمشاهدة ➡️Get Our Courses✔️
    📗 Get My Paid Courses at
    Paid courses (Recorded) videos:
    Java & Selenium Course: www.naveenautomationlabs.com/p...
    API Course: www.naveenautomationlabs.com/p...
    -------------------------------
    ✔️SOCIAL NETWORKS
    Facebook: / naveenqtpexpert
    Twitter: / naveenkhunteta
    Blog: www.naveenautomationlabs.com
    --------------------------------
    Support My Channel✔️Or Buy Me A Coffee
    Paypal: paypal.me/naveenkhunteta
    Google Pay: naveenanimation20@gmail.com
    --------------------------------
    ✔️Thanks for watching!
    देखने के लिए धन्यवाद
    Благодаря за гледането
    感谢您观看
    Merci d'avoir regardé
    Grazie per la visione
    Gracias por ver
    شكرا للمشاهدة
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @happytripathi1015
    @happytripathi1015 19 วันที่ผ่านมา

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

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

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

    Thank-you so much for such a wonderful explanation

  • @artimandale8286
    @artimandale8286 2 หลายเดือนก่อน

    Very well explained Naveen as usual, tons of thank you

  • @kousalya1508
    @kousalya1508 4 ปีที่แล้ว +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?

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

    simplest and most meaningful explanation ever

  • @mariajency8008
    @mariajency8008 5 ปีที่แล้ว

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

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

    Great explanation! 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”

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

    Thank you, this was very helpfull

  • @ionadsouza1066
    @ionadsouza1066 9 หลายเดือนก่อน

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

  • @Biswajit115
    @Biswajit115 6 ปีที่แล้ว

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

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

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

  • @user-ns8ht9tu3d
    @user-ns8ht9tu3d 4 หลายเดือนก่อน

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

  • @venkatkrishna266
    @venkatkrishna266 5 ปีที่แล้ว

    can you create
    videos for merge sort

  • @andreysytnik4547
    @andreysytnik4547 5 ปีที่แล้ว +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 4 ปีที่แล้ว +1

      Hey thanks for sharing this.Its really simple.

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

      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 19 วันที่ผ่านมา

      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]);

  • @aparnapulipati7843
    @aparnapulipati7843 6 ปีที่แล้ว

    Very helpful thank you

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

    Toggle to Debug make sense clear!

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

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

    thank you sir!

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

    Why u used Arrays.toString(numbers) here

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

    Thank you

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

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

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

      mine always show smallest number is 0

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

    What if the array is empty ?

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

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

    Nice

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

    Check against Integer.MinValue instead of array first element

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

      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.

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

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

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

    thank you sooooo much

  • @SRD_AllInOne0331
    @SRD_AllInOne0331 6 ปีที่แล้ว

    how to find 2nd highest number to given array

    • @latadoddi5638
      @latadoddi5638 6 ปีที่แล้ว

      array.length-2

    • @batman-iw6tx
      @batman-iw6tx ปีที่แล้ว

      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);

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

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

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

    For more visit :
    th-cam.com/video/mUTLg9poUL8/w-d-xo.html

  • @Mspavan32
    @Mspavan32 5 ปีที่แล้ว

    //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]);
    }
    }

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

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

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