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++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the total time for // which hero will be under shock 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++) {
// Adding time of attack in total time
totalTime += (attack[i] + D)
- max(prev, attack[i]);
// Changing prev to the time where
// the previous attack ends
prev = attack[i] + D;
}
return totalTime;
} // Driver Code int main()
{ vector< int > attack = { 1, 2, 6 };
int D = 2;
cout << heroInShock(attack, D);
return 0;
} |
// Java program for the above approach import java.util.ArrayList;
class GFG{
// Function to find the total time for // which hero will be under shock 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++)
{
// Adding time of attack in total time
totalTime += (( int )attack.get(i) + D) -
Math.max(prev, ( int )attack.get(i));
// Changing prev to the time where
// the previous attack ends
prev = ( int )attack.get(i) + D;
}
return totalTime;
} // Driver code 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));
} } // This code is contributed by gfking |
# Python code for the above approach # Function to find the total time for # which hero will be under shock def heroInShock(attack, D):
N = len (attack)
totalTime = 0
prev = attack[ 0 ]
for i in range (N):
# Adding time of attack in total time
totalTime + = (attack[i] + D) - max (prev, attack[i])
# Changing prev to the time where
# the previous attack ends
prev = attack[i] + D
return totalTime
# Driver Code attack = [ 1 , 2 , 6 ]
D = 2
print (heroInShock(attack, D))
# This code is contributed by Saurabh Jaiswal |
// C# code to implement above approach using System;
using System.Collections;
class GFG {
// Function to find the total time for
// which hero will be under shock
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++) {
// Adding time of attack in total time
totalTime += (( int )attack[i] + D)
- Math.Max(prev, ( int )attack[i]);
// Changing prev to the time where
// the previous attack ends
prev = ( int )attack[i] + D;
}
return totalTime;
}
// Driver code
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));
}
} // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript code for the above approach
// Function to find the total time for
// which hero will be under shock
function heroInShock(attack, D)
{
let N = attack.length;
let totalTime = 0;
let prev = attack[0];
for (let i = 0; i < N; i++) {
// Adding time of attack in total time
totalTime += (attack[i] + D)
- Math.max(prev, attack[i]);
// Changing prev to the time where
// the previous attack ends
prev = attack[i] + D;
}
return totalTime;
}
// Driver Code
let attack = [1, 2, 6];
let D = 2;
document.write(heroInShock(attack, D));
// This code is contributed by Potta Lokesh </script>
|
5
Time Complexity: O(N)
Auxiliary Space: O(1)