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.

Please provide a solution in Java

Solutions

Expert Solution

Code -

public class Main
{
//function findNegativeIndex to get the index from array
static int findNegativeIndex(int a[], int s,int e){
//declare the varaible
int start = s,end = e-1,index=-1;
//while start < end
while(start<end){
//if negative number is found then return the index
if(a[start]<0)
{
index = start;
break;
}
//if negative number is found then return the index
else if(a[end]<0){
index = end;
break;
}
//else increment start and decement end
else{
start++;
end--;
}
}
return index;
}
//function to divide the array into 2 array
static int findNumberWithNegativeIndex(int a[],int size){
//varaible index be -1
int index = -1;
//divide the array from 0 to size/2 and call function findNegativeIndex
if(findNegativeIndex(a,0,size/2)!=-1){
index = findNegativeIndex(a,0,size/2);
}
//divide the array from size/2 to size and call function findNegativeIndex
else if(findNegativeIndex(a,(size/2),size)!=-1){
index = findNegativeIndex(a,(size/2),size-1);
}
//return the index value
return index;
}
   public static void main(String[] args) {
   //initiazlize an array
   int a[] = {23,56,12,-76,12,54,43,14,67,98};
   //call function to divide the array in two halfs and find the negative
   int index = findNumberWithNegativeIndex(a,a.length);
   //print the negative index value
   if(index!=-1)
   System.out.println("Negative index "+index);
   else
   System.out.println("Negative number not present ");
   }
}

Screenshots -

   int a[] = {23,56,12,-76,12,54,43,14,67,98};

Output -

Timecomplexity average - O(logn)


Related Solutions

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.
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT