Smallest subarray which upon repetition gives the original array
Last Updated :
11 Jul, 2022
Given an array arr[] of N integers, the task is to find the smallest subarray brr[] of size at least 2 such that by performing repeating operation on the array brr[] gives the original array arr[]. Print “-1” if it is not possible to find such a subarray.
A repeating operation on an array is to append all the current element of the array to the same array again.
For Example, if an array arr[] = {1, 2} then on repeating operation array becomes {1, 2, 1, 2}.
Examples:
Input: arr[] = {1, 2, 3, 3, 1, 2, 3, 3}
Output: {1, 2, 3, 3}
Explanation:
{1, 2, 3, 3} is the smallest subarray which when repeated 2 times gives the original array {1, 2, 3, 3, 1, 2, 3, 3}
Input: arr[] = {1, 1, 6, 1, 1, 7}
Output: -1
Explanation:
There doesn’t exist any subarray.
Naive Approach: The idea is to generate all possible subarrays of length at least 2 and check whether repeating those subarrays gives the original array or not.
Time Complexity: O(N3)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by observing the fact that the resultant subarray brr[] must start from the 1st index of the original array to generate arr[] on repeat. Therefore, generate only those subarrays which start from the 1st index and have a length of at least 2 and check whether repeating those subarrays gives the original array or not. Below are the steps:
- Create an auxiliary array brr[] and insert the first two elements of the original array into it as the resulting array must be of at least two in size.
- Traverse over the possible length of subarray [2, N/2 + 1] and check if the array brr[] of length i on repeating gives the original array arr[] or not.
- If yes then print this subarray and break the loop.
- Otherwise, insert the current element into the subarray and check again.
- Repeat the above steps until all the subarrays are checked.
- Print “-1” if the array brr[] is not found.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
void printArray(vector< int >& brr)
{
for ( auto & it : brr) {
cout << it << ' ' ;
}
}
void RepeatingSubarray( int arr[], int N)
{
if (N < 2) {
cout << "-1" ;
}
vector< int > brr;
brr.push_back(arr[0]);
brr.push_back(arr[1]);
for ( int i = 2; i < N / 2 + 1; i++) {
if (N % i == 0) {
bool a = false ;
int n = brr.size();
int j = i;
while (j < N) {
int K = j % i;
if (arr[j] == brr[K]) {
j++;
}
else {
a = true ;
break ;
}
}
if (!a && j == N) {
printArray(brr);
return ;
}
}
brr.push_back(arr[i]);
}
cout << "-1" ;
return ;
}
int main()
{
int arr[] = { 1, 2, 2, 1, 2,
2, 1, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
RepeatingSubarray(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printArray(Vector<Integer> brr)
{
for ( int it : brr)
{
System.out.print(it + " " );
}
}
static void RepeatingSubarray( int arr[], int N)
{
if (N < 2 )
{
System.out.print( "-1" );
}
Vector<Integer> brr = new Vector<Integer>();
brr.add(arr[ 0 ]);
brr.add(arr[ 1 ]);
for ( int i = 2 ; i < N / 2 + 1 ; i++)
{
if (N % i == 0 )
{
boolean a = false ;
int n = brr.size();
int j = i;
while (j < N)
{
int K = j % i;
if (arr[j] == brr.get(K))
{
j++;
}
else
{
a = true ;
break ;
}
}
if (!a && j == N)
{
printArray(brr);
return ;
}
}
brr.add(arr[i]);
}
System.out.print( "-1" );
return ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 1 , 2 ,
2 , 1 , 2 , 2 };
int N = arr.length;
RepeatingSubarray(arr, N);
}
}
|
Python3
def printArray(brr):
for it in brr:
print (it, end = ' ' )
def RepeatingSubarray(arr, N):
if (N < 2 ):
print ( "-1" )
brr = []
brr.append(arr[ 0 ])
brr.append(arr[ 1 ])
for i in range ( 2 , N / / 2 + 1 ):
if (N % i = = 0 ):
a = False
n = len (brr)
j = i
while (j < N):
K = j % i
if (arr[j] = = brr[K]):
j + = 1
else :
a = True
break
if ( not a and j = = N):
printArray(brr)
return
brr.append(arr[i])
print ( "-1" )
return
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 1 , 2 ,
2 , 1 , 2 , 2 ]
N = len (arr)
RepeatingSubarray(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printArray(List< int > brr)
{
foreach ( int it in brr)
{
Console.Write(it + " " );
}
}
static void RepeatingSubarray( int []arr, int N)
{
if (N < 2)
{
Console.Write( "-1" );
}
List< int > brr = new List< int >();
brr.Add(arr[0]);
brr.Add(arr[1]);
for ( int i = 2; i < N / 2 + 1; i++)
{
if (N % i == 0)
{
bool a = false ;
int n = brr.Count;
int j = i;
while (j < N)
{
int K = j % i;
if (arr[j] == brr[K])
{
j++;
}
else
{
a = true ;
break ;
}
}
if (!a && j == N)
{
printArray(brr);
return ;
}
}
brr.Add(arr[i]);
}
Console.Write( "-1" );
return ;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 2, 1,
2, 2, 1, 2, 2};
int N = arr.Length;
RepeatingSubarray(arr, N);
}
}
|
Javascript
<script>
function printArray(brr)
{
for (let it of brr) {
document.write(it + ' ' );
}
}
function RepeatingSubarray(arr, N)
{
if (N < 2) {
document.write( "-1" );
}
let brr = [];
brr.push(arr[0]);
brr.push(arr[1]);
for (let i = 2; i < Math.floor(N / 2) + 1; i++) {
if (N % i == 0) {
let a = false ;
let n = brr.length;
let j = i;
while (j < N) {
let K = j % i;
if (arr[j] == brr[K]) {
j++;
}
else {
a = true ;
break ;
}
}
if (!a && j == N) {
printArray(brr);
return ;
}
}
brr.push(arr[i]);
}
document.write( "-1" );
return ;
}
let arr = [ 1, 2, 2, 1, 2,
2, 1, 2, 2 ];
let N = arr.length;
RepeatingSubarray(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...