Senior
Kirjutage algoritmise, kuidas pöörata massiivi (esimene element saab viimaseks, viimane esimeseks jne.) ilma sisseehitatud funktsioone kasutamata.
sobes.tech AI
Vastus AI-lt
To reverse an array without using built-in functions, you can implement an algorithm that swaps elements from both ends of the array towards the center. This is a classic method that operates in O(n/2) operations.
Example in Python:
arr = [1, 2, 3, 4, 5]
left = 0
right = len(arr) - 1
while left < right:
# swap elements
temp = arr[left]
arr[left] = arr[right]
arr[right] = temp
left += 1
right -= 1
print(arr) # [5, 4, 3, 2, 1]
This approach is universal and easily transferable to other programming languages.