BugCrush 2 Question 1 - BUGC201 - EDITORIAL

BugCrush 2 Question 1 - BUGC201 - EDITORIAL

PROBLEM LINK

Practice
Contest

Author: codechefsrm
Editorialist : codechefsrm

DIFFICULTY

EASY

PREREQUISITES

Array,Vector,Sorting, Math, Algorithm, Finding maximum element.

PROBLEM

Problem Description:
Program to find the maximum sum of 2 elements in an array where n>=2.

Input:
5
1 4 2 5 3

Output:
9

Rules:
Bugs will be mainly logical errors, syntax errors etc. They are specific to C++ language.
Since it is a debugging contest, you will be provided with a bugged solution below the Constraints section.
Please, see that you must adhere to the problem code provided, make changes only where necessary.
Participants can copy the code and compile it using an online compiler (Eg. CodeChef IDE).
Once the bugs are eliminated from the code, the clean code should be submitted by using the “Submit” button on the top-right corner.
Participants will be penalised for changing the entire problem solution or writing their own solution, completely different from the buggy code as provided in the
problem statement as our main intention is to test the debugging abilities of the participants.

Buggy Code:
Please copy the following code and paste it into your compiler for debugging.

#include<bits/stdc++.h>
using namespace std;
#define bruh main
int bruh(){
int n;
cin<<n;
vector<int>v;
for(int i=0;i<=n;i++)
{
	cin>>v[i];
}
sort(v.begin(),v.end())
cout<<v[n]+v[n-1]<<endl;
return 0;
}

SOLUTION

#include<bits/stdc++.h>
using namespace std;
#define bruh main
int bruh(){
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++)
{
	cin>>v[i];
}
sort(v.begin(),v.end());
cout<<v[n-1]+v[n-2]<<endl;
return 0;
}

Explanation

Line No. Error
6 << is used instead of >>
7 size of vector
8 for loop condition
13 output last two variable (i.e)v[n-1] and v[n-2]