Skip to content

Commit 7cfa836

Browse files
author
Geektimus
committed
Refactor code of the array shift to avoid the mods of the step variable
1 parent 7e14fb7 commit 7cfa836

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/java/com/codingmaniacs/codility/CollectionChallenges.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ public static boolean isOrderedBySingleSwap(int data[]) {
6161
* @return number array shifted to the right step times.
6262
*/
6363
public static int[] arrayShiftRight(int[] ns, int step) {
64+
int s = step;
6465
int nsLength = ns.length;
65-
if (nsLength == 0 || nsLength == 1 || step == nsLength)
66+
if (nsLength == 0 || nsLength == 1 || s == nsLength)
6667
return ns;
6768

6869
int[] res = new int[nsLength];
6970

70-
step = (step % nsLength);
71+
s %= nsLength;
7172

72-
int[] l = Arrays.copyOfRange(ns, 0, nsLength - step);
73-
int[] r = Arrays.copyOfRange(ns, nsLength - step, nsLength);
73+
int[] l = Arrays.copyOfRange(ns, 0, nsLength - s);
74+
int[] r = Arrays.copyOfRange(ns, nsLength - s, nsLength);
7475

7576
System.arraycopy(r, 0, res, 0, r.length);
7677
System.arraycopy(l, 0, res, r.length, l.length);

0 commit comments

Comments
 (0)