w3resource

Java: Negative Dominant


78. Check if array is negative dominant

When an array has more unique negative values than unique positive values, it is negative dominant.

Write a Java program that checks whether an array is negative dominant or not. If the array is negative dominant return true otherwise false.

Sample Data:
{1,-2, -5,-4,3,-6} -> true
{1, 2, 5, -4, 3, -6} -> false
{1, 2, -5, -4, -3, 6} -> false

Pictorial Presentation:

Java Array Exercises: Negative Dominant.


Sample Solution-1:

Java Code:

// Import necessary Java classes.
import java.util.Scanner;
import java.util.Arrays;

// Define the 'Main' class.
public class Main {
  // Define the main method for running the program.
  public static void main(String[] args) {
    // Initialize an array of numbers.
    int[] nums = {1, -2, -5, -4, 3, -6};
    // Alternative input arrays:
    // int[] nums = {1, 2, 5, -4, 3, -6};
    // int[] nums = {1, 2, -5, -4, -3, 6};
    System.out.printf("\nOriginal array of numbers:\n" + Arrays.toString(nums));

    // Call the 'test' method to check for negative dominance in the array.
    boolean result = test(nums);
    System.out.printf("\nCheck Negative Dominance in the said array! " + result);
  }

  // Define the 'test' method to check for negative dominance in the array.
  public static boolean test(int[] nums) {
    long count_negative, count_positive;
    // Remove duplicate values from the 'nums' array using the 'distinct' method.
    nums = Arrays.stream(nums).distinct().toArray();
    // Count the number of negative and positive elements in the array.
    count_negative = Arrays.stream(nums).filter(s -> s < 0).count();
    count_positive = Arrays.stream(nums).filter(s -> s > 0).count();
    // Return 'true' if there are more negative elements, indicating negative dominance.
    return count_negative > count_positive;
  }
} 

Sample Output:

Original array of numbers:
[1, -2, -5, -4, 3, -6]
Check Negative Dominance in the said array!true

Flowchart:

Flowchart: Negative Dominant.


Sample Solution-2:

Java Code:

// Import necessary Java classes.
import java.util.Scanner;
import java.util.Arrays;
import java.util.stream.IntStream;

// Define the 'Main' class.
public class Main {
  // Define the main method for running the program.
  public static void main(String[] args) {
    // Initialize an array of numbers.
    int[] nums = {1, -2, -5, -4, 3, -6};
    // Alternative input arrays:
    // int[] nums = {1, 2, 5, -4, 3, -6};
    // int[] nums = {1, 2, -5, -4, -3, 6};
    System.out.printf("Original array of numbers:\n" + Arrays.toString(nums));

    // Call the 'test' method to check for negative dominance in the array.
    boolean result = test(nums);
    System.out.printf("\nCheck Negative Dominance in the said array! " + result);
  }

  // Define the 'test' method to check for negative dominance in the array.
  public static boolean test(int[] nums) {
    // Use IntStream to process the array and check for negative dominance.
    return IntStream.of(nums).distinct().filter(element -> element < 0).count() >
           IntStream.of(nums).distinct().filter(element -> element > 0).count();
  }
} 

Sample Output:

Original array of numbers:
[1, -2, -5, -4, 3, -6]
Check Negative Dominance in the said array!true

Flowchart:

Flowchart: Negative Dominant.


For more Practice: Solve these Related Problems:

  • Write a Java program to check if an array is positive dominant.
  • Write a Java program to count the number of positive and negative numbers in an array.
  • Write a Java program to check whether the sum of negative numbers in an array is greater than the sum of positive numbers.
  • Write a Java program to find the largest negative number and smallest positive number in an array.

Go to:


PREV : Check if array alternates between positive and negative.
NEXT : Find missing letter in a sequence of consecutive letters.

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.