Jul 14, 2024
int array[6]
or new int[6]
10^6
10^7
x
x
, x+1
, x+2
, etc.O(n log n)
O(n)
largest = array[0]
for i in range(1, n):
if array[i] > largest:
largest = array[i]
print(largest)
O(n log n)
O(2n)
largest = array[0]
for i in range(1, n):
if array[i] > largest:
largest = array[i]
second_largest = -1
for i in range(1, n):
if array[i] != largest and array[i] > second_largest:
second_largest = array[i]
print(second_largest)
O(n)
largest = array[0]
second_largest = -1
for i in range(1, n):
if array[i] > largest:
second_largest = largest
largest = array[i]
elif array[i] > second_largest and array[i] != largest:
second_largest = array[i]
print(second_largest)
O(n)
for i in range(1, n):
if array[i] < array[i - 1]:
return False
return True
O(n log n)
O(n)
O(n)
i = 0
for j in range(1, n):
if array[j] != array[i]:
i += 1
array[i] = array[j]
return i + 1