settingsAccountsettings
Menusettings

Q: Critical Breakpoint - Java Task

+10 votes

Imagine a plane (a dimension) and parallel lines in it. Normally they would never meet, but let us imagine that we are working in magical planes and all of them have a point where they bend infinitely in order to connect to the other planes. That point is called the critical breakpoint.

You are given a set of 4 integers separated by a single space, on every line of input, which represents plane coordinates for each line – {x1, y1}, {x2, y2}, the two points that form the line. When you get the command “Break it.”, you END the input. Your task is to find if the given lines form a plane which holds a critical breakpoint.

The critical breakpoint is formed only if the critical ratio of the current line is equal to the others’ critical ratio or is equal to ZERO. The critical ratio is, for each line, the absolute value of “(X2 + Y2) – (X1 + Y1)”.

If you find a non-zero critical ratio value it becomes the actual critical ratio, and if there is even one line, which’s critical ratio does not equal that value, and is not equal to zero, the current lines fail to create a critical breakpoint. Then you print “Critical breakpoint does not exist.”.

If a critical breakpoint is formed you need to calculate it. A critical breakpoint is equal to – the remainder of the division of, the critical ratio (which in this case is one for all the lines) powered by the count of lines, and the count of lines. If we have 4 lines and critical ratios – “10, 0, 0, 10” the breakpoint will be the remainder of: “(104) / 4”, which is 0.

Input:

  •  The input will come in the form of lines on which you will get a set of 4 integers separated by a single space.
  •  The input continues until you receive the command “Break it.”.

Output:

  • If a critical breakpoint has been found you need to print each of the lines in the following format:
  • “Line: [x1, y1, x2, y2]”
  • And after them, print the critical breakpoint in this format:
  • “Critical Breakpoint: {breakpoint}”
  • In case there is no breakpoint you must print only the following:
  • “Critical breakpoint does not exist.”

Constraints:

  • All of the input numbers will be valid integers in the range [-231 + 1, 231 - 1].
  • Allowed time/memory: 250ms/16MB.

Examples:

critical breakpoint java task

asked in Java category by user sam
edited by user golearnweb

1 Answer

+4 votes
 
Best answer

Here's my solution mate:

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class CriticalBreakPoint {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String line;
        List<String> lines = new ArrayList<>();

        long absoluteCriticalRatio = 0;
        while (!"Break it.".equals(line = sc.nextLine())) {
            int[] numbers = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
            int x1 = numbers[0];
            int y1 = numbers[1];
            int x2 = numbers[2];
            int y2 = numbers[3];

            lines.add(Arrays.toString(numbers));

            long currentCriticalRatio = Math.abs(((long) x2 + (long) y2) - ((long) x1 + (long) y1));

            if (absoluteCriticalRatio == 0 && currentCriticalRatio != 0) {
                absoluteCriticalRatio = currentCriticalRatio;
            }

            if (currentCriticalRatio != 0 && currentCriticalRatio != absoluteCriticalRatio) {
                System.out.println("Critical breakpoint does not exist.");
                return;//NOT break;
            }
        }
        for (String linee : lines) {
            System.out.println("Line: " + linee);
        }

        BigInteger criticalBreakPoint = BigInteger.valueOf(absoluteCriticalRatio).pow(lines.size()).remainder(BigInteger.valueOf(lines.size()));

//        long criticalBreakPoint = (long) Math.pow(absoluteCriticalRatio, lines.size()) % lines.size();
        System.out.println("Critical Breakpoint: " + criticalBreakPoint);
    }
}

 

answered by user eiorgert
selected by user golearnweb
...