def relgq_recode(relship: int, qgqtyp: str, final_pop: int) -> int:
    """
    This function returns the recoded value for relgq based on the values of relship, gqgtyp, and final_pop.

    :param relship: The value of the relship variable
    :param qgqtyp: The value of the qgqtyp variable
    :param final_pop: The value of the final_pop variable

    :return: An int representing the recoded value of relgq based on the relship, qgqtyp, and final_pop
    """
    assert (
        relship >= 20 and relship <= 38
    ), f"Only values between 20 and 38 are valid for relship, received: {relship}"

    qgqtyp_mapping = {
        "101": 18,
        "102": 19,
        "103": 20,
        "104": 21,
        "105": 22,
        "106": 23,
        "201": 24,
        "202": 25,
        "203": 26,
        "301": 27,
        "401": 28,
        "402": 29,
        "403": 30,
        "404": 31,
        "405": 32,
        "501": 33,
        "601": 34,
        "602": 35,
        "701": 36,
        "801": 37,
        "802": 38,
        "900": 39,
        "901": 40,
        # Other noninstitutional. Does 997 exist?
        "702": 41,
        "704": 41,
        "706": 41,
        "903": 41,
        "904": 41,
    }

    assert (
        qgqtyp == "   " or qgqtyp in qgqtyp_mapping.keys()
    ), f"Invalid CEF QGQTYPE value found: {qgqtyp}"

    relgq = -1

    relship_mapping = {
        21: 2,
        22: 3,
        23: 4,
        24: 5,
        25: 6,
        26: 7,
        27: 8,
        28: 9,
        29: 10,
        30: 11,
        31: 12,
        32: 13,
        33: 14,
        34: 15,
        35: 16,
        36: 17,
    }

    """
    0: row.RELSHIP == 20 && (JoinWithUnit.FINAL_POP == 1)
    1: row.RELSHIP == 20 && (JoinWithUnit.FINAL_POP > 1)
    """
    if relship == 20:
        assert (
            final_pop >= 0
        ), f"relship is 20 (householder), final_pop variable should exist and be greater than or equal to 1, but is {final_pop}"
        if final_pop == 1:
            relgq = 0
        elif final_pop > 1:
            relgq = 1
    elif relship in relship_mapping:
        relgq = relship_mapping[relship]
    elif qgqtyp in qgqtyp_mapping:
        relgq = qgqtyp_mapping[qgqtyp]

    assert (
        relgq > -1
    ), f"Relgq could not be recoded, for relship: {relship}, qgqtyp: {qgqtyp}, final_pop: {final_pop}"

    return relgq