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 Os temvaluesti] s 109 Osi< num Os target s 5* 109 Example Input: num = 9

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

// 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
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).
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Generic Type
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