Count the number of times graph crosses X-axis

Last Updated : 11 Jan, 2025

Given an integer array arr[] of size n, the task is to find the number of times the graph crosses the X-axis, where a positive number in arr[] means going above its current position by that value and a negative number means going down by that value. Initially, the current position is at the origin.
Examples: 

Input: arr[] = [4, -6, 2, 8, -2, 3, -12]
Output:
Explanation:

So the graph crosses the X-axis 3 times. 

Input: arr[] = [2, 5, -9, 4] 
Output: 2
Explanation: Graph touches the X-axis two times through index 1 to 2, and through index 2 to 3.

Input: arr[] = [1, 3, 5]
Output: 0
Explanation: Graph has not touched the X-axis any time.

Try It Yourself
redirect icon

Approach:

The idea is to iterate through the array and observe the previous and current levels after each update. The current level at index i can be calculated by taking the prefix sum arr[0...i]. As we update the current level by adding the value at each index to the previous level. We increment the crossing count whenever one of these conditions is met:

  1. If the previous level is negative and the current level is zero or positive.
  2. If the previous level is positive and the current level is zero or negative.
C++
// C++ program to count the number of times 
// the graph crosses the x-axis.

#include <iostream>
#include <vector>
using namespace std;

int touchedXaxis(vector<int>& arr) {
    int curr = 0;
    int res = 0;

    // Iterate over the steps array
    for (int i = 0; i < arr.size(); i++) {
        int prev = curr;
        curr = curr + arr[i];

        // Condition to check that the
        // graph crosses the origin.
        if ((prev < 0 && curr >= 0) || 
            	(prev > 0 && curr <= 0)) {
            res++;
        }
    }
    return res;
}

int main() {
    vector<int> arr = {4, -6, 2, 8, -2, 3, -12};
    cout << touchedXaxis(arr) << endl;
}
C
// C program to count the number of times 
// the graph crosses the x-axis.
#include <stdio.h>

int touchedXaxis(int arr[], int n) {
    int curr = 0;
    int res = 0;

    // Iterate over the steps array
    for (int i = 0; i < n; i++) {
        int prev = curr;
        curr = curr + arr[i];

        // Condition to check that the graph crosses the origin
        if ((prev < 0 && curr >= 0) || (prev > 0 && curr <= 0)) {
            res++;
        }
    }
    return res;
}

int main() {
    int arr[] = {4, -6, 2, 8, -2, 3, -12};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", touchedXaxis(arr, n));
    return 0;
}
Java
// Java program to count the number of times 
// the graph crosses the x-axis.
class GfG {
    
  	static int touchedXaxis(int[] arr) {
        int curr = 0;
        int res = 0;

        // Iterate over the steps array
        for (int i = 0; i < arr.length; i++) {
            int prev = curr;
            curr = curr + arr[i];

            // Condition to check that the graph crosses the origin
            if ((prev < 0 && curr >= 0) || (prev > 0 && curr <= 0)) {
                res++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {4, -6, 2, 8, -2, 3, -12};
        System.out.println(touchedXaxis(arr));
    }
}
Python
# Python program to count the number of times 
# the graph crosses the x-axis.

def touchedXaxis(arr):
    curr = 0
    res = 0

    # Iterate over the steps array
    for i in range(len(arr)):
        prev = curr
        curr = curr + arr[i]

        # Condition to check that the graph crosses the origin
        if (prev < 0 and curr >= 0) or (prev > 0 and curr <= 0):
            res += 1
    return res

if __name__ == "__main__":
    arr = [4, -6, 2, 8, -2, 3, -12]
    print(touchedXaxis(arr))
C#
// C# program to count the number of times 
// the graph crosses the x-axis.

using System;

class GfG {
  	static int touchedXaxis(int[] arr) {
        int curr = 0;
        int res = 0;

        // Iterate over the steps array
        for (int i = 0; i < arr.Length; i++) {
            int prev = curr;
            curr = curr + arr[i];

            // Condition to check that the graph crosses the origin
            if ((prev < 0 && curr >= 0) || (prev > 0 && curr <= 0)) {
                res++;
            }
        }
        return res;
    }

    static void Main(string[] args) {
        int[] arr = {4, -6, 2, 8, -2, 3, -12};
        Console.WriteLine(touchedXaxis(arr));
    }
}
JavaScript
// JavaScript program to count the number of times 
// the graph crosses the x-axis.

function touchedXaxis(arr) {
    let curr = 0;
    let res = 0;

    // Iterate over the steps array
    for (let i = 0; i < arr.length; i++) {
        let prev = curr;
        curr = curr + arr[i];

        // Condition to check that the graph crosses the origin
        if ((prev < 0 && curr >= 0) || (prev > 0 && curr <= 0)) {
            res++;
        }
    }
    return res;
}

// Driver Code
const arr = [4, -6, 2, 8, -2, 3, -12];
console.log(touchedXaxis(arr));

Output
3

Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Comment