Program to find maximum and second maximum if elements of array is space separated input

Description

In this program the array elements will be space separated like string. The program
will take elements from string rejecting spaces, then it will find maximum and 
second maximum.

C/C++

/* c Program to find maximum and second maximum if elements of array is space separated input */
//Save it as MaxAndSecondMaxElement.c

#include<stdio.h>
#include<string.h>
#include<limits.h>
int main(){

    char str[100];
    int i, maxVal, secondMax;

    //Array elements are space separated input like string
    printf("Enter the space separated array elements : ");
    gets(str);

    int length = strlen(str);

    int arr[length];
    int count=0;

    //Take input only those having value and reject space
    for(i=0;i<length;i++) {
        if(str[i] == ' '){
            continue;
        }else{
            arr[count++] = (str[i] - '0');
        }
    }

    maxVal = INT_MIN;
    //Checking the maximum element
    for(i=0;i<count;i++) {
        if(arr[i] > maxVal) {
            maxVal = arr[i];
        }
    }

    printf("The maximum value : %d",maxVal);

    secondMax = INT_MIN;

    //Checking the second maximum element
    for(i=0;i<count;i++) {
        if(arr[i]>secondMax && arr[i]!=maxVal) {
            secondMax = arr[i];
        }
    }

    printf("\nThe second maximum value : %d",secondMax);

    return 0;
}
Input:
Enter the space separated array elements : 
7 4 6 9

Output:
The maximum value : 9
The second maximum value : 7

Java

/* Java Program to find maximum and second maximum if elements of array is space separated input */
//Save it as MaxAndSecondMaxElement.java

import java.io.*;
import java.util.Scanner;

public class MaxAndSecondMaxElement {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
                //Array elements are space separated input like string
        System.out.println("Enter the space separated array elements : ");
        String str = scanner.nextLine();
        
                //spliting the from spaces
        String arr[] = str.split(" ");
        
                //declaring an integer array of length
        int array[] = new int[arr.length];
        
                //Taking input by converting it into integer
        for(int i=0;i<arr.length;i++) {
            array[i] = Integer.parseInt(arr[i]);
        }
        
        int max = Integer.MIN_VALUE;
        
                //Checking the maximum element
        for(int i=0;i<array.length;i++) {
            if(array[i] > max) {
                max = array[i];
            }
        }
        
        System.out.println("The maximum value : "+max);
        
        int secondMax = Integer.MIN_VALUE;
        
                //Checking the econd maximum element
        for(int i=0;i<array.length;i++) {
            if(array[i]>secondMax && array[i]!=max) {
                secondMax = array[i];
            }
        }
        System.out.println("The second maximum value : "+secondMax);
    }
}
Input:
Enter the space separated array elements : 
7 4 6 9

Output:
The maximum value : 9
The second maximum value : 7

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to swap maximum and minimum element of Array
4) Program to Find the smallest missing number
5) Program to Find missing odd number in first n odd number
6) Program to Find Value equal to index value
7) write a Program to find index of two array elements whose sum is equal to given value
8) Program to Find K largest elements from array
9) Program to make pair of elements alternatively
10) Program to Print How Many Numbers Smaller than Current Number
Share Me

Leave a Reply