Question

Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...

Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be
distinctly smaller than the largest element. You should also adhere to the following restrictions.
(1) You should traverse through the array ONLY ONCE.
(2) The array could have both positive and negative elements. So, you should NOT initialize any
temporary variables to very small negative values as part of your algorithm.
(3) You should NOT make any assumption regarding the values of the elements and their
distribution in the array.

Show the working of the algorithm with the following two types of example arrays:
(a) An example array of 10 integers that need to have the largest integer appearing at least twice.
(b) An example array with the largest integer and second-largest integer as respectively the first

Please put comments in the code (C++) and prove time complexity is theta(n)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int lar, secLar; //variables to store the largest and second largest elements
lar=arr[0]; //initialising lar with the first element of array
int i=1;
while(arr[i]==lar){
i++; //getting past all the elements equal to lar
}   
if(arr[i]<lar) //getting the first element which is different from lar and comparing it to lar to get
secLar=arr[i]; //initial values for both lar and secLar
else{
secLar=lar;
lar=arr[i];
}
i++;
while(i<n){ //checking the rest of array for any modification in lar and secLar
if(arr[i]==lar || arr[i]==secLar){

} //do nothing if the next element is already lar or second lar
else{
if(arr[i]>lar){
secLar=lar; // if the next element is greater than lar , then secLar would get updated to lar and
lar=arr[i]; // lar would get updated to that element
}
else if(arr[i]>secLar){ //if the next element is smaller than lar, but greater than secLar , secLar would get
secLar=arr[i]; // updated to that element
}
else{ //do nothing if the element is smaller than both lar and secLar

}
}
i++;
}
cout<<"The largest and second largest elements of the array are: "<<lar<<" and "<<secLar<<" respectively"<<endl;
return 0;
}

for example, in case of array: 7 -2 4 6 7 3 2 0 -9 5

the result that comes is : lar: 7 and secLar: 6

the algorithm has the time complexity of o(n) and does the whole work in one scan.

Add a comment
Know the answer?
Add Answer to:
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers)...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A...

    Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...

  • Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index...

    Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code. Consider the declaration: int v[1]; What is the index of the last element of this array? Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the...

  • C++ QUESTION Loops, arrays, functions find the three largest values in an array of integers and...

    C++ QUESTION Loops, arrays, functions find the three largest values in an array of integers and output them to the screen. You must utilize at least three functions, counting main as a function, AND you must use parameters/arguments in one of them. Set up an integer array of 10 elements, read in the ten elements from the keyboard. There are many ways to solve this lab. Here are a couple of ideas: Use a function to find the largest value...

  • in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each...

    in C++ HeapSort Implement the heapsort algorithm for sorting an array of integers. Input structure Each case starts with an integer number which indicates the number of elements to be sorted. Then, the elements follow, one per line. You can assume the input is correctly structured (i.e., no data are missing Output structure Output the sorted sequence separeted by";" (in non-decreasing order). Do not insert spaces or a new line at the beginning or at the end of any element.

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

  • Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

    Please use java code. Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments: 1) commonEnd Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. 2) midThree Given an array of integers, return a new array of length 3 containing the elements...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    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"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT