Saturday, July 2, 2016

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 largest than its a new largest and if the current number is smaller than smallest than its 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 largest and smallest number.

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 number, hence 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 uses 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 the Cracking the Code Interview, 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.

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 its is calculating largest and smallest correctly with negative values or not.

That's all about how to calculate 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 exercise to build your code sense and programming logic, you can also check out following problems for further practice.

P.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 framework like Spring and Hibernate. I have a separate list of books for Java JEE interviews, check that list to prepare well.