Count the number of times graph crosses X-axis
Last Updated :
27 Jan, 2023
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 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 the 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 levels were zero. Increase / Decrease the level by the value given in the array and increase the count in the two cases below.
- 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++
#include <bits/stdc++.h>
using namespace std;
int times( int steps[], int n)
{
int current_level = 0;
int previous_level = 0;
int count = 0;
for ( int i = 0; i < n; i++) {
previous_level = current_level;
current_level = current_level
+ steps[i];
if ((previous_level < 0
&& current_level >= 0)
|| (previous_level > 0
&& current_level <= 0)) {
count++;
}
}
return count;
}
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
class GFG
{
static int times( int []steps, int n)
{
int current_level = 0 ;
int previous_level = 0 ;
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
previous_level = current_level;
current_level = current_level + steps[i];
if ((previous_level < 0 &&
current_level >= 0 )
|| (previous_level > 0
&& current_level <= 0 ))
{
count++;
}
}
return count;
}
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));
}
}
|
Python3
def times(steps, n):
current_level = 0
previous_level = 0
count = 0
for i in range (n):
previous_level = current_level
current_level = current_level + steps[i]
if ((previous_level < 0
and current_level > = 0 )
or (previous_level > 0
and current_level < = 0 )):
count + = 1
return count
steps = [ 1 , - 1 , 0 , 0 , 1 , 1 , - 3 , 2 ]
n = len (steps)
print (times(steps, n))
|
C#
using System;
class GFG
{
static int times( int []steps, int n)
{
int current_level = 0;
int previous_level = 0;
int count = 0;
for ( int i = 0; i < n; i++)
{
previous_level = current_level;
current_level = current_level + steps[i];
if ((previous_level < 0 &&
current_level >= 0)
|| (previous_level > 0
&& current_level <= 0))
{
count++;
}
}
return count;
}
public static void Main ()
{
int []steps = { 1, -1, 0, 0, 1, 1, -3, 2 };
int n = steps.Length;
Console.WriteLine(times(steps, n));
}
}
|
Javascript
<script>
function times(steps, n) {
let current_level = 0;
let previous_level = 0;
let count = 0;
for (let i = 0; i < n; i++) {
previous_level = current_level;
current_level = current_level
+ steps[i];
if ((previous_level < 0
&& current_level >= 0)
|| (previous_level > 0
&& current_level <= 0)) {
count++;
}
}
return count;
}
let steps = [1, -1, 0, 0, 1, 1, -3, 2];
let n = steps.length;
document.write(times(steps, n));
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...