The Wayback Machine - https://web.archive.org/web/20240828171037/https://www.geeksforgeeks.org/split-array-add-first-part-end/
Open In App

Split the array and add the first part to the end

Last Updated : 17 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Easy
37.06%
3.4L

There is a given array and split it from a specified position, and move the first splitted part of the array and then add to the end of array
 

Split the array and add the first part to the end

Examples: 

Input : arr[] = {12, 10, 5, 6, 52, 36}
            k = 2
Output : arr[] = {5, 6, 52, 36, 12, 10}
Explanation : here k is two so first two elements are splitted and they are added at the end of array

Input : arr[] = {3, 1, 2}
           k = 1
Output : arr[] = {1, 2, 3}
Explanation :here k is one  so first one  element  are splitted and it  is added at the end of array

Simple Solution: We will rotate the elements of the array one by one.

Implementation:

C





C++





Java





Python3





C#





PHP





Javascript





Output

5 6 52 36 12 10 

Time complexity: O(n*k). 
Space complexity: O(1), since no extra space has been taken.

Another approach: Another approach is to make a temporary array with double the size and copy our array element into a new array twice and then copy the element from the new array to our array by taking the rotation as starting index up to the length of our array.

Below is the implementation of the above approach.

C++





Java





Python3




# Python3 program to split array and
# move first part to end.
 
# Function to split array and
# move first part to end
def SplitAndAdd(A, length, rotation):
 
    # make a temporary array with double
    # the size and each index is initialized to 0
    tmp = [ 0 for i in range(length * 2)]
 
    # copy array element in to new array twice
    for i in range(length):
        tmp[i] = A[i]
        tmp[i + length] = A[i]
 
    for i in range(rotation,
                   rotation + length, 1):
        A[i - rotation] = tmp[i];
      
# Driver code
arr = [12, 10, 5, 6, 52, 36]
n = len(arr)
position = 2
SplitAndAdd(arr, n, position);
for i in range(n):
    print(arr[i], end = " ")
print()
 
# This code is contributed by SOUMYA SEN


C#




// C# program to split array
// and move first part to end.
using System;
 
class GFG
{
 
    // Function to split array and
    // move first part to end
    public static void SplitAndAdd(int[] A,
                                   int length,
                                   int rotation)
    {
 
        // make a temporary array with double the size
        int[] tmp = new int[length * 2];
 
        // copy array element in to new array twice
        Array.Copy(A, 0, tmp, 0, length);
        Array.Copy(A, 0, tmp, length, length);
        for (int i = rotation; i < rotation + length; i++)
        {
            A[i - rotation] = tmp[i];
        }
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int[] arr = new int[] {12, 10, 5, 6, 52, 36};
        int n = arr.Length;
        int position = 2;
 
        SplitAndAdd(arr, n, position);
 
        for (int i = 0; i < n; ++i)
        {
            Console.Write(arr[i] + " ");
        }
    }
}
 
// This code is contributed by kumar65


Javascript




<script>
// Javascript program to split
// array and move first
// part to end.
     
    // Function to split array and
    // move first part to end
    function SplitAndAdd(A,Length,rotation)
    {
        // make a temporary array
        // with double the size
        let tmp = new Array(Length*2);
        for(let i=0;i<tmp.length;i++)
        {
            tmp[i]=0;
        }
          
        // copy array element in to new array twice
         
        for(let i=0;i<Length;i++)
        {
            tmp[i] = A[i]
            tmp[i + Length] = A[i]
        }
         
        for(let i=rotation;i<rotation+Length;i++)
        {
            A[i - rotation] = tmp[i];
        }
         
    }
     
    // Driver code
    let arr=[12, 10, 5, 6, 52, 36];
    let n = arr.length;
    let position = 2;
    SplitAndAdd(arr, n, position);
     
    for (let i = 0; i < n; ++i)
        document.write(arr[i] + " ");
     
     
    // This code is contributed by avanitrachhadiya2155
     
</script>


Output

5 6 52 36 12 10 

