Codechef Problems: Missing Shoes (SHOM)

 Problem Statement: 

Solution: 

Given, 
    L : Number of Left Shoes 
    R : Number of Right Shoes 

To find: Minimum number of shoes Chef is missing 

Problem background: A Shoe pair has 1 left shoe and 1 right shoe, Here we assume that we will not count the number of shoes which were missing in pairs (i.e., both left and right shoes are missing) because we don't have enough data how much shoes does the chef already had. Due to this assumption will be not be able to find the number of pairs that are missing but it will be irreverent for our problem because our problem states to find minimum number of  missing shoes 

In Accordance to above assumption we can have three condition (possibilities) 

Case 1 : L equals to Y 
If L is equals to Y it means no shoe has been missing or a whole pair is missing but we need to find minimum number of missing shoes so answer will be '0' 

Case 2: L greater than Y 
If L is greater than Y it means there are more left shoes than right shoes, to find the minimum number of missing shoes we will only consider missing individual shoe and ignore the missing pairs so answer will be "L-Y" 

Case 3: Y greater than L 
If Y is greater than L it means there are more right shoes than left shoes, to find the minimum number of missing shoes we will only consider missing individual shoes and ignore the missing pairs so answer will be "Y-L"

Code Solution: 

#include <bits/stdc++.h>

using namespace std;

int main() {
    int l, r;
    cin >> l >> r;

    if (l == r) {
        cout << 0 << endl;
    } else if (l < r) {
        cout << r - l << endl;
    } else {
        cout << l - r << endl;
    }

    return 0;
}

Comments