Largest number

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Given a integer array print the largest number in the array and the index on which this number is present . here indexing is from 1 .

If two largest number present then print index of number which occurs first in the array.

EXPLANATION:

You have given array we have to first find biggest number in it and latter print it and its index also
so the best approach is compare all the numbers to each other and print the biggest number and its index
for example n=4
4 1 6 2
in this number if we compare all numbers to each other then biggest number is 6 and its index is 3 there for output is 6 and 3

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
int k=0,j=0;

for(int i=0;i<n;i++){
if(a[i]>k){
k=a[i];
j=i;
}
}
cout<<k<<"\n"<<(j+1);

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
int k=0,j=0;

for(int i=0;i<n;i++){
if(a[i]>k){
k=a[i];
j=i;
}
}
cout<<k<<"\n"<<(j+1);

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
for(int i=0 ;i<n ;i++){
cin>>a[i];
}
int k=0,j=0;

for(int i=0;i<n;i++){
if(a[i]>k){
k=a[i];
j=i;
}
}
cout<<k<<"\n"<<(j+1);

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}