9. TASK: THE SEQUENCE NUMBER OF THE MAXIMUM DISTANCE-SOLUTION
Enter n, followed by an array of n-integers representing distances between cities. Determine the ordinal number in the sequence where the distance is the largest.
If there are several distances with the same maximum, print the sequence number of the last occurrence of that maximum.
Example
Input
8
45 23 12 56 32 56 18 2
Output
6
If there are several distances with the same maximum, print the sequence number of the last occurrence of that maximum.
Example
Input
8
45 23 12 56 32 56 18 2
Output
6
When working with arrays, we often encounter the problem of finding the maximum value and determining the position of its occurrence. This task shows how to find the sequence number of the last maximum in a sequence, which is a useful example to analyze and understand working with loops and conditions in C++. The original solution shows the sequence number of the first maximum, which provides an excellent opportunity to practice and modify the code.
To return to additional examples from loops, click on Additional examples from the loop topic
If you want to know more about loops in C and C++, you can start by reading the following article: Loops in C/C++ basic
If you want to know more about loops in C and C++, you can start by reading the following article: Loops in C/C++ basic
Defining the problem
The following code shows an example solution that determines the first order maximum.
#include <iostream>
using namespace std;
int main()
{
int n,x; //n-number of cities, x-current distance
cin>>n; //Input n
int maxSN=0; //The initial value of the maximum distance
int seqNumOfMaxS=1; //The initial value of the sequence number of the maximum distance
for(int i=1;i <= n;i++){
cin>>x; //Enter the distance between 2 cities
if(x>maxSN){
maxSN=x; //updates the new maximum
seqNumOfMaxS=i; //It remembers the sequence number of the maximum distance
}
}
cout<<seqNumOfMaxS<<endl; //prints the sequence number of the maximum
return 0;
}
using namespace std;
int main()
{
int n,x; //n-number of cities, x-current distance
cin>>n; //Input n
int maxSN=0; //The initial value of the maximum distance
int seqNumOfMaxS=1; //The initial value of the sequence number of the maximum distance
for(int i=1;i <= n;i++){
cin>>x; //Enter the distance between 2 cities
if(x>maxSN){
maxSN=x; //updates the new maximum
seqNumOfMaxS=i; //It remembers the sequence number of the maximum distance
}
}
cout<<seqNumOfMaxS<<endl; //prints the sequence number of the maximum
return 0;
}
We solve the problem by replacing the condition, inside the if statement, which checks whether the entered number inside the for loop is greater than the current maximum x >maxSN, with the condition x >=maxSN
#include <iostream>
using namespace std;
int main()
{
int n,x; //n-number of cities, x-current distance
cin>>n; //Input n
int maxSN=0; //The initial value of the maximum distance
int seqNumOfMaxS=1; //The initial value of the sequence number of the maximum distance
for(int i=1;i <= n;i++){
cin>>x; //Enter the distance between 2 cities
if(x>=maxSN){
maxSN=x; //updates the new maximum
seqNumOfMaxS=i; //It remembers the sequence number of the maximum distance
}
}
cout<<seqNumOfMaxS<<endl; //prints the sequence number of the maximum
return 0;
}
using namespace std;
int main()
{
int n,x; //n-number of cities, x-current distance
cin>>n; //Input n
int maxSN=0; //The initial value of the maximum distance
int seqNumOfMaxS=1; //The initial value of the sequence number of the maximum distance
for(int i=1;i <= n;i++){
cin>>x; //Enter the distance between 2 cities
if(x>=maxSN){
maxSN=x; //updates the new maximum
seqNumOfMaxS=i; //It remembers the sequence number of the maximum distance
}
}
cout<<seqNumOfMaxS<<endl; //prints the sequence number of the maximum
return 0;
}
The problem contains an initial solution that uses the condition x > maxSN, which shows the sequence number of the first occurrence of the maximum value. To obtain the sequence number of the last maximum, the user should replace the condition
x > maxSN with x >= maxSN. This ensures that the sequence number is updated every time the value of x equals the current maximum, so the variable seqNumOfMaxS eventually contains the sequence number of the last occurrence of the maximum.
Additional tips:
Keep track of the value of the maxSN variable in each iteration to understand why the sequence number is being updated.
Experiment with different input strings to ensure that the changed condition is correct for all possible combinations of maxima.
x > maxSN with x >= maxSN. This ensures that the sequence number is updated every time the value of x equals the current maximum, so the variable seqNumOfMaxS eventually contains the sequence number of the last occurrence of the maximum.
Additional tips:
Keep track of the value of the maxSN variable in each iteration to understand why the sequence number is being updated.
Experiment with different input strings to ensure that the changed condition is correct for all possible combinations of maxima.