Home
Aptitude
Categories


Home » C programs » C One Dimensional Array programs

C program to find second largest elements in a one dimensional array



In this article, we are going to write a C program to find the second maximum element from one dimensional array.
Submitted by Radib Kar, on December 04, 2018

Problem statement: Write a C program to find second largest element in a one dimensional array.

Examples: Type1: (second maximum exists)

    Array size: 4
    Elements: 12 133 24 11
    Output: 24

Examples: Type2: (second maximum doesn’t exist as size of array < 2)

    Array size : 1
    Elements: 4
    Output: no second maximum element exists

Examples: Type3: (second maximum doesn’t exist as all elements are same)

    Array size : 4
    Elements: 3 3 3 3
    Output: no second maximum element exists

Algorithm:

  1. Define two variable max & sec_max
  2. Initialize max to array[0] & sec_max to INT_MIN
  3. Scan the entire array
    • For(int i=0;i<n;i++)
    • if a[i]> max 
          //then update max to array value & 
          //second max to previous max value
      else if a[i] is greater than second max but less than max 
          //then update second max to array value
      End for loop
      
    • If still sec_max has the value INT_MIN
      Then, it has not been updated, that means, no second maximum element exists
      Else
      Print second maximum value



C implementation to find second largest elements in a one dimensional array

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

int findSecondMax(int* a,int n){
	if( n<=1)
		return 0;
	
	//initialize max to first array element
	int max=a[0]; 
	//initialize max to first array element
	int sec_max=INT_MIN,temp=0; 
	
	//scan the entire array
	for(int i=0;i<n;i++){ 
		//if a[i]> max then update max to array value 
		// & second max to previous max value 
		if(a[i]>max){
			sec_max=max;
			max=a[i];
		}
		//else if a[i] is greater than second max but 
		//less than max then update second max to array value
		else if (a[i] > sec_max && a[i] < max) 
			sec_max=a[i];
	}
	//if second max is still at its initialized value 
	//then no second maximum exists at all
	if(sec_max==INT_MIN)
		return 0;
	//else return second maximum
	return sec_max;
}

int main(){
	int n,sec_max=0;

	printf("enter no of elements\n");
	scanf("%d",&n);

	//dynamic array created
	int *a=malloc(sizeof(int)*n);

	printf("enter the elements........\n");
	//taking input
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	//find second maximum value
	sec_max=findSecondMax(a,n);

	if(sec_max==0) //if no second maximum exists
		printf("no second max exists\n");
	else
		printf("The second maximum no is : %d\n",sec_max);

	return 0;
}

Output (first run)

enter no of elements
4
enter the elements........
12 133 24 11
The second maximum no is : 24

Output (second run)

enter no of elements
5
enter the elements........
1 1 1 1 1
no second max exists





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.