Java Program to Find and Display All Negative Subarrays

 Java Programming:

Write a program that displays the "Negative subarrays".

Java Program to Find and Display All Negative Subarrays


Note: Subarrays are contiguous chunks of the main array. For example if the array is {1,2,3,5} then some of the subarrays are {1}, {1,2,3}, {2,3,5}, {1,2,3,5} etc. But {1,2,5} is not an subarray as it is not contiguous. A sub-array is "Negative" if all the integers in that sub-array is negative.

Your code will prompt for length of the array and it will read the N integers. Next, find all negative sub-arrays and display the results. The order of the subarrays is NOT important when you are displaying.


Example Run #1:

Enter array length: 5
Enter array elements: 1 -2 3 -4 5
Negative sub-arrays of the given array are: 
-2 
-4 

Example Run #2:

Enter array length: 6
Enter array elements: 6 -2 -8 -10 -7 5
Negative sub-arrays of the given array are: 
-2 
-8 
-10 
-7
-2 -8 
-8 -10 
-10 -7 
-2 -8 -10
-8 -10 -7
-2 -8 -10 -7

Example Run #3:

Enter array length: 10 
Enter array elements: -2 -5 -1 2 -3 0 1 -7 -4 -6
Negative sub-arrays of the given array are: 


-2 

-5 

-1 

-3 

-7 

-4 

-6 

-2 -5 

-5 -1 

-7 -4 

-4 -6 

-2 -5 -1 

-7 -4 -6 


The answer

Description:

This program reads an array of integers from the user and identifies all "negative subarrays"—that is, all contiguous subarrays where every element is negative. The program first prompts for the array length and elements, then prints each negative subarray on its own line.


Explanation:

A subarray is any sequence of contiguous elements from the array.
The program checks every possible subarray (from every possible starting point to every possible endpoint) and prints those where all elements are negative.

Steps:

  1. Prompt for array length and read the array elements.

  2. For every possible subarray, check if all elements are negative.

  3. If so, print that subarray.


Java Code:

java

import java.util.Scanner; public class NegativeSubarrays { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Input length and elements System.out.print("Enter array length: "); int n = in.nextInt(); int[] arr = new int[n]; System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } System.out.println("Negative sub-arrays of the given array are: "); boolean foundAny = false; // Check all subarrays for (int start = 0; start < n; start++) { for (int end = start; end < n; end++) { boolean allNegative = true; for (int k = start; k <= end; k++) { if (arr[k] >= 0) { allNegative = false; break; } } if (allNegative) { foundAny = true; // Print the subarray for (int k = start; k <= end; k++) { System.out.print(arr[k]); if (k < end) System.out.print(" "); } System.out.println(); } } } // If no negative subarrays were found if (!foundAny) { // Just prints an empty line as in the sample output System.out.println(); } } }

📩 Need a similar solution? Email me: adel455@hotmail.com



Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming