Javascript or python or java   Find a pair of entries from two lists that yield a sum that is as close to a target number as possible, without exceeding it. Each entry in a list is a key-value pair, where the key is a number identifier and the value is also a number. The output must be a list containing the pairs of numbers representing the identifiers that yield the result. If a solution is not possible, return an empty list. Notice the input could be not sorted. Example 1: Input: a = [2, 4, 6]], b = [2], target = 7, Output: [[1, 0]] Explanation: There are only three possible pairs, [0, 0], [1, 0], and [2, 0]. They yield the sum values of 2 + 2 = 4, 4 + 2 = 6 and 6 + 2 = 8 respectively. 6 is the largest number that does not exceed 7, therefore [1, 0] is the optimal pair.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Javascript or python or java

 

Find a pair of entries from two lists that yield a sum that is as close to a target number as possible, without exceeding it.


Each entry in a list is a key-value pair, where the key is a number identifier and the value is also a number. The output must be a list containing the pairs of numbers representing the identifiers that yield the result.
If a solution is not possible, return an empty list.
Notice the input could be not sorted.

Example 1:
Input:
a = [2, 4, 6]], b = [2], target = 7,
Output: [[1, 0]]
Explanation:
There are only three possible pairs, [0, 0], [1, 0], and [2, 0]. They yield the sum values of 2 + 2 = 4, 4 + 2 = 6 and 6 + 2 = 8 respectively.
6 is the largest number that does not exceed 7, therefore [1, 0] is the optimal pair.

Example 2:
Input:
a = [3, 5, 7, 10], b = [2, 3, 4, 5], target = 10,
Output: [[1, 3], [2, 1]]
Explanation:
There are two pairs possible. The element with id = 1 from list a has a value of 5, and the element with id = 3 from list b also has a value of 5. Combined, they add up to 10.
Likewise, the element with id = 2 from a has a value of 7, and the element with id = 1 from b has a value of 3. These also add up to 10.
Therefore, the optimal pairs are [1, 3] and [2, 1].

Example 3:
Input:
a = [8, 7, 14], b = [5, 10, 14], target = 20,
Output: [[2, 0]]

Example 4:
Input:
a = [8, 15, 9], b = [8, 11, 12], target = 20,
Output: [[0, 2], [2, 1]]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education