Codility Cyclic Rotation Java solution

preview_player
Показать описание
Codility is an engineer assessment platform website where you can test your programming skills. Cyclic Rotation is one one of the tests on the Codility site.

In this video, I show a solution to the Cyclic Rotation task on Codility. The solution is done in Java. The challenge is to rotate an array a specified number of times.
Рекомендации по теме
Комментарии
Автор

just found this playlist of yours. As a soon to graduate student who wishes to land a developer job, this tutorials are gems. Thank you.

justmonika
Автор

Great video! Can you make a video of exercises in codility specifically in algorithmic skills? Would be much appreciated.

Leiiiiiii_
Автор

hi David is this series contains all the java problems mentioned in codility

moumitachattarjee
Автор

Hi Dave Merry Chrismas, please make java management systems using OOP

jociliodulamo
Автор

My solution in swift
func cyclicRotation(_ A : inout [Int], _ K : Int) -> [Int] {
var count: Int = 0
while count < K {
count = count + 1
let last = A.last ?? 0
A.removeLast()
A.insert(last, at: 0)

}

return A
}



var array: [Int] = [1, 2, 3, 4, 5, 6]

let response = cyclicRotation(&array, 2)
print("Result => \(response)")

Collins
Автор

This was what I did and passed with 100%
public int[] solution(int[] A, int K) {
int len = A.length;

for(int i=0; i<K; i++) {
int[] copy = A.clone();

for(int j = 0; j<len; j++){
A[0] = copy[len-1];
if(j>0){
A[j] = copy[j-1];
}
}
}

return A;
}

timiokusi
Автор

Probably a better solution in terms of computation(Passes with 100%)
public int[] solution(int[] A, int K) {
int n = A.length;
if(n<=1 || K==n || K==0 || K%n == 0){
return A;
}

int[] res = new int [n];
if(K>n){
K = K%n;
}

for(int i =0; i<n;i++){
res[(i+K)%n] = A[i];
}
return res;
}

praveensrivastava
Автор

Here is my solution in java code

public int[] solution (int[] A, int K){

int[] arrayOutput = new int[A.length];
for (int i =0; i < A.length; i++){
arrayOutput[newIndex(i, A.length, K)] = A[i];
}

return arrayOutput;
}

private int newIndex(int i, int size, int rotationNumber){
int newIndex;

newIndex = i + rotationNumber;

while (newIndex >= size){
newIndex = newIndex - size;
}

return newIndex;
}

amadeucarvalho
Автор

java
public int[] mySolution(int[] A, int K) {

int[] array = new int[A.length];
int newIndex;

for (int i = 0; i < A.length; i++) {
int var = K % A.length;
if (A.length <= i + var) {
newIndex = i + var - A.length;
} else {
newIndex = i + var;
}
array[newIndex] = A[i];
}
return array;
}

mechtarin
Автор

public int[] solution(int[] A, int K) {
if (K > A.length)
K = (K % A.length);
int[] B = new int[A.length];
for (int i = 0; i < A.length; i++) {
int index;
if (A.length - i <= K) {
index = K - (A.length - i);
} else {
index = i + K;
}
B[index] = A[i];
}
return B;
}

soumyajeetpatra
Автор

public int[] solution(int[] A, int K) {
int[] r = new int[A.length];

for (int i = 0; i < A.length; i++) {
int indexAt = (i + K) % A.length;
+ indexAt);
r[indexAt] = A[i];
}
return r;
}

sumanneupane
join shbcf.ru