RELATED POSTS
- Blogger Comment
- Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
C/C++,java,python c++ keywords, identifiers, data types,variables,modifiers,storage classes,operators,for loop ,while loop,do while loop,if else statement,nested loops,break continue goto statement,functions ,call by value call by reference ,arrays ,strings,pointers,data structures ,classes and objects,inheritance,polymorphism,operator overloading,function overloading,data abstraction,data encapsulation,dynamic memory allocation for array and objects,function templates,class templates,
Given array/list can contain duplicate elements.
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First line of each test case or query contains an integer 'N' representing the size of the first array/list.
Second line contains 'N' single space separated integers representing the elements in the array/list.
Third line contains an integer 'X'.
For each test case, print the total number of pairs present in the array/list.
Output for every test case will be printed in a separate line.
1 <= t <= 10^2
0 <= N <= 10^3
0 <= X <= 10^9
Time Limit: 1 sec
1
9
1 3 6 2 5 4 3 2 4
7
7
2
9
1 3 6 2 5 4 3 2 4
12
6
2 8 10 5 -2 5
10
0
2
Since there doesn't exist any pair with sum equal to 12 for the first query, we print 0.
For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5).
c++ code:
void pairSum(int input[], int size, int x) {
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(input[i]+input[j] == x)
{
if(input[i]<input[j])
{
cout<<input[i]<<" "<<input[j]<<endl;
}else
{
cout<<input[j]<<" "<<input[i]<<endl;
}
}
}
}
}
0 comments:
Post a Comment