CONCEPTS USED:
Basic Mathematics
DIFFICULTY LEVEL:
Hard
PROBLEM STATEMENT(SIMPLIFIED):
Given an array
Awhich contains the sorted time points at which a hero namedSaitamapunches and paralyzes his enemyGarou, also given time intervalKfor the time, his enemy is paralyzed with each punch. Your task is to determine the total time for whichGarouis paralyzed.
For Example:
Input :
N = 3, K = 2
A = [1 4 6]
Output : 6
Explanation :
1st punch on time point = 1, paralyzes him for 2 units of time [1 to 2]
2nd punch on time point = 4, paralyzes him for another 2 units of time [4 to 5]
3rd punch on time point = 6, paralyzes him for another 2 units of time [6 to 7]
Therefore, Saitama paralyzes Garou for 6 units of time.
Input :
N = 3, K = 2
A = [1 2 6]
Output : 5
Explanation :
1st punch on time point = 1, paralyzes him for 2 units of time [1 to 2].
2nd punch on time point = 2, Garou is already paralyzed at this point of time, so he was only paralyzed for 2 - 1 = 1 unit of time with the first punch, now again he is paralyzed for another 2 units of time [2 to 3].
3rd punch on time point = 6, paralyzes him for another 2 units of time [6 to 7].
Therefore, Saitama paralyzes Garou for 5 units of time.
SOLVING APPROACH:
Start by adding
Kto the first time point in the array and store it inpalalyze_tillvariable, this will give us the time point till when theGarouis paralyzed by the first punch ofSaitama.Now traverse the array starting from the second element and check whether the calculated
paralyze_tillis less than or equal to current element,A[i]of the array. This impliesGarouwas paralyzed forKunits of time and now is awake. IfYes, increment the value oftotal_timebyKand updateparalyze_till = A[i] + K.If
paralyze_tillis greater than current element,A[i]. This implies thatGarouis still paralyzed and again we need to paralyze it forKunits of time. So we add the time for which it was paralyzed till now and move ahead, updatingtotal_time = A[i] - A[i]andparalyze_till = A[i] + K.Finally add the time for which
Garouwas paralyzed by the last punch i.e.Kunits of time.
SOLUTIONS:
#includeint main() { int t; scanf("%d", &t); while(t--){ int n, k; scanf("%d %d", &n, &k); int arr[n]; for(int i=0; i
#includeusing namespace std; int main() { int t; cin>>t; while(t--){ int n, k; cin>>n>>k; int arr[n]; for(int i=0; i >arr[i]; long long sum = 0; long long curr_time = arr[0] + k; for(int i=1; i
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t --> 0){
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i=0; i
n, k = map(int,input().split()) arr = list(map(int,input().split())) sum = 0 curr_time = arr[0] + k for i in range(1, n): if curr_time <= arr[i]: sum += k else: sum += arr[i] - arr[i - 1] curr_time = arr[i] + k sum += k print(sum)
Space Complexity: O(1)
[forminator_quiz id="558"]
This article tried to discuss the concept of Basic Mathematics. Hope this blog helps you understand and solve the problem. To practice more problems on Basic Mathematics you can check out .
