Created
November 2, 2016 07:40
-
-
Save donygeorgek/a2a33df714bbb3e4b5dd49c942e95fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(NSNumber*)numberOfPairs:(NSArray*) a joinArg2:(NSNumber*) k { | |
//No of Pairs | |
int nop=0; | |
int sumNumber =[k intValue]; | |
//Checking to see if the sum can be perfectly halved so as to avoid an issues with removing duplicates | |
if ((sumNumber % 2) == 0){ | |
//No of occurence of half of sum number to be tested | |
int noSameDivNo=0; | |
NSMutableArray *testArrayModified=[[NSMutableArray alloc] init]; | |
NSMutableArray *testArray=[[NSMutableArray alloc] initWithArray:a]; | |
for (int i=0; i<[testArray count]; i++) { | |
if (!([[testArray objectAtIndex:i] intValue]==sumNumber/2)) { | |
//Modifying the array | |
[testArrayModified addObject:[testArray objectAtIndex:i]]; | |
}else{ | |
noSameDivNo++; | |
} | |
} | |
//Removing multiple occurances of same pair | |
testArrayModified=[[NSMutableArray alloc] initWithArray:[[NSSet setWithArray:testArrayModified] allObjects]]; | |
for (int i=0; i<[testArrayModified count]; i++) { | |
for (int j=0; j<[testArrayModified count]; j++) { | |
if ((([[testArrayModified objectAtIndex:i] intValue])+([[testArrayModified objectAtIndex:j] intValue]))==sumNumber) { | |
nop++; | |
} | |
} | |
} | |
//Checking for multiple halves of sum number | |
if (noSameDivNo>=2) { | |
return [NSNumber numberWithInt:nop/2+1]; | |
}else{ | |
return [NSNumber numberWithInt:nop/2]; | |
} | |
}else{ | |
NSMutableArray *testArray=[[NSMutableArray alloc] initWithArray:[[NSSet setWithArray:a] allObjects]]; | |
for (int i=0; i<[testArray count]; i++) { | |
for (int j=0; j<[testArray count]; j++) { | |
if ((([[testArray objectAtIndex:i] intValue])+([[testArray objectAtIndex:j] intValue]))==sumNumber) { | |
nop++; | |
} | |
} | |
} | |
return [NSNumber numberWithInt:nop/2]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment