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]);
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]);
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));
@@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);
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]);
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!
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”
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
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.
//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]); } }
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);
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]);
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]);
thanks sir , you explained very well, each and every debugging point wise, thanks a lot , God Bless You For Free Helping Tutorials .
reallly awesome explanation sir, create more videos on arrays, thank you, keep it up 👍👍🙂🙂🙂
simplest and most meaningful explanation ever
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));
nice
Hey thanks for sharing this.Its really simple.
This works for lists not arrays
@@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);
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]);
Thank-you so much for such a wonderful explanation
Very well explained Naveen as usual, tons of thank you
Hey Naveen u can do video for How to Find Duplicates Elements in Normal Int arrays?
Toggle to Debug make sense clear!
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?
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!
Sure Anshita, I'll try to cover such questions. If you have any specific questions with you, please mail me the same. Thanks!
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”
Thank you so much Naveen, your videos are very useful to me
If we have to sort the same array where negative and positive numbers would be grouped. Would this logic work ?
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
Great explanation! Thanks
please make a video on how to check whether a number is prime or not...
Thank you, this was very helpfull
what if you used an array +scanner? like you’re gonna input a value pls reply asap. thank you!
mine always show smallest number is 0
Check against Integer.MinValue instead of array first element
Should they ask the Java Question like this for automation testing for fresher?
Yes they will. So be prepared
Thanks👍.. will Prepare...
For more visit :
ua-cam.com/video/mUTLg9poUL8/v-deo.html
why the smallest value is always 0 when I input values in an Array?
can you create
videos for merge sort
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
Thank you very much. Question what if you have string and int within the array. How would you solve the same problem?
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.
Why u used Arrays.toString(numbers) here
Very helpful thank you
What if the array is empty ?
int largest = INTEGER.MIN_VALUE;
int smallest = INTEGER.MAX_VALUE;
I can directly use Streams 😁, but in interview we have to write full code
//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]);
}
}
Thank you
how to find 2nd highest number to given array
array.length-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);
Nice
thank you sooooo much
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);
We don't want to use existing library methods of java. Here interviewer is expecting logic not the direct methods.
thank you sir!