# Calculate the total fine to be collected

Given a date and an array of integer containing the numbers of the cars traveling on that date(an integer), the task is to calculate the total fine collected based on the following rules:

• Odd numbered cars can travel on only odd dates.
• Even numbered cars on only even dates.
• Otherwise a car would be fined 250 Rs.

Examples:

```Input: car_num[] = {3, 4, 1, 2}, date = 15
Output: 500
Car with numbers '4' and '2' will be fined
250 each.

Input: car_num[] = {1, 2, 3} , date = 16
Output: 500
Car with numbers '1' and '3' will be fined
250 each.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Start traversing the given array.
2. Check if the current car number and date doesn’t match i.e. one is even and other is odd or vice-versa.
3. If not matched charge the fine on that car number. Else, not.
4. Print the total fine.

Below is the implementation of above approach:

## C++

 `// C++ implementation to calculate ` `// the total fine collected ` `#include ` `using` `namespace` `std; ` ` `  `// function to calculate the total fine collected ` `int` `totFine(``int` `car_num[], ``int` `n, ``int` `date, ``int` `fine) ` `{ ` `    ``int` `tot_fine = 0; ` ` `  `    ``// traverse the array elements ` `    ``for` `(``int` `i = 0; i < n; i++) ` ` `  `        ``// if both car no and date are odd or ` `        ``// both are even, then statement ` `        ``// evaluates to true ` `        ``if` `(((car_num[i] ^ date) & 1) == 1) ` `            ``tot_fine += fine; ` ` `  `    ``// required total fine ` `    ``return` `tot_fine; ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `car_num[] = { 3, 4, 1, 2 }; ` `    ``int` `n = ``sizeof``(car_num) / ``sizeof``(car_num); ` `    ``int` `date = 15, fine = 250; ` ` `  `    ``cout << totFine(car_num, n, date, fine); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to calculate ` `// the total fine collected ` `class` `GFG  ` `{ ` ` `  `// function to calculate ` `// the total fine collected ` `static` `int` `totFine(``int` `car_num[], ``int` `n, ` `                   ``int` `date, ``int` `fine) ` `{ ` `int` `tot_fine = ``0``; ` ` `  `// traverse the array elements ` `for` `(``int` `i = ``0``; i < n; i++) ` ` `  `    ``// if both car no and date  ` `    ``// are odd or both are even,  ` `    ``// then statement evaluates to true ` `    ``if` `(((car_num[i] ^ date) & ``1``) == ``1``) ` `        ``tot_fine += fine; ` ` `  `// required total fine ` `return` `tot_fine; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `car_num[] = { ``3``, ``4``, ``1``, ``2` `}; ` `    ``int` `n = car_num.length; ` `    ``int` `date = ``15``, fine = ``250``; ` ` `  `    ``System.out.println(totFine(car_num, n,  ` `                               ``date, fine)); ` `} ` `} ` ` `  `// This code is contributed ` `// by ChitraNayal `

## Python 3

 `# Python 3 program to calculate  ` `# the total fine collected  ` ` `  `# function to calculate the total fine collected  ` `def` `totFine(car_num, n, date, fine) : ` ` `  `    ``tot_fine ``=` `0` ` `  `    ``# traverse the array elements  ` `    ``for` `i ``in` `range``(n) : ` `         `  `        ``# if both car no and date are odd or  ` `        ``# both are even, then statement  ` `        ``# evaluates to true  ` `        ``if` `(((car_num[i] ^ date) & ``1``) ``=``=` `1` `): ` `            ``tot_fine ``+``=` `fine ` ` `  `    ``# required total fine  ` `    ``return` `tot_fine ` ` `  `# Driver Program ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``car_num ``=` `[ ``3``, ``4``, ``1``, ``2` `] ` `    ``n ``=` `len``(car_num) ` `    ``date, fine ``=` `15``, ``250` ` `  `    ``# function calling ` `    ``print``(totFine(car_num, n, date, fine)) ` `             `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# implementation to calculate ` `// the total fine collected ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// function to calculate the ` `// total fine collected ` `static` `int` `totFine(``int``[] car_num, ``int` `n, ` `                   ``int` `date, ``int` `fine) ` `{ ` `int` `tot_fine = 0; ` ` `  `// traverse the array elements ` `for` `(``int` `i = 0; i < n; i++) ` ` `  `    ``// if both car no and date  ` `    ``// are odd or both are even,  ` `    ``// then statement evaluates to true ` `    ``if` `(((car_num[i] ^ date) & 1) == 1) ` `        ``tot_fine += fine; ` ` `  `// required total fine ` `return` `tot_fine; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int``[] car_num = { 3, 4, 1, 2 }; ` `    ``int` `n = car_num.Length; ` `    ``int` `date = 15, fine = 250; ` ` `  `    ``Console.Write(totFine(car_num, n,  ` `                          ``date, fine)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by ChitraNayal `

## PHP

 ` `

Output:

```500
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01, chitranayal

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.