Time complexity: O(n)
Space complexity: O(2*n)

An efficient O(n) solution is discussed in the following post: Split the array and add the first part to the end | Set 2
This problem is noted but array rotation problem and we can apply the optimized O(n) array rotation methods here. 
Program for array rotation 
Block swap algorithm for array rotation
Reversal algorithm for array rotation 
Quickly find multiple left rotations of an array | Set 1 
Print left rotation of array in O(n) time and O(1) space

 



Similar Reads

Python3 Program to Split the array and add the first part to the end | Set 2
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 :
2 min read
Javascript Program to Split the array and add the first part to the end | Set 2
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 :
2 min read
Find partitions that maximises sum of count of 0's in left part and count of 1's in right part
Given a binary array nums of length N, The task is to find all possible partitions in given array such that sum of count of 0's in left part and count of 1's in right part is maximized. Example: Input: nums = {0, 0, 1, 0}Output: 2, 4Explanation: Division at indexindex - 0: numsleft is []. numsright is [0, 0, 1, 0]. The sum is 0 + 1 = 1.index - 1: n
7 min read
Split the number into N parts such that difference between the smallest and the largest part is minimum
Given two integers 'X' and 'N', the task is to split the integer 'X' into exactly 'N' parts such that: X1 + X2 + X3 + ... + Xn = X and the difference between the maximum and the minimum number from the sequence is minimized. Print the sequence in the end, if the number cannot be divided into exactly 'N' parts then print '-1' instead. Examples: Inpu
6 min read
Why start + (end - start)/2 is preferable method for calculating middle of an array over (start + end)/2 ?
I am very sure that everyone is able to find middle index of array once you know start index and end index of array, but there are certain benefits of using start + (end - start)/2 over (start + end)/2, which are described below : The very first way of finding middle index is mid = (start + end)/2But there is a problem with this approach, what if t
8 min read
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times
Given an array arr[] consisting of N integers and a positive integer K, the task is to find the last element present in the array obtained by repeatedly removing the first element of the array and appending it twice to the end of the array K times. Examples: Input: arr[] = {1, 2, 3, 4, 5}, K = 5Output: 5Explanation:Initially, the array is {1, 2, 3,
7 min read
Number of ways to split a binary number such that every part is divisible by 2
Given a binary string S, the task is to find the number of ways to split it into parts such that every part is divisible by 2. Examples: Input: S = "100" Output: 2 There are two ways to split the string: {"10", "0"} and {"100"}Input: S = "110" Output: 1 Approach: One observation is that the string can only be split after a 0. Thus, count the number
7 min read
Split the string into minimum parts such that each part is in the another string
Given two strings A and B, the task is to split the string A into the minimum number of substrings such that each substring is in the string B. Note: If there is no way to split the string, then print -1 Examples: Input: A = "abcdab", B = "dabc" Output: 2 Explanation: The two substrings of A which is also present in B are - {"abc", "dab"} Input: A
11 min read
Print Array after moving first occurrence of given element to end in given Array for Q queries
Given an array arr[] of N integers and an array query[] having Q integers, the task is to print the array arr[] after moving the first occurrence of query[i] to the end of the array arr[] for each i in the range [0, Q). Example: Input: arr[] = {1, 3, 1, 3}, query[] = {3, 1}Output: 1 3 3 1Explanation: In 1st iteration, send first occurrence of query
7 min read
Split array to three subarrays such that sum of first and third subarray is equal and maximum
Given an array of N integers, the task is to print the sum of the first subarray by splitting the array into exactly three subarrays such that the sum of the first and third subarray elements are equal and the maximum. Note: All the elements must belong to a subarray and the subarrays can also be empty. Examples: Input: a[] = {1, 3, 1, 1, 4} Output
13 min read
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts
Given an array arr[] consisting of N positive integers, the task is to find the minimum possible length of a rod that can be cut into N equal parts such that every ith part can be cut into arr[i] equal parts. Examples: Input: arr[] = {1, 2}Output: 4Explanation:Consider the length of the rod as 4. Then it can be divided in 2 equal parts, each having
7 min read
Find the other end point of a line with given one end and mid
Given a midpoint of line(m1, m2) and one coordinate of a line (x1, y1), find the other end point(x2, y2) of a line. Examples: Input : x1 = –1, y1 = 2, and m1 = 3, m2 = –6 Output : x2 = 7, y2 = 10 Input : x1 = 6.4, y1 = 3 and m1 = –10.7, m2 = 4 Output : x2 = 3, y2 = 4 The Midpoint Formula: The midpoint of two points, (x1, y2) and (x2, y2) is the poi
4 min read
Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
Given an array arr[] of size N, the task is to split the entire array into a minimum number of subarrays such that for each subarray, the GCD of the first and last element of the subarray is greater than 1. Examples: Input: arr[] = {2, 3, 4, 4, 4, 3} Output: 2 Explanation: Split the given array into [2, 3, 4, 4, 4] and [3]. The first subarray has g
7 min read
Minimum value to add to arr[i] so that an array can be split at index i with equal sum
Given an array arr[] of integers, the task is to find the minimum non-negative integer k such that there exists an index j in the given array such that when arr[j] is updated as arr[j] + k, the sum of elements of an array from index arr[0] to arr[j] is equal to the sum of elements from arr[j + 1] to arr[n - 1] i.e. arr[0] + arr[1] + ... + arr[j] =
13 min read
Find if a number is part of AP whose first element and difference are given
Given three non-negative integers a, d and x. Here, a is the first element, d is the difference of an AP (Arithmetic Progression). We need to find if x is part of the given AP or not. Examples : Input : a = 1, d = 3, x = 7Output : Yes7 is part of given AP, 1 + 3 + 3 = 7Input : a = 10, d = 0, x = 10Output : YesFirstly, in case d = 0, we should outpu
5 min read
Swap first odd and even valued nodes from the beginning and end of a Linked List
Given a singly Linked List, the task is to swap the first odd valued node from the beginning and the first even valued node from the end of the Linked List. If the list contains node values of a single parity, then no modifications are required. Examples: Input: 4 -&gt; 3 -&gt; 5 -&gt; 2 -&gt; 3 -&gt; NULLOutput: 4 -&gt; 2 -&gt; 5 -&gt; 3 -&gt; 3 -
13 min read
Breaking a number such that first part is integral division of second by a power of 10
Given a very large number N, we need to count the total ways such that if we divide the number into two parts a and b, the first part a can be obtained by integral division of second b by some power p of 10 and p&gt;=0. 1 &lt;= No of digits in N &lt;= [Tex]10^5 [/Tex]. Examples: Input : 220 Output : 1 220 can be divided as a = 2 and b = 20 such tha
6 min read
Double the first element and move zero to end
For a given array of n integers and assume that '0' is an invalid number and all others a valid number. Convert the array in such a way that if both current and next element is valid and both have same value then double current value and replace the next number with 0. After the modification, rearrange the array such that all 0’s shift to the end.
14 min read
Sum of array elements possible by appending arr[i] / K to the end of the array K times for array elements divisible by K
Given an array arr[] consisting of N integers and an integer K, the task is to find the sum of the array elements possible by traversing the array and adding arr[i] / K, K number of times at the end of the array, if arr[i] is divisible by K. Otherwise, stop the traversal. Examples: Input: arr[] = {4, 6, 8, 2}, K = 2Output: 44Explanation:The followi
14 min read
Split first N natural numbers into two subsequences with non-coprime sums
Given an integer N (N &amp;e; 3), the task is to split all numbers from 1 to N into two subsequences such that the sum of two subsequences is non-coprime to each other. Examples: Input: N = 5Output:{1, 3, 5}{2, 4}Explanation: Sum of the subsequence X[] = 1 + 3 + 5 = 9.Sum of the subsequence Y[] = 2 + 4 = 6.Since GCD(9, 6) is 3, the sums are not co-
4 min read
Split first N natural numbers into two sets with minimum absolute difference of their sums
Given an integer N, split the first N natural numbers into two sets such that the absolute difference between their sum is minimum. The task is to print the minimum absolute difference that can be obtained. Examples: Input: N = 5Output: 1Explanation:Split the first N (= 5) natural numbers into sets {1, 2, 5} (sum = 8) and {3, 4} (sum = 7).Therefore
13 min read
Move first element to end of a given Linked List
Write a C function that moves first element to end in a given Singly Linked List. For example, if the given Linked List is 1-&gt;2-&gt;3-&gt;4-&gt;5, then the function should change the list to 2-&gt;3-&gt;4-&gt;5-&gt;1. Algorithm: Traverse the list till last node. Use two pointers: one to store the address of last node(last) and other for address
14 min read
Lexicographically smallest string by repeatedly moving one of first K characters to end
Given a string str and a positive integer K. In an operation, you can select a character from the first K characters and move it to the end, the task is to find the smallest lexicographic string that can be made from string str after doing all the required number of operations. Examples: Input: str = "dcab", K = 2Output: abdcExplanation: Remove 'd'
5 min read
Move the First Fibonacci Number to the End of a Linked List
Given a singly linked list, the task is to identify the first Fibonacci number in the list and move that node to the end of the linked list. Examples: Input: 10 -&gt; 15 -&gt; 8 -&gt; 13 -&gt; 21 -&gt; 5 -&gt; 2 -&gt; NULLOutput: 10 -&gt; 15 -&gt; 13 -&gt; 21 -&gt; 5 -&gt; 2 -&gt; 8 -&gt; NULLExplanation: In the given list, the Fibonacci numbers ar
12 min read
Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
Given an array A[] consisting of N integers, the task is to split the array A[] into subsets having equal sum and of length equal to elements in array B[]. Examples: Input: A[] = {17, 13, 21, 20, 50, 29}, B[] = {2, 3, 1} Output: 21 29 17 13 20 50 Input: A[] = { 1, 2, 3, 4, 5, 6}, B[] = { 2, 2, 2} Output: 1 6 2 5 3 4 Approach: Follow the steps below
8 min read
Maximize Array sum by choosing pairs and dividing one part and multiplying other by K
Given an array arr[] of size N and integer K, the task is to maximize the sum of the array by performing the following operations any number of times(possibly zero): Choose two indices, i and j where arr[i] must be a multiple of K.Make arr[i] = arr[i]/K and arr[j] = K*arr[j]After performing the above operations, the sum of the elements of the array
7 min read
Minimize cost of choosing and skipping array elements to reach end of the given array
Given an integer X and an array cost[] consisting of N integers, the task is to find the minimum cost to reach the end of the given array starting from the first element based on the following constraints: Cost to visit point i is cost[i] where 1 ? i ? N.Cost to skip point i is min(cost[i], X) where 1 ? i ? N.At most 2 points can be skipped in a ro
14 min read
Maximize first element of Array by deleting first or adding a previously deleted element
Given an array arr[] of size N, and an integer K, the task is to maximize the first element of the array in K operations where in each operation: If the array is not empty, remove the topmost element of the array.Add any one of the previously removed element back at the starting of the array. Examples: Input: arr[] = [5, 2, 2, 4, 0, 6], K = 4Output
7 min read
Check if set of first X elements of one Array is same as set of first Y elements of other
Given two arrays arr1[] and arr2[] of size N each and an array Q[][2] consisting of M queries of the form [x, y], the task for each query is to check if the set of values contained from first x elements of arr1[] is equal to set of values from in first y elements of arr2[]. Note: Two sets are equal when they have all their distinct elements equal.
15+ min read
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
Given an array arr[] consisting of N integers, the task is to count the number of ways to split the array into two subarrays of equal sum after changing a single array element to 0. Examples: Input: arr[] = {1, 2, -1, 3}Output: 4Explanation: Replacing arr[0] by 0, arr[] is modified to {0, 2, -1, 3}. Only 1 possible split is {0, 2} and {-1, 3}. Repl
11 min read
Article Tags :
Practice Tags :