I have an array of size n and I want to rotate it by d places. Can anyone help me with a good approach?

Eg:

User Input:

```N= 5, T= 3
1,2,3,4,5```

Output:

`4,5,1,2,3`
Mar 3, 2019 in Java 556 views

## 1 answer to this question.

Here is what I came up with,

```import java.util.*;

class Rotate
{
int transitionPoint(int arr[],int n)
{
int low =0,high =n-1;
int mid = 0;
while(low<=high)
{
mid = low+high/2;
if(arr[mid]==0)
{
if(arr[mid+1]==1)
return (mid+1);
low=mid+1;
}
if(arr[mid]==1)
{
if(arr[mid-1]==0)
return (mid+1);
high = mid-1;
}
}
return mid;
}
}

class TransitionPoint
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
Rotate obj = new Rotate();
System.out.println(obj.transitionPoint(arr,n));
}
}
}```

Hope this will help.

• 58,090 points

+1 vote

## Setting selected item of Spinner by value rather than by position

Suppose your Spinner is named newSpinner, and it contains ...READ MORE

## How to filter an array from all elements of another array

This is what I would do: var arr1 = [1,2,3,4], ...READ MORE

## How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

## Array of Objects

You can also do : A[] a = ...READ MORE

+1 vote

## Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

+1 vote

## Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

+1 vote

## What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

## What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE