You are given two independent coding tasks.
Implement a function that computes the square root of a non-negative number without using any built-in square root or exponentiation functions from math libraries.
x
.
y
such that
y * y
approximates
x
within a specified error bound (for example, absolute error <
1e-6
).
+
,
-
,
*
,
/
) and control flow.
sqrt
,
pow
, etc.
x = 0
?
x
?
Define the function signature in a language of your choice, document the precision assumption you make (e.g., 1e-6), and implement the algorithm.
You are given an array of integers. You must rearrange its elements in-place (i.e., using O(1) extra space) so that all numbers at even positions (using 1-based indexing) are strictly smaller than all numbers at odd positions.
arr
of length
n
.
2, 4, 6, ...
) is strictly less than every element at an odd position (indices
1, 3, 5, ...
).
[1, 2, 3, 4, 5]
[5, 1, 4, 2, 3]
[5, 4, 3]
[1, 2]
1, 2
) is less than every odd-position value (
3, 4, 5
).
Describe your algorithm, its time and space complexity, then implement it.