-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCirucularshift.java
More file actions
48 lines (41 loc) · 1.46 KB
/
Copy pathCirucularshift.java
File metadata and controls
48 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*Given an array of 10 two-digit numbers, develop a program that does the following:
The elements in the even position should be shifted right (circular shift) by two
positions and the elements in the odd position should be shifted left (circular shift)
by two positions.
INPUT :
12 23 71 34 92 43 12 67 72 88
OUTPUT:
71 88 92 23 12 34 72 43 12 67 */
class CircularShift {
public static void main(String args[]) {
int arr[]={12,23,71,34,92,43,12,67,77,88}; // Directly assigned elements
int res[]=new int[10];
int iteration=arr.length/2-1;
int j=1;
while(j<=iteration)
{
// Shift odd-indexed elements
for(int i=1;i<arr.length;i+=2) {
int newIndex=i-2;
if(newIndex<0)
newIndex=newIndex+arr.length; // Circular shift for negative index
res[newIndex]=arr[i];
}
// Shift even-indexed elements right by 2 positions
for(int i=0;i<arr.length;i+=2) {
int newIndex=i+2;
if(newIndex>=arr.length)
newIndex=newIndex-arr.length; // Circular shift for exceeding index
res[newIndex]=arr[i];
}
for(int k=0;k<res.length;k++)
{
arr[k]=res[k];
}
j+=1;
}
// Output the result
for(int k=0;k<res.length;k++)
System.out.print(res[k]+" ");
}
}