-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversesubstring.java
More file actions
33 lines (27 loc) · 940 Bytes
/
Reversesubstring.java
File metadata and controls
33 lines (27 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Reversesubstring{
public static void reverseSubstring(int[] arr, int start, int end) {
int n = arr.length;
if (start < 0 || end >= n || start >= end) {
System.out.println("Invalid input or empty substring.");
return;
}
while (start < end) {
// Swap elements without using a temporary variable
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
public static void main(String[] args) {
int arr[] = { 1, 2, 5,6,7,8};
int start = 2; // Start index of the substring
int end = 5; // End index of the substring
reverseSubstring(arr, start, end);
// Print the modified array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}