Refresh a User's Group Badge
Context
A social app stores a user's group memberships in one integer badge. Each set binary position represents membership in a particular group. To refresh a badge, toggle the memberships at the lowest and highest occupied positions, leaving all positions between them unchanged.
Problem
Given a nonnegative integer representing one user's group-membership badge, toggle exactly two positions: the lowest set-bit position and the highest set-bit position. Use exclusive combination so every other position remains unchanged. If the badge is zero, no group position exists, so return zero. If only one position is set, it is both endpoints; toggle it once, producing a badge with that position cleared.
Examples
badge = 00badge = 80badge = 428Constraints
0 <= badge <= 1000000000000000000- Types:
badgeis int; result is int
Function signature
def refresh_group_badge(badge: int) -> int
badgeint- A nonnegative integer whose set positions represent the user's group memberships.
- returns int
- The badge after toggling its lowest and highest set positions once, or zero when the input has no set positions.
Adapted from MBPP problem task_545 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- Only nonnegative badges are accepted; the binary representation has no signed extension positions.
- The endpoint positions are counted from the least-significant position as position zero.