Tutorialspoint
Problem
Solution
Submissions

Find the Determinant of a 2x2 Matrix

Certification: Basic Level Accuracy: 100% Submissions: 5 Points: 5

Write a C++ program to find the determinant of a 2x2 matrix. The determinant of a 2x2 matrix is calculated as (ad - bc), where the matrix is [[a, b], [c, d]].

Example 1
  • Input: matrix = [ [1, 2], [3, 4] ]
  • Output: -2
  • Explanation:
    • Step 1: Identify the values a=1, b=2, c=3, d=4 in the matrix.
    • Step 2: Apply the determinant formula: det = ad - bc.
    • Step 3: Calculate det = (1 × 4) - (2 × 3) = 4 - 6 = -2.
    • Step 4: Return the determinant value -2.
Example 2
  • Input: matrix = [ [5, 6], [7, 8] ]
  • Output: -2
  • Explanation:
    • Step 1: Identify the values a=5, b=6, c=7, d=8 in the matrix.
    • Step 2: Apply the determinant formula: det = ad - bc.
    • Step 3: Calculate det = (5 × 8) - (6 × 7) = 40 - 42 = -2.
    • Step 4: Return the determinant value -2.
Constraints
  • The matrix must be a 2x2 matrix
  • The elements of the matrix are integers
  • Time Complexity: O(1)
  • Space Complexity: O(1)
ArraysNumberIBMShopify
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use the formula (ad - bc) to calculate the determinant.
  • Ensure that the input matrix is a 2x2 matrix.

Steps to solve by this approach:

 Step 1: Define a function determinant_2x2 that takes a constant 2D vector as parameter.
 Step 2: Apply the formula for 2x2 determinant: ad - bc.
 Step 3: Return the calculated determinant value.
 Step 4: In main, create a 2x2 matrix.
 Step 5: Call the function and print the result.

Submitted Code :