Split the array and add the first part to end
Split the array and add the first part to end is a common algorithmic problem that requires rearranging the elements of an array in a particular manner. The problem statement requires us to divide an array into two parts, the first part consisting of 'n' elements, and the second part consisting of the remaining 'm' elements. We then append the first 'n' elements at the end of the array.
To solve the problem, we can use a simple approach of creating a temporary array to store the first 'n' elements of the given array. After this, we can shift the remaining 'm' elements of the array to the left to fill the gap created by removing the first 'n' elements. Finally, we can append the temporary array containing the first 'n' elements to the end of the shifted array.
For example, let's consider the array [1, 2, 3, 4, 5, 6, 7], where we need to split the array at index 3 and append the first part to the end.
We first create a temporary array containing [1, 2, 3]. We then shift the remaining elements [4, 5, 6, 7] to the left to get [4, 5, 6, 7, _, _, _]. Finally, we append the temporary array [1, 2, 3] to the end of the shifted array to get [4, 5, 6, 7, 1, 2, 3].
This algorithm has a time complexity of O(n) and a space complexity of O(n), where 'n' is the length of the array. The problem is often encountered in real-world scenarios where we need to perform operations such as rotating a deck of cards or shifting elements in a circular buffer.
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
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.
New Comment