Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
1.1.
Problem Statement
1.2.
Sample Examples
1.2.1.
Example 1
1.2.2.
Example 2
2.
Brute Force Approach
2.1.
Pseudocode
2.2.
Implementation in C++
2.2.1.
Complexity Analysis
3.
Optimized Approach
3.1.
Pseudocode
3.2.
Implementation in C++
3.2.1.
Complexity Analysis
4.
Frequently Asked Questions
4.1.
What is the difference between an array and a vector in C++?
4.2.
Can we use split() to split our array?
4.3.
What are some disadvantages of an array?
5.
Conclusion
Last Updated: Mar 27, 2024

Split the array and add the first part to the end

Author Md Yawar
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Arrays are one of the most commonly used data structures. One must be fluent in array operations to crack coding interviews. Various operations can be performed on an array. In this blog, we will learn how to split the array and add the first part to the end.

source

Problem Statement

Split the array and add the first part to the end. We split the array into two parts and move the first portion of the array to the end.

Split the array and add the first part to the end

Sample Examples

Example 1

Input: 

arr[] = {2, 1, 5, 6, 15, 26}
k = 3

 

Output: 

arr[] =  {6, 15, 26, 2, 1, 5}

 

Explanation: 

Split the array and add the first part to the end. In this, we split from index 3 and add the first part {6, 15, 26} to the end.

 

Example 2

Input: 

arr[] = {23, 14, 45}
k = 1

 

Output: 

arr[] = {14, 45, 23}

 

Explanation: 

Split the array and add the first part to the end. In this, we split from index 1 and add the first part {14} to the end.

Also See, Array Implementation of Queue and  Rabin Karp Algorithm

Brute Force Approach

To split the array and add the first part to the end, we can rotate the elements of the array one by one.

Pseudocode

Step 1: Initialize the array and the position where we want to split the array.

Step 2: Create a variable to store the element at the 0th position.

Step 3: Replacing each element ‘i’ of the array with the element at the ‘i+1’ position.

Step 4: Replacing the last element of the array with the element stored in the variable created in step 2.

Implementation in C++

#include <iostream>

using namespace std;

// function to split the array and add the first part to the end
void arraySplit(int array[], int j, int k) {
  for (int i = 0; i < k; i++) {

    // Rotate the array by 1
    int x = array[0];

    for (int p = 0; p < j - 1; p++)
      array[p] = array[p + 1];

    array[j - 1] = x;
  }
}

// Driver code
int main() {
  //initializing the array
  int array[] = { 1, 2, 3, 4, 5, 6 };
  int n = sizeof(array) / sizeof(int);
  int pos = 3;

  //calling the function
  arraySplit(array, n, pos);

  // printing the converted array
  for (int i = 0; i < n; ++i)
    cout << " " << array[i];

  return 0;
}

 

Output:

4 5 6 1 2 3

 

Complexity Analysis

Time complexity: O(n*k) where n is the size of the array and k is the number of times we have traversed the array. The time complexity is O(nK) as the array is traversed k times.

Space complexity: O(1) as no new memory is created.

Read More - Time Complexity of Sorting Algorithms

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Optimized Approach

The optimized way is to create a temporary array twice the size of our array and duplicate our array element into the new array twice. The element from the new array is then copied to our array by using the rotation as the beginning index up to the length of our array. The aforementioned technique is implemented in the following manner.

Pseudocode

Step 1: Initialize the array and the position where we want to split the array.

Step 2: Create a temporary array with twice the length of the array.

Step 3: Copy the elements of the array to the temporary array in the exact same order.

Step 4: Copy the elements of the array at position ‘i’ to the temporary array at position ‘i+l’, where l is the length of the array.

Step 5: copy the elements of the temporary array at position ‘i’ to the original array at position ‘i-r’, where r is the position where we want to split the array.

Implementation in C++

#include <bits/stdc++.h>

using namespace std;

// Function to split array and move first part to end
void arrSplit(int arr[], int length, int rotation) {
  int temp[length * 2] = {
    0
  };

  for (int i = 0; i < length; i++) {
    temp[i] = arr[i];
    temp[i + length] = arr[i];
  }

  for (int i = rotation; i < rotation + length; i++) {
    arr[i - rotation] = temp[i];
  }
}

// Driver code
int main() {
  int array[] = { 1, 10, 5, 26, 2, 36 };
  int n = sizeof(array) / sizeof(int);
  int position = 4;

  //calling the function
  arrSplit(array, n, position);

  //printing the array 
  for (int i = 0; i < n; ++i)
    cout << array[i] << ' ';
  return 0;
}

 

Output:

2 36 1 10 5 26

 

Complexity Analysis

Time complexity: O(n) where n is the length of the array. The time complexity is O(n) because we travel the array only once.

Space complexity: O(n) where n is the length of the array because we have created a new array twice the size of the original array.

Frequently Asked Questions

What is the difference between an array and a vector in C++?

Vector is dynamic by nature, its size grows when new items are added. In contrast, an array has a fixed size and cannot be expanded after being established.

Can we use split() to split our array?

The split() technique does not work with arrays directly. However, we may utilize the split() function after first converting the members of our array to a string.

What are some disadvantages of an array?

The number of elements in the array should be known ahead of time. An array is a fixed structure (which means the array is of fixed size). The array's size cannot be changed once it has been defined.

Conclusion

In this blog, we looked at an array problem in which we split the array and add the first part to the end. Split the array and add the first part to the end is a common question and one must practice it to master the array concepts.

Recommended problems -

 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Live masterclass