Question

Write a JAVA program that rotates a 1-dimensional by 2 elements. For example [1,2,3,4,5,6] rotated by...

Write a JAVA program that rotates a 1-dimensional by 2 elements. For example [1,2,3,4,5,6] rotated by two becomes [3,4,5,6,1,2].

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File Name:

LeftRotatoin.java

Program:

import java.util.Scanner;
class LeftRotation
{
   public static void main(String[] args) {

       // Declaring a Scanner variable for taking input
       Scanner in = new Scanner(System.in);
       // Taking the no.of elements of the array and storing
       // it in variable n.
       System.out.println("Enter no.of values");
       int n=in.nextInt();

       // Taking the elements of the Array and storing it in the
       // array arr.
       System.out.println("Enter numbers");
       int[] arr = new int[n];
       for(int i=0; i<n; i++)
       {
           arr[i] = in.nextInt();
       }

       // Printing the array that we've taken.
       System.out.println("\nArray that you've entered:");
       for(int i=0; i<n; i++)
       {
           System.out.print(arr[i]+" ");
       }  

       // Create a new array to store the resulting values.
       int[] result = new int[n];

       // If we rotate an array of lenth "n" by "r" positions,
       // then the values at an index "i" will be moved to an index
       // position which is equal to (i-r+n)%n which means that,
       // "i" is moved to "r" places left and then, since, we have to
       // neutralize the negative sign (Which might come) we
      // are adding "n" and since, the values should be in
      // range 0...n-1, we are finding modulus of the obtained
      // number (i-r+n) with n. Thus making the new index in range of the 0...n-1

       //here r is 2
       for(int i=0; i<n; i++)
       {
           // you can replace 2 with whatever integer
           // and this rotates the array that number times.
           result[(i-2+n)%n] = arr[i];
       }

       // Printing the array after rotation.
       System.out.println("\n\nAfter Rotaion: ");
       for(int i=0; i<n; i++)
       {
           System.out.print(result[i]+" ");
       }
   }
}

Output:

Please appreciate solution if you like it,

feel free to ask doubts in comment section.

Add a comment
Know the answer?
Add Answer to:
Write a JAVA program that rotates a 1-dimensional by 2 elements. For example [1,2,3,4,5,6] rotated by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program that create a 2 dimensional array of type double of size 10...

    Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance

  • Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order...

    Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order (from smallest to largest). You can either get the array as input or hardcode it inside your program.

  • JAVA Program

    1. Write a java program to find the sum of the elements on the diagonal of a matrix. 2. Write a java program to add two matrices. 3. Write a java program to find a given value in a matrix. 4. Write a java program to multiply two matrices. 5. Write a java program to find the transpose of a matrix.

  • Write a java program to remove duplicate elements from an array. Do not use Java library...

    Write a java program to remove duplicate elements from an array. Do not use Java library utilities or 3rd party tools to solve this problem. You need to use the “for loop” and manage the index as you traverse thru the array [i ] while looking for duplicates. For example: Example 1 --------------------- Original Array: 105 123 -921 -1 123 8 8 8 -921 Array with unique values: 105 123 -921 -1 8 Example 2 --------------------------- Original Array: 10 22...

  • given string " This is Amazing" i want to write a java program oops that rotates...

    given string " This is Amazing" i want to write a java program oops that rotates this string. input "This is Amazing" output "Amazing is this" in addition i also want to add a method in the program that gives the following output. "This is Amazing" "gnizama si siht" thank you please write an object orinted program.

  • Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The...

    Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The first row has length 1, the second row has length 2, the third row has length 3, and so on. Then initialize the array using nested for loops so that the value of A[i][j] is i+j. Finally, print out the array in a nice triangular form.

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Write a java program that specifies three parallel one dimensional arrays name length, width, and area....

    Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] =   length [i]* width [i]) after data has been entered display the following output: (in Java)

  • Write a method in java in a most basic way rotatelist that rotates an arraylist to...

    Write a method in java in a most basic way rotatelist that rotates an arraylist to the right by k steps. The user will be asked to enter the arraylist and k in the program. For example : if the input arraylist is [1,2,3,4,5,6,7] and k is 3, then the method will return [5,6,7,1,2,3,4] .

  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT