Question

In: Computer Science

Consider the problem of finding if an array contains a pair of integers with a sum...

Consider the problem of finding if an array contains a pair of integers with a sum of 100. The array contains n integers.

a. Define the signature (header) of a C++ function that solves this problem (hint: inputs/outputs of function)

b. Write the pseudocode or C++ body of the function (extra credit: write the most efficient algorithm)

c. What is the asymptotic complexity of your algorithm?

d. What would be the complexity of the best algorithm for this problem if the array were sorted?

Solutions

Expert Solution

a) bool check_sum(int arr[],int n,int sum);

Return value = bool

True: if pair with sum 100 exists

False:: otherwise

Input: arr[] of integers

n: size of array

sum : sum = 100 value to be obtained

b)

Code:

#include<bits/stdc++.h>
using namespace std;

bool check_sum(int arr[],int n,int sum){
        map <int,int> mp; // use a map
        for(int i=0;i<n;i++){
                int complement = sum - arr[i];
                if(mp.find(complement)!=mp.end()){ 
                        return true; // sum exists so return true
                }
                mp[arr[i]] = 1; // put value to map
        }
        return false;
}

int main() {
        int n = 5; //size of array
        int arr[] = {80,10,20,10,5};
        
        bool res = check_sum(arr,n,100);
        if(res == true){
                cout<<"A pair with sum = 100 exists";
        } else{
                cout<<"Pair with Sum = 100 doesn't exist";
        }
        
        return 0;
}

Code Screenshot:

Code output:

========================

c) The algorithm traverses through array once and uses a map to find if pair exists.

Time: O(n)

Space: O(n)

=========================

d) If the array was sorted we can use a two pointer approach to find if the sum exists or not.

Time: O(n)

Space : O(1)

===========================


Related Solutions

Write an application that uses multithreading to compute the sum of the integers in an array...
Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For example, an array A1 of length 5 might contain the integers 3, 6, 5, 1, and 4. In an array of this form, one number between 1 and n+1 will be missing. In A1, the integer 2 is missing from the array. As another example, consider the array A2 that contain the integers 4, 7, 5, 2, 6, and 1. A2 has size 6,...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
Consider Cardano's problem of finding two numbers whose sum is 10 and whose product is 40....
Consider Cardano's problem of finding two numbers whose sum is 10 and whose product is 40. a) Cardano knew beforehand that no such (real) numbers existed. How did he know? Can you prove it? b) Solve the system of equations x+y=10 and xy=40 to find Cardano's complex solution. c) Check that this solution does work-that is, thatb the sum of your complex numbers is 10 and that their product is 40.
When finding the minimum positive subsequence sum (mpss) for an array a = a0, a1, ....
When finding the minimum positive subsequence sum (mpss) for an array a = a0, a1, . . . , an−1 of integers, one can use the following divide-and-conquer algorithm. Divide the array into two equal subarrays aleft and aright, and recursively compute the mpss of both subarrays. Then compute the mpss, call if mpssmid, of any subsequence that crosses the boundary between aleft and aright. Finally, take the minimum positive value of the three answers. Answer the following questions pertaining...
This is a Combinatorics Problem Consider the problem of finding the number of ways to distribute...
This is a Combinatorics Problem Consider the problem of finding the number of ways to distribute 7 identical pieces of candy to 3 children so that no child gets more than 4 pieces. Except Stanley (one of the 3 children) has had too much candy already, so he’s only allowed up to 2 pieces. Write a generating function & use your generating function to solve this problem.
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT