Reverse an array upto a given position

Sanjay Singh
May 21, 2022

--

1 2 3 4 5 6 7 8 9
o/p
4 3 2 1 5 6 7 8 9

public class Application {
public static void main(String[] args) {
Integer[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int length = x.length;
int pos = 4;
int temp;
for (int i = 0; i < length — 1; i++)
System.out.print(x[i] + “ “);
for (int i = 0; i < pos / 2; i++) {
temp = x[i];
x[i] = x[pos — i — 1];
x[pos — i — 1] = temp;}

System.out.println();
for (int i = 0; i < length — 1; i++)
System.out.print(x[i] + “ “);}}

O/p
1 2 3 4 5 6 7 8 9
4 3 2 1 5 6 7 8 9

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

No responses yet