Single File Programming QuestionProblem StatementKrish is a computer science student who loves solving challenging problems. Recently, he came across an interesting problem related to arrays. The problem requires Krish to find all pairs of elements in a given array whose sum equals a specific target value. Write a program for Krish that takes an array of integers and a target sum as input. The program should print all pairs of elements from the array whose sum equals the target sum. If no such pairs are found, it should print "Not found".ExampleInput:5 // number of elements10 20 30 40 50 // elements60 // target sumOutput:10 5020 40Explanation: The pair with 60 as sum are 20 + 40 = 60 and 10 + 50 = 60.Input format :The first line consists of an integer N, representing the number of elements in the array.The second line consists of N space-separated integers, representing the elements of the array.The third line consists of an integer M, representing the target sum.Output format :For each pairs found, print two integers separated by a space, whose sum equals the target sum.If no such pairs are found, print "Not found".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:2 ≤ N ≤ 251 ≤ Each element ≤ 1003 ≤ M ≤ 200Sample test cases :Input 1 :32 3 65Output 1 :2 3Input 2 :510 20 30 40 5060Output 2 :10 5020 40Input 3 :42 3 4 78Output 3 :Not foundInput 4 :310 6 410Output 4 :6 4
Question
Single File Programming QuestionProblem StatementKrish is a computer science student who loves solving challenging problems. Recently, he came across an interesting problem related to arrays. The problem requires Krish to find all pairs of elements in a given array whose sum equals a specific target value. Write a program for Krish that takes an array of integers and a target sum as input. The program should print all pairs of elements from the array whose sum equals the target sum. If no such pairs are found, it should print "Not found".ExampleInput:5 // number of elements10 20 30 40 50 // elements60 // target sumOutput:10 5020 40Explanation: The pair with 60 as sum are 20 + 40 = 60 and 10 + 50 = 60.Input format :The first line consists of an integer N, representing the number of elements in the array.The second line consists of N space-separated integers, representing the elements of the array.The third line consists of an integer M, representing the target sum.Output format :For each pairs found, print two integers separated by a space, whose sum equals the target sum.If no such pairs are found, print "Not found".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:2 ≤ N ≤ 251 ≤ Each element ≤ 1003 ≤ M ≤ 200Sample test cases :Input 1 :32 3 65Output 1 :2 3Input 2 :510 20 30 40 5060Output 2 :10 5020 40Input 3 :42 3 4 78Output 3 :Not foundInput 4 :310 6 410Output 4 :6 4
Solution
Here is a Python solution for the problem:
def find_pairs(arr, target):
pairs = []
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] + arr[j] == target:
pairs.append((arr[i], arr[j]))
return pairs
# read the number of elements
n = int(input().strip())
# read the elements
arr = list(map(int, input().strip().split()))
# read the target sum
target = int(input().strip())
# find the pairs
pairs = find_pairs(arr, target)
# print the pairs
if pairs:
for pair in pairs:
print(pair[0], pair[1])
else:
print("Not found
Similar Questions
Single File Programming QuestionProblem statementTheo, an aspiring mathematician, has presented you with a challenge. He wants you to create a program that calculates the absolute difference between the sum of two arrays.Create a program to find the absolute difference between two arrays with a function named calculateArraySum and calculateAbsoluteDifference where the array is passed as an argument.Input 10-100 -49 -87 5 6 7 0 100 37 5710-100 -75 -48 -86 -98 98 97 98 67 100Output 77ExplanationTwo arrays with 10 elements each are given as input.The first array has elements: -100, -49, -87, 5, 6, 7, 0, 100, 37, 57, and the sum of the first array is - 24.The second array has elements: -100, -75, -48, -86, -98, 98, 97, 98, 67, 100, and the sum of the second array is 53.The absolute difference of the sum is calculated by subtracting the second array from first array |-24 - 53| = 77, and the result is printed.Input format :The first line of input is an integer value 'N1', representing the number of elements in the first array.The second line of input consists of N1 space-separated integers arr1[i], representing the elements of the first array.The third line of input is an integer value 'N2', representing the number of elements in the second array.The fourth line of input consists of N2 space-separated integers arr2[i], representing the elements of the second array.Output format :The output displays a single integer the absolute difference between the sums of the elements in the two arrays.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N1, N2 ≤ 10-100 ≤ arr1[i], arr2[i] ≤ 100Sample test cases :Input 1 :35 8 332 6 4Output 1 :4Input 2 :6-2 5 0 8 -1 364 -3 2 7 1 6Output 2 :4Input 3 :10-100 -49 -87 5 6 7 0 100 37 5710-100 -75 -48 -86 -98 98 97 98 67 100Output 3 :77
Stefan is working on a coding project that involves finding all pairs in an integer array that sum up to a specific target value.He needs to create a program to automate this task. The program should take an array of integers and a target value as input and then find and display all pairs of integers in the array that add up to the given target.Help Stefan to complete the project.Input format :The first line of input consists of an integer N, representing the number of elements in the array.The second line consists of N space-separated integers, representing the elements of the array.The third line consists of an integer T, which is the sum Stefan wants to find pairs for.Output format :The first line of output prints "Pairs that sum up to [T]:".The following lines print all pairs of integers that sum up to the given target in the format: "X + Y", where X and Y are the integers that sum up to T.Refer to the sample output for formatting specifications.Code constraints :1 ≤ N ≤ 201 ≤ array elements, T ≤ 100Sample test cases :Input 1 :52 3 5 7 810Output 1 :Pairs that sum up to 10:2 + 83 + 7Input 2 :83 8 15 6 12 7 9 217Output 2 :Pairs that sum up to 17:8 + 915 + 2Input 3 :126 10 14 25 3 11 18 1 7 40 15 934Output 3 :Pairs that sum up to 34:25 + 9
Single File Programming QuestionProblem StatementYou are given an integer array of N elements. Find out the sum of the first N/2 elements and the sum of the last N/2 elements using pointers.ExampleInput: 102 2 2 2 2 3 3 3 3 3Output: 1015Explanation:N = 10 -> N/2 = 5sum of the first 5 elements = 2+2+2+2+2 = 10sum of the last 5 elements = 3+3+3+3+3 = 15Note: This question helps in clearing conceptual interviews of service-based companies.Input format :The first line of input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the array of elements.Output format :The first line of output prints the sum of the first N/2 elements of the given array.The second line prints the sum of the last N/2 elements of the given array.Refer to the sample output for formatting specifications.Code constraints :N is an even number.10 ≤ N ≤ 50-50 ≤ array elements ≤ 50Sample test cases :Input 1 :102 2 2 2 2 3 3 3 3 3Output 1 :1015Input 2 :207 26 17 23 -6 -23 26 -23 -25 -18 -18 -5 3 22 -9 -22 -28 18 -16 21Output 2 :4-34
Single File Programming QuestionProblem statementGinny, an inquisitive mind fascinated by the intricacies of arrays, has posed a challenge for you. She needs a program that can identify and display the unique elements present in an array. Write a program that includes a function named findUniqueElements where the array is passed as an argument.Input format :The first line of input is an integer value 'N', representing the size of the array.The second line of input consists of N space-separated integers arr[i], representing the elements of the array.Output format :The output displays the integers, representing the unique elements identified within the array, separated by spaces.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 151 ≤ arr[i] ≤ 100Sample test cases :Input 1 :73 1 5 2 5 4 3Output 1 :1 2 4 Input 2 :155 2 7 1 4 9 8 6 3 2 5 10 7 12 100Output 2 :1 4 9 8 6 3 10 12 100 Input 3 :23 4Output 3 :3 4
Single File Programming QuestionProblem StatementAlex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.Refer to the sample output for formatting specifications.Code constraints :The given test cases will fall under the following constraints:2 ≤ N ≤ 101 ≤ elements ≤ 1000Sample test cases :Input 1 :514 29 37 48 5948Output 1 :The target value 48 is present at index 3Input 2 :10125 137 248 358 489 587 652 741 823 912850Output 2 :The target value 850 is not present in the array
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.