Q-3) Cyclically rotate an array by one
Given an array, rotate the array by one position in clockwise direction.
Example 1:
Input:
N = 5
A[] = {1, 2, 3, 4, 5}
Output:
5 1 2 3 4
Example 2:
Input:
N = 8
A[] = {9, 8, 7, 6, 4, 2, 1, 3}
Output:
3 9 8 7 6 4 2 1
SOLUTION :
QUESTION LINK: https://practice.geeksforgeeks.org/problems/cyclically-rotate-an-array-by-one2614/1#import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); // Inputting the testcases while(t-->0) { int n = Integer.parseInt(br.readLine().trim()); int a[] = new int[n]; // long getAnswer[] = new long[(int)(n)]; String inputLine[] = br.readLine().trim().split(" "); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(inputLine[i]); } Compute obj = new Compute(); obj.rotate(a, n); StringBuilder output = new StringBuilder(); for(int i=0;i<n;i++) output.append(a[i]+" "); System.out.println(output); } } }
class Compute { public void rotate(int arr[], int n) { int i=0; int temp=arr[n-1]; for( i=n-1; i>0;i--) { arr[i]=arr[i-1]; } arr[0]=temp; //for(i=0;i<n;i++) //ystem.out.print(arr[i]+" "); } }
ReplyDeleteimport java.util.Scanner;
public class rotateArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter no of elements in arrray");
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i0;i--)
{
arr[i]=arr[i-1];
}
arr[0]=temp;
for(int j=0;j<n;j++)
{
System.out.println(arr[j]+"\t");
}
}
}