Algorithm


Problem Name: Mathematics - Find the Point

Problem Link: https://www.hackerrank.com/challenges/find-point/problem?isFullScreen=true

In this HackerRank in Mathematics - Find the Point solutions,

Consider two points, p = (px,py) and q = (qx,qy) . We consider the inversion or point reflection, r = (rx,ry), of point p across point q to be a 180 degree rotation of point p around q.

Given n sets of points p and q , find r for each pair of points and print two space-separated integers denoting the respective values of rx and ry on a new line.

Function Description

 

Complete the findPoint function in the editor below.

 

findPoint has the following parameters:

  • int px, py, qx, qy: x and y coordinates for points p and q.

Returns

  • int[2]: x and y coordinates of the reflected point r

Input Format

The first line contains an integer, n, denoting the number of sets of points.
Each of the n subsequent lines contains four space-separated integers that describe the respective values of px ,py , qx and qy defining points p = (px,py) and q = (qx,qy)

Constraints

  • 1 <= n <=15
  • -100 <= px ,py , qx , q<= 100

Sample Input

2
0 0 1 1
1 1 2 2

Sample Output

2 2
3 3

Explanation

The graphs below depict points p,q and r for the n = 2 points given as Sample Input:

  1. find-point-0011.png

  2. find-point-1122.png

 

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int px,py,qx,qy,rx,ry;
        scanf("%d%d%d%d",&px,&py,&qx,&qy);
        rx=2*qx-px;
        ry=2*qy-py;
        printf("%d %d\n",rx,ry);
    }
    return 0;
}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


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

int main()
{
    int T, a, b, c, d;
    cin >> T;
    for(int i = 1; i  < = T; i++){
        cin >> a >> b >> c >> d;
        
        cout << (2 * c) - a << " " << (2 * d) - b << endl;
    }

    return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with Java Programming

Code - Java Programming


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        int n, px, py, qx, qy;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();

        for (int i = 0; i  <  n; i++) {
            px = sc.nextInt();
            py = sc.nextInt();
            qx = sc.nextInt();
            qy = sc.nextInt();

            System.out.println(((qx-px)+qx) + " " + ((qy-py)+qy));
        }
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Javascript Programming

Code - Javascript Programming


'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the 'findPoint' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER px
 *  2. INTEGER py
 *  3. INTEGER qx
 *  4. INTEGER qy
 */

function findPoint(px, py, qx, qy) {
    let first = qx - px + qx,
        last = qy - py + qy;

    return [first, last];
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const n = parseInt(readLine().trim(), 10);

    for (let nItr = 0; nItr  <  n; nItr++) {
        const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');

        const px = parseInt(firstMultipleInput[0], 10);

        const py = parseInt(firstMultipleInput[1], 10);

        const qx = parseInt(firstMultipleInput[2], 10);

        const qy = parseInt(firstMultipleInput[3], 10);

        const result = findPoint(px, py, qx, qy);

        ws.write(result.join(' ') + '\n');
    }

    ws.end();
}
Copy The Code & Try With Live Editor

#5 Code Example with C# Programming

Code - C# Programming


using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

    /*
     * Complete the 'findPoint' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER px
     *  2. INTEGER py
     *  3. INTEGER qx
     *  4. INTEGER qy
     */

    public static List < int> findPoint(int px, int py, int qx, int qy) =>
    new List<int>() { -(px - qx) + qx, -(py - qy) + qy };

}

class Solution
{
    public static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine().Trim());

        for (int nItr = 0; nItr  <  n; nItr++)
        {
            string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');

            int px = Convert.ToInt32(firstMultipleInput[0]);

            int py = Convert.ToInt32(firstMultipleInput[1]);

            int qx = Convert.ToInt32(firstMultipleInput[2]);

            int qy = Convert.ToInt32(firstMultipleInput[3]);

            List < int> result = Result.findPoint(px, py, qx, qy);

            textWriter.WriteLine(String.Join(" ", result));
        }

        textWriter.Flush();
        textWriter.Close();
    }
}
Copy The Code & Try With Live Editor

#6 Code Example with Python Programming

Code - Python Programming


#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'findPoint' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER px
#  2. INTEGER py
#  3. INTEGER qx
#  4. INTEGER qy
#

def findPoint(px, py, qx, qy):
    p2q_x = qx - px
    p2q_y = qy - py
    r_pt = [qx + p2q_x, qy + p2q_y]
    return r_pt

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

    n = int(input().strip())

    for n_itr in range(n):
        first_multiple_input = input().rstrip().split()

        px = int(first_multiple_input[0])

        py = int(first_multiple_input[1])

        qx = int(first_multiple_input[2])

        qy = int(first_multiple_input[3])

        result = findPoint(px, py, qx, qy)

        fptr.write(' '.join(map(str, result)))
        fptr.write('\n')

    fptr.close()
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Array and Queries solution in Hackerrank - Hacerrank solution C, C++, java,js, Python
Next
[Solved] Maximum Draws solution in Hackerrank - Hacerrank solution C, C++, java,js, Python