0% found this document useful (0 votes)
14 views3 pages

New Text Document

The document contains code snippets for reversing, left rotating, and right rotating arrays in Java. The reverse array code uses a swap approach to reverse the elements between the start and end indices of an input array. The left rotation code creates a temporary array, copies a slice of elements from the input array, and overwrites the original array. The right rotation code works similarly but slices elements from the end of the input array instead of the beginning.

Uploaded by

akpolineni143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

New Text Document

The document contains code snippets for reversing, left rotating, and right rotating arrays in Java. The reverse array code uses a swap approach to reverse the elements between the start and end indices of an input array. The left rotation code creates a temporary array, copies a slice of elements from the input array, and overwrites the original array. The right rotation code works similarly but slices elements from the end of the input array instead of the beginning.

Uploaded by

akpolineni143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

reverse array

public class GFG {

static void rvereseArray(int arr[],


int start, int end)
{
int temp;

while (start < end)


{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

static void printArray(int arr[],


int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");

System.out.println();
}

public static void main(String args[]) {

int arr[] = {1, 2, 3, 4, 5, 6};


printArray(arr, 6);
rvereseArray(arr, 0, 5);
System.out.print("Reversed array is \n");
printArray(arr, 6);

}
}

// Recursive Java Program to reverse an array


import java.io.*;

class ReverseArray {

/* Function to reverse arr[] from start to end*/


static void rvereseArray(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseArray(arr, start+1, end-1);
}

/* Utility that prints out an array on a line */


static void printArray(int arr[], int size)
{
for (int i=0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}

/*Driver function to check for above functions*/


public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
System.out.println("Reversed array is ");
printArray(arr, 6);
}
}

Left rotation by d positions

import java.io.*;
public class Main
{
static void rotateArray(int arr[],int d,int n){
int temp[] = new int[n];
int k=0;
for(int i=d;i<n;i++){
temp[k] = arr[i];
k++;
}
for(int i=0;i<d;i++){
temp[k]=arr[i];
k++;
}
for(int i=0;i<n;i++ ){
arr[i]=temp[i];

static void printArray(int arr[],int size){


for (int i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
}

public static void main(String[] args) {


int arr[]={1,2,3,4,5,6};
int D=2;
int N= arr.length;
rotateArray(arr,D,N);
printArray(arr,N);
}
}

right rotations

import java.io.*;
public class Main
{
static void rotateArray(int arr[],int d,int n){
int temp[] = new int[n];
int k=0;
int p=n-d;
for(int i=p;i<n;i++){
temp[k] = arr[i];
k++;
}
for(int i=0;i<p;i++){
temp[k]=arr[i];
k++;
}
for(int i=0;i<n;i++ ){
arr[i]=temp[i];

static void printArray(int arr[],int size){


for (int i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
}

public static void main(String[] args) {


int arr[]={1,2,3,4,5,6};
int D=2;
int N= arr.length;
rotateArray(arr,D,N);
printArray(arr,N);
}
}

You might also like