Given a jumpy ball that is thrown vertically upward from the ground to a height of h, it rebounds to a height of floor(h / 2) every time it hits the ground. The rebounding process continues until the rebound height becomes 0. Return the total distance traveled by the ball.
Examples:
Input: h = 10
Output: 36
Explanation: The ball travels: 10 + 10 + 5 + 5 + 2 + 2 + 1 + 1 = 36.Input: h = 20
Output: 76
Explanation: The ball travels: 20 + 20 + 10 + 10 + 5 + 5 + 2 + 2 + 1 + 1 = 76.
Table of Content
Simulate Every Upward and Downward Movement - O(log h) Time and O(1) Space
The idea is to simulate the complete motion of the ball. Starting from the initial height, we explicitly account for the upward and downward travel at each bounce.
Working of Approach:
- Start with the initial height of the ball.
- Add the upward distance traveled by the ball.
- Add the downward distance when the ball returns to the ground.
- Reduce the rebound height to floor(h / 2) after every bounce.
- Repeat the process until the rebound height becomes zero.
#include <iostream>
using namespace std;
int jumpyBall(int h)
{
int dis = 0;
int height = h;
// Add the initial upward distance.
dis += h;
// Simulate every bounce.
while (h > 0)
{
// Ball comes down to the ground.
dis += h;
// Compute the rebound height.
h /= 2;
// Ball moves upward again if rebound exists.
if (height > 0)
dis += h;
}
return dis;
}
int main()
{
int h = 20;
cout << jumpyBall(h);
return 0;
}
public class GFG {
public static int jumpyBall(int h)
{
int dis = 0;
int height = h;
// Add the initial upward distance.
dis += h;
// Simulate every bounce.
while (h > 0) {
// Ball comes down to the ground.
dis += h;
// Compute the rebound height.
h /= 2;
// Ball moves upward again if rebound exists.
if (height > 0)
dis += h;
}
return dis;
}
public static void main(String[] args)
{
int h = 20;
System.out.println(jumpyBall(h));
}
}
def jumpyBall(h):
dis = 0
height = h
# Add the initial upward distance.
dis += h
# Simulate every bounce.
while h > 0:
# Ball comes down to the ground.
dis += h
# Compute the rebound height.
h //= 2
# Ball moves upward again if rebound exists.
if height > 0:
dis += h
return dis
if __name__ == '__main__':
h = 20
print(jumpyBall(h))
using System;
class GFG {
static int jumpyBall(int h)
{
int dis = 0;
int height = h;
// Add the initial upward distance.
dis += h;
// Simulate every bounce.
while (h > 0) {
// Ball comes down to the ground.
dis += h;
// Compute the rebound height.
h /= 2;
// Ball moves upward again if rebound exists.
if (height > 0)
dis += h;
}
return dis;
}
static void Main()
{
int h = 20;
Console.WriteLine(jumpyBall(h));
}
}
function jumpyBall(h)
{
let dis = 0;
let height = h;
// Add the initial upward distance.
dis += h;
// Simulate every bounce.
while (h > 0) {
// Ball comes down to the ground.
dis += h;
// Compute the rebound height.
h = Math.floor(h / 2);
// Ball moves upward again if rebound exists.
if (height > 0)
dis += h;
}
return dis;
}
// Driver Code
let h = 20;
console.log(jumpyBall(h));
Output
76
Add Round-Trip Distance for Every Height - O(log h) Time and O(1) Space
Instead of handling both movements separately, we directly add 2 × h for each height and then reduce the height to floor(h / 2). This simplifies the implementation while producing the same result.
Working of Approach:
- Initialize the total distance as 0.
- For every positive height, add 2 × height to the answer.
- Divide the current height by 2 to get the rebound height.
- Continue processing until the rebound height becomes 0.
- Return the accumulated distance.
Let us understand with an example:
Input: h = 20
- Initially, the ball travels 20 units upward and 20 units downward, adding 40 to the total distance.
- It then rebounds to a height of 10, contributing another 10 + 10 = 20 units.
- The subsequent rebound heights are 5, 2, and 1, adding 10, 4, and 2 units, respectively.
- After every bounce, the rebound height is updated to floor(h / 2) until it becomes 0.
- Thus, the total distance traveled is 40 + 20 + 10 + 4 + 2 = 76 units.
#include <iostream>
using namespace std;
int jumpyBall(int h)
{
int dis = 0;
// Add the upward and downward distance for every positive height.
while (h > 0)
{
dis += 2 * h;
h /= 2;
}
return dis;
}
int main()
{
int h = 20;
cout << jumpyBall(h);
return 0;
}
public class GFG {
public static int jumpyBall(int h)
{
int dis = 0;
// Add the upward and downward distance for every
// positive height.
while (h > 0) {
dis += 2 * h;
h /= 2;
}
return dis;
}
public static void main(String[] args)
{
int h = 20;
System.out.println(jumpyBall(h));
}
}
def jumpyBall(h):
dis = 0
# Add the upward and downward distance for every positive height.
while h > 0:
dis += 2 * h
h //= 2
return dis
if __name__ == "__main__":
h = 20
print(jumpyBall(h))
using System;
public class GFG {
public static int jumpyBall(int h)
{
int dis = 0;
// Add the upward and downward distance for every
// positive height.
while (h > 0) {
dis += 2 * h;
h /= 2;
}
return dis;
}
public static void Main()
{
int h = 20;
Console.WriteLine(jumpyBall(h));
}
}
function jumpyBall(h)
{
let dis = 0;
// Add the upward and downward distance for every
// positive height.
while (h > 0) {
dis += 2 * h;
h = Math.floor(h / 2);
}
return dis;
}
// Driver Code
let h = 20;
console.log(jumpyBall(h));
Output
76