Find index of 0 or Any Negative Integer Element Present in Array in Java


As per the problem statement, we have given an array with some random integer values and we have to find out and print the index which contains any zero or negative values.

Note - Take an integer array

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Given Array= [1, 2, -3, -4, 0, 5]

The index contains Zero and negative values = 2, 3, 4

Instance-2

Given Array= [-1, 0, 4, 6, 8, -5]

The index contains Zero and negative values = 0, 1, 5

Instance-3

Given Array= [-2, 3, -9, 12, 0, -7]

The index contains Zero and negative values = 0, 2, 4, 5

Algorithm

Step 1 −Declare an array with some random integer values by static input method.

Step 2 −Take a for loop in which we will iterate all the elements and check for zero or negative values at every iteration.

Step 3 −If we get any zero values or negative values, we print that index number as output.

Step 4 − If we do not get any of zero or negative value in our array then we print as “N/A”.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it-

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Method

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Method

In this approach, we declare an array with some random integer values and by using our algorithm we find the zero and negative values and print the respective index numbers as output.

Example

import java.util.*;
public class Main {
   public static void main(String[] args){
      
      // declare an integer type of array and store some random value to it by static input method
      int[] inputArray = {-34, 25, 7, 0, 9};
      
      //declare a integer variable to store the count value
      int count=0;
      
      //print the output
      System.out.print("The indexes which contain Zero and negative values = ");
      
      //initiate the loop to find the indexes
      for(int i=0; i< inputArray.length; i++){
         if(inputArray[i] <= 0){
            
            // Print the indexes
            count+=1;
            System.out.print(i+" ");
         }
      } 
      
      //if the array doesn't contain any zro or negative values
      if(count==0)
      System.out.print("The array does not contain any negative or 0 value");
   }
}

Output

The indexes which contain Zero and negative values = 0 3

Approach-2: By Using User Defined Method

In this approach, we declare an array with some random integer values and pass that array as a parameter to our user defined method and in user defined method using the algorithm, we find the indexes which contain zero or negative values and print those index values as output.

Example

import java.util.*;
public class Main {
   public static void main(String[] args){
      
      // declare an integer type array and initialize it
      int[] inputArray = { -34, 0, 25, -67 , 87};
      // call the user-defined method and pass the inputArray[]
      printIndex(inputArray);
   }
   //user-defined method to print the indexes which contains zero or negative values
   static void printIndex(int[] inpArr){
      int count=0;
      //print the output
      System.out.print("The index contains Zero and negative values = ");
      //take a for loop to iterate and find the indexes
      for(int i=0; i< inpArr.length; i++){
         if(inpArr[i]<=0){
            // Print the array as output
            count+=1;
            System.out.print(i+" ");
         }
      } 
            
      //print if the array doesn't contain any zro or negative values
      if(count==0)
      System.out.print(" N/A");
   }
}

Output

The index contains Zero and negative values = 0 1 3

In this article, we explored different approaches to find index of 0 or any negative element by using Java programming language.

Updated on: 06-Mar-2023

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements