Question

In: Computer Science

LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...

LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints.

void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest)

• Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions.

• Comment on the logic of your code, e.g., what is true after the two recursive calls?

• Answer the three questions in the comment of your code. •

Test the function in main(), by calling the function to find out largest and second largest values in a vector of length 2, 3 and 4, and make sure you include cases where the largest and second largest values are the same, e.g., for vector {3,4,4}, the top two largest values are both 4.

Pseudocode

/* return largest and second largest element in list l[left...right] precondition: left+1<=right, i.e., the list contains at least two elements postcondition: the largest and secondLargest will be set to the largest element and second largest element respectively */

LargestTwo (l[], left, right, largest, secondLargest)

{ //Write base case(s) and solutions here if (left+1==right) //i.e., there are two elements in the list

//Fill in the blank else

if (left+2==right) //i.e., there are three elements in the list

//Fill in the blank, i.e., try to set largest, secondLargest

// to the largest and secondLargest values in l[left...right]

mid = (left+right)/2

LargestTwo (l, left, mid, leftL, leftSL)

LargestTwo (l, mid+1, right, rightL, rightSL)

//Fill in the rest of the code

// Note that based upon the postcondition of LargestTwo, we have

// leftL and leftSL are largest and secondLargest value from l[left...mid] // rightL and rightSL are largest and secondLargest value from l[mid+1...right] }

Solutions

Expert Solution

c++ Code:

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

//function the LargestTwo
void LargestTwo (vector<int> l, int left, int right, int & largest, int & secondLargest)
{
// base cases
if (left+1 == right) //i.e., there are two elements in the list
{
int a = max(l[left], l[right]);
int b = min(l[left], l[right]);
if(max(a, b) > largest)
{
secondLargest = max(largest, min(a, b));
largest = max(a, b);
  
}
else if(max(a, b) == largest)
{
secondLargest = largest;
}
else if(max(a, b) > secondLargest)
secondLargest = max(a, b);
return;
}
else if (left+2==right)
{
int arr[] = {l[left], l[left+1], l[right]};
sort(arr, arr+3);
int a = arr[2];
int b = arr[1];
if(max(a, b) > largest)
{
secondLargest = max(largest, min(a, b));
largest = max(a, b);
  
}
else if(max(a, b) == largest)
{
secondLargest = largest;
}
else if(max(a, b) > secondLargest)
secondLargest = max(a, b);
return;
}
int mid = (left+right)/2;
LargestTwo (l, left, mid, largest, secondLargest);
LargestTwo (l, mid+1, right, largest, secondLargest);
  
}
//main function
int main()
{
int N;
cout << " enter the size:";
cin>>N;
cout <<" enter the elements: ";
vector<int >v(N);
for(int i = 0; i< N; i++)
cin>>v[i];
int largest = INT_MIN, secondLargest = INT_MIN;
LargestTwo(v, 0, N, largest, secondLargest);
cout<<"the largest:"<<largest<<"\nthe secondLargest:"<<secondLargest;
}

Output:

if u like my answer then please give a thumbs up..!!


Related Solutions

1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum...
Programming language: JAVA First, implement a recursive, Divide&Conquer-based algorithm to identify both the Minimum and Maximum element in an unsorted list. Second, convert your recursive algorithm to a non-recursive (or iterative) implementation. For your input, populate an "unsorted list" with random elements between 1 and 1,000,000.
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
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
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.
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set of integers using only one array that contains all the integers. Test your programs and compute the CPU time for different sets of integers generated by a random number generator. Must sure have the CPU time.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT