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

  • Write an algorithm with θ (n**0.59) average complexity that locates the location ( index) of negative...

    Write an algorithm with θ (n**0.59) average complexity that locates the location ( index) of negative integer in an array of positive integers of size n( only one negative integer in the array). You may have to write two algorithms.

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

  • Given an n-element unsorted array A of n integers and an integer k, describe and implement...

    Given an n-element unsorted array A of n integers and an integer k, describe and implement a recursive algorithm for rearranging the elements in A so that all elements less than or equal to k come before any elements larger than k. What is the running time of your algorithm? Please prove the running time in the comments . in c++ please

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

  • Create the abstract data type IntSet used to implement a mathematical set of non-negative integers (includes...

    Create the abstract data type IntSet used to implement a mathematical set of non-negative integers (includes zero). Each object of class IntSet can keep track of whether integers are in the set or not. Implement as follows. A set is to be represented internally as a bool array containing true and false. For example, if you call the array set, then set[num] is true if the integer num is in the set, false if num is not in the set....

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