Boomerang Problem :
Follow this page to get early questions and more coding-related stuff.
Difficulty Level: Easy
Problem: Boomerang
Question :
A boomerang is a set of 3 points that are all distinct and not in a straight line. given a list of three points in a plane, return whether this points are boomerang
Test Case :
1). Input-1 :
[ [1,1], [2,2], [3,3] ]
Output-1 :
false
2). Input-2 :
[ [1,1], [2,3], [3,2] ]
Output-2 :
true
Solution
C++ | Java | Python
C++
/*
Author : _computer_engineers_
*/
#include <iostream>
using namespace std;
int main()
{
int x[3], y[3];
double slope[2];
bool result;
// Input of Points
for(int i=0; i<3; i++)
{
cout << "Enter Point-" << (i+1) << " : ";
cin >> x[i] >> y[i];
}
/*
If slope is equal, then points are linear as per Maths
Hence, it will not create Boomerang
*/
// Finding Slopes
slope[0] = (y[1]-y[0])/(x[1]-x[0]);
slope[1] = (y[2]-y[1])/(x[2]-x[1]);
// Checking Result
if(slope[0] == slope[1])
result = false;
else
result = true;
// Output
cout << "\nResult : " << boolalpha << result << endl;
return 0;
}
Java
//Author : _computer_engineers_
import java.util.Scanner;
public class Boomerang {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x[] = new int[3];
int y[] = new int[3];
double slope[] = new double[2];
boolean result;
// Input of Points
for(int i=0; i<3; i++) {
System.out.println("Enter Point-" + (i+1) + " : ");
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
/*
If slope is equal, then points are linear as per Maths
Hence, it will not create Boomerang
*/
// Finding Slopes
slope[0] = (y[1]-y[0])/(x[1]-x[0]);
slope[1] = (y[2]-y[1])/(x[2]-x[1]);
// Checking Result
if(slope[0] == slope[1])
result = false;
else
result = true;
// Output
System.out.println("\nResult : " + result);
sc.close();
}
}
Python
'''
Author : _computer_engineers_
'''
x = list()
y = list()
# Input of Points
for i in range(3):
print("Enter Point-{} : ".format(i+1))
x.append(int(input()))
y.append(int(input()))
'''
If slope is equal, then points are linear as per Maths
Hence, it will not create Boomerang
'''
# Finding Slope
slope = list()
slope.append((y[1]-y[0])/(x[1]-x[0]))
slope.append((y[2]-y[1])/(x[2]-x[1]))
if slope[0] == slope[1]:
result = False
else:
result = True
# Output
print("\nResult : {}".format(result))
if you like it please share & subscribe for more updates.
See other posts :
Thanks for sharing your
ReplyDeleteknowledge realted to programming
welcome :)) happy to share you
Deleteplease share it!
DeletePost a Comment
Like post ? Comment & Share