We need to parse the problem. It gives two non-increasing arrays A and B, both size n. The monkiness of two numbers is defined as:
M(A[i], B[j]) = j - i if j >= i and B[j] >= A[i], else 0.
Then monkiness of the two arrays M(A,B) = max_{i,j} M(A[i],B[j]) (for 0 <= i, j <= n-1, but problem statement says "for 0 ≤ i, j< n-1", but that seems off; likely 0 ≤ i, j < n). I think it's 0-indexed and up to n-1.
Goal: compute the maximum value of j-i subject to i <= j and B[j] >= A[i].
We have both arrays non-increasing. So A sorted descending, B sorted descending.
We need to find max (j - i) where A[i] <= B[j] and i <= j.
We can try for each i, find the largest j >= i such that B[j] >= A[i], and compute j-i. Then take max over i. Because arrays are sorted (non-increasing), we can use two pointers.
Given that A and B are non-increasing, for a given i, as j increases, B[j] is non-increasing. But condition is B[j] >= A[i]. Since B is non-increasing, the set of j satisfying B[j] >= A[i] is a prefix? Actually, since B is sorted in non-increasing order (largest first), as j increases, B[j] decreases. So for a fixed i, if we consider j from i to n-1, B[j] is decreasing. The condition B[j] >= A[i] will be true for a range of j from i up to some maximum index Jmax(i) where B[Jmax(i)] >= A[i] and beyond that, B[j] < A[i]. So we can find the rightmost j >= i such that B[j] >= A[i]. Since both arrays are sorted, we can use two pointers: pointer j starts at some point and moves as i increases.
Specifically, we can iterate i from 0 to n-1, and maintain a pointer j that starts at something. Because we need j >= i. Typically, we can set j = i initially, but we can also keep a j that is always at least i. For each i, we want to find the largest j (>= i) such that B[j] >= A[i]. Since arrays are sorted, as i increases, A[i] decreases (non-increasing: A[i] >= A[i+1], so A[i] becomes smaller or equal). So condition B[j] >= A[i] becomes easier to satisfy for larger i (since A[i] is smaller). So the maximum j that satisfies the condition for i+1 might be larger than or equal to the maximum j for i. So we can use a two-pointer approach: initialize j = 0, but we need j >= i. Actually, we can set j = i or j = current maximum index. Let's think:
We want for each i, find the maximum j >= i with B[j] >= A[i]. If we start i = 0, we can find j by moving j from n-1 downward? Or we can use binary search: for each i, binary search in B from i to n-1 for the largest j with B[j] >= A[i]. Since B is sorted non-increasing, we can do binary search (O(log n) per i) => O(n log n) which should be fine for n up to 1e5 and test cases up to 50? 50 * 1e5 log = about 50 * 1e5 * 17 = 85e6, borderline but likely okay in C++ but we are to provide a solution in maybe any language, but we want efficient. Two-pointer O(n) is better.
Because A and B are sorted, we can use two-pointer scanning from left to right? Actually typical approach for such problems (max j-i with arr1[i] <= arr2[j]) is to use two pointers: i from 0 to n-1, j from 0 to n-1. But we need j >= i. Alternative: iterate over j and find smallest i? But similar.
Let's define: For each i, we want max j >= i with B[j] >= A[i]. Since A is non-increasing, the condition B[j] >= A[i] becomes easier for larger i. So we can maintain a j pointer that starts at the rightmost possible (n-1) and then as i increments, we might need to move j left? Wait, as i increases, A[i] decreases, so the threshold B[j] >= A[i] is easier, meaning that for a fixed j, it's more likely to satisfy. So the maximum j that satisfies the condition is non-decreasing as i increases? Let's test: For i=0, A[0] is largest, so the condition B[j] >= A[0] might only hold for small j (since B[j] decreases, after some index, B[j] < A[0]). The maximum j that satisfies might be relatively small. For i=1, A[1] <= A[0], so the threshold is lower, so we can potentially go to a larger j (since B[j] might still be >= A[1] for larger j). So maximum j_i (call it rightmost) is non-decreasing as i increases. So we can have two pointers: i from 0 to n-1, j starting at 0? Actually we need j to be at least i. Let's define j = some index; we want to find for each i, the maximum j such that B[j] >= A[i]. Because j_max_i is non-decreasing, we can start j at either i or at previous j. But we also need j >= i. Since j_max_i >= i? Possibly not? Actually the maximum j satisfying condition must be at least i if condition is satisfied at j = i? Not necessarily; it could be that for some i, even at j=i, B[i] < A[i], then the maximum j may be < i? But condition also requires j >= i, so if no j >= i satisfies B[j] >= A[i], then the answer for that i is 0 (or not considered). In that case j_max_i < i, meaning no valid j. Since we only consider j >= i, the set of j is from i to n-1. So we want the maximum j in [i, n-1] such that B[j] >= A[i]. If B[i] < A[i], then it's possible that for all j >= i, B[j] <= B[i] < A[i], so no valid j. Then j_max_i does not exist (or we treat as -inf). But as i increases, i itself increases. If for i, no valid j, then for larger i, maybe there will be valid j. So we can't just keep j as a global pointer that is always >= i, but we can start j from the current j (which might be less than i) and then move it forward to at least i and then extend as far as condition holds.
Simplify: Use binary search per i. n up to 1e5, tc up to 50, n total across test cases up to 5e6? Not given, but each test case can be up to 1e5, so worst-case total n = 50*1e5 = 5e6, which is okay for O(n log n) maybe, but O(n) is nicer.
Better: two-pointer: start i = 0, j = 0 (or maybe j = 0). But we need j >= i, so we can set j = i initially for each i and increase j until condition fails? That would be O(n^2) worst-case.
Alternatively, we can iterate over j from 0 to n-1 and find minimal i such that A[i] <= B[j] and i <= j. Then j-i is candidate. But since we need max j-i, we can keep track of minimal i that satisfies condition for each j. Because arrays are non-increasing, we can do two-pointer from left to right.
Let's think of known problem: Given two sorted arrays, find maximum difference j-i such that arr1[i] <= arr2[j]. The typical O(n) solution: use two pointers i=0, j=0, but we need j-i max. Actually we can iterate j from 0 to n-1, and for each j, we want the smallest i (<= j) such that A[i] <= B[j]. Since A is non-increasing, as j increases, the smallest i that works may also increase? Let's explore.
We want max(j-i) given A[i] <= B[j]. For a fixed j, we want the smallest i such that A[i] <= B[j]. Because if we can satisfy the condition with a smaller i, then j-i is larger. However, we also need i <= j. So we can maintain a pointer i that moves forward as needed.
Specifically, as j increases, B[j] becomes smaller, so condition A[i] <= B[j] becomes harder to satisfy (since B[j] is decreasing). So for larger j, the set of i that satisfy A[i] <= B[j] is possibly smaller (i needs to be larger because A[i] is larger for small i, and for larger i, A[i] is smaller). So the smallest i that satisfies A[i] <= B[j] will be non-decreasing as j increases. That is typical: i moves forward as j increases. So we can use two pointers: i = 0; for j from 0 to n-1, while i <= j and A[i] > B[j] (i.e., condition not satisfied), increment i. After the loop, if i <= j, then condition holds (A[i] <= B[j]), record j - i. But careful: we want the maximum j-i; but we are not necessarily using the smallest i for each j? Actually after the while loop, i is the smallest index such that A[i] <= B[j] (because we increment i while condition fails). However, we also need to ensure that i <= j; the while condition might stop when i <= j and A[i] <= B[j] or when i becomes > j (no valid i). For each j, if i <= j, we have a valid pair with i being the smallest possible i, thus maximizing j-i for that j. Then we compute candidate = j - i. Then we keep max candidate. Additionally, after processing j, we might not want to reset i, because for next j+1, the smallest i that works will be at least current i (since B[j+1] <= B[j]), so we can continue from current i.
This algorithm yields O(n) time. It works for arrays sorted in non-increasing order? Let's test with sample:
Case1: A: [7,7,3,3,3,2,2,2,1]; B: [8,8,7,7,5,5,4,3,2]
n=9.
We process j=0..8.
Initialize i=0.
j=0: B[0]=8. While i<=j and A[i] > B[j]? A[0]=7 <=8, so condition true, i=0, candidate=0-0=0.
j=1: B[1]=8, i=0, A[0]=7<=8, candidate=1-0=1. max=1.
j=2: B[2]=7, i=0, A[0]=7<=7? yes, condition holds (>=). candidate=2-0=2. max=2.
j=3: B[3]=7, i=0, A[0]=7<=7, candidate=3-0=3. max=3.
j=4: B[4]=5, i=0, A[0]=7>5? yes, 7 > 5 so condition fails. while i <= j (i=0, j=4) and A[i] > B[j] => true, so i++ => i=1. Now i=1, A[1]=7 >5, condition fails, i++ => i=2. Now i=2, A[2]=3 <=5? 3 <=5, true, so exit while. i=2 <=j=4, candidate=4-2=2. max remains 3.
j=5: B[5]=5, i=2, A[2]=3 <=5, candidate=5-2=3. max=3? but earlier we had 3, same.
j=6: B[6]=4, i=2, A[2]=3 <=4, candidate=6-2=4. max=4.
j=7: B[7]=3, i=2, A[2]=3 <=3, candidate=7-2=5. max=5.
j=8: B[8]=2, i=2, A[2]=3 >2? 3>2, so while: i=2, A[2]>B[8] true, i=3; A[3]=3>2, i=4; A[4]=3>2, i=5; A[5]=2 <=2? 2<=2 true, exit with i=5 <= j=8, candidate=8-5=3. max stays 5.
Output max=5. Works.
Case2: A: [6,5,4,4,4,4]; B: [2,2,2,2,2,2].
i=0.
j=0: B[0]=2, A[0]=6>2 => while i<=j: i=0, A[0]>2 true => i=1, but i=1 > j=0? Actually while condition is i <= j and A[i] > B[j]. At i=0, i<=j (0<=0) and A[0]>2 true, so i=1. Now i=1, check condition: i <= j? 1 <=0 false, exit loop. No valid i, no candidate.
j=1: B[1]=2, i=1 (since i persists). Check i<=j? 1<=1 true, A[1]=5>2 true => while: i=2, now i=2 > j=1? condition fails, exit. No candidate.
j=2: i=2, i<=2 true, A[2]=4>2 => i=3, i=3 > j=2? exit. No candidate.
j=3: i=3, i<=3 true, A[3]=4>2 => i=4, i=4 > j=3? exit.
j=4: i=4, i<=4 true, A[4]=4>2 => i=5, i=5 > j=4? exit.
j=5: i=5, i<=5 true, A[5]=4>2 => i=6, i=6 > j=5? exit. So no candidate, max stays 0. Output 0.
Works.
Thus the algorithm is correct: For each j from 0 to n-1, maintain i (starting at 0) as the smallest index such that A[i] <= B[j] (and i <= j). Actually we need to ensure i <= j; the while loop increments i while A[i] > B[j] and i <= j. After the loop, if i <= j, then A[i] <= B[j] and i is the smallest such index (because we incremented until condition holds). Then candidate = j - i. Keep max.
Time O(n) per test case. Space O(1) besides arrays.
We must be careful about indexing: constraints say arrays are 0-indexed? The problem statement says "0 ≤ i, j < n-1". That might be a typo; likely they mean 0 ≤ i, j ≤ n-1. But we can assume standard 0-indexed. Also note that they say "non-increasing array of integers A,B i.e A[i] ≥ A[i+1] and B[i] ≥ B[i+1] and for all i, 0 ≤ i < n-1." So indeed arrays size n, indices 0 to n-1. So our algorithm fits.
Edge cases: All numbers up to 1e12, so int64 needed. In C++, we use long long. The answer (j-i) up to n-1, so within 10^5, fits in int, but for safety we can use int. But to be consistent with large numbers, we can use long long.
We need to read multiple test cases: first line is tc. Then for each test case: read n, then read A (n integers), then read B (n integers). Output one integer per test case.
Complexities: n total may be up to 5e6, reading input efficiently needed.
We'll implement in C++.
Implementation details:
- Use scanf for fast input.
- Use vector or array of long long for A and B.
- For each test case:
- read n.
- read A of size n.
- read B of size n.
- int i = 0; int maxDiff = 0;
- for (int j = 0; j < n; ++j) {
while (i <= j && A[i] > B[j]) {
i++;
}
if (i <= j) {
maxDiff = max(maxDiff, j - i);
}
}
- print maxDiff.
Check: Is it guaranteed that i won't exceed n? While loop condition i <= j ensures that when i becomes > j we break; after that, if i > j, we skip candidate. In the next iteration j+1, i may be > j+1? Actually i could be > j+1? Let's see: after processing j, i may be, say, j+1 (if we incremented until i > j). Then for next j+1, i = j+1 initially. Check condition: i <= j+1 (true). While A[i] > B[j+1]? If true, we may increment i further, but i will stay >= j+1. So safe.
Thus algorithm works.
Now consider if arrays are non-increasing but we have equal numbers, that's fine.
Proof: For any pair (i, j) that satisfies A[i] <= B[j] and i <= j, we have that when processing j using our algorithm, i is set to the smallest index i' such that A[i'] <= B[j]. Then i' <= i (since i is a valid index, and i' is the smallest). Then j - i' >= j - i, so the algorithm captures a pair at least as good. Therefore the maximum j-i is found.
Thus algorithm yields correct answer.
Let's test with a few custom cases:
Case: A: [5,4,3]; B: [6,5,4]. n=3.
- j=0: B0=6, i=0, A0=5<=6 => cand 0.
- j=1: B1=5, i=0, A0=5<=5 => cand 1-0=1.
- j=2: B2=4, i=0, A0=5>4 => i=1; A1=4<=4 => cand 2-1=1. max=1. But we also have pair (i=2,j=2): A2=3 <=4? yes, j-i=0. So max is 1. However, pair (i=0,j=2) fails because A0=5 > B2=4. So okay.
Case: A: [4,3,2]; B: [5,4,3]. n=3.
- j=0: B0=5, i=0, A0=4<=5 => cand 0.
- j=1: B1=4, i=0, A0=4<=4 => cand 1.
- j=2: B2=3, i=0, A0=4>3 => i=1; A1=3<=3 => cand 1. max=1. However, there is pair (i=0,j=1) gives 1, (i=1,j=2) gives 1, (i=0,j=2) invalid. So okay.
Case: A: [3,2,1]; B: [3,2,1]. n=3.
- j=0: B0=3, i=0, A0=3<=3 => cand 0.
- j=1: B1=2, i=0, A0=3>2 => i=1; A1=2<=2 => cand 0.
- j=2: B2=1, i=1, A1=2>1 => i=2; A2=1<=1 => cand 0. max=0. But we can have pair (i=0,j=1)? A0=3 > B1=2 invalid; (i=0,j=2): 3>1 invalid; (i=1,j=2): 2>1 invalid. So answer 0. Good.
But note there is pair (i=0,j=0) gives 0, etc. So max 0.
Case: A: [1,1,1]; B: [1,1,1]. n=3.
- j=0: i=0, A0=1<=1 => cand 0.
- j=1: i=0, A0=1<=1 => cand 1.
- j=2: i=0, A0=1<=1 => cand 2. max=2. Indeed all pairs valid because A[i]<=B[j] always true (equal) and i <= j? Actually (i=0,j=2) gives 2. So answer 2. Our algorithm gave that because for j=2, while condition A[0] > B[2]? 1>1 false, so i remains 0, candidate 2. So good.
Case: A: [10,9,8,7,6]; B: [5,5,5,5,5]. n=5.
- j=0: i=0, A0=10>5 => while i<=0 and A0>5 true => i=1, i>j => exit. no candidate.
- j=1: i=1, i<=1, A1=9>5 => i=2, i>j => exit.
- j=2: i=2, A2=8>5 => i=3, i>j => exit.
- j=3: i=3, A3=7>5 => i=4, i>j => exit.
- j=4: i=4, A4=6>5 => i=5, i>j => exit. max=0. Correct.
Now test scenario where A is very small and B is large: A: [1,1,1], B: [100,100,100]. n=3.
- j=0: i=0, A0=1<=100 => cand 0.
- j=1: i=0, cand 1.
- j=2: i=0, cand 2 => max=2. Good.
Another scenario: A: [5,5,4,4,3], B: [6,5,5,4,4]. n=5. Let's compute manually:
- pairs: best? Probably i=0, j=4? A0=5, B4=4 -> fails. i=1=5, B4=4 fails. i=2=4, B4=4 works, diff=2. i=0, j=3: A0=5, B3=4 fails. i=1, j=3: 5>4 fails. i=0,j=2: A0=5, B2=5 works? B2=5, so works diff=2. i=0,j=1: 5<=5 diff=1. i=1,j=2: 5<=5 diff=1. i=2,j=4: 4<=4 diff=2. So max=2. Algorithm:
i=0.
j0: B0=6, A0=5<=6 -> cand0.
j1: B1=5, A0=5<=5 -> cand1.
j2: B2=5, A0=5<=5 -> cand2.
j3: B3=4, A0=5>4 => i=1; A1=5>4 => i=2; A2=4<=4 -> i=2 <=j3 -> cand 1 (3-2=1). Actually j=3, i=2, diff=1.
j4: B4=4, i=2, A2=4<=4 -> cand 2 (4-2=2). So max becomes 2 (from earlier j=2 diff=2? Actually j=2 diff=2 gave 2, so max=2). Good.
Thus algorithm yields correct.
Now think about boundary conditions: n up to 1e5, so int for indices fine.
Also note: arrays are given as non-increasing, but we shouldn't assume input always respects that? According to problem, they guarantee it. So we can rely on sorted order.
Now about reading input: first line tc. Then for each test case, read n, then read A of size n, then B of size n. The sample input shows:
2
9
7 7 3 3 3 2 2 2 1
8 8 7 7 5 5 4 3 2
6
6 5 4 4 4 4
2 2 2 2 2 2
Yes.
Now we produce code.
Potential pitfalls: If we use long long for A and B, but for comparison >, it's fine.
Time complexity O(tc * n). With tc up to 50 and n up to 1e5 => 5e6 operations, fine.
Memory: O(n) per test case.
We can also use two vectors and reuse them.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
int n;
scanf("%d", &n);
vector<long long> A(n), B(n);
for (int i = 0; i < n; i++) scanf("%lld", &A[i]);
for (int i = 0; i < n; i++) scanf("%lld", &B[i]);
int i = 0, maxDiff = 0;
for (int j = 0; j < n; j++) {
while (i <= j && A[i] > B[j]) {
i++;
}
if (i <= j) {
maxDiff = max(maxDiff, j - i);
}
}
printf("%d\n", maxDiff);
}
return 0;
}
But note: maxDiff can be up to n-1, which fits int. So it's fine.
Let's test with given sample:
Input:
2
9
7 7 3 3 3 2 2 2 1
8 8 7 7 5 5 4 3 2
6
6 5 4 4 4 4
2 2 2 2 2 2
Our code yields 5 and 0. Good.
Potential issues: The problem statement says "M (A,B)= max (M(A[i],B[j])) for 0≤ i, j< n-1." That suggests indices go to n-2? But that's probably a mistake. However, sample uses n=9, indices 0..8. In the explanation, they refer to positions difference 5 for "3 in the second array" and "3 in the first array". Let's verify: A has 3 at indices 2,3,4; B has 3 at index 7. They say difference of positions is 5. Which indices? If A index 2 and B index 7, difference =5. So that's j-i=5 with i=2, j=7. So indeed indices up to 8. So the correct range is 0..n-1.
Thus our code is fine.
One more nuance: The arrays are non-increasing, but we are using the condition A[i] > B[j] in while. That's correct because we want A[i] <= B[j]. The condition to move i is while A[i] > B[j] and i <= j.
Edge: If n=1, then i=0, j=0. While loop condition? i<=j (true) and A[0] > B[0]? If false, then candidate =0-0=0, max=0. Output 0. That's correct because j-i=0, and if A[0]<=B[0] then M = j-i=0, else 0. So max is 0. But is there any scenario where answer should be >0 for n=1? j-i can only be 0. So fine.
Now, let's consider correctness proof more formally: The algorithm finds for each j the minimal i that satisfies A[i] <= B[j] and i <= j. That yields the maximum j-i for that j. Taking max over all j yields the overall maximum.
We need to ensure that the minimal i for each j indeed yields a valid i (since we check i <= j) and that no pair (i,j) with i > minimal but still valid could give larger j-i? Actually for fixed j, j-i is decreasing as i increases. So the smallest i yields the largest j-i. So if there exists any i satisfying condition, then the smallest such i gives the maximum possible j-i for that j. Therefore scanning j from 0 to n-1 and taking max over j of (j - minimal_i(j)) gives the global maximum because any optimal pair (i*, j*) will be considered when j = j*, and at that time, the minimal i (call i_min) for that j* will be <= i*, so j* - i_min >= j* - i*, so the algorithm's candidate is at least as large as the optimal value, and it can't exceed the true maximum because j - i_min is still a valid pair (i_min, j). So the algorithm yields exactly the maximum.
Thus correct.
Now we could also do the other direction: iterate i and find max j, which is also possible with two pointers, but we already have a neat solution.
Now we need to produce the final answer in the required format: print the monkiness for each test case on its own line.
Potential pitfalls: Input reading - need to handle large numbers, use %lld. Output use %d.
But there is nuance: In C++, if we use vector and scanf with %lld, it's fine.
Also, test case count could be large, so we should not mix cout and printf without syncing; using printf is fine.
Now, about constraints: Ai, Bi up to 1e12, which fits in long long.
Thus final solution is ready.
We'll also consider alternative languages like Python, but problem statement doesn't specify; we can provide code in C++ for efficiency.
Now we produce final answer: We'll include explanation and code.
But as ChatGPT, we need to output the final answer: I'll provide the solution description and code.
Given the instruction: "Print the monkiness of the two arrays." So just the integer per test case.
I think we're done.
Double-check: In the problem, they mention "M (A[i],B[j]) = j - i, if j ≥ i and B[j] ≥ A[i], or 0 otherwise." That's exactly what we use.
Edge case: What if j-i is negative? By definition, it's 0 if j < i. So we only consider j>=i.
Now, we can test a few more random cases with brute force for small n to verify algorithm.
Write a quick mental test:
n=5
A: [5,4,4,2,1]
B: [4,4,3,2,1]
Brute force pairs:
i=0,j=0: 5>4 false? Actually 5>4 => condition fails => 0.
i=0,j=1:5>4 fails
i=0,j=2:5>3 fails
i=0,j=3:5>2 fails
i=0,j=4:5>1 fails
i=1,j=1:4<=4 => diff=0
i=1,j=2:4>3 fails
i=1,j=3:4>2 fails
i=1,j=4:4>1 fails
i=2,j=2:4>3 fails? Actually A[2]=4>3 fails.
i=2,j=3:4>2 fails
i=2,j=4:4>1 fails
i=3,j=3:2<=2 diff=0
i=3,j=4:2>1 fails? 2>1 fails? Condition B[j] >= A[i] => B[4]=1 >=2? false. So fails.
i=4,j=4:1<=1 diff=0
Thus max=0.
Our algorithm:
i=0.
j=0: B0=4, while i<=j && A[0]>4? 5>4 true => i=1, i>j? 1>0 break. No candidate.
j=1: i=1, i<=1, A[1]=4>4? false, so candidate=1-1=0. max=0.
j=2: i=1? Actually i stays 1. i<=2 true, A[1]=4 > B[2]=3? true => while: i=2, i<=2 true, A[2]=4 >3 => i=3, i>j? 3>2 break. No candidate.
j=3: i=3, i<=3 true, A[3]=2 > B[3]=2? false => candidate=3-3=0.
j=4: i=3, i<=4 true, A[3]=2 > B[4]=1? true => while: i=4, i<=4 true, A[4]=1 >1? false, exit? Actually condition A[i] > B[j] => 1>1 false. So exit while with i=4 <= j=4, candidate=4-4=0. So max=0. Works.
Another test:
A: [5,5,3,2,2]
B: [6,5,4,3,2]
Expect best: i=2 (3) and j=4 (2) fails because B[4]=2 <3; i=2 and j=3 (3) works? B[3]=3 >=3 => diff=1; i=0,j=2:5>4 fails; i=0,j=1:5<=5 diff=1; i=1,j=2:5>4 fails; i=1,j=1:0; i=2,j=2:0; i=2,j=3:1; i=3,j=4:2? A[3]=2 <= B[4]=2 => diff=1? Actually 4-3=1; i=4,j=4:0; i=3,j=4=1; i=2,j=4 fails; i=3,j=3:0; i=0,j=4 fails. So max=1? Wait, i=0,j=2 fails; i=1,j=3? B[3]=3>=5? false; i=0,j=3:5>3 fails. So max=1.
But maybe there is i=0,j=1 diff=1; i=2,j=3 diff=1; i=3,j=4 diff=1. So max=1.
Algorithm:
i=0.
j0: B0=6, A0=5<=6 -> cand0.
j1: B1=5, A0=5<=5 -> cand1.
j2: B2=4, A0=5>4 => i=1; A1=5>4 => i=2; A2=3<=4 -> i=2 <=j2 -> cand0? 2-2=0.
j3: B3=3, i=2, A2=3<=3 -> cand 3-2=1.
j4: B4=2, i=2, A2=3>2 => while: i=3, A3=2<=2 -> i=3 <=j4 -> cand 4-3=1. max=1. Good.
Now test with A: [4,3,2,1]; B: [5,4,3,2]. n=4.
Brute: i=0,j=3: 4<=2? false; i=0,j=2:4<=3 false; i=0,j=1:4<=4 true diff=1; i=0,j=0:0; i=1,j=2:3<=3 true diff=1; i=1,j=3:3<=2 false; i=2,j=3:2<=2 true diff=1; i=2,j=2:0; i=3,j=3:0. max=1.
Algorithm: i=0.
j0: B0=5, A0=4<=5 cand0.
j1: B1=4, A0=4<=4 cand1.
j2: B2=3, A0=4>3 => i=1; A1=3<=3 cand 2-1=1.
j3: B3=2, i=1, A1=3>2 => i=2; A2=2<=2 cand 3-2=1. max=1.
Works.
Now test with A: [6,5,4,3]; B: [4,4,4,4]. n=4.
Brute: i=3,j=3:3<=4 diff0; i=2,j=3:4<=4 diff1; i=2,j=2:0; i=1,j=3:5>4 false; i=0,... fails. max=1.
Algorithm: i=0.
j0: B0=4, A0=6>4 => while i<=0 true => i=1, i>j break no candidate.
j1: i=1, i<=1, A1=5>4 => i=2, i>j break.
j2: i=2, i<=2, A2=4<=4 => cand0.
j3: i=2, i<=3, A2=4<=4 => cand 3-2=1. max=1. Good.
Now test with A: [10,9,8,7]; B: [9,8,7,6]. n=4.
Brute: i=0,j=0:10>9 false =>0; i=0,j=1:10>8 false; i=0,j=2:10>7; i=0,j=3:10>6; i=1,j=1:9>8 false; i=1,j=2:9>7 false; i=1,j=3:9>6 false; i=2,j=2:8>7 false; i=2,j=3:8>6 false; i=3,j=3:7>6 false; All fail => max=0.
Algorithm: i=0.
j0: B0=9, A0=10>9 => i=1 >j? i=1 >0? Actually while: i=0, condition true => i=1; now i=1>0 => break. no candidate.
j1: i=1, i<=1, A1=9>8 => i=2 >1 break.
j2: i=2, i<=2, A2=8>7 => i=3 >2 break.
j3: i=3, i<=3, A3=7>6 => i=4 >3 break. So max=0. Good.
All good.
One more edge: if multiple valid pairs with same difference, fine.
Thus solution stands.
Now, we must ensure to handle multiple test cases correctly: after each test case, we output answer.
We might also consider optimizing input reading using fast IO: ios_base::sync_with_stdio(false); cin.tie(0); and using cin/cout. But since we used scanf/printf, it's fine.
Potential problem: In some judges, "long long" may require %I64d instead of %lld on Windows. But typically in modern online judges, %lld works. To be safe, we can use cin/cout with sync disabled. However, with large input, scanf is fine. I'll use scanf.
Now we will produce final answer.
One more nuance: The problem might have 1-indexing? In sample, they say "difference between their positions is 5". They didn't specify if positions start from 0 or 1. But they used 0-index? Let's compute: In first sample, A has 3 at positions? If we count from 1, A's 3 first appears at index 3? Actually A: 7 (1), 7 (2), 3 (3), 3 (4), 3 (5), 2 (6), 2 (7), 2 (8), 1 (9). B: 8 (1), 8 (2), 7 (3), 7 (4), 5 (5), 5 (6), 4 (7), 3 (8), 2 (9). So the 3 in A at position 3 and the 3 in B at position 8 => difference = 5 (8-3). That's if positions start at 1. Our algorithm uses zero-based indices, yielding j-i = 7-2 =5, which matches if we subtract 1 from each position? Actually position = index+1. So (8-3) = 5 and (7-2)=5. So it's consistent regardless. So answer is the same numeric value.
Thus output is integer difference of positions (1-indexed? Actually they gave 5 as answer). So our computed j-i with zero-based indices gives 5. So that matches.
Therefore code works.
Now we produce final solution.
I will include explanation in final answer.
But the user only requested the code? The problem says "Print the monkiness of the two arrays." and it's a coding problem. We need to produce code. Usually in such interactive environment, we just need to output the code. But the user's request: "Big Chandan is a dire lover of Biryani... Find the monkiness..." So they want solution. I'll provide code in C++.
I'll also include brief explanation? The instruction says "Output format: Print the monkiness of the two arrays." So the answer is the number(s). But as ChatGPT we are to help solve the problem, we can output the code.
Given the context, I think we should provide the solution code. Also note that the problem statement might be from HackerEarth or similar, and they expect code.
Thus final: code.
But maybe we should also mention the algorithm. But I'll just output the code.
Given typical format, I'll include:
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
int n;
scanf("%d", &n);
vector<long long> A(n), B(n);
for (int i=0; i<n; ++i) scanf("%lld", &A[i]);
for (int i=0; i<n; ++i) scanf("%lld", &B[i]);
int i = 0, ans = 0;
for (int j = 0; j < n; ++j) {
while (i <= j && A[i] > B[j]) ++i;
if (i <= j) ans = max(ans, j - i);
}
printf("%d\n", ans);
}
return 0;
}
One more thing: The constraint says 1 ≤ Ai, Bi ≤ 10^12, so we used long long. Also note that we might need to use unsigned long long? But comparisons are fine with signed, as they are positive.
Edge: Could A[i] and B[j] be large enough that subtraction might be needed? No.
Now, the algorithm uses O(n) extra memory for arrays. Could we reduce memory by reading A and B and processing on the fly? Not straightforward because we need both arrays for two-pointer (we need to access A[i] and B[j] as we scan). However, we could read A first, store it, then read B and simultaneously process? But we need to process after reading both? Actually we can process while reading B? The algorithm processes j in order from 0 to n-1, and it requires A array for comparisons. Since we need to access A[i] for i up to n-1, we must have A stored. B we can read sequentially as we process j. That would reduce memory usage slightly (only need to store A, and read B element by element). But it's not required. However, we can implement that for efficiency. But since memory limit is likely generous, storing both is fine.
We can implement:
- Read A into vector.
- Then for j=0 to n-1: read B_j (maybe from input) and process.
But careful: reading B as we go requires that B is given exactly after A. That's true. So we can do:
vector<long long> A(n);
for i: read A[i];
int i = 0, ans = 0;
long long b; // each B[j]
for (int j = 0; j < n; ++j) {
scanf("%lld", &b);
while (i <= j && A[i] > b) ++i;
if (i <= j) ans = max(ans, j - i);
}
That uses O(1) extra memory for B, only store A. But A is size n. That's fine. This reduces memory usage by half, but not necessary.
I'll present the simpler version with both vectors.
But maybe I'll present the optimized version? I think either is acceptable.
Let's consider input reading: If we use scanf for each B[j] as we iterate, we need to ensure we read exactly n numbers. That's fine.
Potential issue: The input might not have newline after A? It will have numbers separated by spaces or newlines. scanf will handle it.
Thus we can do:
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
int n;
scanf("%d", &n);
vector<long long> A(n);
for (int i = 0; i < n; ++i) scanf("%lld", &A[i]);
int i = 0, ans = 0;
for (int j = 0; j < n; ++j) {
long long b;
scanf("%lld", &b);
while (i <= j && A[i] > b) ++i;
if (i <= j) ans = max(ans, j - i);
}
printf("%d\n", ans);
}
return 0;
}
That works and reduces memory. However, note that the array A is non-increasing. We assume input order is correct.
We need to be careful: The while condition uses A[i] > b. But if i becomes > j, we break. Since i can increase beyond j, later when j increases, i may be > j. But while condition includes i <= j. So it's fine. But after i passes j, we may still have i <= j later? Actually i only increases, so if i becomes > j, then for later j larger, it's possible that i becomes <= j again? For example, if i becomes 5 after processing j=4, then for j=5, i=5, it's <= j (5<=5). So yes, it's possible that i becomes equal to j later. So that's fine.
Now, what about the case when i becomes > n? While loop increments i while i <= j and condition holds. Since i max is n, but j max is n-1, so i could become n (i == n) if condition holds for all i up to n-1? At j=n-1, while could increment i to n if for all i <= j condition A[i] > b, but if i becomes n, then i <= j? n <= n-1 false, so while stops. Then after while, i is n, then we check if i <= j? false, so no candidate. That's safe. So we should ensure array A has size n; we only access A[i] when i <= j, and we already check i <= j before accessing A[i] in while condition. But after while, we may access A[i]? Only in while condition we access A[i] when i <= j, safe. So fine.
Thus the single-array version is safe.
I'll present that to show efficiency.
But the problem may require to print answer per test case. So I'll output code.
Now, I will write final answer accordingly.
Also note: The constraints test cases up to 50 and n up to 1e5, so total input numbers up to 2 * 5e6 = 1e7, which is fine for scanf.
Now final.
I'll produce final solution in C++.
Optionally include using int for indices.
Thus final code:
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<long long> a(n);
for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
int i = 0, ans = 0;
for (int j = 0; j < n; ++j) {
long long b;
scanf("%lld", &b);
while (i <= j && a[i] > b) ++i;
if (i <= j) {
int diff = j - i;
if (diff > ans) ans = diff;
}
}
printf("%d\n", ans);
}
return 0;
}
Yes.
Now, I will output this as final answer. ```cpp
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector A(n);
for (int i = 0; i < n; ++i)
scanf("%lld", &A[i]);
int i = 0, ans = 0;
for (int j = 0; j < n; ++j) {
long long b;
scanf("%lld", &b);
while (i <= j && A[i] > b)
++i;
if (i <= j)
ans = max(ans, j - i);
}
printf("%d\n", ans);
}
return 0;
}