Equal Arrays
Practice
4.4 (7 votes)
Greedy algorithms
Basics of greedy algorithms
Algorithms
Problem
58% Success 2114 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given two arrays \(A\) and \(B\) of length \(N\) and \(M\) respectively. You can perform the following operation any number of times on \(A\) and \(B\).
- Replace any subarray of the array with the sum of the elements in the subarray.
You want to make \(A\) and \(B\) equal. Find the maximum possible length of arrays \(A\) and \(B\) such that \(A\) is equal to \(B\). If it is impossible to make \(A\) and \(B\) equal then print \(-1\).
Input format
- The first line contains an integer \(T\), which denotes the number of test cases.
- The first line of each test case contains two integers \(N\) and \(M\), denoting the length of the array \(A\) and \(B\).
- The second line of each test case contains \(N\) space-separated integers, elements of array \(A\).
- The third line of each test case contains \(M\) space-separated integers, elements of array \(B\).
Output format
For each test case, print the maximum possible length of arrays \(A\) and \(B\) such that \(A\) is equal to \(B\). If it is impossible to make \(A\) and \(B\) equal then print \(-1\) in a new line.
Constraints
\(1 \leq T \leq 10 \\ 1 \leq N, M \leq 10^5 \\ 1 \leq A[i], B[i] \leq 10^5\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
Greedy AlgorithmsAlgorithmsBasics of Greedy AlgorithmsGreedy algorithm
Points:50
43 votes
Tags:
TrieApprovedMedium-HardGreedy algorithm
3.Flip AB
Points:30
4 votes
Tags:
Brute ForceGreedy AlgorithmsBasics of Greedy AlgorithmsAlgorithms
Editorial