Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1379

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1379

Mean Median Problem

 

By Pablo Heiber Argentina

Timelimit: 1

The mean of three integers A, B and C is (A + B + C)/3. The median of three integers is the one that would be in the middle if they are sorted in non-decreasing order. Given two integers A and B, return the minimum possible integer C such that the mean and the median of A, B and C are equal.

 

Input

 

Each test case is given in a single line that contains two integers A and B (1 ≤ AB ≤ 109). The last test case is followed by a line containing two zeros.

 

Output

 

For each test case output one line containing the minimum possible integer C such that the mean and the median of A, B and C are equal.

 

 

 

Sample Input Sample Output

1 2
6 10
1 1000000000
0 0

0
2
-999999998

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>

int main(void) {
    int a, b;
    while (scanf("%d %d", &a, &b) == 2) {
        if (a == 0 && b == 0) break;
        printf("%d\n", 2 * a - b);
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2
6 10
1 1000000000
0 0

Output

x
+
cmd
0
2
-999999998

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main(void) {
    int a, b;
    while (cin >> a >> b) {
        if (a == 0 && b == 0) break;
        cout << 2 * a - b << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2
6 10
1 1000000000
0 0

Output

x
+
cmd
0
2
-999999998

#3 Code Example with Java Programming

Code - Java Programming


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
   static Reader in = new Reader(System.in);
    static Writer out = new Writer(System.out);

    public static void main(String[] args) throws IOException {
        int[] a;
        while (true) {
            a = in.nextIntArray(2);
            if (a[0] == 0 && a[1] == 0) {
                break;
            }
            Arrays.sort(a);
            out.println(2 * a[0] - a[1]);
        }
        in.close();
        out.flush();
        out.close();
    }

    static class Reader implements Closeable {

        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        public Reader(InputStream input) {
            reader = new BufferedReader(
                    new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        private StringTokenizer getTokenizer() throws IOException {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                String line = nextLine();
                if (line == null) {
                    return null;
                }
                tokenizer = new StringTokenizer(line);
            }
            return tokenizer;
        }

        public boolean hasNext() throws IOException {
            return getTokenizer() != null;
        }

        public String next() throws IOException {
            return hasNext() ? tokenizer.nextToken() : null;
        }

        public String nextLine() throws IOException {
            tokenizer = null;
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public float nextFloat() throws IOException {
            return Float.parseFloat(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public String[] nextStringArray(int size) throws IOException {
            String[] array = new String[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = next();
            }
            return array;
        }

        public int[] nextIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        public long[] nextLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextLong();
            }
            return array;
        }

        public double[] nextDoubleArray(int size) throws IOException {
            double[] array = new double[size];
            for (int i = 0; i  <  size; i++) {
                array[i] = nextDouble();
            }
            return array;
        }

        @Override
        public void close() throws IOException {
            tokenizer = null;
            reader.close();
        }
    }

    static class Writer {

        private final PrintWriter writer;

        public Writer(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public void print(Object... objects) {
            for (int i = 0; i  <  objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            writer.flush();
        }

    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2
6 10
1 1000000000
0 0

Output

x
+
cmd
0
2
-999999998

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n").map((line) => line.split(" "))

function main() {
	const responses = []

	for (const [A, B] of input) {
		if (A === "0" && B === "0") break
		responses.push(2 * (+A) - (+B))
	}

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2
6 10
1 1000000000
0 0

Output

x
+
cmd
0
2
-999999998

#5 Code Example with Python Programming

Code - Python Programming


while True:
    e = str(input()).split()
    a = int(e[0])
    b = int(e[1])
    if a == 0 == b: break
    print(2 * a - b)

Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2
6 10
1 1000000000
0 0

Output

x
+
cmd
0
2
-999999998
Advertisements

Demonstration


Previous
#1375 Beecrowd Online Judge Solution 1375 Pole Position Solution in C, C++, Java, Js and Python
Next
#1383 Beecrowd Online Judge Solution 1383 Sudoku Solution in C, C++, Java, Js and Python