Given two force vectors, find out whether they are parallel, perpendicular or neither. Let the first vector be A = a1 i +a2 j + a3 k and the second vector be B = b1 i + b2 j + b3 k.
A.B = a1 * b1 + a2 * b2 + a3 * b3
A x B = (a2 * b3 – a3 * b2) i – (a1 * b3 – b1* a3) j + (a1 * b2 – a2 * b1) k
|A|2 = a12 + a22 + a32
If A.B = 0, then A and B are perpendicular.
If |A X B|2 = 0, then A and B are parallel.
Return 1 if they are parallel to each other, 2 if they are perpendicular to each other or 0 otherwise.
Examples:
Input: A = {3, 2, 1}, B = {6, 4, 2}
Output: 1
Explanation: |A X B|2 = 0Input: A = {4, 6, 1}, B = {1, -1, 2}
Output: 2
Explanation: A.B = 0
Approach: Follow the steps to solve this problem:
- Find A.B and A X BÂ
- If A.B = 0, then return 2.
- Else if, |A X B| = 0, then  |A X B|2 is also 0, then return 1.
- Else, return 0.
Below is the implementation of the above approach.
C++
// C++ code to implement the approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to find out whether they// are parallel, perpendicular or neitherint find(vector<int> A, vector<int> B){Â Â Â Â // Find A.BÂ Â Â Â int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];Â
    // Find A X B    int par = (A[1] * B[2] - A[2] * B[1])                  * (A[1] * B[2] - A[2] * B[1])              + (A[0] * B[2] - B[0] * A[2])                    * (A[0] * B[2] - B[0] * A[2])              + (A[0] * B[1] - A[1] * B[0])                    * (A[0] * B[1] - A[1] * B[0]);Â
    if (per == 0) {        return 2;    }Â
    else if (par == 0) {        return 1;    }Â
    else {        return 0;    }}Â
// Driver Codeint main(){Â Â Â Â vector<int> a = { 3, 2, 1 };Â Â Â Â vector<int> b = { 6, 4, 2 };Â
    // Function call    cout << find(a, b) << endl;    return 0;} |
Java
// Java code for the above approachimport java.io.*;Â
class GFG {Â
  // Function to find out whether they are parallel,  // perpendicular or neither  static int find(int[] A, int[] B)  {    // Find A.B    int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];Â
    // Find A X B    int par = (A[1] * B[2] - A[2] * B[1])      * (A[1] * B[2] - A[2] * B[1])      + (A[0] * B[2] - B[0] * A[2])      * (A[0] * B[2] - B[0] * A[2])      + (A[0] * B[1] - A[1] * B[0])      * (A[0] * B[1] - A[1] * B[0]);Â
    if (per == 0) {      return 2;    }Â
    else if (par == 0) {      return 1;    }Â
    else {      return 0;    }  }Â
  public static void main(String[] args)  {    int[] a = { 3, 2, 1 };    int[] b = { 6, 4, 2 };Â
    // Function call    System.out.println(find(a, b));  }}Â
// This code is contributed by lokeshmvs21. |
Python3
# Python3 code to implement the approachÂ
# Function to find out whether they# are parallel, perpendicular or neitherdef find(A, B):Â
    # Find A.B    per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2]Â
    # Find A X B    par = (A[1] * B[2] - A[2] * B[1]) * (A[1] * B[2] - A[2] * B[1]) + (A[0] * B[2] - B[0] * A[2]) * \        (A[0] * B[2] - B[0] * A[2]) + (A[0] * B[1] -                                       A[1] * B[0]) * (A[0] * B[1] - A[1] * B[0])Â
    if (per == 0):        return 2Â
    elif (par == 0):        return 1Â
    else:        return 0Â
# Driver Codeif __name__ == "__main__":Â
    a = [3, 2, 1]    b = [6, 4, 2]Â
    # Function call    print(find(a, b))Â
    # This code is contributed by rakeshsahni |
C#
using System;Â
public class GFG{Â
  // Function to find out whether they  // are parallel, perpendicular or neither  public static int find(int[] A, int[] B)  {Â
    // Find A.B    int per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];Â
    // Find A X B    int par = (A[1] * B[2] - A[2] * B[1])      * (A[1] * B[2] - A[2] * B[1])      + (A[0] * B[2] - B[0] * A[2])      * (A[0] * B[2] - B[0] * A[2])      + (A[0] * B[1] - A[1] * B[0])      * (A[0] * B[1] - A[1] * B[0]);Â
    if (per == 0) {      return 2;    }Â
    else if (par == 0) {      return 1;    }Â
    else {      return 0;    }  }Â
  static public void Main()  {Â
    int[] a = { 3, 2, 1 };    int[] b = { 6, 4, 2 };Â
    // Function call    Console.WriteLine(find(a, b));  }}Â
// This code is contributed by akashish__ |
Javascript
<script>Â Â Â Â Â Â Â Â // JavaScript code for the above approachÂ
        // Function to find out whether they        // are parallel, perpendicular or neither        function find(A, B)        {                     // Find A.B            let per = A[0] * B[0] + A[1] * B[1] + A[2] * B[2];Â
            // Find A X B            let par = (A[1] * B[2] - A[2] * B[1])                * (A[1] * B[2] - A[2] * B[1])                + (A[0] * B[2] - B[0] * A[2])                * (A[0] * B[2] - B[0] * A[2])                + (A[0] * B[1] - A[1] * B[0])                * (A[0] * B[1] - A[1] * B[0]);Â
            if (per == 0) {                return 2;            }Â
            else if (par == 0) {                return 1;            }Â
            else {                return 0;            }        }Â
        // Driver Code        let a = [3, 2, 1];        let b = [6, 4, 2];Â
        // Function call        document.write(find(a, b));Â
 // This code is contributed by Potta LokeshÂ
    </script> |
1
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
