One of the common programming questions is, how do you find the largest and smallest number in N numbers without using arrays in Java? Can you write a program to solve this problem? Well, it's very similar to the problem we have seen before, find the largest and smallest of 3 integers. You can use the same approach and generalize it for N numbers. All you need to do is start with largest as Integer.MIN_VALUE and smallest number as Integer.MAX_VALUE and loop through N numbers. At each iteration, compare the number with the largest and smallest number, if the current number is larger than the largest then it's a new largest, and if the current number is smaller than the smallest then it's a new smallest number.
Once you finish the iteration, you have the largest and smallest number without using an array. You can also assign the largest number as zero if there is no negative number in your input, but it won't work if the user can enter both negative and positive integers.
To make this program more interactive, you can accept all N numbers from the user from the command line by using Scanner. The user will keep entering an integer number on each iteration and you would be printing the largest and smallest number.
Alternatively, you can get all N numbers in a List and loop through that list to display the largest and smallest numbers.
Also, basic knowledge of essential data structure is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course from Udemy and Couerara to fill the gaps in your understanding.
If you are sure that your input will only be a positive number then you can initialize the largest with zero instead of Integer.MIN_VALUE.
Once you finish the iteration, you have the largest and smallest number without using an array. You can also assign the largest number as zero if there is no negative number in your input, but it won't work if the user can enter both negative and positive integers.
To make this program more interactive, you can accept all N numbers from the user from the command line by using Scanner. The user will keep entering an integer number on each iteration and you would be printing the largest and smallest number.
Alternatively, you can get all N numbers in a List and loop through that list to display the largest and smallest numbers.
Also, basic knowledge of essential data structure is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course from Udemy and Couerara to fill the gaps in your understanding.
Java Program to find largest and smallest of N numbers without arrays
Here is our sample program to find the smallest and largest of N integers without using an array. This program handles both positive and negative numbers, hence the largest value is initialized with Integer.MIN_VALUE and smallest number are initialized with Integer.MAX_VALUE.If you are sure that your input will only be a positive number then you can initialize the largest with zero instead of Integer.MIN_VALUE.
The program is simple, just take input from the user about how many numbers and then use a for loop to get that many numbers as input by using Scanner.nextInt() method.
At the same time, we also keep comparing the largest and smallest numbers with the value entered by the user, so that at the end of the loop we have the largest and smallest number from the set of values entered by users.
Btw, if you are preparing coding problems for interviews, to get your first job, or looking for a new job, you should check out these coding interview books, it contains around 190 programming questions and their solution from various tech interviews. It will give you both common questions and experience on how to solve them.
At the same time, we also keep comparing the largest and smallest numbers with the value entered by the user, so that at the end of the loop we have the largest and smallest number from the set of values entered by users.
Btw, if you are preparing coding problems for interviews, to get your first job, or looking for a new job, you should check out these coding interview books, it contains around 190 programming questions and their solution from various tech interviews. It will give you both common questions and experience on how to solve them.
Java Program to print largest and smallest of N numbers
import java.util.Scanner; /* * Java Program to find the largest and smallest of N numbers * without using arrays. */ public class LargestOfN { public static void main(String[] args) { System.out.println("Welcome to Java Program to find " + "largest and smallest number without using array"); System.out.println("Please enter value of N: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int largest = Integer.MIN_VALUE; int smallest = Integer.MAX_VALUE; System.out.printf("Please enter %d numbers %n", n); for (int i = 0; i < n; i++) { int current = sc.nextInt(); if (current > largest) { largest = current; } if (current < smallest) { smallest = current; } } System.out.println("largest of N number is : " + largest); System.out.println("smallest of N number is : " + smallest); } }
You can see that our program has correctly printed the largest and smallest number from the 10 numbers entered by the user. You can also run this program and further check with 20, 30, or any value of N. You can also enter negative numbers to verify if it is calculating largest and smallest correctly with negative values or not.
That's all about how to calculate the largest and smallest value in N numbers without using arrays. It's an interesting exercise and teaches you how to use the if-else statements to solve the problem.
If you are a beginner programmer and doing these exercises to build your code sense and programming logic, you can also check out the following problems for further practice.
Some more array based coding problems for Programmers
P.S. - I have shared some books to prepare programming/coding interviews, you can check that list to boost your preparation.
P.S S.. - For Java developers, preparing just coding-based problems won't be enough, you need to also prepare theoretical questions based upon concepts of Java programming language and frameworks like Spring and Hibernate. I have a separate list of books for Java JEE interviews, check that list to prepare well.
- How to find the missing number in an array of 1 to 100? (solution)
- How to find duplicate numbers on an integer array? (solution)
- How do find the largest and smallest numbers in the unsorted array? (solution)
- How to reverse an array in place in Java? (solution)
- 100+ data structure questions with answers (data structure questions)
- 75 Programming interview questions with answers (programming questions)
- 10 Dynamic Programming problems for practice (dynamic programming problems)
- 25 Recursion coding problems for practice (recursion problems)
- How to find all pairs in an integer array whose sum is equal to a given number? (solution)
- How to find duplicates if an array contains multiple duplicate elements? (solution)
- How to remove duplicate elements from the array in Java? (solution)
- How to find the top two numbers from an int array? (solution)
- How to remove duplicate elements from the array in place? (solution)
- How to sort an array using the Quicksort algorithm? (solution)
P.S. - I have shared some books to prepare programming/coding interviews, you can check that list to boost your preparation.
P.S S.. - For Java developers, preparing just coding-based problems won't be enough, you need to also prepare theoretical questions based upon concepts of Java programming language and frameworks like Spring and Hibernate. I have a separate list of books for Java JEE interviews, check that list to prepare well.
And lastly one question for you? How do you find if a given String contains only unique characters and no duplicates? This one is also a popular interview question and If you know let me know your approach and code in comments, all the best !!.
7 comments :
if i give input n=4 and 1,2,3,4 then output will be
largest of N number is : 4
smallest of N number is : 2147483647
but it should be
largest of N number is : 4
smallest of N number is : 1
We can use else instead of else if
if (current > largest) {
largest = current;
} if (current < smallest) {
smallest = current;
@Ashish and @Anonymous, good catch, it should be either else or just a simple if instead of else if. Corrected now. Thanks for pointing it out.
I think this algorithm is incorrect. Because i entered 4 7 2 1 8 like that and
result is
largest of N number is : -2147483648
smallest of N number is : 1
hi, may i know whats the reasoning for largest= Integer.MIN_VALUE and smallest= Integer.MIN_VALUE
Hello Clement, we are starting with the smallest possible value for integer in Java.
how it is finding the min and max value as we are entering no for example 3 4 6 , it has to be store all the no in largest and smallest because all the no we are entering it is less than the Integer.MaxValue and greater than the Integer.MinValue. So why only 6 no is saving in largest variable not in smallest variable as it is also lesser than Integer.MAXVALUE! so it has to also store in smallest variable but this is not happening.
Post a Comment