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

preview_player
Показать описание
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:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion:

-~-~~-~~~-~~-~-
========================================================
Please watch: "Selenium & Automation Interview Preparation - By Naveen AutomationLabs"
-~-~~-~~~-~~-~- Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

Paid courses (Recorded) videos:
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------

✔️SOCIAL NETWORKS
--------------------------------

Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️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:
-------------------------------

✔️SOCIAL NETWORKS
--------------------------------

Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
Рекомендации по теме
Комментарии
Автор

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

SmartProgramming
Автор

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


andreysytnik
Автор

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

dharmendharjarwani
Автор

Thank-you so much for such a wonderful explanation

saileshramesh
Автор

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

saikumarperamsetty
Автор

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

ionadsouza
Автор

Why u used Arrays.toString(numbers) here

dissCOMFORTZONE
Автор

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?

kousalya
Автор

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!

ABAutomationHub
Автор

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("Largest number is "+a[a.length-1]);
System.out.println("Smallest number is "+a[0]);

happytripathi
Автор

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<=op.length;i++){
For(int j=1;j<=op.length;j++){
If(op[i]>op[j]){
System.out.println(op[i]);}
Else{
System.out.println(op[j]);}

Umasalve-tchf
Автор

//One more solution
public class LargestSmallestNum {
public static void main(String[] args) {
int[]a={45, 8, -78, 65, 187};
Set<Integer>i=new TreeSet<Integer>();
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]);
}
}

Mspavan
Автор

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

palaksinghal
Автор

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

kenetart
Автор

Check against Integer.MinValue instead of array first element

Shishiraithal
Автор

Very well explained Naveen as usual, tons of thank you

artimandale
Автор

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

khushbuvyas
Автор

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”

Ajit-kdoi
Автор

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

mariajency
Автор

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

Rashmi_Chauhan