Ayy (أي) asks which member of a shared group is intended. Kam (كم) asks about number or quantity. One expects a selected item in reply; the other expects a numerical measure.
«أَيُّ الْفَرِيقَيْنِ خَيْرٌ مَقَامًا»
Which of the two groups is better in standing?
«كَمْ آتَيْنَاهُمْ مِنْ آيَةٍ بَيِّنَةٍ»
How many clear signs did We give them?
The first expression sets competing groups side by side; the second draws attention to quantity. Ayy is not confined to comparisons of exactly two individuals: it selects a member within a contextually defined set.