Program depicting array’s three largest numbers with one loop

Program depicting array's three largest numbers with one loop
Rate this post

Programming:

Programming in software is the main skill every software company needs, while going to any developer interview some of the questions would tense us a lot. We will solve some of those related questions with best performance that is which has best space complexity and making operations simpler. Other programs on different streams are also being provided which gives best solutions of all making every Datastructure basic concepts quite simpler to move forward with algorithms. Here is a program depicting array’s three largest numbers with one loop. To know resources to learn programming visit               best resources to learn  programming for free.

Program depicting array’s three largest numbers with one loop.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

void main()
{
int *arr,n,a,b,c,i;
printf(“enter a number of elements”);
scanf(“%d”,&n);
arr=(int*)malloc(n);

for(int i=0;i<n;scanf(“%d”,&arr[i++]));
i=0;
a=arr[i];
b=INT_MIN;
c=INT_MIN;
for(int i=3;i<n;i++)
if(arr[i]>a)
{
c=b;
b=a;
a=arr[i];
}
else if(arr[i]>b&&arr[i]<a)
{
c=b;
b=arr[i];
}
else if(arr[i]>c&&arr[i]<b)
c=arr[i];

printf(“first largest %d\n”,a);
if(a==b)
printf(“no second largest number\n”);
else
printf(“second largest %d\n”,b);
if(b==c)
printf(“no third largest number\n”);
else printf(“third largest %d\n”,c);

}

The above program runs for numbers excluding duplicates, this is the program of 0(n) complexity and 0(1) space complexity. I hope this would solve the problem.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here