Skip to content
Related Articles

Related Articles

Count the number of times graph crosses X-axis
  • Last Updated : 02 Mar, 2020

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++




// 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;
}

Java




// 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

Python3




# 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

C#




// 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
Output:
3



My Personal Notes arrow_drop_up
Recommended Articles
Page :