So do you wear pants, or do you only wear a pair of pants? I wear pants. I bought some pants the other day. When my friend Tim wore some shorts that were a little too tight, everyone told him to "put some pants on!" These are just some examples I have offhand that I hope make clear that there's no requirement for pair of.
I will concede that in the particular sentence above, though, I would probably say "How many pairs of pants do you need?"
The "necesitas" tells us that it's the familiar you (tú) form. For the first person singular form (I), it would be "necesito". In the present tense, the endings are distinct.
I'm giving a condensed form here that mostly avoids differences in -ar, -er, and -ir verbs: First person singular (I) always ends in "o", first person plural (we) always ends in "mos", second person familiar singular (tú) always ends in "s", third person singular (él, ella, usted) ends in "a" for -ar verbs and "e" for -er and -ir verbs, and third person plural (ellos, ellas, ustedes) always ends in "n".
Here's a more complete set for regular verbs of all three types: https://www.wikihow.com/Sample/Present-Tense-Conjugation-of-Regular-Spanish-Verbs
The only things that change depending on gender are adjectives, and that's why, as you say, it's "cuantos pantalones".
But necesitar is the verb "to need", and verbs don't ever change according to what is masculine or feminine. What determines the form a verb takes is the person (or thing) it's associated with. In this sentence, it's "...do you need", so you need the verb form for you.
Duolingo's sentence uses the singular familiar "you": tú, and the correct verb form is "[tú] necesitas". Including the pronoun tú is optional in Spanish.
The English doesn't tell us whether the "you" is formal or familiar, or singular or plural, so any Spanish "you" could be used. Duolingo just happened to choose tú.
You can always look up all the verb conjugations for any verb. For example, for necesitar, see https://www.spanishdict.com/conjugate/necesitar.