Count non-overlapping Subarrays of size K with equal alternate elements
Last Updated :
28 Dec, 2022
Given an array arr[] of length N, the task is to find the count of non-overlapping subarrays of size K such that the alternate elements are equal.
Examples:
Input: arr[] = {2, 4, 2, 7}, K = 3
Output: 1
Explanation: Given subarray {2, 4, 2} is a valid array because the elements in even position(index no.0 and 2) are equal. Hence the count of the subarrays with size K = 3 is 1.
Input: arr[] = {5, 2, 5, 2, 3, 9, 7, 9, 7}, K = 4
Output: 2
Explanation: Subarray {5, 2, 5, 2} and {9, 7, 9, 7} are valid subarrays because the elements in even position(index no.0 and 2) and odd positions(index no.1 and 3) are equal. Hence the count of the subarrays with size K = 4 is 2.
Approach: Implement the idea below to solve the problem
Initially find all the non-overlapping subarrays where alternate elements are equal. Comparing the lengths of each such subarray with K, we can find out how many valid subarrays of size K can be extracted from each of those.
Follow the below steps to implement the idea:
- Initialize t = 2 in starting and c = 0, to maintain the count of subarrays.
- Iterate over the array from index 2.
- Whenever we found arr[i] = arr[i-2], increment t by 1.
- When t gets equal to K, increase c by 1 and reset t = 0.
- If the length of the given array arr[] is less than 2, see the value of K and return the output accordingly.
- After iterating over the array, return c as the count subarrays.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int fun( int arr[], int k, int n)
{
if (n <= 2) {
if (k == 1) {
return n;
}
else if (k == 2) {
return 1;
}
else {
return 0;
}
}
int t = 2;
int c = 0;
for ( int i = 2; i < n; i++)
{
if (arr[i] == arr[i - 2]) {
t = t + 1;
}
else {
t = 2;
}
if (t == k)
{
c = c + 1;
t = 0;
}
}
return c;
}
int main() {
int arr[] = { 5, 2, 5, 2, 5, 2, 5, 2, 3, 9, 7, 9, 7 };
int K = 4;
int n = sizeof (arr) / sizeof (arr[0]);
cout << fun(arr, K, n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int fun( int [] arr, int k)
{
if (arr.length <= 2 ) {
if (k == 1 ) {
return arr.length;
}
else if (k == 2 ) {
return 1 ;
}
else {
return 0 ;
}
}
int t = 2 ;
int c = 0 ;
for ( int i = 2 ; i < arr.length; i++)
{
if (arr[i] == arr[i - 2 ]) {
t = t + 1 ;
}
else {
t = 2 ;
}
if (t == k)
{
c = c + 1 ;
t = 0 ;
}
}
return c;
}
public static void main(String[] args)
{
int [] arr
= { 5 , 2 , 5 , 2 , 5 , 2 , 5 , 2 , 3 , 9 , 7 , 9 , 7 };
int K = 4 ;
System.out.print(fun(arr, K));
}
}
|
Python3
def fun(arr, k):
if len (arr) < = 2 :
if k = = 1 :
return len (arr)
elif k = = 2 :
return 1
else :
return 0
else :
t = 2
c = 0
for i in range ( 2 , len (arr)):
if arr[i] = = arr[i - 2 ]:
t = t + 1
else :
t = 2
if t = = k:
c + = 1
t = 0
return c
if __name__ = = '__main__' :
arr = [ 5 , 2 , 5 , 2 , 5 , 2 , 5 , 2 , 3 , 9 , 7 , 9 , 7 ]
K = 4
print (fun(arr, K))
|
C#
using System;
class GFG {
static int fun( int [] arr, int k)
{
if (arr.Length <= 2) {
if (k == 1) {
return arr.Length;
}
else if (k == 2) {
return 1;
}
else {
return 0;
}
}
int t = 2;
int c = 0;
for ( int i = 2; i < arr.Length; i++)
{
if (arr[i] == arr[i - 2]) {
t = t + 1;
}
else {
t = 2;
}
if (t == k)
{
c = c + 1;
t = 0;
}
}
return c;
}
static public void Main ()
{
int [] arr= { 5, 2, 5, 2, 5, 2, 5, 2, 3, 9, 7, 9, 7 };
int K = 4;
Console.Write(fun(arr, K));
}
}
|
Javascript
function fun(arr, k, n)
{
if (n <= 2) {
if (k == 1) {
return n;
}
else if (k == 2) {
return 1;
}
else {
return 0;
}
}
let t = 2;
let c = 0;
for (let i = 2; i < n; i++)
{
if (arr[i] == arr[i - 2]) {
t = t + 1;
}
else {
t = 2;
}
if (t == k)
{
c = c + 1;
t = 0;
}
}
return c;
}
let arr = [ 5, 2, 5, 2, 5, 2, 5, 2, 3, 9, 7, 9, 7 ];
let K = 4;
let n = arr.length;
console.log(fun(arr, K, n));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...