给定一个大小为N的整数数组arr [] ,任务是查找图形与X轴交叉的次数,其中正数表示以该值超出其当前位置,负数表示以该值向下。最初,当前位置在原点。
例子:
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
方法:遍历数组并将上一级和当前一级的值保留为两个变量。最初,两个级别均为零。在以下两种情况下,按数组中给定的值增加/减少电平,并增加计数。
- 如果前一个级别小于零并且当前级别大于或等于零。
- 如果前一个级别大于零并且当前级别小于或等于零。
下面是上述方法的实现。
C++
// C++ implementation to count the
// number of times the graph
// crosses the x-axis.
#include
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