Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

This problem is asking to find if there exists a pair of integers in a given array such that the difference between them equals a given number K.

Here is a step-by-step solution:

  1. First, we need to read the number of test cases T.

  2. For each test case, we read the size of the array N and the number K.

  3. We then read the elements of the array.

  4. For each pair of elements in the array, we check if the absolute difference between them equals K. If such a pair is found, we print "true" and move on to the next test case.

  5. If no such pair is found after checking all pairs, we print "false".

  6. We repeat steps 2-5 for all test cases.

Here is a Python solution for the problem:

T = int(input())
for _ in range(T):
    N, K = map(int, input().split

This problem has been solved

Similar Questions

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

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.Constraints30 points1 <= T <= 1001 <= N <= 1000-106 <= A[i] <= 10670 points1 <= T <= 5001 <= N <= 10000-106 <= A[i] <= 106ExampleInput25-3 5 8 2 -465 -10 8 4 2 -3OutputYesNoExplanationExample 1:Possible values: 8 + (-4) = (-3) + 5 + 2.Example 2:No 2 elements exist whose sum is equal to the sum of the remaining array elements.

Given an array of unique integer elements, print all the subsets of the given array in lexicographical order.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array and the second line contains the elements of the array.Output FormatFor each test case, print the subsets of the given array in lexicographical order, separated by new line. Print an extra newline between output of different test cases.Constraints1 <= T <= 1001 <= N <= 100 <= A[i] <= 100ExampleInput335 15 3 257 96 43 15 8 23 Output3 3 5 3 5 15 3 15 5 5 15 15 57 57 96 96 3 3 8 3 8 15 3 8 15 23 3 8 23 3 15 3 15 23 3 23 8 8 15 8 15 23 8 23 15 15 23 23

You are given an array of integers. Sort them by frequency. See examples for more clarifications.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the elements of the array sorted by frequency. In case 2 elements have the same frequency, print the smaller element first.Constraints1 <= T <= 1001 <= N <= 10000-1000 <= A[i] <= 1000ExampleInput264 -2 10 12 -8 48176 -272 -272 -45 269 -327 -945 176Output-8 -2 10 12 4 4-945 -327 -45 269 -272 -272 176 176

You are given an array of 0's and 1's. Sort the array in ascending order and print it.Note: Solve using two-pointer technique.Input FormatFirst line of input contains T - the number of test cases. Its followed by 2T lines, the first line contains N - the size of the array.The second line contains the elements of the array.Constraints1 <= T <= 10001 <= N <= 10000 <= A[i] <= 1Output FormatFor each test case, sort the array in ascending order and print it on a new line.Sample Input 0250 1 1 0 161 1 1 1 1 0Sample Output 00 0 1 1 10 1 1 1 1 1Explanation 0

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.