Angoor khatte hain!

A hungry fox, Tim was searching for food and saw a vine of grapes at some distance from him. Tim was standing at the point A when he saw the vine which is a point B. At a time, he can jump a distance of 5,2, or 1.

Help Tim to find out the minimum number of jumps he can have to reach the point B.

Example:

Input: a = 2, b = 10
Output: 3

Approach

Java


public class AngoorKhatteHai {
    public static void main(String[] args) {
        int a = 2;
        int b = 10;
        int output = 0;
        int c = b - a;
        if (c < 0)
            c = c * -1;
        output = output + (c / 5);
        c = c % 5;
        output = output + (c / 2);
        c = c % 2;
        output = output + (c / 1);
        c = c % 1;
        System.out.println(output);
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a = 2;
    int b = 10;
    int output = 0;
    int c = b - a;
    if (c < 0)
        c = c * -1;
    output = output + (c / 5);
    c = c % 5;
    output = output + (c / 2);
    c = c % 2;
    output = output + c;

    cout << output << "\n";

    return 0;
}


No comments:

Post a Comment