SmartPhone CodeChef Solution

Today we will be solving Smartphone Codechef Solution which is the part of DSA Learning Series.

SmartPhone CodeChef Solution

Problem

You are developing a smartphone app. You have a list of potential customers for your app. Each customer has a budget and will buy the app at your declared price if and only if the price is less than or equal to the customer’s budget.

You want to fix a price so that the revenue you earn from the app is maximized. Find this maximum possible revenue.

For instance, suppose you have 4 potential customers and their budgets are 30, 20, 53 and 14. In this case, the maximum revenue you can get is 60.

Input Format

Line 1 : N, the total number of potential customers.

Lines 2 to N+1: Each line has the budget of a potential customer.

Output Format

The output consists of a single integer, the maximum possible revenue you can earn from selling your app.

Sample Input 1 

4
30
20
53
14

Sample Output 1

60

Sample Input 2

5
40
3
65
33
21

Sample Output 2

99

Test Data

Each customers’ budget is between 1 and 108, inclusive.

Subtask 1 (30 marks) : 1 ≤ N ≤ 5000.

Subtask 2 (70 marks) : 1 ≤ N ≤ 5×105.

Live evaluation Data

There are 15 test inputs on the server during the exam. The grouping into subtasks is as follows.

  • Subtask 1: Test inputs 0,…,5
  • Subtask 2: Test inputs 6,…,14

Tip

The answer might not fit in a variable of type int. We recommend that you use variables of type long long to read the input and compute the answer. If you use printf and scanf, you can use %lld for long long. 

Solution – SmartPhone CodeChef Solution

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long arr[] = new long[n];
		for(int i=0 ; i<n ;i++)
		{
		      arr[i]=sc.nextInt();
		}
		Arrays.sort(arr);
			for(int i=0 ; i<n ;i++)
		{
		      arr[i]=arr[i]*(n-i);
		}
		Arrays.sort(arr);
		System.out.println(arr[n-1]);
	}
}
SmartPhone CodeChef Solution

C++

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

int main()
{
   long long int n;
   cin>>n;
   long long int arr[n];
   for(int i=0;i<n;i++)
   {
   	cin>>arr[i];
   }
   sort(arr,arr+n);
   
   long long int x=0;
   for(int i=0;i<n;i++)
   {
   	 long long int temp=arr[i]*(n-i);
   	 if(temp>x){
   	 	x=temp;}
   }
   cout<<x;
}
SmartPhone CodeChef Solution

Disclaimer: The above problem (Smart Phone: ZCO14003) which is a part of DSA learning series is generated by CodeChef but the solution is provided by CodingBroz.

Broz Who Code

CodingBroz

1 thought on “SmartPhone CodeChef Solution”

  1. Exception in thread “main” java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at Codechef.main(Main.java:14)

    facing this error

Leave a Comment

Your email address will not be published. Required fields are marked *