Memory limit exceeding in sub array problem
I'm trying to solve a problem, but I'm exceeding the memory limit.
Problem Description
Given an integer array A and two integers B and C.
You need to find the number of subarrays in which the number of occurrences of B is equal to number of occurrences of C.
NOTE: Don't count empty subarrays.
Input Format First argument is an integer array A.
Second argument is an integer B.
Third argument is an integer C.
Output Format Return an integer denoting the number of subarrays in which the number of occurrences of B is equal to number of occurrences of C.
/**
* @input A : Integer array
* @input n1 : Integer array's ( A ) length
* @input B : Integer
* @input C : Integer
*
* @Output Integer
*/
int SubArrays(int* arr, int start, int end, int size, int B, int C)
{
int countB = 0,countC = 0;
// Stop if we have reached the end of the array
if (end == size)
return 0;
// Increment the end point and start from 0
else if (start > end)
SubArrays(arr, 0, end + 1, size, B, C);
// Print the subarray and increment the starting point
else {
int A[end-start+1];
int i, j;
for ( i = start; i < end; i++){
for(j =0; j<(end-start+1);j++){
A[j] = arr[i];
}
}
for(j =0; j<(end-start+1);j++){
if(A[j]==B) countB ++;
if(A[j]==C) countC ++;
}
// cout << arr[end] << "]" << endl;
SubArrays(arr, start + 1, end, size, B, C);
}
if(countB == countC)
return 1;
else
return 0;
}
int solve(int* A, int n1, int B, int C) {
int count = 0;
count = count + SubArrays(A,0,0,n1,B,C);
return count;
}
Comments
Post a Comment