Or possibly it wasn’t a treasure, just some object, but if you could get it without walking on the carpet, you’d become the next ruler or something like that?
The way to do it is to roll the carpet up from the edge until you could reach the object.
It’s not in the Motif-Index of Folk Literature (at least, not under ‘carpet’ or ‘treasure’). There’s a more specific index to the Arabian Nights which I don’t have; maybe in there?
Was it an apple that the ruler challenged folks to pick up — and none of them figured it out, and so the ruler himself showed them the trick? Sultan Mehmed?
Maybe you could just pay a kid (the equiv. of) $5 to fetch it for you. That ruler should come up with some challenges that don’t have a million ways to defeat them.