Count passing car pairs
Last Updated :
13 Sep, 2023
A non-empty binary array A consisting of size N is given where,
0 represents a car traveling east,
1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 <= P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
Examples:
Input : arr[] = {0, 1, 0, 1, 1}
Output : 5
The 5 pairs are (A[0], A[1]), (A[0], A[3]), (A[0], A[4]),
(A[2], A[3]) and (A[2], A[4]). Note that in all pairs
first element is 0, second element is 1 and index of
first element is smaller than index of second element.
Input : arr[] = {1, 0, 0, 0, 1}
Output : 3
Input : arr[] = {0, 0, 1, 0, 0}
Output : 2
A simple solution is to use two nested loops. The outer loop searches for a 0, the inner loop counts a number of 1s after 0. Finally, we return the total count.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int getPassingCars( int A[], int n)
{
int result = 0;
for ( int i=0; i<n-1; i++)
{
if (A[i] == 0)
{
for ( int j=i+1; j<n; j++)
if (A[j])
result++;
}
}
return result;
}
int main()
{
int A[] = {0, 1, 0, 1, 1};
int n = sizeof (A)/ sizeof (A[0]);
cout << getPassingCars(A, n);
return 0;
}
|
Java
class GFG
{
static int getPassingCars( int [] A, int n)
{
int result = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (A[i] == 0 )
{
for ( int j = i + 1 ; j < n; j++)
if (A[j] == 1 )
result++;
}
}
return result;
}
public static void main(String[] args)
{
int [] A = { 0 , 1 , 0 , 1 , 1 };
int n = A.length;
System.out.println(getPassingCars(A, n));
}
}
|
Python3
def getPassingCars(A, n):
result = 0
for i in range ( 0 , n - 1 , 1 ):
if (A[i] = = 0 ):
for j in range (i + 1 , n, 1 ):
if (A[j]):
result + = 1
return result
if __name__ = = '__main__' :
A = [ 0 , 1 , 0 , 1 , 1 ]
n = len (A)
print (getPassingCars(A, n))
|
C#
using System;
class GFG
{
static int getPassingCars( int [] A, int n)
{
int result = 0;
for ( int i = 0; i < n - 1; i++)
{
if (A[i] == 0)
{
for ( int j = i + 1; j < n; j++)
if (A[j] == 1)
result++;
}
}
return result;
}
public static void Main()
{
int [] A = {0, 1, 0, 1, 1};
int n = A.Length;
Console.WriteLine(getPassingCars(A, n));
}
}
|
PHP
<?php
function getPassingCars( $A , $n )
{
$result = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $A [ $i ] == 0)
{
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $A [ $j ])
$result ++;
}
}
return $result ;
}
$A = array (0, 1, 0, 1, 1);
$n = sizeof( $A );
echo getPassingCars( $A , $n );
?>
|
Javascript
<script>
function getPassingCars(A, n)
{
let result = 0;
for (let i = 0; i < n - 1; i++)
{
if (A[i] == 0)
{
for (let j = i + 1; j < n; j++)
if (A[j] == 1)
result++;
}
}
return result;
}
let A = [0, 1, 0, 1, 1];
let n = A.length;
document.write(getPassingCars(A, n));
</script>
|
Time Complexity : O(n2)
Auxiliary Space : O(1)
An efficient solution to traverse array from right, keep track of counts of 1s from the right. Whenever we see a 0, we increment the result by a count of 1s.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int getPassingCars( int A[], int n)
{
int countOne = 0, result = 0;
while (n >= 1)
{
if (A[n-1] == 1)
countOne++;
else
result += countOne;
n--;
}
return result;
}
int main()
{
int A[] = {0, 1, 0, 1, 1};
int n = sizeof (A)/ sizeof (A[0]);
cout << getPassingCars(A, n);
return 0;
}
|
Java
class GFG
{
static int getPassingCars( int A[], int n)
{
int countOne = 0 , result = 0 ;
while (n >= 1 )
{
if (A[n- 1 ] == 1 )
countOne++;
else
result += countOne;
n--;
}
return result;
}
public static void main(String[] args)
{
int A[] = { 0 , 1 , 0 , 1 , 1 };
int n = A.length;
System.out.println(getPassingCars(A, n));
}
}
|
Python3
def getPassingCars(A, n):
countOne = 0 ; result = 0
while n > = 1 :
if A[n - 1 ] = = 1 :
countOne + = 1
else :
result + = countOne
n - = 1
return result
A = [ 0 , 1 , 0 , 1 , 1 ]
n = len (A)
print (getPassingCars(A, n))
|
C#
using System;
class GFG
{
static int getPassingCars( int []A, int n)
{
int countOne = 0, result = 0;
while (n >= 1)
{
if (A[n - 1] == 1)
countOne++;
else
result += countOne;
n--;
}
return result;
}
public static void Main()
{
int []A = {0, 1, 0, 1, 1};
int n = A.Length;
Console.Write(getPassingCars(A, n));
}
}
|
PHP
<?php
function getPassingCars( $A , $n )
{
$countOne = 0; $result = 0;
while ( $n >= 1)
{
if ( $A [ $n -1] == 1)
$countOne ++;
else
$result += $countOne ;
$n --;
}
return $result ;
}
$A = array (0, 1, 0, 1, 1);
$n = sizeof( $A );
echo getPassingCars( $A , $n );
?>
|
Javascript
<script>
function getPassingCars(A, n)
{
let countOne = 0, result = 0;
while (n >= 1)
{
if (A[n - 1] == 1)
countOne++;
else
result += countOne;
n--;
}
return result;
}
let A = [0, 1, 0, 1, 1];
let n = A.length;
document.write(getPassingCars(A, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...