Jump it up Solution | CodeChef [Easy]

Problem

Three dinos go on a camping trip. There they decide to play a game. They mark distances of 1 km each on the ground up to 100 km i.e. 1, 2, 3…100. They can sit only on these 100 spots, with each one at a different spot. In one move, one of the outer dinos can hop into a spot between the other two. Help them maximise their duration of play.

CodeChef Problem Link

Input

Three numbers(integers), l, m and n (0 < l < m < n < 100), the starting spots of the dinosaurs.

Output

Output the largest number of jumps the dinosaurs can make in all.

Constraints
  • 0 < l < m < n <  100
Sample Input
5 6 11
Sample Output
4
Solution

Greedy Approach: Choose the maximum distance between the dinosaurs among 1st & 2nd and 2nd & 3rd.

Code Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
//  main.cpp
//  Jump it up
//
//  Created by Himanshu on 16/04/22.
//
 
#include <iostream>
using namespace std;
 
int main() {
    int a, b, c;
    cin>>a>>b>>c;
     
    if ((b-a)>(c-b)) {
        cout<<(b-a-1)<<endl;
    } else {
        cout<<(c-b-1)<<endl;
    }
     
    return 0;
}

Time Complexity: O(1)

Leave a Reply

Your email address will not be published. Required fields are marked *