Posted on by Kalkicode
Code Array

# Split the array and add the first part to end

The problem at hand is about splitting an array into two parts and then adding the first part to the end of the array. This process involves a series of array manipulations, particularly reversing sections of the array. The goal is to gain an understanding of the problem statement, the approach to solve it, and the resulting output.

## Problem Statement

Given an array, the objective is to split it into two parts: the first part containing the initial 'k' elements, and the second part containing the remaining elements. Then, the first part is to be added to the end of the second part, essentially rotating the array by 'k' positions.

## Example

Let's take an array `arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]` and 'k' as 3.

• Initial Array: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`
• After splitting and rotating by 3: `[4, 5, 6, 7, 8, 9, 1, 2, 3]`

## Idea to Solve

1. Reverse 'k' elements

First, reverse the initial 'k' elements of the array.

2. Reverse remaining elements

Next, reverse the remaining elements after the first 'k' elements.

3. Reverse the entire array

Finally, reverse the entire array.

This sequence of reversals effectively adds the first part of the array to the end, achieving the desired rotation.

## Algorithm

1. Define a function `reverse(arr[], start, end)` to reverse elements between indices 'start' and 'end'.
2. Define a function `split(arr[], size, k)` to implement the rotation:
• Check if `size` is greater than 1 and 'k' is within the valid range.
• Display the initial array.
• Reverse the first 'k' elements using `reverse()`.
• Reverse the remaining elements using `reverse()`.
• Reverse the entire array.
• Display the rotated array.

## Pseudocode

``````function reverse(arr[], start, end):
for i from start to end:
swap arr[i] with arr[end]
decrement end

function split(arr[], size, k):
if size <= 1 or k < 1 or k >= size:
return
display "Before" array
display arr
reverse(arr, 0, k - 1)
reverse(arr, k, size - 1)
reverse(arr, 0, size - 1)
display "After" array
display arr

main():
initialize arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
size = size of arr
k = 3
split(arr, size, k)``````

## Code Solution

``````//C Program
//Split the array and add the first part to end
#include<stdio.h>

void reverse(int arr[],int start,int end)
{
int temp=0;

for (int i = start,j=end; i <= end && j>i; ++i,--j)
{
//reverse the array element
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;

}
}
//Display array elements
void dispay(int arr[],int size)
{

for (int i = 0; i < size; ++i)
{
printf("%3d",arr[i] );
}
printf("\n");
}
void split(int arr[],int size, int k)
{
if(size<=1 && k<1 && k >= size)
{
return;
}
printf("  Before \n");

dispay(arr,size);

//reversing given first part
reverse(arr,0,k-1);

//reversing remaining second part
reverse(arr,k,size-1);

//reversing array
reverse(arr,0,size-1);
printf("  After\n  Adding first %d elements into end\n",k);
dispay(arr,size);
}

int main()
{
//define array elements
int arr[]={1,2,3,4,5,6,7,8,9};

//Get the size of array
int size=sizeof(arr)/sizeof(arr[0]);

//3 is number of element
int k=3;

split(arr,size,k);

}```
```

#### Output

``````  Before
1  2  3  4  5  6  7  8  9
After
Adding first 3 elements into end
4  5  6  7  8  9  1  2  3``````
``````/*
C++ Program
Split the array and add the first part to end
*/
#include<iostream>

using namespace std;

class MyArray {
public:

//Display array elements
void dispay(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << arr[i];
}
cout << "\n";
}
void reverse(int arr[], int start, int end) {
int temp = 0;
for (int i = start, j = end; i <= end && j > i; ++i, --j) {
//reverse the array element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
void split(int arr[], int size, int k) {
if (size <= 1 && k < 1 && k >= size) {
return;
}
cout << " Before \n";
this->dispay(arr, size);
//reversing given first part
this->reverse(arr, 0, k - 1);
//reversing remaining second part
this->reverse(arr, k, size - 1);
//reversing array
this->reverse(arr, 0, size - 1);
cout << " After\n Adding first " << k << " elements into end\n";
this->dispay(arr, size);
}
};
int main() {
MyArray obj = MyArray();
int arr[] = {
1,
2,
3,
4,
5,
6,
7,
8,
9
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
//3 is number of element
int k = 3;
obj.split(arr, size, k);
return 0;
}```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````/*
Java Program
Split the array and add the first part to end
*/

public class MyArray
{

//Display array elements
public void dispay(int []arr,int size)
{

for (int i = 0; i < size; ++i)
{
System.out.print("  "+arr[i] );
}
System.out.print("\n");
}
void reverse(int []arr,int start,int end)
{
int temp=0;

for (int i = start,j=end; i <= end && j>i; ++i,--j)
{
//reverse the array element
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;

}
}

void split(int []arr,int size, int k)
{
if(size<=1 && k<1 && k >= size)
{
return;
}
System.out.print("  Before \n");

dispay(arr,size);

//reversing given first part
reverse(arr,0,k-1);

//reversing remaining second part
reverse(arr,k,size-1);

//reversing array
reverse(arr,0,size-1);
System.out.print("  After\n  Adding first "+k+" elements into end\n");
dispay(arr,size);
}
public static void main(String[] args) {
MyArray obj = new MyArray();
//Define the value of array elements
int []arr = {1,2,3,4,5,6,7,8,9};
//Get the size of array
int size = arr.length;

//3 is number of element
int k=3;

obj.split(arr,size,k);
}
}```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````/*
C# Program
Split the array and add the first part to end
*/
using System;
public class MyArray {
//Display array elements
public void dispay(int[] arr, int size) {
for (int i = 0; i < size; ++i) {
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
void reverse(int[] arr, int start, int end) {
int temp = 0;
for (int i = start, j = end; i <= end && j > i; ++i, --j) {
//reverse the array element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
void split(int[] arr, int size, int k) {
if (size <= 1 && k < 1 && k >= size) {
return;
}
Console.Write(" Before \n");
dispay(arr, size);
reverse(arr, 0, k - 1);
reverse(arr, k, size - 1);
reverse(arr, 0, size - 1);
Console.Write(" After\n Adding first " + k + " elements into end\n");
dispay(arr, size);
}
public static void Main(String[] args) {
MyArray obj = new MyArray();
int[]
//Define the value of array elements
arr = {
1,
2,
3,
4,
5,
6,
7,
8,
9
};
//Get the size of array
int size = arr.Length;
//3 is number of element
int k = 3;
obj.split(arr, size, k);
}
}```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````<?php
/*
Php Program
Split the array and add the first part to end
*/
class MyArray {
//Display array elements

public 	function dispay(\$arr, \$size) {
for (\$i = 0; \$i < \$size; ++\$i) {
echo(" ". \$arr[\$i]);
}
echo("\n");
}

function reverse(&\$arr, \$start, \$end) {
\$temp = 0;
for (\$i = \$start, \$j = \$end; \$i <= \$end && \$j > \$i; ++\$i, --\$j) {
//reverse the array element
\$temp = \$arr[\$i];
\$arr[\$i] = \$arr[\$j];
\$arr[\$j] = \$temp;
}
}

function split(&\$arr, \$size, \$k) {
if (\$size <= 1 && \$k < 1 && \$k >= \$size) {
return;
}
echo(" Before \n");
\$this->dispay(\$arr, \$size);
//reversing given first part
\$this->reverse(\$arr, 0, \$k - 1);
//reversing remaining second part
\$this->reverse(\$arr, \$k, \$size - 1);
//reversing array
\$this->reverse(\$arr, 0, \$size - 1);
echo(" After\n Adding first ". \$k ." elements into end\n");
\$this->dispay(\$arr, \$size);
}
}

function main() {
\$obj = new MyArray();
//Define the value of array elements
\$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//Get the size of array
\$size = count(\$arr);
//3 is number of element
\$k = 3;
\$obj->split(\$arr, \$size, \$k);

}
main();```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````/*
Node Js Program
Split the array and add the first part to end
*/
class MyArray {
//Display array elements
dispay(arr, size) {
for (var i = 0; i < size; ++i) {
process.stdout.write(" " + arr[i]);
}

process.stdout.write("\n");
}
reverse(arr, start, end) {
var temp = 0;
for (var i = start,j = end; i <= end && j > i; ++i, --j) {
//reverse the array element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
split(arr, size, k) {
if (size <= 1 && k < 1 && k >= size) {
return;
}

process.stdout.write(" Before \n");
this.dispay(arr, size);
//reversing given first part
this.reverse(arr, 0, k - 1);
//reversing remaining second part
this.reverse(arr, k, size - 1);
//reversing array
this.reverse(arr, 0, size - 1);
process.stdout.write(" After\n Adding first " + k + " elements into end\n");
this.dispay(arr, size);
}
}

function main(args) {
var obj = new MyArray();
//Define the value of array elements
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
//Get the size of array
var size = arr.length;
//3 is number of element
var k = 3;
obj.split(arr, size, k);
}

main();```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````# Python 3 Program
# Split the array and add the first part to end
class MyArray :
# Display array elements
def dispay(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print("\n", end = "")

def reverse(self, arr, start, end) :
temp = 0
i = start
j = end
while (i <= end and j > i) :
# reverse the array element
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i += 1
j -= 1

def split(self, arr, size, k) :
if (size <= 1 and k < 1 and k >= size) :
return

print(" Before \n", end = "")
self.dispay(arr, size)
self.reverse(arr, 0, k - 1)
self.reverse(arr, k, size - 1)
self.reverse(arr, 0, size - 1)
print(" After\n Adding first ", k ," elements into end\n", end = "")
self.dispay(arr, size)

def main() :
obj = MyArray()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
size = len(arr)
k = 3
obj.split(arr, size, k)

if __name__ == "__main__":
main()```
```

#### Output

`````` Before
1  2  3  4  5  6  7  8  9
After
Adding first  3  elements into end
4  5  6  7  8  9  1  2  3``````
``````# Ruby Program
# Split the array and add the first part to end
class MyArray
# Display array elements
def dispay(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
print("\n")
end
def reverse(arr, start, last)
temp = 0
i = start
j = last
while (i <= last && j > i)
# reverse the array element
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i += 1
j -= 1
end
end
def split(arr, size, k)
if (size <= 1 && k < 1 && k >= size)
return
end
print(" Before \n")
self.dispay(arr, size)
self.reverse(arr, 0, k - 1)
self.reverse(arr, k, size - 1)
self.reverse(arr, 0, size - 1)
print(" After\n Adding first ", k ," elements into end\n")
self.dispay(arr, size)
end
end
def main()
obj = MyArray.new()
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
size = arr.length
k = 3
obj.split(arr, size, k)
end
main()```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3
``````
``````/*
Scala Program
Split the array and add the first part to end
*/
class MyArray {
//Display array elements
def dispay(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size) {
print(" " + arr(i));
i += 1;
}
print("\n");
}
def reverse(arr: Array[Int], start: Int, end: Int): Unit = {
var temp: Int = 0;
var i: Int = start;
var j: Int = end;
while (i <= end && j > i) {
//reverse the array element
temp = arr(i);
arr(i) = arr(j);
arr(j) = temp;
i += 1;
j -= 1;
}
}
def split(arr: Array[Int], size: Int, k: Int): Unit = {
if (size <= 1 && k < 1 && k >= size) {
return;
}
print(" Before \n");
this.dispay(arr, size);
this.reverse(arr, 0, k - 1);
this.reverse(arr, k, size - 1);
this.reverse(arr, 0, size - 1);
print(" After\n Adding first " + k + " elements into end\n");
this.dispay(arr, size);
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyArray = new MyArray();
var arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9);
val size: Int = arr.length;
val k: Int = 3;
obj.split(arr, size, k);
}
}```
```

#### Output

`````` Before
1 2 3 4 5 6 7 8 9
After
Adding first 3 elements into end
4 5 6 7 8 9 1 2 3``````
``````/*
Swift Program
Split the array and add the first part to end
*/
class MyArray {
//Display array elements
func dispay(_ arr: [Int], _ size: Int) {
var i: Int = 0;
while (i < size) {
print(" ", arr[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
func reverse(_ arr: inout [Int], _ start: Int, _ end: Int) {
var temp: Int = 0;
var i: Int = start;
var j: Int = end;
while (i <= end && j > i) {
//reverse the array element
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 1;
j -= 1;
}
}
func split(_ arr: inout [Int], _ size: Int, _ k: Int) {
if (size <= 1 && k < 1 && k >= size) {
return;
}
print(" Before \n", terminator: "");
self.dispay(arr, size);
self.reverse(&arr, 0, k - 1);
self.reverse(&arr, k, size - 1);
self.reverse(&arr, 0, size - 1);
print(" After\n Adding first ", k ," elements into end\n", terminator: "");
self.dispay(arr, size);
}
}
func main() {
let obj: MyArray = MyArray();
var arr: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let size: Int = arr.count;
let k: Int = 3;
obj.split(&arr, size, k);
}
main();```
```

#### Output

`````` Before
1  2  3  4  5  6  7  8  9
After
Adding first  3  elements into end
4  5  6  7  8  9  1  2  3``````

## Time Complexity

The time complexity of this algorithm is dominated by the three reverse operations, each taking O(n) time, where 'n' is the size of the array. Therefore, the overall time complexity of the solution is O(n).

## Result Explanation

As shown in the example, the algorithm successfully rotates the array by 'k' positions, resulting in the first 'k' elements being added to the end of the array. The "Before" and "After" arrays are displayed for clarity. The "Before" array represents the initial array, and the "After" array shows the rotated array.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post