ISC Practicals and Theory - Cyclic Shift (Using a Class)
Updated 20 Oct 2026
A cyclic shift (rotation) of an array moves every element a fixed number of places in one direction, and the elements pushed off one end re-enter at the other end. This note shifts the array to the left by $k$ positions, using a class whose data members hold the array and whose methods do the separate jobs.
Definition
In a left cyclic shift by one position, every element moves one place to the left and the first element goes to the end. A shift by $k$ positions is this done $k$ times.
- For $\{1, 2, 3, 4, 5\}$ and $k = 2$: after one shift $\{2, 3, 4, 5, 1\}$, after two shifts $\{3, 4, 5, 1, 2\}$.
- Shifting an array of $n$ elements by $n$ gives back the original, so a shift by $k$ equals a shift by $k \bmod n$. For $n = 5$ and $k = 7$, only $7 \bmod 5 = 2$ shifts are needed.
Algorithm
mainreads the sizesize. If it is less than 1 or more than 20, it printsInvalid sizeand stops.maincreates an objectobwithnew CyclicShift(size). The constructor stores the size innand creates the arraya.ob.accept(sc)reads thenelements intoa.mainreadsk. If it is negative, it printsInvalid shiftand stops.ob.shiftLeft(k)does the shift:- It replaces
kbyk % n. - It calls
shiftOnce()exactlyktimes. shiftOnce()savesa[0]infirst, movesa[i + 1]intoa[i]forifrom 0 ton - 2, and putsfirstina[n - 1].
- It replaces
ob.display()prints the array.
Program
The constructor sets up the array, accept() reads the elements, shiftOnce() shifts by one place, shiftLeft() repeats it the right number of times and display() prints the array.
import java.util.Scanner;
public class CyclicShift
{
int a[];
int n;
CyclicShift(int size)
{
n = size;
a = new int[n];
}
void accept(Scanner sc)
{
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
}
void shiftOnce()
{
int first = a[0];
for (int i = 0; i < n - 1; i++)
a[i] = a[i + 1];
a[n - 1] = first;
}
void shiftLeft(int k)
{
k = k % n;
for (int s = 1; s <= k; s++)
shiftOnce();
System.out.println("Array after shifting left by " + k + " positions:");
}
void display()
{
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
if (size < 1 || size > 20)
{
System.out.println("Invalid size");
return;
}
CyclicShift ob = new CyclicShift(size);
ob.accept(sc);
System.out.print("Enter the number of positions to shift left: ");
int k = sc.nextInt();
if (k < 0)
{
System.out.println("Invalid shift");
return;
}
ob.shiftLeft(k);
ob.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
a | int[] | Data member: stores the elements of the array |
n | int | Data member: number of elements in the array |
size (constructor parameter) | int | Size passed to the constructor, copied into n |
sc | Scanner | Reads the size, the elements and the shift from the keyboard |
i | int | Loop counter for the array index |
first | int | Holds the first element while the others move left |
k | int | Number of positions to shift left; reduced to k % n in shiftLeft |
s | int | Counts the calls of shiftOnce() made so far |
args | String[] | Command line arguments of main; not used |
size (in main) | int | Size of the array typed by the user |
ob | CyclicShift | Object on which the methods are called |
Sample input and output
Sample 1
Enter the size of the array: 5
Enter 5 elements:
1 2 3 4 5
Enter the number of positions to shift left: 2
Array after shifting left by 2 positions:
3 4 5 1 2 Sample 2
Enter the size of the array: 5
Enter 5 elements:
1 2 3 4 5
Enter the number of positions to shift left: 7
Array after shifting left by 2 positions:
3 4 5 1 2 Sample 3
Enter the size of the array: 3
Enter 3 elements:
5 6 7
Enter the number of positions to shift left: 0
Array after shifting left by 0 positions:
5 6 7 Sample 4
Enter the size of the array: 0
Invalid sizeSample 5
Enter the size of the array: 4
Enter 4 elements:
1 2 3 4
Enter the number of positions to shift left: -1
Invalid shiftDry run
Input: size = 5, array $\{1, 2, 3, 4, 5\}$, k = 7. In shiftLeft, $k = 7 \bmod 5 = 2$.
| s | first (in shiftOnce) | Array after shiftOnce() |
|---|---|---|
| 1 | 1 | 2 3 4 5 1 |
| 2 | 2 | 3 4 5 1 2 |
display() then prints 3 4 5 1 2, which matches the second sample run.
Variations
- Right shift "Write a program with a class to shift the elements of an array cyclically to the right by k positions."
- String "Shift the characters of a String cyclically by k places using methods."
- 2D array "Rotate each row of a matrix by k positions."