Javascript Program to Split the array and add the first part to the end | Set 2 Last Updated : 24 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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, 1. Reverse array from 0 to n - 1 (where n is size of the array). 2. Reverse array from 0 to n - k - 1. 3. Reverse array from n - k to n - 1. JavaScript <script> // Javascript program to Split the array and // add the first part to the end /* Function to reverse arr[] from index start to end*/ function rvereseArray(arr, start, end) { while (start < end) { let temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } // Function to print an array function printArray(arr, size) { for (let i = 0; i < size; i++) document.write(arr[i] + " "); } /* Function to left rotate arr[] of size n by k */ function splitArr(arr, k, n) { rvereseArray(arr, 0, n - 1); rvereseArray(arr, 0, n - k - 1); rvereseArray(arr, n - k, n - 1); } /* Driver program to test above functions */ let arr = new Array( 12, 10, 5, 6, 52, 36 ); let n = arr.length; let k = 2; // Function calling splitArr(arr, k, n); printArray(arr, n); // This code is contributed // by _saurabh_jaiswal </script> Output: 5 6 52 36 12 10 Please refer complete article on Split the array and add the first part to the end | Set 2 for more details! Comment More infoAdvertise with us Next Article Javascript Program to Split the array and add the first part to the end | Set 2 K kartik Follow Improve Article Tags : JavaScript Web Technologies DSA Arrays rotation array-rearrange Reverse +3 More Practice Tags : ArraysReverse Similar Reads 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 6 min read Split the array and add the first part to the end 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 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 9 min read Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0 5 min read Javascript Program to Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts Given an array A[] of length N, where N is an even number, the task is to answer Q independent queries where each query consists of a positive integer K representing the number of circular shifts performed on the array and find the sum of elements by performing Bitwise OR operation on the divided ar 5 min read Find the sum of the first half and second half elements of an array Given an array arr of size N. The task is to find the sum of the first half (N/2) elements and the second half elements (N - N/2) of an array. Examples: Input : arr[] = {20, 30, 60, 10, 25, 15, 40}Â Output : 110, 90Â Sum of first N/2 elements 20 + 30 + 60 is 110Input : arr[] = {50, 35, 20, 15}Â Outp 10 min read Like