Knowee
Questions
Features
Study Tools

Given an array of integers and a number K, check if there exist a pair of indices i,j s.t. a[i] + a[j] = K and i!=j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, first line of each test case contains N - the size of the array and K, and the next line contains N space separated integers - the elements of the array.Output FormatFor each test case, print "True" if such a pair exists, "False" otherwise, separated by newline.Constraints30 points1 <= T <= 1002 <= N <= 100070 points1 <= T <= 3002 <= N <= 10000General Constraints-108 <= K <= 108-108 <= ar[i] <= 108ExampleInput35 -15-30 15 20 10 -102 2010 104 7-4 0 10 -7OutputTrueTrueFalse

Question

Given an array of integers and a number K, check if there exist a pair of indices i,j s.t. a[i] + a[j] = K and i!=j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, first line of each test case contains N - the size of the array and K, and the next line contains N space separated integers - the elements of the array.Output FormatFor each test case, print "True" if such a pair exists, "False" otherwise, separated by newline.Constraints30 points1 <= T <= 1002 <= N <= 100070 points1 <= T <= 3002 <= N <= 10000General Constraints-108 <= K <= 108-108 <= ar[i] <= 108ExampleInput35 -15-30 15 20 10 -102 2010 104 7-4 0 10 -7OutputTrueTrueFalse

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is asking to find if there are two numbers in an array that add up to a given number K. Here are the steps to solve this problem:

  1. Parse the input. The first line of input is the number of test cases T. Each test case has two lines. The first line of the test case contains the size of the array N and the target sum K. The second line of the test case contains N space-separated integers - the elements of the array.

  2. For each test case, do the following:

    a. Initialize an empty set.

    b. Iterate over each element in the array. For each element, calculate the difference between K and the current

This problem has been solved

Similar Questions

You are given an integer array and an integer K. You have to tell if there exists a pair of integers in the given array such that ar[i]-ar[j]=K and i≠j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N and K - the size of the array and the number K. The second line contains the elements of the array.Output FormatFor each test case, print "true" if the arrays contains such elements, "false" otherwise, separated by new line.Constraints40 points2 <= N <= 100060 points2 <= N <= 100000General Constraints1 <= T <= 100-105 <= ar[i], K <= 105ExampleInput25 601 20 40 100 8010 1112 45 52 65 21 645 234 14 575 112Outputtruefalse

You are given 2 arrays A and B of same size N. A pair is said to be a valid if and only if i < j, A[i] == B[j] and A[j] == B[i]. You have to count total number of valid pairs.Input FormatFirst line of input contains T - number of test cases. First line of each test case contains N - size of the array. The second and third line of each test case contains N integers - elements of the array A and B.Output FormatFor each test case, print the count of number of valid pairs, separated by newline.Constraints30 points1 <= N <= 10370 points1 <= N <= 105General Constraints1 <= T <= 1000 <= A[i], B[i] <= 109ExampleInput231 2 33 2 151 3 7 9 93 1 9 7 7Output13ExplanationTest Case 1:There is only one valid pair - (0, 2). This is because A[0] == B[2] and A[2] == B[0].Test Case 2:There are 3 valid pairs - (0, 1), (2, 3), (2, 4). For example (0, 1) is valid because A[0] == B[1] and A[1] == B[0].

Sum of 2 NumbersGiven an array, check if there exist 2 elements of the array such that their sum is equal to the sum of the remaining elements of the array.Input FormatThe first line of input contains T - the number of test cases. It is followed by 2T lines, the first line contains N - the size of the array. The second line contains N integers - elements of the array.Output FormatFor each test case, print "Yes" if such elements exist, and "No" otherwise, separated by a new line.

Consider an array of integers, . Find and print the total number of pairs such that where .Input FormatThe first line contains an integer, , denoting the number of elements in the array.The second line consists of space-separated integers describing the respective values of .ConstraintsScoring for of the test cases. for of the test cases. for of the test cases.Output FormatPrint a long integer denoting the total number pairs satisfying where .Sample Input5 1 1 2 4 2Sample Output8ExplanationThere are eight pairs of indices satisfying the given criteria: , , , , , , , and . Thus, we print as our answer.Contest ends in 2 hoursSubmissions: 43Max Score: 10Difficulty: AdvancedRate This Challenge: More Python 31#!/bin/python32​3import math4import os5import random6import re7import sys8​9#10# Complete the 'solve' function below.11#12# The function is expected to return a LONG_INTEGER.13# The function accepts INTEGER_ARRAY arr as parameter.14#15​16def solve(arr):17    # Write your code here18    19if __name__ == '__main__':20    fptr = open(os.environ['OUTPUT_PATH'], 'w')21​22    arr_count = int(input().strip())23​24    arr = list(map(int, input().rstrip().split()))25​26    result = solve(arr)27​28    fptr.write(str(result) + '\n')29​30    fptr.close()31​

Given an array of elements. Find two elements in the array such that their sum is equal to the given element K.Input format :The first line of the input consists of the value of n.The second line of the input consists of the array of elements separated by space.The third line of the input consists of the sum.Output format :The output prints whether the array has a pair of elements with the given sum.Sample test cases :Input 1 :61 4 45 6 10 -816Output 1 :Array has two elements with given sum 16Input 2 :61 4 45 6 10 -860Output 2 :Array doesn't have two element

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.