Count the number of times graph crosses X-axis

Given an integer array arr[] of size N, the task is to find the number of times graph crosses X-axis, where positive number 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: 3
Explanation:
So the graph crosses X-axis 3 times

Input: arr[] = {1, 1, -3, 2}
Output: 2

Approach: Iterate over the array and keep the value of the previous level and current level into two variables. Initially, both the levels are zero. Increase / Decrease the level by the value given in the array and increase the count in below two cases.



  • If the previous level is less than zero and the current level is greater than or equal to zero.
  • If the previous level is greater than zero and the current level is less than or equal to zero.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to count the
// number of times the graph
// crosses the x-axis.
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to to count the
// number of times the graph
// crosses the x-axis.
int times(int steps[], int n)
{
  
    int current_level = 0;
    int previous_level = 0;
    int count = 0;
  
    // Iterate over the steps array
    for (int i = 0; i < n; i++) {
  
        // Update the previous level and
        // current level by value given
        // in the steps array
        previous_level = current_level;
        current_level = current_level
                        + steps[i];
  
        // Condition to check that the
        // graph crosses the origin.
        if ((previous_level < 0
             && current_level >= 0)
            || (previous_level > 0
                && current_level <= 0)) {
            count++;
        }
    }
    return count;
}
  
// Driver Code
int main()
{
    int steps[12] = { 1, -1, 0, 0, 1, 1, -3, 2 };
    int n = sizeof(steps) / sizeof(int);
  
    cout << times(steps, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count the 
// number of times the graph 
// crosses the x-axis. 
class GFG 
{
      
    // Function to to count the 
    // number of times the graph 
    // crosses the x-axis. 
    static int times(int []steps, int n) 
    
        int current_level = 0
        int previous_level = 0
        int count = 0
      
        // Iterate over the steps array 
        for (int i = 0; i < n; i++)
        
      
            // Update the previous level and 
            // current level by value given 
            // in the steps array 
            previous_level = current_level; 
            current_level = current_level + steps[i]; 
      
            // Condition to check that the 
            // graph crosses the origin. 
            if ((previous_level < 0 && 
                current_level >= 0
                || (previous_level > 0
                && current_level <= 0)) 
            
                count++; 
            
        
        return count; 
    
      
    // Driver Code 
    public static void main (String[] args)
    
        int steps[] = { 1, -1, 0, 0, 1, 1, -3, 2 }; 
        int n = steps.length; 
      
        System.out.println(times(steps, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to count the
# number of times the graph
# crosses the x-axis.
  
# Function to to count the
# number of times the graph
# crosses the x-axis.
def times(steps, n):
  
    current_level = 0
    previous_level = 0
    count = 0
  
    # Iterate over the steps array
    for i in range(n):
  
        # Update the previous level and
        # current level by value given
        #in the steps array
        previous_level = current_level
        current_level = current_level+ steps[i]
  
        # Condition to check that the
        # graph crosses the origin.
        if ((previous_level < 0
            and current_level >= 0)
            or (previous_level > 0
                and current_level <= 0)):
            count += 1
  
    return count
  
# Driver Code
steps = [1, -1, 0, 0, 1, 1, -3, 2]
n = len(steps)
  
print(times(steps, n))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count the 
// number of times the graph 
// crosses the x-axis.
using System;
  
class GFG 
{
      
    // Function to to count the 
    // number of times the graph 
    // crosses the x-axis. 
    static int times(int []steps, int n) 
    
        int current_level = 0; 
        int previous_level = 0; 
        int count = 0; 
      
        // Iterate over the steps array 
        for (int i = 0; i < n; i++)
        
      
            // Update the previous level and 
            // current level by value given 
            // in the steps array 
            previous_level = current_level; 
            current_level = current_level + steps[i]; 
      
            // Condition to check that the 
            // graph crosses the origin. 
            if ((previous_level < 0 && 
                current_level >= 0) 
                || (previous_level > 0
                && current_level <= 0)) 
            
                count++; 
            
        
        return count; 
    
      
    // Driver Code 
    public static void Main ()
    
        int []steps = { 1, -1, 0, 0, 1, 1, -3, 2 }; 
        int n = steps.Length; 
      
        Console.WriteLine(times(steps, n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.