Array Quartering

Easy
0/40
0 upvote

Problem statement

You are given an array of integers. Your task is to write a program that splits this array into exactly four parts (or quarters).


The division rule is as follows:

Calculate the base size for each part by dividing the total number of elements, N, by 4 using integer division (part_size = N / 4).

The first three parts will each contain part_size elements.

The fourth part will contain all the remaining elements. This means if N is not perfectly divisible by 4, the last part will be larger than the others.


If the array has fewer than four elements, some of the first three parts may be empty.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer N, the number of elements in the array.

The second line contains N space-separated integers, representing the elements of the array.


Output Format:
Print four lines, each representing one part of the split array.
The elements in each part should be separated by spaces.
If a part is empty, print a blank line.


Note:
This problem tests your understanding of integer arithmetic and array/slice manipulation. The core of the solution lies in correctly calculating the indices for slicing the original array.
Sample Input 1:
6
1 2 3 4 5 6


Sample Output 1:
1
2
3
4 5 6


Explanation for Sample 1:
Total elements N = 6.
Base part size = 6 / 4 = 1.
Part 1 gets 1 element: 1.
Part 2 gets 1 element: 2.
Part 3 gets 1 element: 3.
Part 4 gets the rest: 4 5 6.


Sample Input 2:
9
1 2 3 4 5 6 7 8 9


Sample Output 2:
1 2
3 4
5 6
7 8 9


Explanation for Sample 2:
Total elements N = 9.
Base part size = 9 / 4 = 2.
Part 1 gets 2 elements: 1 2.
Part 2 gets 2 elements: 3 4.
Part 3 gets 2 elements: 5 6.
Part 4 gets the rest: 7 8 9.


Expected Time Complexity:
The expected time complexity is O(N).


Constraints:
0 <= N <= 1000
-1000 <= array element <= 1000

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Array Quartering
Full screen
Console