Counting Sign Changes in a Java Array Example
This article explores how to count the number of sign changes in an array using Java. Let us delve into understanding how to use a Java array to count sign changes efficiently.
1. Overview
In numerical analysis or data processing, identifying the number of sign changes in a series of numbers can be important — especially in signal processing, mathematical analysis, and even in trading algorithms. A sign change happens when two consecutive numbers in an array have opposite signs (positive to negative or vice versa). For example, in the array: [1, -3, 4, -2, 6, -1], the sign changes happen between:
1 -> -3 -3 -> 4 4 -> -2 -2 -> 6 6 -> -1
That results in 5 sign changes.
We will now look at two approaches in Java to compute this.
2. Iterating over the array
This is the most straightforward approach, where we iterate over the array and check if the current element and the previous element have opposite signs.
// Java code: Iterative Approach
public class SignChangeCounter {
public static int countSignChanges(int[] nums) {
int count = 0;
for (int i = 1; i < nums.length; i++) {
if ((nums[i] > 0 && nums[i - 1] < 0) ||
(nums[i] < 0 && nums[i - 1] > 0)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] input = {1, -3, 4, -2, 6, -1};
int result = countSignChanges(input);
System.out.println("Number of sign changes: " + result);
}
}
2.1 Code Explanation and Output
This Java program defines a class named SignChangeCounter with a method countSignChanges that takes an integer array as input and returns the number of sign changes within it. A sign change is counted when two consecutive elements have opposite signs—one positive and the other negative. The method initializes a counter to zero and iterates through the array starting from index 1. For each element, it checks whether it and its previous element have opposite signs using a conditional if-statement. If they do, the counter is incremented. The main method demonstrates this logic by passing an array {1, -3, 4, -2, 6, -1} to the method and printing the resulting number of sign changes to the console, which in this case is 5.
When the code is executed, the following output is displayed on the IDE console:
Number of sign changes: 5
3. The Java Streams Approach
Java 8 Streams do not directly support consecutive element comparisons, but we can simulate this using an IntStream and a range of indices.
// Java code: Using Java Streams
import java.util.stream.IntStream;
public class SignChangeCounterWithStreams {
public static int countSignChanges(int[] nums) {
return (int) IntStream.range(1, nums.length)
.filter(i -> (nums[i] > 0 && nums[i - 1] < 0) ||
(nums[i] < 0 && nums[i - 1] > 0))
.count();
}
public static void main(String[] args) {
int[] input = {1, -3, 4, -2, 6, -1};
int result = countSignChanges(input);
System.out.println("Number of sign changes: " + result);
}
}
3.1 Code Explanation and Output
This Java program, SignChangeCounterWithStreams, utilizes Java 8’s Stream API to count the number of sign changes in an integer array. The method countSignChanges takes an array as input and uses IntStream.range to iterate over indices from 1 to the end of the array. For each index, it checks whether the current and previous elements have opposite signs—indicating a sign change—using a filter with logical conditions. The filtered count is cast to an integer and returned. In the main method, an example array {1, -3, 4, -2, 6, -1} is passed to this function, and the result is printed to the console, which outputs the number of sign changes as 5.
When the code is executed, the following output is displayed on the IDE console:
Number of sign changes: 5
4. Conclusion
Detecting sign changes in an array is a useful operation in many computational domains. The iterative approach is simple and readable, while the stream-based approach is more functional and concise but may be slightly less intuitive for beginners.
- A sign change occurs when consecutive numbers have opposite signs.
- Iteration gives full control and is more efficient for very large arrays.
- Java Streams provide a declarative way to solve the problem with cleaner syntax.
Whether using traditional loops or modern Java streams, both approaches are effective for counting sign changes in an integer array.

