Number of ordered points pair satisfying line equation
Last Updated :
25 Sep, 2022
Given an array of n integers, slope of a line i. e., m and the intercept of the line i.e c, Count the number of ordered pairs(i, j) of points where i ? j, such that point (Ai, Aj) satisfies the line formed with given slope and intercept.
Note : The equation of the line is y = mx + c, where m is the slope of the line and c is the intercept
Examples :
Input : m = 1, c = 1, arr[] = [ 1, 2, 3, 4, 2 ]
Output : 5 ordered points
Explanation : The equation of the line with given slope and intercept is : y = x + 1. The Number of pairs (i, j), for which (arri, arrj) satisfies the above equation of the line are : (1, 2), (1, 5), (2, 3), (3, 4), (5, 3).
Input : m = 2, c = 1, arr[] = [ 1, 2, 3, 4, 2, 5 ]
Output : 3 ordered points
Method 1 (Brute Force):
Generate all possible pairs (i, j) and check if a particular ordered pair (i, j) is such that, (arri, arrj) satisfies the given equation of the line y = mx + c, and i ? j. If the point is valid(a point is valid if the above condition is satisfied), increment the counter which stores the total number of valid points.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValid( int arr[], int i, int j,
int m, int c)
{
if (i == j)
return false ;
int lhs = arr[j];
int rhs = m * arr[i] + c;
return (lhs == rhs);
}
int findOrderedPoints( int arr[], int n,
int m, int c)
{
int counter = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
int firstIndex = i, secondIndex = j;
if (isValid(arr, firstIndex, secondIndex, m, c))
counter++;
}
}
return counter;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 1, c = 1;
cout << findOrderedPoints(arr, n, m, c);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean isValid( int []arr, int i,
int j, int m, int c)
{
if (i == j)
return false ;
int lhs = arr[j];
int rhs = m * arr[i] + c;
return (lhs == rhs);
}
static int findOrderedPoints( int []arr,
int n, int m, int c)
{
int counter = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
int firstIndex = i,
secondIndex = j;
if (isValid(arr, firstIndex,
secondIndex, m, c))
counter++;
}
}
return counter;
}
public static void main(String args[])
{
int []arr = { 1 , 2 , 3 , 4 , 2 };
int n = arr.length;
int m = 1 , c = 1 ;
System.out.print(
findOrderedPoints(arr, n, m, c));
}
}
|
Python3
def isValid(arr, i, j, m, c) :
if (i = = j) :
return False
lhs = arr[j];
rhs = m * arr[i] + c
return (lhs = = rhs)
def findOrderedPoints(arr, n, m, c) :
counter = 0
for i in range ( 0 , n) :
for j in range ( 0 , n) :
firstIndex = i
secondIndex = j
if (isValid(arr, firstIndex,
secondIndex, m, c)) :
counter = counter + 1
return counter
arr = [ 1 , 2 , 3 , 4 , 2 ]
n = len (arr)
m = 1
c = 1
print (findOrderedPoints(arr, n, m, c))
|
C#
using System;
class GFG {
static bool isValid( int []arr, int i,
int j, int m, int c)
{
if (i == j)
return false ;
int lhs = arr[j];
int rhs = m * arr[i] + c;
return (lhs == rhs);
}
static int findOrderedPoints( int []arr, int n,
int m, int c)
{
int counter = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
int firstIndex = i, secondIndex = j;
if (isValid(arr, firstIndex, secondIndex, m, c))
counter++;
}
}
return counter;
}
public static void Main()
{
int []arr = { 1, 2, 3, 4, 2 };
int n = arr.Length;
int m = 1, c = 1;
Console.Write(findOrderedPoints(arr, n, m, c));
}
}
|
PHP
<?php
function isValid( $arr , $i ,
$j , $m , $c )
{
if ( $i == $j )
return false;
$lhs = $arr [ $j ];
$rhs = $m * $arr [ $i ] + $c ;
return ( $lhs == $rhs );
}
function findOrderedPoints( $arr , $n ,
$m , $c )
{
$counter = 0;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
$firstIndex = $i ; $secondIndex = $j ;
if (isValid( $arr , $firstIndex ,
$secondIndex , $m , $c ))
$counter ++;
}
}
return $counter ;
}
$arr = array ( 1, 2, 3, 4, 2 );
$n = count ( $arr );
$m = 1; $c = 1;
echo (findOrderedPoints( $arr , $n , $m , $c ));
?>
|
Javascript
<script>
function isValid(arr, i, j, m, c)
{
if (i == j) return false ;
var lhs = arr[j];
var rhs = m * arr[i] + c;
return lhs == rhs;
}
function findOrderedPoints(arr, n, m, c) {
var counter = 0;
for ( var i = 0; i < n; i++)
{
for ( var j = 0; j < n; j++)
{
var firstIndex = i,
secondIndex = j;
if (isValid(arr, firstIndex, secondIndex, m, c)) counter++;
}
}
return counter;
}
var arr = [1, 2, 3, 4, 2];
var n = arr.length;
var m = 1,
c = 1;
document.write(findOrderedPoints(arr, n, m, c));
</script>
|
Complexity Analysis:
- Time Complexity : O(n2)
- Auxiliary Space: O(1)
Method 2 (Efficient) :
Given a x coordinate of a point, for each x there is a unique value of y and the value of y is nothing but m * x + c. So, for each possible x coordinate of the array arr, calculate how many times the unique value of y which satisfies the equation of the line occurs in that array. Store count of all the integers of array, arr in a map. Now, for each value, arri, add to the answer, the number of occurrences of m * arri + c. For a given i, m * a[i] + c occurs x times in the array, then, add x to our counter for total valid points, but need to check that if a[i] = m * a[i] + c then, it is obvious that since this occurs x times in the array then one occurrence is at the ith index and rest (x – 1) occurrences are the valid y coordinates so add (x – 1) to our points counter.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findOrderedPoints( int arr[], int n,
int m, int c)
{
int counter = 0;
unordered_map< int , int > frequency;
for ( int i = 0; i < n; i++)
frequency[arr[i]]++;
for ( int i = 0; i < n; i++)
{
int xCoordinate = arr[i];
int yCoordinate = (m * arr[i] + c);
if (frequency.find(yCoordinate) !=
frequency.end())
counter += frequency[yCoordinate];
if (xCoordinate == yCoordinate)
counter--;
}
return counter;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int m = 1, c = 1;
cout << findOrderedPoints(arr, n, m, c);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int findOrderedPoints( int arr[], int n, int m, int c){
int counter = 0 ;
HashMap<Integer,Integer> frequency = new HashMap<>();
for ( int i= 0 ;i<n;i++){
if (frequency.get(arr[i])== null ){
frequency.put(arr[i], 1 );
} else {
frequency.put(arr[i],frequency.get(arr[i])+ 1 );
}
}
for ( int i = 0 ; i < n; i++)
{
int xCoordinate = arr[i];
int yCoordinate = (m * arr[i] + c);
if (frequency.get(yCoordinate) != null )
counter += frequency.get(yCoordinate);
if (xCoordinate == yCoordinate)
counter--;
}
return counter;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 2 };
int n= 5 ,m= 1 ,c= 1 ;
System.out.println(findOrderedPoints(arr,n,m,c));
}
}
|
Python3
def findOrderedPoints(arr, n, m, c):
counter = 0
frequency = dict ()
for i in range (n):
if (arr[i] in frequency):
frequency[arr[i]] + = 1
else :
frequency[arr[i]] = 1
for i in range (n):
xCoordinate = arr[i]
yCoordinate = (m * arr[i] + c)
if (yCoordinate in frequency):
counter + = frequency[yCoordinate]
if (xCoordinate = = yCoordinate):
counter - = 1
return counter
arr = [ 1 , 2 , 3 , 4 , 2 ]
n = len (arr)
m = 1
c = 1
print (findOrderedPoints(arr, n, m, c))
|
Javascript
function findOrderedPoints(arr, n, m, c)
{
let counter = 0;
let frequency = new Map();
for (let i = 0; i < n; i++){
if (frequency.has(arr[i])){
frequency.set(arr[i], frequency.get(arr[i]) + 1);
}
else {
frequency.set(arr[i], 1);
}
}
for (let i = 0; i < n; i++)
{
let xCoordinate = arr[i];
let yCoordinate = (m * arr[i] + c);
if (frequency.has(yCoordinate))
counter += frequency.get(yCoordinate);
if (xCoordinate == yCoordinate)
counter--;
}
return counter;
}
let arr = [1, 2, 3, 4, 2];
let n = arr.length;
let m = 1, c = 1;
console.log(findOrderedPoints(arr, n, m, c));
|
C#
using System;
using System.Collections.Generic;
public static class GFG {
public static int findOrderedPoints( int [] arr, int n,
int m, int c)
{
int counter = 0;
Dictionary< int , int > frequency
= new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (frequency.ContainsKey(arr[i])) {
var val = frequency[arr[i]];
frequency.Remove(arr[i]);
frequency.Add(arr[i], val + 1);
}
else {
frequency.Add(arr[i], 1);
}
}
for ( int i = 0; i < n; i++) {
int xCoordinate = arr[i];
int yCoordinate = (m * arr[i] + c);
if (frequency.ContainsKey(yCoordinate)) {
counter += frequency[yCoordinate];
}
if (xCoordinate == yCoordinate) {
counter--;
}
}
return counter;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 2 };
int n = arr.Length;
int m = 1;
int c = 1;
Console.Write(findOrderedPoints(arr, n, m, c));
}
}
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(n) because it is using extra space for unoredered_map frequency.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...