Determining Mobile Phone Charging Time Based on Rates
Last Updated :
05 Dec, 2023
Given an initial charge amount C in a mobile phone, it undergoes varying charging rates per minute, as specified within specific ranges 'Rate' based on the current charge level. These charging rate ranges are organized in Sorted Order, where each range is defined by three values: a starting point (inclusive), an ending point (non-inclusive), and an associated charging rate (See Example for more clarity). The collective span of all the ranges within the 'Rate' array encompasses values from 0 to 100, covering all intermediary ranges. Your task is to calculate the time required to charge the mobile phone to a specified charge level (T).
Examples:
Input: C = 30, T = 90, Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]]
Output: 9
Explanation: Initially mobile charged C = 30 so, to reach 30 to 60 it will charge 10 unit per min which will take 3 min, then to reach 60 to 90 it will charge 5 unit per min which will take 6 min, So in total it will take 3+6=9 min.
Input: C = 70, T = 95, Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]]
Output: 4.5
Explanation: Initially mobile charged C = 70 so, to reach 70 to 90 it will charge 5 unit per min which will take 4 min, then to reach 90 to 95 it will charge 10 unit per min which will take 0.5 min, So in total it will take 4+0.5 = 4.5 min .
Approach: To solve the problem follow the below idea:
Iterate through the charging rate ranges (Rates), checking if the current charge level (C) has met the target charge level (T) to exit the loop early. If not, it verifies if C falls within a specific range, computes the needed charge and corresponding time to reach the range's end using the provided charging rate, and continuously adds up these times to calculate the overall charging duration. Ultimately, it returns the total time required for the entire charging process.
Below is the implementation of the above approach.
C++
// C++ code for the above approach:
#include <iostream>
#include <vector>
using namespace std;
// Define a function to calculate the time
// required to reach a target charge level.
double chargeNeeded(int C, int T,
vector<vector<int> >& Rate)
{
// Initialize the total time spent charging
double Time = 0;
// Loop through the charging rate ranges.
for (const vector<int>& range : Rate) {
int start = range[0];
int end = range[1];
int charge_per_minute = range[2];
// Check if the current charge level 'C'
// has reached the target charge level 'T'.
if (C == T) {
break;
}
// Check if the current charge level 'C'
// is within the current rate range.
else if (C < end) {
// Calculate the amount of charge
// needed to reach the end of this range.
int charge_needed = min(end, T) - C;
// Calculate the time required to reach
// that charge using the current rate.
double time_required
= static_cast<double>(charge_needed)
/ charge_per_minute;
// Add this time to the total time
// spent charging.
Time += time_required;
// Update the current charge level
// 'C' to the end of this range.
C += charge_needed;
}
}
// Return the total time spent charging.
return Time;
}
// Drivers code
int main()
{
int C = 30;
int T = 90;
vector<vector<int> > Rate = { { 0, 30, 4 },
{ 30, 60, 10 },
{ 60, 90, 5 },
{ 90, 100, 10 } };
// Call the function and print the result
cout << chargeNeeded(C, T, Rate) << endl;
return 0;
}
Java
// Java code for the above approach:
import java.util.ArrayList;
import java.util.List;
public class ChargingTime {
// Define a function to calculate the time
// required to reach a target charge level.
static int chargeNeeded(int C, int T,
List<List<Integer>> Rate) {
// Initialize the total time spent charging
int Time = 0;
// Loop through the charging rate ranges.
for (List<Integer> range : Rate) {
int start = range.get(0);
int end = range.get(1);
int chargePerMinute = range.get(2);
// Check if the current charge level 'C'
// has reached the target charge level 'T'.
if (C == T) {
break;
}
// Check if the current charge level 'C'
// is within the current rate range.
else if (C < end) {
// Calculate the amount of charge
// needed to reach the end of this range.
int chargeNeeded = Math.min(end, T) - C;
// Calculate the time required to reach
// that charge using the current rate.
double timeRequired = (double) chargeNeeded / chargePerMinute;
// Add this time to the total time
// spent charging.
Time += timeRequired;
// Update the current charge level
// 'C' to the end of this range.
C += chargeNeeded;
}
}
// Return the total time spent charging.
return Time;
}
// Driver code
public static void main(String[] args) {
int C = 30;
int T = 90;
List<List<Integer>> Rate = new ArrayList<>();
Rate.add(List.of(0, 30, 4));
Rate.add(List.of(30, 60, 10));
Rate.add(List.of(60, 90, 5));
Rate.add(List.of(90, 100, 10));
// Call the function and print the result
System.out.println(chargeNeeded(C, T, Rate));
}
}
// this code is contributed by uttamdp_10
Python3
# Define a function to calculate the time required to reach a target charge level.
def chargeNeeded(C, T, Rate):
Time = 0 # Initialize the total time spent charging
# Loop through the charging rate ranges.
for start, end, charge_per_minute in Rate:
# Check if the current charge level 'C' has reached the target charge level 'T'.
if C == T:
break
# Check if the current charge level 'C' is within the current rate range.
elif C < end:
# Calculate the amount of charge needed to reach the end of this range.
charge_needed = min(end, T) - C
# Calculate the time required to reach that charge using the current rate.
time_required = charge_needed / charge_per_minute
# Add this time to the total time spent charging.
Time += time_required
# Update the current charge level 'C' to the end of this range.
C += charge_needed
return Time # Return the total time spent charging.
# Driver Code
C = 30
T = 90
Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]]
# Call the function and print the result
print(chargeNeeded(C, T, Rate))
C#
using System;
using System.Collections.Generic;
class GFG
{
static double ChargeNeeded(int C, int T, List<List<int>> Rate)
{
// Initialize the total time spent charging
double Time = 0;
// Loop through the charging rate ranges.
foreach (List<int> range in Rate)
{
int start = range[0];
int end = range[1];
int chargePerMinute = range[2];
// Check if the current charge level 'C'
// has reached the target charge level 'T'.
if (C == T)
{
break;
}
// Check if the current charge level 'C' is within the current rate range.
else if (C < end)
{
// Calculate the amount of charge
// needed to reach the end of this range.
int chargeNeeded = Math.Min(end, T) - C;
// Calculate the time required to reach
double timeRequired = (double)chargeNeeded / chargePerMinute;
Time += timeRequired;
// Update the current charge level
C += chargeNeeded;
}
}
// Return the total time spent charging.
return Time;
}
// Driver code
static void Main()
{
int C = 30;
int T = 90;
List<List<int>> Rate = new List<List<int>>()
{
new List<int> {0, 30, 4},
new List<int> {30, 60, 10},
new List<int> {60, 90, 5},
new List<int> {90, 100, 10}
};
// Call the function and print the result
Console.WriteLine(ChargeNeeded(C, T, Rate));
}
}
JavaScript
function GFG(C, T, Rate) {
let Time = 0; // Initialize the total time spent charging
// Loop through the charging rate ranges.
for (let i = 0; i < Rate.length; i++) {
const [start, end, chargePerMinute] = Rate[i];
// Check if the current charge level 'C' has reached the
// target charge level 'T'.
if (C === T) {
break;
}
// Check if the current charge level 'C' is within
// the current rate range.
else if (C < end) {
// Calculate the amount of charge needed to reach the end of this range.
const GFG = Math.min(end, T) - C;
// Calculate the time required to reach that charge using
// the current rate.
const timeRequired = GFG / chargePerMinute;
// Add this time to the total time spent charging.
Time += timeRequired;
// Update the current charge level 'C' to
// the end of this range.
C += GFG;
}
}
return Time; // Return the total time spent charging.
}
// Driver Code
const C = 30;
const T = 90;
const Rate = [[0, 30, 4], [30, 60, 10], [60, 90, 5], [90, 100, 10]];
// Call the function and print the result
console.log(GFG(C, T, Rate));
Time Complexity: O(N), where n is the number of different Rates.
Auxiliary Space: O(1).
Similar Reads
Program to find the time after K minutes from given time You are given a time T in 24-hour format (hh:mm) and a positive integer K, you have to tell the time after K minutes in 24-hour time.Examples: Input: T = 12:43, K = 21 Output: 13:04 Input: T = 20:39, K = 534 Output: 05:33 Approach: Convert the given time in minutesAdd K to it let it be equal to M.Co
6 min read
Time taken per hour for stoppage of Car A car travels with an average speed of S km/h without any stoppage and with stoppage the speed of car reduces to an average of S1 km/h. The task is to find the time wasted per hour for the stoppage. Examples: Input: S = 50, S1 = 30 Output: 24 minInput: S = 30, S1 = 10 Output: 40 min Approach: Take t
3 min read
Time difference between expected time and given time Given the initial clock time h1:m1 and the present clock time h2:m2, denoting hour and minutes in 24-hours clock format. The present clock time h2:m2 may or may not be correct. Also given a variable K which denotes the number of hours passed. The task is to calculate the delay in seconds i.e. time d
5 min read
Total money to be paid after traveling the given number of hours Given the travel cost of a cab is m rupees for first n hours per hour and then is x rupees per hour, given the total time of travel in a minute the task is to find the total cost.Note: If an hour starts then the traveler has to give the full cost for that hour.Examples: Input: m = 50, n = 5, x = 67,
3 min read
Program to find the rate percentage from compound interest of consecutive years Given two integers N1 and N2 which is the Compound Interest of two consecutive years. The task is to calculate the rate percentage.Examples: Input: N1 = 660, N2 = 720 Output: 9.09091 %Input: N1 = 100, N2 = 120 Output: 20 % Approach: The rate percentage can be calculated with the formula ((N2 - N1) *
3 min read
Difference between two given times Given two times in string "HH:MM" format. Here 'H' shows hours and 'M' shows minutes. You have to find the difference in the same string format between these two strings. But both given strings should follow these cases. 1) Time 1 will be less than or equal to time2 2) Hours will be possible from 0
5 min read