abhinavvohra1982
BAN USERHow about this? The below piece of code has the input Arrays 'a' and 'b'.
static void Main(string[] args)
{
int x = 0;
int y = 0;
int[] a = { 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1};
int[] b = { 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1};
int sum_of_Array_A = 0;
int sum_of_Array_B = 0;
int n = 0;
int i = 0;
int j = 0;
n = a.Length;
for (x = 0; x < n; x++)
{
for (y = x; y < n; y++)
{
sum_of_Array_A = sum_of_Array_A + a[y];
sum_of_Array_B = sum_of_Array_B + b[y];
if (sum_of_Array_A == sum_of_Array_B)
{
if (y - x > j - i)
{
i = x;
j = y;
}
}
}
sum_of_Array_A = 0;
sum_of_Array_B = 0;
}
Console.WriteLine("The maximum length of the sub-array is {0} and the indices are {1}, {2}", j - i, i, j);
}
I have tested the below code with the input arrays commented in the code.
- abhinavvohra1982 August 14, 2013