Split the array and add the first part to the end | Set 2
Last Updated :
16 Oct, 2022
Given an array and split it from a specified position, and move the first part of array add to the end.
Examples:
Input : arr[] = {12, 10, 5, 6, 52, 36}
k = 2
Output : arr[] = {5, 6, 52, 36, 12, 10}
Explanation : Split from index 2 and first part {12, 10} add to the end .
Input : arr[] = {3, 1, 2}
k = 1
Output : arr[] = {1, 2, 3}
Explanation : Split from index 1 and first part add to the end.
A O(n*k) solution is discussed here.
This problem can be solved in O(n) time using the reversal algorithm discussed below,
- Reverse array from 0 to n – 1 (where n is size of the array).
- Reverse array from 0 to n – k – 1.
- Reverse array from n – k to n – 1.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void rvereseArray( int arr[], int start, int end)
{
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
void printArray( int arr[], int size)
{
for ( int i = 0; i < size; i++)
cout << arr[i] << " " ;
}
void splitArr( int arr[], int k, int n)
{
rvereseArray(arr, 0, n - 1);
rvereseArray(arr, 0, n - k - 1);
rvereseArray(arr, n - k, n - 1);
}
int main()
{
int arr[] = { 12, 10, 5, 6, 52, 36 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
splitArr(arr, k, n);
printArray(arr, n);
return 0;
}
|
Java
class Geeks
{
static void rvereseArray( int arr[], int start, int end)
{
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void printArray( int arr[], int size)
{
for ( int i = 0 ; i < size; i++)
System.out.print(arr[i] + " " );
}
static void splitArr( int arr[], int k, int n)
{
rvereseArray(arr, 0 , n - 1 );
rvereseArray(arr, 0 , n - k - 1 );
rvereseArray(arr, n - k, n - 1 );
}
public static void main(String args[])
{
int arr[] = { 12 , 10 , 5 , 6 , 52 , 36 };
int n = arr.length;
int k = 2 ;
splitArr(arr, k, n);
printArray(arr, n);
}
}
|
Python3
def rvereseArray(arr, start, end):
while start < end :
temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
start + = 1
end - = 1
def printArray(arr, n) :
for i in range (n):
print (arr[i], end = " " )
def splitArr(arr, k, n):
rvereseArray(arr, 0 , n - 1 )
rvereseArray(arr, 0 , n - k - 1 )
rvereseArray(arr, n - k, n - 1 )
arr = [ 12 , 10 , 5 , 6 , 52 , 36 ]
n = len (arr)
k = 2
splitArr(arr, k, n)
printArray(arr, n)
|
C#
using System;
class GFG
{
static void rvereseArray( int [] arr, int start, int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void printArray( int [] arr, int size)
{
for ( int i = 0; i < size; i++)
Console.Write(arr[i] + " " );
}
static void splitArr( int [] arr, int k, int n)
{
rvereseArray(arr, 0, n - 1);
rvereseArray(arr, 0, n - k - 1);
rvereseArray(arr, n - k, n - 1);
}
public static void Main()
{
int [] arr = { 12, 10, 5, 6, 52, 36 };
int n = arr.Length;
int k = 2;
splitArr(arr, k, n);
printArray(arr, n);
}
}
|
PHP
<?php
function rvereseArray(& $arr , $start , $end )
{
while ( $start < $end )
{
$temp = $arr [ $start ];
$arr [ $start ] = $arr [ $end ];
$arr [ $end ] = $temp ;
$start ++;
$end --;
}
}
function printArray(& $arr , $size )
{
for ( $i = 0; $i < $size ; $i ++)
echo $arr [ $i ] . " " ;
}
function splitArr(& $arr , $k , $n )
{
rvereseArray( $arr , 0, $n - 1);
rvereseArray( $arr , 0, $n - $k - 1);
rvereseArray( $arr , $n - $k , $n - 1);
}
$arr = array ( 12, 10, 5, 6, 52, 36 );
$n = sizeof( $arr );
$k = 2;
splitArr( $arr , $k , $n );
printArray( $arr , $n );
?>
|
Javascript
<script>
function rvereseArray(arr, start, end)
{
while (start < end)
{
let temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
function printArray(arr, size)
{
for (let i = 0; i < size; i++)
document.write(arr[i] + " " );
}
function splitArr(arr, k, n)
{
rvereseArray(arr, 0, n - 1);
rvereseArray(arr, 0, n - k - 1);
rvereseArray(arr, n - k, n - 1);
}
let arr = new Array( 12, 10, 5, 6, 52, 36 );
let n = arr.length;
let k = 2;
splitArr(arr, k, n);
printArray(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...