Skip to content

Instantly share code, notes, and snippets.

@timxor
Last active November 23, 2025 08:25
Show Gist options
  • Select an option

  • Save timxor/053e04a5e0a7130d42c286e6d3555e31 to your computer and use it in GitHub Desktop.

Select an option

Save timxor/053e04a5e0a7130d42c286e6d3555e31 to your computer and use it in GitHub Desktop.
Processes left-to-right and works safely only when ranges don't overlap.
// Forward Copy (left-to-right)
// Suitable when source and destination ranges do NOT overlap,
// or when destination is entirely to the left of source
public static void forwardCopy(int[] arr, int srcStart, int destStart, int length) {
for (int i = 0; i < length; i++) {
arr[destStart + i] = arr[srcStart + i];
}
}
// Example: Copy elements without overlap
// Original: [1, 2, 3, 4, 5]
// Copy indices 1-3 to indices 3-5
// Result: [1, 2, 3, 2, 3, 4] (indices 3-5 now contain [2, 3, 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment