Given an array nums[ ] of size N, the task is to split the array into groups of size K using the following procedure:
- The first group consists of the first K elements of the array, the second group consists of the next K element of the Array, and so on. Each element can be a part of exactly one group.
- For the last group, if the array does not have K elements remaining, use 0 to complete the group.
Examples:
Input: nums[ ] = {1,2,3,4,5,6,7,8}, K = 4
Output: [[1, 2, 3, 4] [ 5, 6, 7, 8]]
Explanation:
The first 4 element [1, 2, 3, 4] form the first group.
The next 4 elements [ 5, 6, 7, 8] form the second group.
Since all groups can be completely filled by element from the array, no need to use 0.
Input: nums[ ] = {3,2,5,7,9,1,3,5,7}, K = 2
Output: [[3, 2] ,[5, 7], [9,1], [3, 5], [7, 0]]
Explanation: The last group was one short of being of size 2. So, one 0 is used.
Approach: This is an easy implementation related problem. Follow the steps mentioned below to solve the problem:
- Maintain a temp vector which represents each group in the string.
- If the index i+1 is divisible by K then it can be concluded that group has ended with this ith index.
- Push the temp into the ans vector if group ends.
- Check whether last group has a size K or not.
- If it is not equal then add K – (len+1) sized fill with 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector< int > > divideArray( int nums[],
int K, int N)
{
vector<vector< int > > ans;
vector< int > temp;
for ( int i = 0; i < N; i++) {
temp.push_back(nums[i]);
if (((i+1)%K)==0) {
ans.push_back(temp);
temp.clear();
}
}
if (!temp.empty()) {
int a = temp.size();
while (a != K) {
temp.push_back(0);
a++;
}
ans.push_back(temp);
}
return ans;
}
void printArray(vector<vector< int > >& a)
{
int n = a.size();
cout << n;
for ( int i = 0; i < n; i++) {
cout << "[ " ;
for ( int j = 0; j < a[i].size(); j++)
cout << a[i][j] << " " ;
cout << "]" ;
}
}
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = sizeof (nums) / sizeof (nums[0]);
int K = 4;
vector<vector< int > > ans
= divideArray(nums, K, N);
printArray(ans);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static ArrayList<ArrayList<Integer>> divideArray( int nums[], int K, int N) {
ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> temp = new ArrayList<Integer>();
for ( int i = 0 ; i < N; i++) {
temp.add(nums[i]);
if (((i + 1 ) % K) == 0 ) {
ans.add(temp);
temp = new ArrayList<Integer>();
}
}
if (temp.size() != 0 ) {
int a = temp.size();
while (a != K) {
temp.add( 0 );
a++;
}
ans.add(temp);
}
return ans;
}
static void printArray(ArrayList<ArrayList<Integer>> a) {
int n = a.size();
for ( int i = 0 ; i < n; i++) {
System.out.print( "[ " );
for ( int j = 0 ; j < a.get(i).size(); j++)
System.out.print(a.get(i).get(j) + " " );
System.out.print( "]" );
}
}
public static void main(String args[]) {
int nums[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };
int N = nums.length;
int K = 4 ;
ArrayList<ArrayList<Integer>> ans = divideArray(nums, K, N);
printArray(ans);
}
}
|
Python3
def divideArray(nums, K, N):
ans = []
temp = []
for i in range ( 0 , N):
temp.append(nums[i])
if (((i + 1 ) % K) = = 0 ):
ans.append(temp.copy())
temp.clear()
if ( len (temp) ! = 0 ):
a = len (temp)
while (a ! = K):
temp.append( 0 )
a + = 1
ans.append(temp)
return ans
def printArray(a):
n = len (a)
for i in range ( 0 , n):
print ( "[ " , end = "")
for j in range ( 0 , len (a[i])):
print (a[i][j], end = " " )
print ( "]" , end = "")
if __name__ = = "__main__" :
nums = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]
N = len (nums)
K = 4
ans = divideArray(nums, K, N)
printArray(ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List<List< int > > divideArray( int [] nums, int K,
int N)
{
List<List< int > > ans = new List<List< int > >();
;
List< int > temp = new List< int >();
for ( int i = 0; i < N; i++) {
temp.Add(nums[i]);
if (((i + 1) % K) == 0) {
ans.Add(temp);
temp = new List< int >();
}
}
if (temp.Count != 0) {
int a = temp.Count;
while (a != K) {
temp.Add(0);
a++;
}
ans.Add(temp);
}
return ans;
}
static void printArray(List<List< int > > a)
{
int n = a.Count;
for ( int i = 0; i < n; i++) {
Console.Write( "[ " );
for ( int j = 0; j < a[i].Count; j++)
Console.Write(a[i][j] + " " );
Console.Write( "]" );
}
}
public static int Main()
{
int [] nums = new int [] { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = nums.Length;
int K = 4;
List<List< int > > ans = divideArray(nums, K, N);
printArray(ans);
return 0;
}
}
|
Javascript
<script>
function divideArray(nums, K, N) {
let ans = [];
let temp = [];
for (let i = 0; i < N; i++) {
temp.push(nums[i]);
if (((i + 1) % K) == 0) {
ans.push(temp);
temp = [];
}
}
if (temp.length != 0) {
let a = temp.length;
while (a != K) {
temp.push(0);
a++;
}
ans.push(temp);
}
return ans;
}
function printArray(a) {
let n = a.length;
for (let i = 0; i < n; i++) {
document.write( "[ " );
for (let j = 0; j < a[i].length; j++)
document.write(a[i][j] + " " );
document.write( "]" );
}
}
let nums = [1, 2, 3, 4, 5, 6, 7, 8];
let N = nums.length;
let K = 4;
let ans = divideArray(nums, K, N);
printArray(ans);
</script>
|
Output
[ 1 2 3 4 ][ 5 6 7 8 ]
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Feb, 2022
Like Article
Save Article