Rotate elements of array by n position

0 votes

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 in Java by Nikita
25 views

1 answer to this question.

0 votes

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.

answered Mar 3 by Priyaj
• 56,200 points

Related Questions In Java

0 votes
1 answer

Setting selected item of Spinner by value rather than by position

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

answered Aug 10, 2018 in Java by 93.lynn
• 1,560 points
2,624 views
0 votes
3 answers

How can I add new elements to an Array in Java

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

answered Sep 19, 2018 in Java by Sushmita
• 6,850 points
94 views
0 votes
2 answers

Array of Objects

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

answered Aug 3, 2018 in Java by sharth
• 3,320 points
32 views
0 votes
1 answer

In which class is the "length" property of array defined ?

It's defined in the Java language specification: The members ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,540 points
35 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

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

answered May 10, 2018 in Java by Rishabh
• 3,540 points
51 views
0 votes
1 answer

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

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
42 views
+1 vote
3 answers

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

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

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
250 views
0 votes
2 answers

What is the syntax to initialize an array?

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

answered Aug 2, 2018 in Java by samarth295
• 2,190 points
44 views
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 56,200 points
102 views
0 votes
1 answer

How to rotate an array from a particular index?

This is the code for the above ...READ MORE

answered Mar 4 in Java by Priyaj
• 56,200 points
18 views