CPCEJC2 - Editorial

PROBLEM LINK:

Practice

Author: Manish Motwani
Tester: Lovish Tater
Editorialist: Lovish Tater

DIFFICULTY:

SIMPLE

PREREQUISITES:

Array, Sorting of an array.

PROBLEM:

The task is to identify the highest and lowest value among the given sequence.

QUICK EXPLANATION:

Store the sequence in an array. Sort the array and print the last and first element of the array.

EXPLANATION:

In this question, we have to find the largest and shortest number in the sequence. for this, we will take an array input using for loop. Then we will sort the array using Arrays.sort() in java or sort() function in CPP. After sorting the array the smallest number will be at the 0th index and the largest number will be at the n-1th index of the array. hence we will print 0th and n-1th index of the array.

SOLUTIONS:

Setter's Solution
#include<iostream>
using namespace std;
int main()
{
int num,i;
cin>>num;
int array[num];
for(i=0;i<num;i++)
cin>>array[i];
int maxi=0,mini=100000;
for(i=0;i<num;i++)
{
	if(array[i]>maxi)
	maxi=array[i];
}
for(i=0;i<num;i++)
{
	if(array[i]<mini)
	mini=array[i];
}
cout<<maxi<<" "<<mini;
}
Tester's Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
   Scanner sc = new Scanner(System.in);
       int n = sc.nextInt(); 
       int arr[]= new int [n];
       for(int i = 0 ; i<n ; i++){
           arr[i]= sc.nextInt();
       }
       Arrays.sort(arr);
       System.out.println(arr[n-1]);
       System.out.println(arr[0]);
   }
}
Editorialist's Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
   Scanner sc = new Scanner(System.in);
       int n = sc.nextInt(); 
       int arr[]= new int [n];
       for(int i = 0 ; i<n ; i++){
           arr[i]= sc.nextInt();
       }
       Arrays.sort(arr);
       System.out.println(arr[n-1]);
       System.out.println(arr[0]);
   }
}