The following block of code is from a function that finds the minimum number of coins required to achieve a certain amount given by the user. Here two queue "sums" and "costs" are used.
while(Sums.front()<=TargetSum){
int tempSum = Sums.front();
Sums.pop();
int tempCost = Costs.front();
Costs.pop();
for(int i=0;i<TypesOfCoins;i++)
{
Sums.push(coins[i]+tempSum);
Costs.push(tempCost+1);
if(Sums.back()==TargetSum)
{
cout<<"Sums:"; DisplayQueue(Sums);
cout<<"Cost:"; DisplayQueue(Costs);
return Costs.back();
}
}
}
As far as I know, for nested loops times complexity is the numbers of times innermost loop iterates, so time complexity for this loop should be O(n^2), shouldn't it?
TypesOfCoins(inner loop range) is a constant (does not depend on any input that is being processed), then the complexity is NOT n^2. And complexity of a code is NOT how many times it iterates.