Bài đăng phổ biến

6/02/2019

Problem 2

Write a program that takes two integers N and K and a sorted array of N integers as input. Your program should output the number of different pair of positions (a,b) from the array such that the sum of the two elements in positions 'a' and in position 'b' equals K.

Note 1: The integers N and K will be given in the first line separated by a single space and the array of N integers will be given in the second line. Each integer separated by a single space character ' '.

Note 2: Note that two pairs of positions (a,b) and (c,d) are considered equal if and only if 'a' is equal to 'c' and 'b' is equal to 'd'.

Note 3: Note that for a pair of positions (a,b), 'a' can be equal to 'b' in some cases.

Case 1:

For input provided as follows:

5 3
1 1 1 2 3

Output of the program will be:

6

Description of the output:

The following pair of 0-based positions {(0,3), (3,0), (1,3), (3,1), (2,3), (3,2)} sums to 3, as 1+2 and 2+1 equals 3.

Case 2:

For input provided as follows:

4 4
1 1 3 3

Output of the program will be:

8

Description of the output:

There are exactly 8 pairs in which the sum of the elements in their positions equals 4. They're {(0,2), (2,0), (0,3), (3,0), (1,2), (2,1), (1,3), (3,1)}.

0 comments:

Đăng nhận xét