Algorithms STL quiz ques -2

sir how is the option 2 is correct . I think compleity of binary_search is log(n).

@Jun18APP0112 please share the question along with the options.

Q2. Algorithms STL#2 Choose the correct output and time complexity for the following code : list< int > myList = { 2, 6, 12, 13, 15, 18, 20}; cout << binary_search(myList.begin(), myList.end(), 20) ; Output is 20 and time complexity is Linear in size of the list. Output is 1 and time complexity is Linear in size of the list. Output is 20 and time complexity is Logarithmic in size of the list. Output is 1 and time complexity is Logarithmic in size of the list.

https://online.codingblocks.com/app/player/36594/content/53656/7190/quiz/146/s/38138?code=BkdYAQoVKiG9GJFLLpig2yc1kYhMIL8b&q=2

Study the following code snippet: 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); Will the above code compile? If yes then what is the output? swap is not a defined function so an error is thrown. output is 142, no error is thrown. max_element is not a defined function so an error is thrown. output is 138, no error is thrown

in ques -8 also i dont understand how 138 is right answer

@Jun18APP0112 you cannot perform binary search on a singly linked list as you cannot go back and forth, you can only move in one direction, hence linear search will be performed which has o(n) time complexity

@Jun18APP0112 in ques 8, first data[2] and data[5] are swapped, so vector becomes
{100, 142, 149, 96, 32, 138}
*max_element(data.begin(), data.begin() +3) gives the maximum element in the range [0, 3) ie, 0 is inclusive, 3 is exclusive. Max element in data[0]… data[2] is 149, so val1 = 149
*max_element(data.begin()+3,data.end()); gives maximum element in range [3, 6) so val2 becomes 138
min(149, 138) is 138, hence answer is 138.

thankyou mam…