Consider the problem where you are given an array of n digits
[di] and a positive integer
b, and you need to compute the value of the number in that
base.
In general, you need to compute
For example:
(1011)2 = 1(1) + 1(2) + 0(4) + 1(8) = 11;
(1021)3 = 1(1) + 2(3) + 0(9) + 1(27) = 34, and
(1023)4 = 3(1) + 2(4) + 0(16) + 1(64) = 75.
In these examples, I give the digits in the order d3d2d1d0, which corresponds to how we would normally write these numbers, though you can assume that di is in index i of the array for the questions below. (Yes, the indices will be numbered 0 to n - 1, not 1 to n.)
1. Give pseudocode for a divide-and-conquer algorithm that solves this problem by dividing the digit array into two subarrays of (roughly) the same size.
For example, d5d4d3d2d1d0 would be split into d5d4d3 and d2d1d0.
2. Give pseudocode for a divide-and-conquer algorithm that solves this problem by dividing the digit array into two interleaved arrays of (roughly) the same size.
For example, d5d4d3d2d1d0 would be split into d5d3d1 and d4d2d0.
1. Finding the value of function using divide and conquer.
//Before calling this function reverse the array to give you correct results.
int sumArray(int array[],int left,int right,int b) //initialize
left and right as -1 if the size of array is 0 and it assuming you
have initialized left and right as (1,n)
{
int sum=0,index;
if(left==-1 && right==-1)
return 0;
else if(left==right)
{
sum=pow(b,left)*array[0]; //sum
will give you (b^i)*d
}
//Divide and conquer part
int mid=right/2;
int lsum=sumArray(array,left,mid,b);
int rsum=sumArray(array,mid+1,right);
return lsum+rsum;
}
2. Using the same method as above to divide and conquer
int sumArray(int array1[],int array2[],int size,int b) //array 1
will store the values and array 2 will store indices
{
int sum=0,int array3[],array4[],pos=0;
if(size==0)
return 0;
else if(siz==1)
{
sum=pow(b,array2[0])*array1[0];
//sum will give you (b^i)*d
}
//Divide and conquer part
pos=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
array3[pos]=array1[i];
array4[pos]=array2[i];
}
}
int sum1=sumArray(array3,array4,i,b);//this will call
sum function for 1st interleaved array
for(int i=0;i<n;i++)
{
if(i%2!=0)
{
array3[pos]=array1[i];
array4[pos]=array2[i];
}
}
int sum2=sumArray(array3,array4,i,b);//this will call
sum function for 2nd interleaved array
return sum1+sum2;
}
Consider the problem where you are given an array of n digits [di] and a positive...
The zigzag sorting problem takes an array data of size n and outputs a per- mutation where data[1] <data[2] > data[3] = data[4] > data[5] S..., all the way to the end of the array. (In general data[i] = data[i+1] if i is odd and data[i] > data[i+1] if i is even.) Answer the following questions about developing algorithms for the zigzag sorting problem. 1. Give pseudocode for a brute force algorithm for zigzag sorting. Your pseudocode just needs to...
A divide-and-conquer algorithm solves a problem by dividing the input (of size n>1, T(1) =0) into two inputs half as big using n/2-1 steps. The algorithm does n steps to combine the solutions to get a solution for the original input. Write a recurrence equation for the algorithm and solve it.
A divide-and-conquer algorithm solves a problem by dividing the input (of size n>1, T(1) =0) into two inputs half as big using n/2-1 steps. The algorithm does n steps to combine the solutions to get a solution for the original input. Write a recurrence equation for the algorithm and solve it.
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
(5 +2 points) You are given an array A[1..n] of positive numbers where Ai] is the stock price on day i. You are allowed to buy the stock once and sell it at some point later. For each day you own the stock you pay S1 fee. Design a divide-and-conquer algorithm that will return a pair (i,j) such that buying the stock on day i and selling it on day j will maximize your gain The complexity of the algorithm...
Analysis Divide & Conquer: Analyze the complexity of algorithm A1 where the problem of size n is solved by dividing into 4 subprograms of size n - 4 to be recursively solved and then combining the solutions of the subprograms takes O(n2) time. Determine the recurrence and whether it is “Subtract and Conquer” or “Divide and Conquer“ type of problem. Solve the problem to the big O notation. Use the master theorem to solve, state which theorem you are using...
Given an n*m chocolate bar, you need to break it into n*m 1*1 pieces. You can break a bar only in a straight line, and only one bar can be broken at a time. Design an algorithm that solves the problem with the minimum number of bar breaks. Decrease and Conquer Divide and Conquer Transform and Conquer
(13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...
Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)
need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...