Number of ordered points pair satisfying line equation in C++


The line equation that should be satisfied is y = mx + c. Given an array, m, and c, we have to find the number of order points satisfying the line equation. Let's see an example.

Input

arr = [1, 2, 3]
m = 1
c = 1

Output

2

The pairs satisfying the line equation are

2 1
3 2

Algorithm

  • Initialise the array, m, and c.
  • Write two loops to get all pairs from the array.
    • Check whether the pair is satisfying the line equation or not.
    • We can check the whether the equation is satisfied or not by substituting the values into the line equation.
    • If the pair satisfies the line equation, then increment the count.
  • Return the count.

Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h>
using namespace std;
bool isSatisfyingLineEquation(int arr[], int i, int j, int m, int c) {
   if (i == j) {
      return false;
   }
   return arr[j] == m * arr[i] + c;
}
int getOrderedPointsPairCount(int arr[], int n, int m, int c) {
   int count = 0;
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         if (isSatisfyingLineEquation(arr, i, j, m, c)) {
            count++;
         }
      }
   }
   return count;
}
int main() {
   int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   int n = 10;
   int m = 1, c = 1;
   cout << getOrderedPointsPairCount(arr, n, m, c) << endl;
   return 0;
}

Output

If you run the above code, then you will get the following result.

9

Updated on: 26-Oct-2021

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements