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, 2019 in Java by Nikita
673 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, 2019 by Priyaj
• 58,020 points

Related Questions In Java

+1 vote
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,600 points
21,734 views
0 votes
1 answer

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

answered Nov 7, 2022 in Java by Damonlang
• 700 points
4,228 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,920 points
13,320 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,370 points
856 views
+1 vote
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,620 points
1,347 views
+1 vote
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,290 points
1,210 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,220 points
3,623 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,220 points
1,048 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
• 58,020 points
1,261 views
0 votes
3 answers

How to rotate an array from a particular index?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Java by oisoucknfn
2,149 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP