Contents

Parallel (with.Java)

   May 14, 2024     2 min read

This article looks into the “parallel (with.Java)” problem.

As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.

Let’s look at the problem first.

problem

A two-dimensional array dots containing the coordinates of four points is given as a parameter as follows.

[[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

Complete the solution function so that when the given four points are divided into two, if the two straight lines are parallel, it returns 1, and if not, it returns 0.

Restrictions

  • Length of dots = 4
  • The elements of dots are in the form [x, y], where x and y are integers.
  • 0 ≤ x, y ≤ 100
  • There is no case where two or more different points overlap.
  • Even if two straight lines overlap (coincide), please return 1.
  • It is not given if the straight line connecting any two points is parallel to the x-axis or y-axis.

Input/Output Example

dotsresult
[[1, 4], [9, 2], [3, 8], [11, 6]]1
[[3, 5], [4, 1], [2, 4], [5, 10]]0

My solution to the problem

class Solution {
     public int solution(int[][] dots) {
         int answer = 0;
         for(int i =0; i<dots.length;i++) {

                 float temp =gradient(dots[i],dots[(i+1)%4]);
                 float temp2 =gradient(dots[(i+2)%4],dots[(i+3)%4]);

                 if(temp==temp2) {
                     answer = 1;
                 }
         }
         return answer;
     }
     public static float gradient(int[]a1,int[]a2) {
         float denom, mole;

             denom=a1[0]-a2[0];
             mole=a1[1]-a2[1];

         return mole/denom;
     }
}

Solution explanation

The solution method takes a given array of points, dots, as an argument.

The answer variable stores the result indicating whether it is square or not.

It is initially set to 0.

A for loop iterates over an array of dots.

At this time, based on the ith point, the slopes between the other three points excluding that point are compared.

The gradient method calculates the gradient between two points. This allows you to find the slope of the line formed by two points.

If all sides have the same slope (temp and temp2 are the same), set answer to 1.

After completing all loops, the answer value is returned.

Provides a simple method to check whether a square is formed using given points.

Although the number of points and the shape of the array may change depending on the given problem, you can use the method to determine the shape of the polygon.