Skip to content
Onsite

Common Elements in Ascending and Descending Arrays

EasyP3Coding

a and b both have N elements. a is sorted in strictly ascending order and b is sorted in strictly descending order, so neither contains duplicates. Return the number of values that appear in both arrays.

Example 1

Input
a = [1,2,3,4], b = [4,3,2,1]
Output
4

Example 2

Input
a = [1,3,5], b = [6,4,2]
Output
0

Example 3

Input
a = [1,2,3], b = [3,2,1]
Output
3

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= a[i], b[i] <= 10^9
  • a is strictly ascending, b is strictly descending.

Hints

Run checks the 3 visible tests, Submit all 11.