Java Program to Check if an Array of Integers without 0 and 1

In the previous article, we have seen Java Program to Find the Difference between Largest and Smallest Element of an Array of Integer

In this article we will see how we can check if an array of integers without 0 and 1.

Java Program to Check if an Array of Integers without 0 and 1

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to check if an array of integers without 0 and 1.

Method-1: Java Program to Check if an Array of Integers without 0 and 1 By Static Initialization of Array Elements

Approach:

  1. Initialize the array.
  2. Iterate through the array and check if any element is equal to 0 or 1 then return false.
  3. Else return true.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize the array
        int[] arr = { 9, 2, 3, 7, 5, 6 };
        System.out.println("The array is : ");
        //calling printArray() method
        printArray(arr);
        // calling the checkNoZeroOne() method
        if(checkNoZeroOne(arr))
            System.out.println("Array of integers without 1 and 0");
        else
            System.out.println("Array of integers with 1 or 0");
    }
    
    //checkNoZeroOne() method to check if array contains 1 or 0
    public static boolean checkNoZeroOne(int[] arr) {
        // iterate through the array
        for (int i : arr) {
            // if the element is 0 or 1, return false
            if (i == 0 || i == 1) 
            {
                return false;
            }

        }
        // if the loop completes, return true
        return true;
    }
    
    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}
Output:

The array is : 
9 2 3 7 5 6 
Array of integers without 1 and 0

Method-2: Java Program to Check if an Array of Integers without 0 and 1 By Dynamic Initialization of Array Elements

Approach:

  1. Take the input of array length from user.
  2. Then take the input of array elements from user.
  3. Iterate through the array and check if any element is equal to 0 or 1 then return false.
  4. Else return true.

Program:

import java.util.*;

public class Main
{
    public static void main(String[] args) 
    {
        // Create a Scanner class object
        Scanner sc = new Scanner(System.in);
        // ask the user for number elements in the array
        System.out.println("Enter number of elements in the array: ");
        int n = sc.nextInt();
        // create an array of size n
        int[] arr = new int[n];
        System.out.println("Enter the array elements: ");
        // ask the user  to enter array elements
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }


        System.out.println("The array is : ");
        //calling printArray() method
        printArray(arr);
        // calling the checkNoZeroOne() method
        if(checkNoZeroOne(arr))
            System.out.println("Array of integers without 1 and 0");
        else
            System.out.println("Array of integers with 1 or 0");
    }
    
    //checkNoZeroOne() method to check if array contains 1 or 0
    public static boolean checkNoZeroOne(int[] arr) {
        // iterate through the array
        for (int i : arr) {
            // if the element is 0 or 1, return false
            if (i == 0 || i == 1) 
            {
                return false;
            }

        }
        // if the loop completes, return true
        return true;
    }
    
    //printArray() method to print the array 
    static void printArray(int[] arr) 
    { 
        // printing array 
        for (int i=0; i<arr.length; i++) 
        { 
            System.out.print(arr[i] + " "); 
        } 
        System.out.println("");
    }
}
Output:

Case-1

Enter number of elements in the array: 5
Enter the array elements: 2 5 1 6 4 
The array is : 
2 5 1 6 4 
Array of integers with 1 or 0

Case-2

Enter number of elements in the array: 5
Enter the array elements: 2 5 3 6 4 
The array is : 
2 5 1 6 4 
Array of integers without 1 and 0

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Also Read: Java Program to Check if One Array is Subset of Another Array or Not

Related Java Programs: