Question

In: Computer Science

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.

Solutions

Expert Solution

#include <iostream>

#include <climits>

using namespace std;

void findNegativeDivideAndConquer(int arr[], int start, int end, int& ans)

{

if (start == end)

{

if (arr[start] < 0)

ans = arr[start];

return;

}

int mid = (start + end) / 2;

findNegativeDivideAndConquer(arr, start, mid, ans);

findNegativeDivideAndConquer(arr, mid + 1, end, ans);

}

int main()

{

int arr[] = { 3,4,5,2,5,-9,5,4,1,3,4,5,78};

int ans = 0;

findNegativeDivideAndConquer(arr, 0, 12, ans);

cout << "Negative Element is: " << ans<<endl;

return 0;

}

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

SEE OUTPUT, Time Complexity is O(n), Average case is also N

Thanks, PLEASE COMMENT if there is any concern.

ALGORITHM f

ALGORITHM findNegativeDivideAndConquer ( arr[], start, end, ans )

START

if start == end

if (arr[start] < 0)

ans = arr[start];

return ans

mid = (start + end) / 2

findNegativeDivideAndConquer(arr, start, mid, ans)

findNegativeDivideAndConquer(arr, mid + 1, end, ans)

END

This will take O(N) time in average, PLEASE UPVOTE


Related Solutions

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.
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 ? integers. Divide ? into three subsets such that the total...
Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you
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)
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
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).
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is...
3. Suppose that a divide and conquer algorithm for multiplication of n x n matrices is found such that it requires 6 multiplications and 31 additions of n/2 x n/2 submatrices. Write the recurrence for the running time T(n) of this algorithm and find the order of T(n).
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of...
The following divide-and-conquer algorithm is designed to return TRUE if and only if all elements of the array have equal values. For simplicity, suppose the array size is n=2k for some integer k. Input S is the starting index, and n is the number of elements starting at S. The initial call is SAME(A, 0, n). Boolean SAME (int A[ ], int S, int n) { Boolean T1, T2, T3; if (n == 1) return TRUE; T1 = SAME (A,...
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5...
Q. Explain how a divide and conquer algorithm for detecting whether or not the number 5 exists in an array would work. Your explanation should include: - a description of your algorithm using psuedocode - a proof of correctness of your algorithm - an analysis of the runtime of the algorithm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT