Skip to content

Commit ade0208

Browse files
committed
Migrate development environment from Windows to macOS and continue project updates
- Shifted primary development environment from Windows to macOS to ensure better performance, stability, and streamlined workflow for Java development. - Removed OS-specific metadata and ensured platform-agnostic project compatibility. - Normalized line endings to LF for cross-platform consistency. - Verified and adjusted file paths, permissions, and encoding settings to suit macOS environment. - Cleaned up unnecessary Windows-generated files and updated Git configuration accordingly. - Future commits and development will now be managed entirely from macOS. This migration ensures a cleaner, more consistent setup moving forward and prepares the project for scalable development across Unix-based systems. Future development will continue on macOS for a smoother, Unix-based experience. Signed-off-by: Someshdiwan <Someshdiwan369@gmail.com>
1 parent 0a11259 commit ade0208

10 files changed

Lines changed: 76 additions & 0 deletions
34.2 KB
Loading
41.5 KB
Loading
252 Bytes
Binary file not shown.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
To reverse an array in place means that we need to reverse the elements without using any additional array or extra space.
2+
We can do this by swapping the elements starting from the two ends of the array, working towards the middle.
3+
4+
Approach:
5+
6+
1. Start two pointers:
7+
- One at the beginning (left = 0).
8+
- One at the end (right = n - 1).
9+
2. Swap the elements at the left and right pointers.
10+
3. Move the left pointer to the right and the right pointer to the left.
11+
4. Continue this process until left is greater than or equal to right.
12+
13+
Algorithm:
14+
15+
1. Initialize two pointers left = 0 and right = n - 1.
16+
2. Swap arr[left] and arr[right].
17+
3. Increment left and decrement right.
18+
4. Repeat the process until left >= right.
19+
20+
Code Implementation in Java:
21+
22+
import java.util.Arrays;
23+
24+
class Solution {
25+
public static void reverseArray(int[] arr, int n) {
26+
int left = 0;
27+
int right = n - 1;
28+
29+
// Swap elements while left is less than right
30+
while (left < right) {
31+
// Swap arr[left] and arr[right]
32+
int temp = arr[left];
33+
arr[left] = arr[right];
34+
arr[right] = temp;
35+
36+
// Move pointers
37+
left++;
38+
right--;
39+
}
40+
}
41+
42+
public static void main(String[] args) {
43+
int[] arr = {1, 2, 3, 4, 5}; // Example input
44+
int n = arr.length;
45+
46+
// Function call to reverse the array
47+
reverseArray(arr, n);
48+
49+
// Output the reversed array
50+
System.out.println(Arrays.toString(arr)); // [5, 4, 3, 2, 1]
51+
}
52+
}
53+
54+
Explanation:
55+
- left starts at index 0 (the first element) and right starts at index n - 1 (the last element).
56+
- Inside the while loop, we swap the elements at arr[left] and arr[right].
57+
- After swapping, we increment left and decrement right, moving the pointers towards the center of the array.
58+
- The loop continues until left >= right.
59+
60+
Example:
61+
62+
For input:
63+
gi
64+
arr = [1, 2, 3, 4, 5]
65+
66+
- First iteration: left = 0, right = 4 → Swap arr[0] with arr[4] → [5, 2, 3, 4, 1]
67+
- Second iteration: left = 1, right = 3 → Swap arr[1] with arr[3] → [5, 4, 3, 2, 1]
68+
- Third iteration: left = 2`, right = 2 → The loop stops because left is not less than right.
69+
70+
The reversed array is [5, 4, 3, 2, 1].
71+
72+
Time Complexity:
73+
- O(n) where n is the number of elements in the array, because we are swapping each element once.
74+
75+
Space Complexity:
76+
- O(1), as we are not using any extra space; the array is reversed in place.
1.08 KB
Binary file not shown.
1.35 KB
Binary file not shown.
1.54 KB
Binary file not shown.
717 Bytes
Binary file not shown.
1.3 KB
Binary file not shown.
1.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)