ANSHU11-Editorial

PROBLEM LINK:

Contest

Setter: priyansh2802

Tester: mishra_roshan

Editorialist: priyansh2802

DIFFICULTY:

Cakewalk

PREREQUISITES:

Basic Maths

PROBLEM:

Tanushree went to a restaurant to eat food and since is a big foodie she is having difficulties choosing which cuisine she would like to eat but ultimately she ended up buying an N-number of dishes which may or may not be out of her budget, fortunately, her debit card was offering 10% off on bills more than 1000.

If the quantity, budget, and the price of individual dishes are the input calculate whether the bill will be out of her budget or not.

NOTE: The budget may or may not be an Integer take Input accordingly.

EXPLANATION

We are given the number of dishes Tanushree bought, her budget, and the prices of all the dishes she bought.
Another condition which is given is that her bank is offering her a 10% discount on all payments above 1000.
So if the addition of all the prices is above 1000 we need to subtract the 10% of her bill and that will be her final amount which she has to pay to the restaurant.
Now since we are also given the budget if the total amount is strictly above her budget then she can’t afford the bill and the output should be “NO”
else if the total amount is smaller than or equals to her budget then she can pay the amount to the restaurant and the answer should be “YES”

TIME COMPLEXITY

Time complexity is O(N) for traversing the prices of the dishes.

SOLUTIONS:

Setter's Solution
C++
#include<iostream>
#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve();
int32_t main(void)
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
}
void solve()
{
    int n;
    double b;
    cin>>n>>b;
    vector<int>arr(n);
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int sum=0;
    for(auto i:arr)
    {
        sum+=i;
    }
    double ans=0;
    if(sum>1000)
    {
        ans=(double)sum-(double)sum*0.1;
    }
    else
    {
        ans=sum;
    }
    if(ans>b)
    {
        cout<<"NO"<<endl;
    }
    else
    {
        cout<<"YES"<<endl;
    }
}
Tester's Solution
Java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
			int n=sc.nextInt();
			double b=sc.nextDouble();
			double totalCost=0;
			for(int i=1;i<=n;i++){
				double cost=sc.nextDouble();
				totalCost+=cost;
			}		
			if(totalCost>1000){
			    totalCost=totalCost-totalCost*0.1;
			}
			if(totalCost<=b){
			    System.out.println("YES");
			}
			else{
			    System.out.println("NO");
			}
		}
	}
}

I think this is the correct link to the problem:

https://www.codechef.com/problems/ANSHU11

Could anyone please suggest methods to improvise or make this code more efficient? (This is in Python 3.6)

t = int(input())
for _ in range(t):
    total = 0
    dish, budget = input().split()
    prices = input().split()
    for i in prices:
        total += float(i)
    if total > 1000:
        final = total*0.9
    else:
        final = total
    if final <= int(budget):
        print("YES")
    else:
        print("NO")