Count passing car pairs in C++


We are given an array of length N containing 0’s and 1’s only. The value 1 represents a car going towards west direction and value 0 represents a car going towards east direction.

We count passing cars as 1 if a pair of car A and car B is such that 0<=A<B<N. Here A is going towards east and B is going towards west. Ultimately we’ll count pairs of (0,1) where index of 0 is less than the index of 1.

Let us understand with examples

Input − arr[] = {1,0,1,0,1}

Output − Count of passing car pairs are: 3

Explanation − The pairs of (0,1) where index of 0 is less than 1 are − (arr[1],arr[2]), (arr[1],arr[4]), (arr[3],arr[4])

Input − arr[] = {1,0,0,0,0}

Output − Count of passing car pairs are: 0

Explanation − there is no pair of (0,1) where index of 0 is less than 1.

Approach used in the below program is as follows

We will use two approaches. First naive approach using two for loops. Start traversing the array, just when the 0 is encountered traverse array from that point to end again and increment count when 1 is encountered.

  • Take an array arr[] containing 0’s and 1’s.

  • Function count_cars(int arr[], int size) takes the array and length as input and return the count of passing cars.

  • Take the initial count as 0.

  • Traverse array from index i=0 to i<length-1.

  • If arr[i] is 0, traverse array again from index j=i+1 to j<length.

  • For each arr[j] as 1 increment count as pair (arr[i],arr[j]) is (0,1) and i<j.

  • At last we will get the total count.

  • Return count as result.

Efficient Approach

In this approach we will traverse the array from the end. Count all 1’s from the end. For each first 0 (from the end), no. of pairs will be count of 1’s that is temp.

  • Take an array arr[] containing 0’s and 1’s.

  • Function count_cars(int arr[], int size) takes the array and length as input and returns the count of passing cars.

  • Take the initial count as 0.

  • Traverse array from end using while loop till size>=1.

  • If arr[size-1] is 1, increment the variable temp for the number of 1’s found so far.

  • Else it is 0 (which has an index lower than all 1’s which is remp). Pairs will be temp. Set count = count+temp.

  • Decrement size for next element.

  • At last we will get the total count.

  • Return count as result.

Example (naive approach)

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int count_cars(int arr[], int size){
   int count = 0;
   for (int i=0; i<size-1; i++){
      if(arr[i] == 0){
         for (int j=i+1; j<size; j++)
         if (arr[j]==1){
            count++;
         }
      }
   }
   return count;
}
int main(){
   int arr[] = {1, 1, 0, 0, 1};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of passing car pairs are: "<<count_cars(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of passing car pairs are: 2

Example (Efficient Approach)

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int count_cars(int arr[], int size){
   int count = 0;
   int temp = 0;
   while (size >= 1){
      if (arr[size-1] == 1){
         temp++;
      }
      else{
         count = count + temp;
      }
      size--;
   }
   return count;
}
int main(){
   int arr[] = {1, 1, 0, 1, 1};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of passing car pairs are: "<<count_cars(arr, size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of passing car pairs are: 2

Updated on: 02-Dec-2020

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements