Question: Can someone compute time complexity for this program #include using namespace std; int main() { int n; printf(Enter the value of n:); cin>>n; assert(n>=6);//prograame will
Can someone compute time complexity for this program
#include
using namespace std;
int main()
{
int n;
printf("Enter the value of n:");
cin>>n;
assert(n>=6);//prograame will abort if value of n is less than 6
int a[n];
for(int i=0;i
cin>>a[i];
int smallest1,smallest2,smallest3;
int largest1,largest2,largest3;
smallest1=INT_MAX;
smallest2=INT_MAX;
smallest3=INT_MAX;//assign the maximum possible value of int
largest1=INT_MIN;
largest2=INT_MIN;
largest3=INT_MIN;//assign the minimum possible value of int
for(int i=0;i
{
if(a[i]>largest1)/* If current element is greater than largest1*/
{
largest3=largest2;
largest2=largest1;
largest1=a[i];
}
else if(a[i]>largest2)/* If a[i] is in between largest1 and largest2 then update largest2 */
{
largest3=largest2;
largest2=a[i];
}
else if(a[i]>largest3)/* If current element is greater than largest3*/
{
largest3=a[i];
}
if(a[i]
{
smallest3=smallest2;
smallest2=smallest1;
smallest1=a[i];
}
else if(a[i]
{
smallest3=smallest2;
smallest2=a[i];
}
else if(a[i]
{
smallest3=a[i];
}
}
cout<<"Three Largest Elements are:"<
cout<
cout<<"Three Smallest Elements are:"<
cout<
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
