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 |
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.
Recommended Posts:
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Count of subarrays which contains a given number exactly K times
- Find the Number Occurring Odd Number of Times
- C/C++ Program to Find the Number Occurring Odd Number of Times
- Count all elements in the array which appears at least K times after their first occurrence
- Count different numbers possible using all the digits their frequency times
- Given an array of size n and a number k, find all elements that appear more than n/k times
- First element that appears even number of times in an array
- Print all the sum pairs which occur maximum number of times
- Find the number of different numbers in the array after applying the given operation q times
- Number of times the given string occurs in the array in the range [l, r]
- Minimum deletions required such that any number X will occur exactly X times
- Queries to check whether all the elements in the given range occurs even number of times
- GCD of elements which occur prime number of times
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Check if given array can be made 0 with given operations performed any number of times
- GCD of elements occurring Fibonacci number of times in an Array
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Find the elements appearing even number of times in an Array
- Queries to increment array elements in a given range by a given value for a given number of times
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.


