Develop an algorithm in form of a pseudocode to solve the following problem: You are given an array of positive numbers, determine if there are any two integers in the array whose sum is equal to the given value X, and display all pairs of integers in the array that add up to X. Look at the below test case to guide you. (NB: do not write a C++ program but pseudocode) [5 Marks] Array: {7,1,2,8,4,3} Target sum/value (X)= 10 Expected Output: 7 + 3 = 10, 2 + 8 = 10
// arr - array of integers
// X - target sum
Set S = empty // set of pairs with sum equal X
for i in {0, length(arr)}
for j in {i + 1, length(arr)}
if arr[i] + arr[j] == X and { arr[i], arr[j] } not in S
// add pair to set
S += { arr[i], arr[j] }
for {x,y} in S
print "${x} + ${y} = ${X}\m"
Comments
Leave a comment