Problem Description :
#import <Foundation/Foundation.h>
int leftArraySum(NSMutableArray *A,int n)
{
NSInteger sum = 0;
for(int i=0;i<n;i++)
{
sum += [[A objectAtIndex:i] integerValue];
}
return sum;
}
int reightArraySum(NSMutableArray *A,int n)
{
NSInteger sum = 0;
for(int i=n+1;i<[A count];i++)
{
sum += [[A objectAtIndex:i] integerValue];
}
return sum;
}
int topArraySum(NSMutableArray *A,int n)
{
NSInteger sum = 0;
for(int i=0;i<n;i++)
{
sum += [[A objectAtIndex:i] integerValue];
}
return sum;
}
int bottomArraySum(NSMutableArray *A,int n)
{
NSInteger sum = 0;
for(int i=n+1;i<[A count];i++)
{
sum += [[A objectAtIndex:i] integerValue];
}
return sum;
}
int solution(NSMutableArray *A) {
// write your code in Objective-C 2.0
NSMutableArray *rowArr = [A objectAtIndex:0];
int colCount = [rowArr count];
int rowCount = [A count];
NSMutableArray *arrRowSum = [[NSMutableArray alloc] initWithCapacity: rowCount];
NSMutableArray *arrColSum = [[NSMutableArray alloc] initWithCapacity: colCount];
for(int i=0;i<rowCount;i++)
{
NSArray *colArr = [A objectAtIndex:i];
NSNumber * sum = [colArr valueForKeyPath:@"@sum.self"];
[arrRowSum insertObject:sum atIndex:i];
}
for(int i=0;i<colCount;i++)
{
NSInteger sum = 0;
for(int j=0;j<rowCount;j++)
{
sum += [[[A objectAtIndex:j] objectAtIndex:i] integerValue];
}
NSNumber *sumVal = [NSNumber numberWithInt:sum];
[arrColSum insertObject:sumVal atIndex:i];
}
int result = 0;
for (int i=1;i<rowCount;i++)
{
int left = leftArraySum(arrRowSum,i);
int right = reightArraySum(A,i);
if(left == right)
{
for(int j=0;j<colCount;j++)
{
int top = topArraySum(A,j);
int bottom = bottomArraySum(A,j);
if(top == bottom )
{
result = i+j;
break;
}
}
}
}
return result;
}