Given six integers representing the x, y, and z coordinates of two vectors, the task is to check if the two given vectors are collinear or not.
Examples:
Input: x1 = 4, y1 = 8, z1 = 12, x2 = 8, y2 = 16, z2 = 24
Output: Yes
Explanation: The given vectors: 4i + 8j + 12k and 8i + 16j + 24k are collinear.
Input: x1 = 2, y1 = 8, z1 = -4, x2 = 4, y2 = 16, z2 = 8
Output: No
Explanation: The given vectors: 2i + 8j – 4k and 4i + 16j + 8k are not collinear.
Approach: The problem can be solved based on the idea that two vectors are collinear if any of the following conditions are satisfied:
- Two vectors A and B are collinear if there exists a number n, such that A = n · b.
- Two vectors are collinear if relations of their coordinates are equal, i.e. x1 / x2 = y1 / y2 = z1 / z2.
Note: This condition is not valid if one of the components of the vector is zero.
- Two vectors are collinear if their cross product is equal to the NULL Vector.
Therefore, to solve the problem, the idea is to check if the cross-product of the two given vectors is equal to the NULL Vector or not. If found to be true, then print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void crossProduct( int vect_A[],
int vect_B[],
int cross_P[])
{
cross_P[0]
= vect_A[1] * vect_B[2]
- vect_A[2] * vect_B[1];
cross_P[1]
= vect_A[2] * vect_B[0]
- vect_A[0] * vect_B[2];
cross_P[2]
= vect_A[0] * vect_B[1]
- vect_A[1] * vect_B[0];
}
void checkCollinearity( int x1, int y1,
int z1, int x2,
int y2, int z2)
{
int A[3] = { x1, y1, z1 };
int B[3] = { x2, y2, z2 };
int cross_P[3];
crossProduct(A, B, cross_P);
if (cross_P[0] == 0 && cross_P[1] == 0
&& cross_P[2] == 0)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int x1 = 4, y1 = 8, z1 = 12;
int x2 = 8, y2 = 16, z2 = 24;
checkCollinearity(x1, y1, z1,
x2, y2, z2);
return 0;
}
|
Java
class GFG{
static void crossProduct( int vect_A[],
int vect_B[],
int cross_P[])
{
cross_P[ 0 ] = vect_A[ 1 ] * vect_B[ 2 ] -
vect_A[ 2 ] * vect_B[ 1 ];
cross_P[ 1 ] = vect_A[ 2 ] * vect_B[ 0 ] -
vect_A[ 0 ] * vect_B[ 2 ];
cross_P[ 2 ] = vect_A[ 0 ] * vect_B[ 1 ] -
vect_A[ 1 ] * vect_B[ 0 ];
}
static void checkCollinearity( int x1, int y1,
int z1, int x2,
int y2, int z2)
{
int A[] = { x1, y1, z1 };
int B[] = { x2, y2, z2 };
int cross_P[] = new int [ 3 ];
crossProduct(A, B, cross_P);
if (cross_P[ 0 ] == 0 && cross_P[ 1 ] == 0 &&
cross_P[ 2 ] == 0 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main (String[] args)
{
int x1 = 4 , y1 = 8 , z1 = 12 ;
int x2 = 8 , y2 = 16 , z2 = 24 ;
checkCollinearity(x1, y1, z1,
x2, y2, z2);
}
}
|
Python3
def crossProduct(vect_A, vect_B, cross_P):
cross_P[ 0 ] = (vect_A[ 1 ] * vect_B[ 2 ] -
vect_A[ 2 ] * vect_B[ 1 ])
cross_P[ 1 ] = (vect_A[ 2 ] * vect_B[ 0 ] -
vect_A[ 0 ] * vect_B[ 2 ])
cross_P[ 2 ] = (vect_A[ 0 ] * vect_B[ 1 ] -
vect_A[ 1 ] * vect_B[ 0 ])
def checkCollinearity(x1, y1, z1, x2, y2, z2):
A = [x1, y1, z1]
B = [x2, y2, z2]
cross_P = [ 0 for i in range ( 3 )]
crossProduct(A, B, cross_P)
if (cross_P[ 0 ] = = 0 and
cross_P[ 1 ] = = 0 and
cross_P[ 2 ] = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
x1 = 4
y1 = 8
z1 = 12
x2 = 8
y2 = 16
z2 = 24
checkCollinearity(x1, y1, z1, x2, y2, z2)
|
C#
using System;
class GFG{
static void crossProduct( int []vect_A,
int []vect_B,
int []cross_P)
{
cross_P[0] = vect_A[1] * vect_B[2] -
vect_A[2] * vect_B[1];
cross_P[1] = vect_A[2] * vect_B[0] -
vect_A[0] * vect_B[2];
cross_P[2] = vect_A[0] * vect_B[1] -
vect_A[1] * vect_B[0];
}
static void checkCollinearity( int x1, int y1,
int z1, int x2,
int y2, int z2)
{
int []A = { x1, y1, z1 };
int []B = { x2, y2, z2 };
int []cross_P = new int [3];
crossProduct(A, B, cross_P);
if (cross_P[0] == 0 && cross_P[1] == 0 &&
cross_P[2] == 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main ( string [] args)
{
int x1 = 4, y1 = 8, z1 = 12;
int x2 = 8, y2 = 16, z2 = 24;
checkCollinearity(x1, y1, z1,
x2, y2, z2);
}
}
|
Javascript
<script>
function crossProduct(vect_A,
vect_B,
cross_P) {
cross_P[0]
= vect_A[1] * vect_B[2]
- vect_A[2] * vect_B[1];
cross_P[1]
= vect_A[2] * vect_B[0]
- vect_A[0] * vect_B[2];
cross_P[2]
= vect_A[0] * vect_B[1]
- vect_A[1] * vect_B[0];
}
function checkCollinearity(x1, y1,
z1, x2,
y2, z2) {
let A = [x1, y1, z1];
let B = [x2, y2, z2];
let cross_P = [];
crossProduct(A, B, cross_P);
if (cross_P[0] == 0 && cross_P[1] == 0
&& cross_P[2] == 0)
document.write( "Yes" )
else
document.write( "No" )
}
let x1 = 4, y1 = 8, z1 = 12;
let x2 = 8, y2 = 16, z2 = 24;
checkCollinearity(x1, y1, z1,
x2, y2, z2);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Using the Cross Product:
Approach:
The cross product of two collinear vectors will be zero. We can use this property to check if two vectors are collinear or not.
Take input for the two vectors.
Calculate the cross product of the two vectors.
Check if the cross product is zero using the all() function.
If the cross product is zero, the vectors are collinear. Otherwise, they are not collinear
C++
#include <iostream>
using namespace std;
int main() {
int x1 = 4, y1 = 8, z1 = 12;
int x2 = 8, y2 = 16, z2 = 24;
int cross_product[3] = {y1 * z2 - z1 * y2, z1 * x2 - x1 * z2, x1 * y2 - y1 * x2};
if (cross_product[0] == 0 && cross_product[1] == 0 && cross_product[2] == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
public class CrossProductCheck {
public static void main(String[] args) {
int x1 = 4 , y1 = 8 , z1 = 12 ;
int x2 = 8 , y2 = 16 , z2 = 24 ;
int [] crossProduct = {
y1 * z2 - z1 * y2,
z1 * x2 - x1 * z2,
x1 * y2 - y1 * x2
};
if (crossProduct[ 0 ] == 0 && crossProduct[ 1 ] == 0 && crossProduct[ 2 ] == 0 ) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
x1, y1, z1 = 4 , 8 , 12
x2, y2, z2 = 8 , 16 , 24
cross_product = (y1 * z2 - z1 * y2, z1 * x2 - x1 * z2, x1 * y2 - y1 * x2)
if all (i = = 0 for i in cross_product):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
public static void Main()
{
int x1 = 4, y1 = 8, z1 = 12;
int x2 = 8, y2 = 16, z2 = 24;
int [] crossProduct = { y1 * z2 - z1 * y2, z1 * x2 - x1 * z2, x1 * y2 - y1 * x2 };
if (crossProduct[0] == 0 && crossProduct[1] == 0 && crossProduct[2] == 0)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function calculateCrossProduct(x1, y1, z1, x2, y2, z2) {
return [
y1 * z2 - z1 * y2,
z1 * x2 - x1 * z2,
x1 * y2 - y1 * x2
];
}
function isZeroCrossProduct(crossProduct) {
return crossProduct[0] === 0 && crossProduct[1] === 0 && crossProduct[2] === 0;
}
let x1 = 4, y1 = 8, z1 = 12;
let x2 = 8, y2 = 16, z2 = 24;
let crossProduct = calculateCrossProduct(x1, y1, z1, x2, y2, z2);
if (isZeroCrossProduct(crossProduct)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Oct, 2023
Like Article
Save Article