CHFSPL - Editorial

PROBLEM LINK:

Practice
Contest: Division 3
Contest: Division 2
Contest: Division 1

Author: Souradeep
Tester : Riley Borgard
Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

Greedy

PROBLEM:

Chef has three spells. Their powers are A, B, and C respectively. Initially, Chef has 0 hit points, and if Chef uses a spell with power P, then his number of hit points increases by P.

Before going to sleep, Chef wants to use exactly two spells out of these three. Find the maximum number of hit points Chef can have after using the spells.

EXPLANATION:

Since our goal is to maximize the number of hit points, it is optimal to use those spells with maximum powers. As we need to take exactly two spells, just take the maximum and second maximum power spell.

In another way, our answer is just a maximum of [A+B, B+C, C+A]

TIME COMPLEXITY:

O(1) per test case

SOLUTIONS:

Author
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    Scanner sc=new Scanner(System.in);
	   // BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	    int t=1;
	    t=sc.nextInt();
	    //int t=Integer.parseInt(br.readLine());
	    while(--t>=0){
	        int x=sc.nextInt();
	        int y=sc.nextInt();
	        int z=sc.nextInt();
	        int g=Math.max(Math.max(x+y,y+z),x+z);
	        System.out.println(g);
	        
	        
	     
	        
	    }
	    
	}
}
Tester

#include <bits/stdc++.h>

#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int te;
    cin >> te;
    while(te--) {
        int a, b, c;
        cin >> a >> b >> c;
        cout << max({a + b, b + c, c + a}) << '\n';
    }
}
Editorialist
#include<bits/stdc++.h>
using namespace std;

void solve()
{
	int a[3];
	cin>>a[0]>>a[1]>>a[2];

	sort(a,a+3);

	cout<<(a[2]+a[1])<<"\n";
}

int main()
{
 
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  int t;
  cin>>t;

  while(t--)
    solve();

return 0;
}

whats the problem in this code??

 #include <bits/stdc++.h>
 using namespace std;

  int main() {
// your code goes here
int t,a,b,c;
cin>>t;
while(t--){
    cin>>a>>b>>c;
    if((a+b)>(a+c) && (a+b)>(b+c))
    cout<<a+b<<endl;
    
    else if((b+c)>(a+c) && (b+c)>(a+b))
    cout<<b+c<<endl;
    
    else
    cout<<a+c<<endl;
}
return 0;
  }

Consider the test input:

1
4 5 4

Whats the mistake?

indent preformatted #include <stdio.h>

#include <stdlib.h>

int main()
{
int t,n,a,b,c,x,y,z,i;
printf(“No. of test cases\n”);
scanf("%d",&t);
for(i=0;i<t;i++)
{
printf(“Enter three inputs\n”);
scanf("%d\t%d\t%d",&a,&b,&c);
x=a+b;
y=b+c;
z=a+c;
if(x>y && x>z)
{
printf("%d",x);
}
else if(y>a && y>z)
{
printf("%d",y);
}
else
{
printf("%d",z);
}

}
}text by 4 spaces

use “\n” at the end of the code , or there is no need to write “printf(“No. of test cases\n”)” .

please find the mistake ?

// your code goes here

#include <iostream>

using namespace std;

int main() {

int t ;

cin>>t ;

while(t--){

    int arr[3];

   int arr1[3];

    int sum1;

    int sum2;

    for(int i = 0;i<3; i++){

        cin>>arr[i];

    }

    for(int i =0; i<3; i++){

        sum1 =0;

        sum2 = 0;

        for(int j = i+1; j<3;j++){

            sum1 += arr[j];

        }

          for(int k = 0; k<i;k++){

            sum2 += arr[k];

        }

        arr1[i] = (sum2 + sum1);

    }

    int max = 0;

    for(int i = 0; i<3; i++){

        if(arr1[i]>max)

            max = arr1[i];

    }

    cout<<max;

}

return 0;

}