Total time for which the hero will be in shock
Last Updated :
31 Dec, 2021
Given an array attack containing N integers in non-decreasing order, and an integer D. Every element in array attack denotes the time at which hero is attacked inside a game and D denotes the duration for which the hero will be in shock and won’t be able to fight back. The task is to find the total time for which the hero will be under shock.
Example:
Input: attack = [1, 4], d= 2
Output: 4
Explanation:
At second 1, Hero is attacked, and stay in shock for seconds 1 and 2.
At second 4, Hero is attacked, and stay in shock for seconds 4 and 5.
which is 4 seconds in total.
Input: attack = [1, 2], d= 2
Output: 3
Explanation:
At second 1, Hero is attacked, and stay in shock for seconds 1 and 2.
At second 2, Hero is attacked, and stay in shock for seconds 2 and 3.
which is 3 seconds in total.
Approach: Calculate the time for which the hero will be in shock using the formula:
Shock time for attack[i] = (attack[i]+D) – max(Time at which the previous attack ends, attack[i])
Now to solve this problem, follow the below steps:
- Create a variable totalTime, to store the answer to this problem.
- Create a variable, prev and initialise it to the attack[0]. This variable will store the time at which the previous attack ends for each individual attack.
- Run a loop from i=0 to i<N, and in each iteration:
- Add the shock time of attack[i] i, e. (attack[i] + D) – max(prev, attack[i]) to totalTime.
- Change prev to the time at which the previous attack ends, so prev=attack[i]+D.
- Return totalTime as the answer after the loop ends.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int heroInShock(vector< int >& attack, int D)
{
int N = attack.size();
int totalTime = 0;
int prev = attack[0];
for ( int i = 0; i < N; i++) {
totalTime += (attack[i] + D)
- max(prev, attack[i]);
prev = attack[i] + D;
}
return totalTime;
}
int main()
{
vector< int > attack = { 1, 2, 6 };
int D = 2;
cout << heroInShock(attack, D);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG{
static int heroInShock(ArrayList<Integer> attack, int D)
{
int N = attack.size();
int totalTime = 0 ;
int prev = ( int )attack.get( 0 );
for ( int i = 0 ; i < N; i++)
{
totalTime += (( int )attack.get(i) + D) -
Math.max(prev, ( int )attack.get(i));
prev = ( int )attack.get(i) + D;
}
return totalTime;
}
public static void main(String args[])
{
ArrayList<Integer> attack = new ArrayList<Integer>();
attack.add( 1 );
attack.add( 2 );
attack.add( 6 );
int D = 2 ;
System.out.println(heroInShock(attack, D));
}
}
|
Python3
def heroInShock(attack, D):
N = len (attack)
totalTime = 0
prev = attack[ 0 ]
for i in range (N):
totalTime + = (attack[i] + D) - max (prev, attack[i])
prev = attack[i] + D
return totalTime
attack = [ 1 , 2 , 6 ]
D = 2
print (heroInShock(attack, D))
|
C#
using System;
using System.Collections;
class GFG {
static int heroInShock(ArrayList attack, int D)
{
int N = attack.Count;
int totalTime = 0;
int prev = ( int )attack[0];
for ( int i = 0; i < N; i++) {
totalTime += (( int )attack[i] + D)
- Math.Max(prev, ( int )attack[i]);
prev = ( int )attack[i] + D;
}
return totalTime;
}
public static void Main()
{
ArrayList attack = new ArrayList();
attack.Add(1);
attack.Add(2);
attack.Add(6);
int D = 2;
Console.Write(heroInShock(attack, D));
}
}
|
Javascript
<script>
function heroInShock(attack, D)
{
let N = attack.length;
let totalTime = 0;
let prev = attack[0];
for (let i = 0; i < N; i++) {
totalTime += (attack[i] + D)
- Math.max(prev, attack[i]);
prev = attack[i] + D;
}
return totalTime;
}
let attack = [1, 2, 6];
let D = 2;
document.write(heroInShock(attack, D));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...