Ilya lives in the beautiful city of Bytes lying in a hilly terrain. She loves to ride her bicycle on the hills whenever she gets a chance to do so.
There are check-posts located on the hill at a unit distance from each other. The height of the check posts is given in an array A.
Ilya has to visit her aunt who lives N check-posts away.
Being a hilly area, the terrain in between the check-posts is either monotonically increasing or decreasing. She is initially standing at checkpoint indexed 1.
However, Ilya suffers from a rare sleeping disorder in which she goes to sleep while riding her bicycle if the terrain is uniform. She wakes up whenever there is a change in nature of terrain i.e from increasing to decreasing or from decreasing to increasing.
You have been given an easy task of counting number of times Ilya wakes up during her sleep as she passes through these N check-posts falling on the way to her aunt's house.
Note
You can safely assume that the terrain between i^th and (i + 1)^th checkpoint will be uniform i.e it will have either increasing slope or decreasing slope.
It is guaranteed that no two adjacent check-posts have same heights.
Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of the test case consists of an integer N denoting the number of check-posts.
The first line of each test case contains a single integer N denoting the number of check-posts. The second line contains N space-separated integers A1, A2, ..., AN denoting the heights of check-posts falling on the way.
Output
For each test case, output a single integer denoting number of times Ilya wakes up from her sleep in a separate line .
Constraints
1 ≤ T ≤ 10
2 ≤ N ≤ 10^5
-10^9 ≤ Ai ≤ 10^9
Example
Input:
2
3
1 3 2
5
1 2 3 4 5
Output:
1
0
Explanation
Example case 1. She wakes up when the height changes from 3 to 2
Example case 2. She never wakes up in between the sleep
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read problem carefully. The description: Ilya is initially standing at checkpoint indexed 1. She has to visit her aunt who lives N check-posts away. So she will travel from checkpoint 1 to checkpoint N, passing through all checkposts? Usually we consider she starts at checkpoint 1 and travels to checkpoint N, covering the path between checkposts. The terrain between i and i+1 is either monotonically increasing or decreasing. It is given that no two adjacent check-posts have same heights. So the slope between i and i+1 is either up (if A[i] < A[i+1]) or down (if A[i] > A[i+1]).
Ilya has a sleeping disorder: she goes to sleep while riding her bicycle if the terrain is uniform. She wakes up whenever there is a change in nature of terrain i.e from increasing to decreasing or from decreasing to increasing. So we need to count number of times she wakes up during her sleep as she passes through these N check-posts falling on the way to her aunt's house.
Interpretation: She is initially at checkpoint 1, presumably awake? Or maybe she starts sleeping only when terrain is uniform? The phrase: "She goes to sleep while riding her bicycle if the terrain is uniform. She wakes up whenever there is a change in nature of terrain." So initially, before starting, she is presumably awake. When she starts riding, if the terrain (the segment between 1 and 2) is uniform? But all segments are uniform (monotonically increasing or decreasing). Actually "if the terrain is uniform" might mean if the nature of the slope is consistent over a stretch. But the statement: "You can safely assume that the terrain between i^th and (i + 1)^th checkpoint will be uniform i.e it will have either increasing slope or decreasing slope." That means each segment individually has a uniform slope (since it's a straight line? Actually uniform probably means constant slope, but as a continuous hill it's either increasing or decreasing across that segment). However the sleeping disorder: she goes to sleep if the terrain is uniform. That might mean that if she is on a stretch of terrain that is monotonic (i.e., no changes in direction), she will be asleep. She wakes up when the nature changes (i.e., at a point where the slope changes sign). So as she travels from checkpoint 1 to N, she will be asleep on uniform segments, but she wakes up at points where the terrain changes from increasing to decreasing or vice versa. But note: The checkpoints are at unit distance apart; between checkpoints the terrain is either increasing or decreasing. So the terrain's nature can change only at checkpoints (i.e., at a checkpoint, the slope from previous segment to next segment might be different). So Ilya will wake up at certain checkpoints when the direction changes.
But careful: The phrase: "She goes to sleep while riding her bicycle if the terrain is uniform. She wakes up whenever there is a change in nature of terrain i.e from increasing to decreasing or from decreasing to increasing." This could be interpreted as: She starts awake. As she rides, if the terrain is uniform (i.e., a constant slope, which it always is between two consecutive checkpoints), she will fall asleep. However, if she falls asleep immediately when the terrain is uniform, she would fall asleep on the first segment and then wake up at the point where nature changes. But what about the beginning? At the start, she is at checkpoint 1, about to travel to 2. The terrain from 1 to 2 is either increasing or decreasing. That is uniform. So she might fall asleep at the start of the ride? Or maybe she is awake initially and falls asleep as soon as she starts moving on a uniform terrain. Then she'd be asleep on the first segment. Then at checkpoint 2, if the slope from 2 to 3 is same as from 1 to 2, then terrain remains uniform across the junction? Actually at the checkpoint, the terrain changes slope? If the slopes are the same (both up or both down), then the overall terrain (across the checkpoint) is still monotonic? However the problem says: "whenever there is a change in nature of terrain i.e from increasing to decreasing or from decreasing to increasing." So she wakes up only if the direction changes. If it remains the same, she continues sleeping. So she would wake up at checkpoints where A[i-1] to A[i] and A[i] to A[i+1] have opposite slopes.
But do we count the moment she wakes up at a checkpoint as she passes through that checkpoint? The problem: "counting number of times Ilya wakes up during her sleep as she passes through these N check-posts falling on the way to her aunt's house." So we count how many times she wakes up. Possibly the answer is the number of inflection points (local maxima/minima) in the sequence where the direction changes.
But note: What about the start? Is she considered to be asleep initially? It says "She goes to sleep while riding her bicycle if the terrain is uniform." So maybe she starts awake, and as soon as she starts riding on uniform terrain (i.e., from checkpoint 1 to 2) she falls asleep. That does not count as waking up. So the first segment she is asleep. Then at checkpoint 2, if slope changes, she wakes up. That would be one waking. Then if she continues, on the next uniform segment she will fall asleep again (since terrain is uniform), and wake up at next change. So essentially she wakes up at each change point. But note: When she wakes up, she is awake. Then as she continues on the next uniform segment, she will fall asleep again (maybe immediately). So number of wakes equals number of changes.
But what if the terrain remains same direction from start to end? Then she falls asleep at start and never wakes up until she reaches aunt's house? The example: N=5, sequence 1 2 3 4 5 (strictly increasing). Output is 0. So that matches: no wake-ups because terrain never changes direction.
Example 2: 1 3 2: A = [1,3,2]. Slopes: 1->3 up, 3->2 down. Change from up to down at checkpoint 2, so she wakes up once. Output is 1. So that matches.
Thus answer = number of indices i from 2 to N-1 where (A[i] - A[i-1]) and (A[i+1] - A[i]) have opposite signs. (i.e., A[i-1] < A[i] > A[i+1] OR A[i-1] > A[i] < A[i+1]).
But careful: Could there be any wake-ups at the start or end? The problem says she passes through N check-posts. She starts at checkpoint 1 (maybe awake) and ends at checkpoint N (aunt's house). Does she wake up upon arrival? Probably not counted because it's the end. The phrase "during her sleep" suggests counting times she wakes up while riding. So only at interior points.
Thus solution: For each test case, compute count of i such that (A[i] - A[i-1]) * (A[i+1] - A[i]) < 0. Since no adjacent heights equal, product will not be zero.
Constraints: N up to 1e5, T up to 10, so O(N) per test case.
Edge cases: N=2. There are no interior points, so answer = 0. Indeed no change possible.
Thus straightforward.
But wait: There is nuance: The problem statement mentions "She is initially standing at checkpoint indexed 1." Possibly she is awake initially. Then she starts riding. The phrase: "She goes to sleep while riding her bicycle if the terrain is uniform." This could be interpreted that she falls asleep when the terrain is uniform (i.e., when the slope is constant over a stretch). But note that between any two consecutive checkposts, the terrain is uniform. So as soon as she starts moving on the first segment, she falls asleep. Then at checkpoint 2, if the slope changes, she wakes up. Then on the next segment (2->3) terrain is uniform again, so she will fall asleep again, and if slope changes at checkpoint 3, she wakes up again, etc. So number of wakes = number of changes. That matches.
But also consider possibility that at a checkpoint, if slope doesn't change, she remains asleep. So no wake-up. So counting changes is correct.
But there is a subtlety: What if she is already asleep from previous segment and arrives at a checkpoint where slope changes, she wakes up. That counts as one wake-up. Then she immediately falls asleep again on the next uniform segment. So each change yields a wake-up.
Thus answer is number of times the sign of differences changes.
Therefore solution: For each test case, read N and array A. Compute diff[i] = sign(A[i+1] - A[i]) for i=1 to N-1. Then count how many i from 1 to N-2 where diff[i] != diff[i+1]. That count is answer.
Alternatively, count i from 2 to N-1 where (A[i] - A[i-1]) * (A[i+1] - A[i]) < 0.
Implementation notes: Use long for A (since up to 1e9). But sign only matters.
Edge Cases: N=2 => answer 0.
Complexities: O(N) per test case, memory O(1).
Will there be any overlapping? No.
Thus we can produce code.
But wait: The input format: The first line contains T. Then for each test case: "The first line of the test case consists of an integer N denoting the number of check-posts." Actually the problem statement says: "The first line of the test case consists of an integer N denoting the number of check-posts. The first line of each test case contains a single integer N denoting the number of check-posts. The second line contains N space-separated integers A1, A2, ..., AN" So that's fine.
Thus we can implement.
Let's double-check sample:
Test case 1: N=3, A=[1,3,2]. diffs: + (1->3 up), - (3->2 down). change from + to - at i=1? Actually diff indices: diff1=+, diff2=-. diff1 != diff2 => count=1.
Test case 2: N=5, A=[1,2,3,4,5]. diffs: all +. No changes => 0.
Ok.
Thus answer.
But wait, could there be any wake-up at the beginning or end? Suppose the terrain changes at the very start? Not possible because need two segments. So no.
Thus we implement.
Now confirm constraints: T up to 10, N up to 1e5, total up to 1e6, easily.
One more nuance: "She is initially standing at checkpoint indexed 1." Possibly she is already sleeping? But problem says "she goes to sleep while riding her bicycle if the terrain is uniform." So only while riding. So at the moment she starts riding, she's awake. Then after moving on uniform terrain, she falls asleep. So our interpretation is fine.
If she was already sleeping at checkpoint 1, then she'd wake up at the first change, but also maybe at the start? No.
Thus we keep as is.
So final answer: count of inflection points.
Let's just ensure that we count correctly even if there are plateaus? But guaranteed no two adjacent equal heights.
Thus final solution: For each test case:
Read N.
Read array A.
Initialize count = 0.
For i from 1 to N-2 (0-indexed from 0 to N-3):
if (A[i+1]-A[i]) * (A[i+2]-A[i+1]) < 0: count++.
Print count.
Time complexity O(N).
Edge Cases: Large negative values but difference sign works.
Now we produce solution in any language. Typically for code we'll use fast input for large N (e.g., BufferedReader in Java, scanf in C++). But even cin with sync off works for 1e5 * 10.
I'll produce a solution in Python for readability, but careful about T=10, N=1e5, Python is fine if using sys.stdin.read for fast input.
Let's produce Python code:
python
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it))
A = [int(next(it)) for _ in range(n)]
count = 0
for i in range(n-2):
diff1 = A[i+1] - A[i]
diff2 = A[i+2] - A[i+1]
if diff1 * diff2 < 0:
count += 1
out.append(str(count))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
That should work.
But consider if N=2, loop range(n-2) -> range(0) -> no iteration, count=0.
All good.
Thus answer.
But hold on: The problem statement says: "She goes to sleep while riding her bicycle if the terrain is uniform. She wakes up whenever there is a change in nature of terrain i.e from increasing to decreasing or from decreasing to increasing." There's also note: "You can safely assume that the terrain between i^th and (i + 1)^th checkpoint will be uniform i.e it will have either increasing slope or decreasing slope." So the terrain between checkpoints is uniform, but what about at a checkpoint? The terrain may be considered a point. So the change occurs at a checkpoint.
Thus our answer stands.
Thus we are done.
But I recall that sometimes such problems might ask for number of times she wakes up, which could also include the moment she wakes up at the very end if terrain changes? But she reaches aunt's house at checkpoint N, and presumably stops. Even if the terrain changed at checkpoint N? But there is no segment after N, so no change from N to N+1, so no wake-up at N. So no.
Therefore answer = number of peaks and valleys.
Now double-check sample: Output as expected.
So we can provide solution.
Also note: The problem might be from CodeChef or similar, they sometimes have tricky parts: "She is initially standing at checkpoint indexed 1." Could be interpreted as she is at index 1, but she needs to travel N check-posts away, i.e., to checkpoint N. So yes.
Thus final.
But I'm thinking: The terrain is uniform between checkpoints. She falls asleep when the terrain is uniform, so she falls asleep on each segment (including the first). However, if she is awake at the start, she falls asleep as soon as she starts moving. That's one event, but not a wake-up. So it's fine.
But what if she is awake on a segment where terrain is not uniform? But all segments are uniform, so she is always asleep while moving. So she only wakes at changes.
Thus answer.
Thus we are done.
I'll produce explanation and code.
But wait: The problem statement: "She goes to sleep while riding her bicycle if the terrain is uniform." Could also be interpreted that she falls asleep if the terrain is uniform, meaning if the nature (slope) doesn't change for a while, she falls asleep, and wakes up when it changes. That is essentially same: she falls asleep on a monotonic stretch, but only after some time? But since they say terrain between i and i+1 is uniform, she would fall asleep on that stretch. However, she might fall asleep after some time, but they probably mean she is asleep during uniform terrain. So we stick with above.
Thus solution.
Now I'll produce final answer with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
# read the heights
A = [int(next(it)) for _ in range(n)]
wake_ups = 0
# check every interior point (i = 1 .. n-2 in 1‑based indexing)
for i in range(n - 2):
diff1 = A[i + 1] - A[i]
diff2 = A[i + 2] - A[i + 1]
if diff1 * diff2 < 0: # signs are opposite
wake_ups += 1
out_lines.append(str(wake_ups))
sys.stdout.write("\n".join(out_lines))