Calculate the total fine to be collected - GeeksforGeeks

31 Mar, 2021
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.
 
Approach:  
Start traversing the given array.Check if the current car number and date doesn’t match i.e. one is even and other is odd or vice-versa.If not matched charge the fine on that car number. Else, not.Print the total fine.
Start traversing the given array.
Check if the current car number and date doesn’t match i.e. one is even and other is odd or vice-versa.
If not matched charge the fine on that car number. Else, not.
Print the total fine.
Below is the implementation of above approach:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ implementation to calculate// the total fine collected#include <bits/stdc++.h>using namespace std; // function to calculate the total fine collectedint 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 aboveint main(){    int car_num[] = { 3, 4, 1, 2 };    int n = sizeof(car_num) / sizeof(car_num[0]);    int date = 15, fine = 250;     cout << totFine(car_num, n, date, fine);     return 0;}
// Java implementation to calculate// the total fine collectedclass GFG{ // function to calculate// the total fine collectedstatic int totFine(int car_num[], int n,                   int date, int fine){int tot_fine = 0; // traverse the array elementsfor (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 finereturn tot_fine;} // Driver Codepublic 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 program to calculate# the total fine collected # function to calculate the total fine collecteddef 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 Programif __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# implementation to calculate// the total fine collectedusing System; class GFG{ // function to calculate the// total fine collectedstatic int totFine(int[] car_num, int n,                   int date, int fine){int tot_fine = 0; // traverse the array elementsfor (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 finereturn tot_fine;} // Driver Codepublic 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// PHP implementation to calculate// the total fine collected // function to calculate the// total fine collectedfunction totFine(&$car_num, $n,                  $date, $fine){    $tot_fine = 0;     // traverse the array elements    for ($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$car_num = array(3, 4, 1, 2 );$n = sizeof($car_num);$date = 15;$fine = 250; echo totFine($car_num, $n,             $date, $fine); // This code is contributed// by ChitraNayal?>
<script> // Javascript implementation to calculate// the total fine collected // function to calculate the total fine collectedfunction totFine(car_num, n, date, fine){    let tot_fine = 0;     // traverse the array elements    for (let 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     let car_num = [ 3, 4, 1, 2 ];    let n = car_num.length;    let date = 15, fine = 250;     document.write(totFine(car_num, n, date, fine)); //This code is contributed by Mayank Tyagi</script>
500
 
Source:https://www.geeksforgeeks.org/microsoft-interview-experience-for-internship/  
ankthon
ukasp
mayanktyagi1709
BIT
Microsoft
Arrays
Bit Magic
Microsoft
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Move all negative numbers to beginning and positive to end with constant extra space
Program to find sum of elements in a given array
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
Cyclic Redundancy Check and Modulo-2 Division