Question

Develop a divide-conquer algorithm with complexity nlogn for finding the maximum difference of a give array...

Develop a divide-conquer algorithm with complexity nlogn for finding the maximum difference of a give array A= a0,a1, ……an-1, i.e finding the indices m and k, m>=0 ,k<=n-1, and m<k such that ak- am is the maximum difference.

Provide a solution in Java.

Homework Answers

Answer #1

CODE

public class Main

{

public static int maxDiff (int arr[])

{

int n = arr.length;

int diff = arr[1] - arr[0];

int curr_sum = diff;

int max_sum = curr_sum;

for(int i = 1; i < n - 1; i++)

{

diff = arr[i + 1] - arr[i];

if (curr_sum > 0)

curr_sum += diff;

else

curr_sum = diff;

if (curr_sum > max_sum)

max_sum = curr_sum;

}

return max_sum;

}

public static void main(String[] args)

{

int arr[] = {47, 64, 2, 18, 98, 12};

System.out.print("Maximum difference is " + maxDiff(arr));

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Assume that we have a sorted array a[n]with n non-negative numbers. a. Develop an algorithm using...
Assume that we have a sorted array a[n]with n non-negative numbers. a. Develop an algorithm using divide-and-conquer to search for an element x in this sorted array a[n]. This algorithm will take an input of a sorted array a[n], and return the index of element x is an element of a[n], or return -1if xis NOT an element of this array. b. Analyze the time complexity of this algorithm.
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If...
USING C++. The following divide-and-conquer algorithm is proposed for finding the simultaneous maximum and minimum: If there is one item, it is the maximum and minimum, and if there are two items, then compare them, and in one comparison you can find the maximum and minimum. Otherwise, split the input into two halves, divided as evenly as possibly (if N is odd, one of the two halves will have one more element than the other). Recursively find the maximum and...
3. (10 marks) Describe a recursive algorithm for finding the maximum element in a array A...
3. Describe a recursive algorithm for finding the maximum element in a array A of n elements. Analyze its time complexity using a recursion tree.
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
A speedy Decrease-and-Conquer search. Use your newly acquired knowledge of “Decrease-and-Conquer” algorithm design strategy to design...
A speedy Decrease-and-Conquer search. Use your newly acquired knowledge of “Decrease-and-Conquer” algorithm design strategy to design a O( n ) algorithm to search for a given number in an n × n matrix in which every row and every column in this matrix is sorted in increasing order Write your algorithm as an elegant java method named findElement(int [][] arr, int element) that returns the index of that element in the array as a small int array made of 2...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
You are an algorithm designer and you came up with four different divide-and-conquer algorithms for some...
You are an algorithm designer and you came up with four different divide-and-conquer algorithms for some problem Q. Those four algorithms are described below in parts (1), (2), (3), and (4). You wrote those descriptions a long time ago so now you want to remind yourself, for each one of them, what was the corresponding recurrence relation and provide an upper bound on the running time. So first give the recurrence then solve it using any method of your choice...
JAVA - algorithm analysis and sorting Question 1 Which of the following features grows fastest? 1....
JAVA - algorithm analysis and sorting Question 1 Which of the following features grows fastest? 1. N2 2. log N 3. N log N 4. N 5. 10 Question 2 Given the following code segment: for( int i = 1; i < n; i++ ) for( int j = 1; j < i; j++ ) k = k + i + j; What is the runtime of the code segment? 1. None of the above 2. O(i*N) 3. O(N2) 4....