Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

// IMPORT LIBRARY PACKAGES NEEDED BY YOUR PROGRAM
import java.util.List;
// SOME CLASSES WITHIN A PACKAGE MAY BE RESTRICTED
// DEFINE ANY CLASS AND METHOD NEEDED
// CLASS BEGINS, THIS CLASS IS REQUIRED
class Solution
{
// METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED
int itemPairs(int num, List<Integer> itemValues, int target)
{
// WRITE YOUR CODE HERE
}
// METHOD SIGNATURE ENDS
}

You are given an array of integers, where each item in the array represents the price of a
given item. You are also given an integer representing the target sale value. You must gather
distinct pairs of items that satisfy the sales target. Distinct pairs are pairs that differ in at least
one element.
Given the list of prices, write an algorithm to find the number of distinct pairs of items where
the sum of each pair's values is exactly equal to the target sales.
Input
The input to the function/method consists of three arguments:
num, an integer representing the number of items;
itemValues, a list of integers representing the item price;
target, an integer representing the target sales.
Output
Return an integer representing the number of pairs having the sum of their prices equals to
the target sales.
Constraints
1 s num s 5* 105
OsitemValues[i] s 109
Osi< num
Os target s 5* 109
Example
Input:
num = 9
expand button
Transcribed Image Text:You are given an array of integers, where each item in the array represents the price of a given item. You are also given an integer representing the target sale value. You must gather distinct pairs of items that satisfy the sales target. Distinct pairs are pairs that differ in at least one element. Given the list of prices, write an algorithm to find the number of distinct pairs of items where the sum of each pair's values is exactly equal to the target sales. Input The input to the function/method consists of three arguments: num, an integer representing the number of items; itemValues, a list of integers representing the item price; target, an integer representing the target sales. Output Return an integer representing the number of pairs having the sum of their prices equals to the target sales. Constraints 1 s num s 5* 105 OsitemValues[i] s 109 Osi< num Os target s 5* 109 Example Input: num = 9
Example
Input:
num = 9
itemValues = [5, 7, 9, 13, 11, 6, 6, 3, 3]
target = 12
Output:
3
Explanation:
There are 8 possible pairs of prices that have the sum of their values equal to the target 12.
(itemValues[0] = 5, itemValues[1] = 7)
(itemValues[1] = 7, itemValues[0] = 5)
(itemValues[2] = 9, itemValues[7] = 3)
(itemValues[7] = 3, itemValues[2] = 9)
(itemValues[2] = 9, itemValues[8] = 3)
(itemValues[8] = 3, itemValues[2] = 9)
(itemValues[5] = 6, itemValues[6] = 6)
(itemValues[6] = 6, itemValues[5] = 6)
%3D
The first two pairs are same and considered as one distinct pair. Next four pairs are same and
considered as one distinct pair. Similarly, last two pairs are same and considered as one
distinct pair.
There are only 3 distinct pairs of prices: (5, 7), (9, 3) and (6, 6).
expand button
Transcribed Image Text:Example Input: num = 9 itemValues = [5, 7, 9, 13, 11, 6, 6, 3, 3] target = 12 Output: 3 Explanation: There are 8 possible pairs of prices that have the sum of their values equal to the target 12. (itemValues[0] = 5, itemValues[1] = 7) (itemValues[1] = 7, itemValues[0] = 5) (itemValues[2] = 9, itemValues[7] = 3) (itemValues[7] = 3, itemValues[2] = 9) (itemValues[2] = 9, itemValues[8] = 3) (itemValues[8] = 3, itemValues[2] = 9) (itemValues[5] = 6, itemValues[6] = 6) (itemValues[6] = 6, itemValues[5] = 6) %3D The first two pairs are same and considered as one distinct pair. Next four pairs are same and considered as one distinct pair. Similarly, last two pairs are same and considered as one distinct pair. There are only 3 distinct pairs of prices: (5, 7), (9, 3) and (6, 6).
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education