How to comprehend 'if and only if'?


  • 1
    H

    does it mean that there is only one pair of indices that matches the condition?


  • 1

    No. It says "only if", not something like "if only one".

    "Return true if and only if there is such a pair" means you must return true if there is such a pair and you must return true only if there is such a pair. The latter part, the one you misunderstand, means you must not return true if there is no such pair. Without that, you could just write:

    class Solution:
        def containsNearbyDuplicate(self, nums, k):
            return True
    

    It's a standard term, there's even a wikipedia article about it.

    In other words, instead of "return true if and only if there is such a pair" the problem could say "find out whether there is such a pair". That means the same thing (in the context of having to return a boolean) but might be easier to understand for people not used to "if and only if".


  • 0

    Thanks to your suggestion @pochmann! I have changed the problem description so it is easier to understand.


  • 0
    H

    wow that is really concise thx!!


  • 0
    H

    ok.. now it's easy to read


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.