Sort by

recency

|

2044 Discussions

|

  • + 0 comments
    using namespace std;
    
    
    int main(){
        int n;
        cin >> n;
        int p;
        cin >> p;
        int min_left = 0;
        int cur_left = 0, cur_right = 1;
        while (cur_left != p && cur_right != p) {
            ++min_left;
            cur_left += 2;
            cur_right += 2;
        }
        int min_right = 0;
        cur_left = n % 2 == 0 ? n : n - 1;
        cur_right = cur_left + 1;
        
        while (cur_left != p && cur_right != p) {
            ++min_right;
            cur_left -= 2;
            cur_right -= 2;
        }
        
        cout << std::min(min_left, min_right);
        return 0;
    }
    
  • + 0 comments

    !/bin/python3

    import math import os import random import re import sys

    def pageCount(n, p): return min(p//2,n//2-p//2)

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())
    
    p = int(input().strip())
    
    result = pageCount(n, p)
    
    fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 0 comments

    TS SOLUTION:

    function pageCount(n: number, p: number): number { // Write your code here const leftFlips: number = Math.floor(p / 2) const rightFlips: number = Math.ceil(((n - n % 2) - p) / 2) return Math.min(leftFlips, rightFlips) }

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/1T650YeAwRI

    int pageCount(int n, int p) {
        return min(p/2, n/2-p/2);
    }
    
  • + 0 comments

    java(8)

    public static int pageCount(int n, int p) {
        // Write your code here
    
        int firstStart = 0;
        int lastStart = 0;
        for (int i = 0; i < n; i++) {
    
            firstStart++;
            if (i == p) {
                break;
            }
    
        }
        for (int i = n; i >= 0; i--) {
    
            lastStart++;
            if (i == p) {
                break;
            }
    
        }
        int f;
        int l;
        if (n % 2 == 0) {
            f = (firstStart / 2);
            l = (lastStart / 2);
        } else {
            f = (firstStart / 2);
            l = (lastStart / 2) - 1;
        }
    
        return Math.min(f, l);
    
    }