Counting Subarrays with elements repeated twice after swapping
Last Updated :
02 Aug, 2023
Given an array A[] of N integers, the task is to find the number of subarrays of A[], which is the repetition of its’ elements twice, after having swapped the inside elements of this subarray any number of times.
Note: Each element of the array is between 0 and 9 (inclusive of both).
Examples:
Input: N = 8, A[] = {2, 0, 2, 3, 0, 3, 2, 2}
Output: 4
Explanation: First subarray -> {2, 0, 2, 3, 0, 3}.
In this subarray, swap indices 3 and 4. We get {2, 0, 3, 2, 0, 3} which is the repetition of {2, 0, 3} twice.
Second subarray -> {2, 0, 2, 3, 0, 3, 2, 2}.
Swap the following pairs of indices :
2 and 3 -> {2, 2, 0, 3, 0, 3, 2, 2}
5 and 7 -> {2, 2, 0, 3, 2, 3, 0, 2}
6 and 8 -> {2, 2, 0, 3, 2, 2, 0, 3}
The array becomes a repetition of {2, 2, 0, 3}.
Similarly, the rest of the subarrays are {0, 2, 3, 0, 3, 2} and {2, 2}.
Input: N = 15, A[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7}
Output: 29
Approach: To solve the problem follow the below idea:
This problem is observation-based. Subarray from index i+1 to j can be made a repetition of the same array twice if and only if, X{i} is equal to X{j} (for all num=0 to 9). This is because the parity of each digit at ith and jth indices are same. So, each digit will occur an even number of times from the i+1 to the jth index.
Below are the steps that were to follow the above approach:
- Initialize a map (say “mp”) of the key vector of int and value of type int. This map “mp” stores the number of times the parity of each integer (from 0 to 9) becomes the same.
- Also, initialize a vector of integers, say “cnt”. It stores the number of occurrences of each integer (from 0 to 9) modulo 2, till the current index.
- Iterate through the array A[] and update “mp” and “cnt” accordingly.
- Initialize a variable “ans” with 0 to store the answer to the required problem.
- Iterate through the map and add the temp*(temp-1)/2 to the answer at each iteration, where the temp is the value of each key of the map.
- Return the final answer.
Below is the code to implement the above steps:
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int noOfSubarrays( int N, int A[])
{
map<vector< int >, int > mp;
vector< int > cnt(10, 0);
mp[cnt]++;
for ( int i = 0; i < N; i++) {
cnt[A[i]]++;
cnt[A[i]] %= 2;
mp[cnt]++;
}
int ans = 0;
for ( auto it : mp) {
int temp = it.second;
ans += (temp * (temp - 1)) / 2;
}
return ans;
}
int32_t main()
{
int N = 8;
int A[] = { 2, 0, 2, 3, 0, 3, 2, 2 };
int answer = noOfSubarrays(N, A);
cout << answer << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int noOfSubarrays( int N, int [] A)
{
Map<String, Integer> mp = new HashMap<>();
int [] cnt = new int [ 10 ];
Arrays.fill(cnt, 0 );
mp.put(Arrays.toString(cnt), 1 );
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
cnt[A[i]]++;
cnt[A[i]] %= 2 ;
String cntStr = Arrays.toString(cnt);
ans += mp.getOrDefault(cntStr, 0 );
mp.put(cntStr, mp.getOrDefault(cntStr, 0 ) + 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 8 ;
int [] A = { 2 , 0 , 2 , 3 , 0 , 3 , 2 , 2 };
int answer = noOfSubarrays(N, A);
System.out.println(answer);
}
}
|
Python3
from collections import defaultdict
def noOfSubarrays(N, A):
mp = defaultdict( int )
cnt = [ 0 ] * 10
mp[ tuple (cnt)] + = 1
for i in range (N):
cnt[A[i]] + = 1
cnt[A[i]] % = 2
mp[ tuple (cnt)] + = 1
ans = 0
for it in mp:
temp = mp[it]
ans + = (temp * (temp - 1 )) / / 2
return ans
if __name__ = = '__main__' :
N = 8
A = [ 2 , 0 , 2 , 3 , 0 , 3 , 2 , 2 ]
answer = noOfSubarrays(N, A)
print (answer)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int NoOfSubarrays( int N, int [] A)
{
Dictionary< string , int > mp
= new Dictionary< string , int >();
int [] cnt = new int [10];
Array.Fill(cnt, 0);
mp[ArrayToString(cnt)] = 1;
int ans = 0;
for ( int i = 0; i < N; i++) {
cnt[A[i]]++;
cnt[A[i]] %= 2;
string cntStr = ArrayToString(cnt);
ans += mp.GetValueOrDefault(cntStr, 0);
mp[cntStr]
= mp.GetValueOrDefault(cntStr, 0) + 1;
}
return ans;
}
public static string ArrayToString( int [] arr)
{
return "[" + string .Join( "," , arr) + "]" ;
}
public static void Main( string [] args)
{
int N = 8;
int [] A = { 2, 0, 2, 3, 0, 3, 2, 2 };
int answer = NoOfSubarrays(N, A);
Console.WriteLine(answer);
}
}
|
Javascript
function noOfSubarrays(N, A) {
const mp = new Map();
const cnt = new Array(10).fill(0);
mp.set(cnt.join( ',' ), 1);
let ans = 0;
for (let i = 0; i < N; i++) {
cnt[A[i]]++;
cnt[A[i]] %= 2;
const cntStr = cnt.join( ',' );
ans += mp.get(cntStr) || 0;
mp.set(cntStr, (mp.get(cntStr) || 0) + 1);
}
return ans;
}
const N = 8;
const A = [2, 0, 2, 3, 0, 3, 2, 2];
const answer = noOfSubarrays(N, A);
console.log(answer);
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...