Question

In: Computer Science

Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem: input: an array A[lo..hi]...

Design and analyze asymptotically an O(nlgn) transform-conquer algorithm for the following problem:

    • input: an array A[lo..hi] of n real values;
    • output: true iff the array contains two elements (at different indices) whose sum is 2020.

Solutions

Expert Solution

# This is a simple C++ solution to the following Problem.

Algorithm:

step1: for each element in array

         push occurance of each element in the dictionay

step2: Again for each element in array:

         if dictionay[2020-arrary_element] is in the dictionay

                  print index of the array element

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

int main() {
        unordered_map<int, int> m; // creating a dictionary
        
        int n; // array size
        cin>>n;
        vector<int> arr(n);
        
        // taking array input
        for(int i=0; i<n; i++)
        {
            cin>>arr[i];
        }
        
        // storing the occurrence of each element of array in dictionary
        for(int i=0; i<n; i++){
            m[arr[i]]++;
        }
        
        for(int i=0; i<n; i++){
            if (m[2020-arr[i]]){
                cout<<arr[i]<<" "<<2020-arr[i]<<"\n";
                // I an priring the array pairs whose sum is 2020 but according to your requirement
                // you can print True/False or index of pairs
            }
        }
        return 0;
}

# Analysis:

The Time complexity of the above program will be O(n), best time complexity

We are traversing the whole array only once

We can use Brout force method for solving the above problem but time complexity will be O(n^2)

But in the above program First we transform whole array in the form of dictionary ( In C++ dictionary refers as map) and then we solve the next subproblem of this problem.


Related Solutions

Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array...
Divide and conquer approach to find the minimum absolute difference in array A[lo..hi] Input an array A[lo..hi] of n real numbers. Requirement: Shouldn't use a sorting algorithm. Complexity O(nlgn)
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
Design and analyze a divide-and-conquer algorithm for finding the maximum element in a list: L[0: n – 1].
The following submission rules apply:·    For those questions requiring programs, the solutions must be implemented using JavaScript or Java.o Appropriate self-documenting comments in the source code are mandatory, consistent with good programming practices.o Solutions must be provided in plain text so that formatting is not lost.·    All answers must be provided in this document.·    Sources must be given accurate and complete citations sufficient for the instructor to find and confirm them.Design and analyze a divide-and-conquer algorithm for finding the maximum...
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After...
a. Develop a divide-and-conquer algorithm to perform a parallel merge sort of an array. Hint: After division, each process sorts its part of the array using an efficient algorithm. Then, the subarrays are merged into larger sorted subarrays. b. Analyze the communication and computation times if the number of processes is equal to the number of array elements, n. c. Repeat Part b if the number of processes is less than the number of array elements. Assume that the computation...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity. Please provide a solution in Java
What is O(g(n)) for the following method? What sorting algorithm is this? /*Function to sort array...
What is O(g(n)) for the following method? What sorting algorithm is this? /*Function to sort array using ??? sort*/ void sort(int arr[]) { int n = arr.length; for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j];...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT