Printing Largest and Smallest of N numbers without using Array in Java

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.

How to find Largest and Smallest of N numbers without using Array in 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 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.

Some more array based coding problems for Programmers
  • How to find the missing number in an array of 1 to 100? (solution)
  • How to find duplicate numbers on  integer array? (solution)
  • How to find largest and smallest number in the unsorted array? (solution)
  • How to reverse an array in place in Java? (solution)
  • How to find all pairs in integer array whose sum is equal to 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 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. - 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.

3 comments :

Ashish Agarwal said...

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

Anonymous said...

if (current > largest) {
largest = current;
} if (current < smallest) {
smallest = current;

Javin Paul said...

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

Post a Comment