Skip to main content
Question Protected by CommunityBot
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
deleted 3 characters in body; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Need feedback on my codility solution of Codility cyclic rotation solution in PHP

Problem statement :

Problem statement

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

My Solution :

My Solution

function solution($A, $K) {
   // when array is empty or has only one element 
   if(count($A) == 0 || count($A) == 1){
        return $A;
    }
    //runs k times
    for($j=1; $j<=$K; $j++){
        $last_element = $A[count($A)-1];
        //runs for each element
        for($i=(count($A)-1); $i>0; $i--){
            $A[$i] = $A[$i-1];
        }
        $A[0] = $last_element;  
    }
    return $A;
}

$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);

Output :

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Need feedback on my codility solution of cyclic rotation

Problem statement :

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

My Solution :

function solution($A, $K) {
   // when array is empty or has only one element 
   if(count($A) == 0 || count($A) == 1){
        return $A;
    }
    //runs k times
    for($j=1; $j<=$K; $j++){
        $last_element = $A[count($A)-1];
        //runs for each element
        for($i=(count($A)-1); $i>0; $i--){
            $A[$i] = $A[$i-1];
        }
        $A[0] = $last_element;  
    }
    return $A;
}

$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);

Output :

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Codility cyclic rotation solution in PHP

Problem statement

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

My Solution

function solution($A, $K) {
   // when array is empty or has only one element 
   if(count($A) == 0 || count($A) == 1){
        return $A;
    }
    //runs k times
    for($j=1; $j<=$K; $j++){
        $last_element = $A[count($A)-1];
        //runs for each element
        for($i=(count($A)-1); $i>0; $i--){
            $A[$i] = $A[$i-1];
        }
        $A[0] = $last_element;  
    }
    return $A;
}

$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
Source Link

Need feedback on my codility solution of cyclic rotation

Problem statement :

A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.

For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.

My Solution :

function solution($A, $K) {
   // when array is empty or has only one element 
   if(count($A) == 0 || count($A) == 1){
        return $A;
    }
    //runs k times
    for($j=1; $j<=$K; $j++){
        $last_element = $A[count($A)-1];
        //runs for each element
        for($i=(count($A)-1); $i>0; $i--){
            $A[$i] = $A[$i-1];
        }
        $A[0] = $last_element;  
    }
    return $A;
}

$A = [1, 2, 3, 4];
$K = 4;
$result = solution($A, $K);
print_r($result);

Output :

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)