Please clear these doubts related to Quiz-2

  1. list< int > myList = { 2, 6, 12, 13, 15, 18, 20};

cout << binary_search(myList.begin(), myList.end(), 20) ;

Why is its output 1 and time complexity linear? I think it should be 20 and logarithmic.

  1. vector< int > data = {100, 142, 138, 96, 32, 149};

swap(data[2], data[5]);

int val1 = *max_element(data.begin(), data.begin() +3);

int val2 = *max_element(data.begin()+3,data.end());

cout<< min(val1, val2);

Why is output 138 and not 142?

  1. string s = “bca”;

do {

    cout << s << ' ';

} 

while(next_permutation(s.begin(), s.end()));

cout << s;

What is the output of the given code? I think it should be never ending loop.

After swapping [100,142,149,96,32,138]
val1=max([100,142,149])=149
val2=max([96,32,138])=138
ans is min(138,149)=138

the answer will be 1(it’ll return bool value) with o(n) complexity only as it’s a linked list so it will take o(n) time to iterate over elements.

Answer should be bca cab cba cba.
Initially you were having bca
next permutation of it is cab
next permutation of it is cba
Then it come out of the loop and again prints cba.
It stops at cba because there is no greater permutation than this

It means that next_permutation(); works only if the permutation will result in greater value than initial else will not work??

Yup…