[Solved] Collisions of Events with Java, C++

Collisions of Events: Lucarnos Film Festival is an annual film festival and is also known for being a prestigious platform for art house films. This year at the Lucarnos Film festival there are many movies to be screened, each of different genre ranging from drama movies to comedy ones and teen movies to horror ones. The festival is a long-running event this time as the organizers are planning to screen only one movie per day. The organizers have populated their schedule in the form of a matrix where ‘i’ is the movie number and ‘j’ is the day number. Eij is the movie preference dates.

You are given a matrix E of N rows and M columns where Eij is 1 if the i-th movie is to be screened on j-th day, otherwise it will be 0. Note that it is not necessary that if a movie x will be screened on day y, then day y should screen only movie x.

You know that if there are two different movies x and y, which are to be screened on the same day z, and then there will be a collision. Can you calculate the number of different collisions at this movie festival? Note that order of movies in the collision doesn’t matter.

Input Format:
The first line of the input contains two space separated integers NM denoting the number of movies and days, respectively.
Each of the following N lines contain M characters, each of them is either ‘0’ or ‘1’.

Output Format:
Output a single line containing an integer corresponding to the number of collisions at the film festival.
Refer sample input and output for formatting specifications.

Sample Input 1:
4 3
111
100
110
000

Sample Output 1:
4

Sample Input 2:
2 2
10
01

Sample Output 2:
0

Solution

import java.io.*;
import java.util.*;
import java.lang.*;

class Main{
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] e = new int[n][m];
        int collisions = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                e[i][j] = sc.nextInt();
            }
        }
//sauravhathi
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(e[i][j] == 1){
                    for(int k = 0; k < n; k++){
                        if(e[k][j] == 1){
                            if(i != k){
                                collisions++;
                            }
                        }
                    }
                }
            }
        }
//sauravhathi
        System.out.println(collisions/2);
    }
}
#include <iostream>
using namespace std;

int main()
{
    int N, M, i, j, k, collision = 0;
    cin >> N >> M;
    int Eij[N][M];
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        {
            cin >> Eij[i][j];
        }
    }
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M - 1; j++)
        {

//sauravhathi
            if (Eij[i][j] == 1)
            {
                for (k = 0; k < M - j - 1; k++)
                {
                    if (Eij[i][j] == Eij[i][j + k + 1])
                    {
                        collision = collision + 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
    }
    cout << collision;
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *