C Program to find Second smallest element in the array

Get array size n and n elements of array, then find the second smallest element among those elements.

Sample Input 1:

5 5 7 9 3 1

Sample Output 1:

3

Program or Solution

				
			
					
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main()
{
	int *a,n,i,min,second_min;
	printf("Enter size of array:");
	scanf("%d",&n);

	a=malloc(sizeof(int)*n);
	printf("Enter %d Elements:",n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	
	min=INT_MAX;
	second_min=INT_MAX;
	for(i=0;i<n;i++)
	{
		if(a[i]<min)
		{
			second_min=min;
			min=a[i];
		}
		else if(a[i] < second_min && a[i]!=min)
		{
			second_min=a[i];
		}
	}

	printf("The second minimum is : %d",second_min);
	return 0;
}
			
				
			

Program Explanation

Initialize min and second_min with INT_MAX(INT_MAX is constant holds maximum integer value) visit every location in the array a) If the current element in array a[i] is less than min.

Then update min and second_min as, second_min = min min = a[i] b) If the current element is greater than min and less than second_min, then update second_min to store the value of current variable as second_min = arr[i] print the value stored in second_min.

Comments

ubaTaeCJ
-1 OR 2+412-412-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3+412-412-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3*2<(0+5+412-412) --
ubaTaeCJ
-1 OR 3*2>(0+5+412-412) --
ubaTaeCJ
-1 OR 2+582-582-1=0+0+0+1
ubaTaeCJ
-1 OR 3+582-582-1=0+0+0+1
ubaTaeCJ
-1 OR 3*2<(0+5+582-582)
ubaTaeCJ
-1 OR 3*2>(0+5+582-582)
ubaTaeCJ
-1' OR 2+975-975-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3+975-975-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3*2<(0+5+975-975) --
ubaTaeCJ
-1' OR 3*2>(0+5+975-975) --
ubaTaeCJ
-1' OR 2+889-889-1=0+0+0+1 or 'rQKCOOUB'='
ubaTaeCJ
-1' OR 3+889-889-1=0+0+0+1 or 'rQKCOOUB'='
ubaTaeCJ
-1' OR 3*2<(0+5+889-889) or 'rQKCOOUB'='
ubaTaeCJ
-1' OR 3*2>(0+5+889-889) or 'rQKCOOUB'='
ubaTaeCJ
-1" OR 2+332-332-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3+332-332-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3*2<(0+5+332-332) --
ubaTaeCJ
-1" OR 3*2>(0+5+332-332) --
ubaTaeCJ
if(now()=sysdate(),sleep(15),0)
ubaTaeCJ
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
ubaTaeCJ
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
ubaTaeCJ
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
ubaTaeCJ
-1; waitfor delay '0:0:15' --
ubaTaeCJ
-1); waitfor delay '0:0:15' --
ubaTaeCJ
1 waitfor delay '0:0:15' --
ubaTaeCJ
fBuC7jAI'; waitfor delay '0:0:15' --
ubaTaeCJ
-5 OR 775=(SELECT 775 FROM PG_SLEEP(15))--
ubaTaeCJ
-5) OR 965=(SELECT 965 FROM PG_SLEEP(15))--
ubaTaeCJ
-1)) OR 780=(SELECT 780 FROM PG_SLEEP(15))--
ubaTaeCJ
rlJzNTsN' OR 214=(SELECT 214 FROM PG_SLEEP(15))--
ubaTaeCJ
QKtjTZmT') OR 986=(SELECT 986 FROM PG_SLEEP(15))--
ubaTaeCJ
duKf6dX3')) OR 196=(SELECT 196 FROM PG_SLEEP(15))--
ubaTaeCJ
*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
ubaTaeCJ
'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
ubaTaeCJ
1'"
ubaTaeCJ
@@F2c6k