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

Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
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)
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,...
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...
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.
Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
Write a recursive method to sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
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}.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT