deepak.hebbar
BAN USERint powerTobeFound = 2147483648;
if ((powerTobeFound & (~powerTobeFound+1)) == powerTobeFound) {
NSLog(@"Power of TWO");
}
Objective c solution,
int x = 100;
__block int doublesCount = 0;
NSMutableArray *squaresArray = [NSMutableArray new];
for (int j = 1; j<sqrt(x); j++) {
[squaresArray addObject:@(j*j)];
}
[squaresArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
for (NSUInteger j = idx; j<squaresArray.count; j++) {
if (([obj integerValue] + [squaresArray[j] integerValue]) == x) {
doublesCount++;
}
}
}];
Objective C solution to this ,
NSArray *interArr1 = @[@1,@3,@6,@10];
NSArray *interArr2 = @[@2,@3,@5,@6];
__block NSMutableArray *interFinalArray = [NSMutableArray new];
[interArr1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([interArr2 containsObject:obj]) {
[interFinalArray addObject:obj];
}
}];
Objective C solution to this problem,
NSArray *threeTimesArray = @[@2,@3,@5,@1,@2,@2,@5,@3,@5,@3];
__block NSNumber *uniqueNumer ;
[threeTimesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableArray *remainingArray = [[NSMutableArray alloc]initWithArray:threeTimesArray];
[remainingArray removeObjectAtIndex:idx];
if (![remainingArray containsObject:obj]) {
uniqueNumer = obj;
}
}];
NSArray* input = @[@[@2, @5, @10],@[@25, @100, @105],@[@7, @56, @42]];
NSMutableArray *unsortedArray = [NSMutableArray new];
NSArray *sortedArray = [NSArray new];
[input enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray *currentArray = obj;
for (id object in currentArray) {
[unsortedArray addObject:object];
}
}];
sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSArray *givenArray1 = @[@"bag", @"bat", @"tab"];
NSMutableArray *reversedArray = [NSMutableArray new];
[givenArray1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *currentString = obj;
NSMutableString *reversedString = [NSMutableString new];
[currentString enumerateSubstringsInRange:NSMakeRange(0, currentString.length) options:NSStringEnumerationByComposedCharacterSequences|NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[reversedString appendString:substring];
}];
[reversedArray addObject:reversedString];
}];
NSString *foundValue = [givenArray1 firstObjectCommonWithArray:reversedArray];
if (foundValue) {
return YES;
}else{
return NO;
}
The objective C solution to this,
NSMutableString *finalValue = [NSMutableString new];
__block int count = 1;
__block NSString *currentCharStr = [[NSString alloc]initWithString:[inputString substringWithRange:NSMakeRange(0, 1)]];
[inputString enumerateSubstringsInRange:NSMakeRange(1, inputString.length-1) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if ([currentCharStr isEqualToString:substring]) {
count++;
}else{
[finalValue appendFormat:[NSString stringWithFormat:@"%@%d",currentCharStr,count]];
count = 1;
currentCharStr = substring;
}
if (substringRange.location==inputString.length-1) {
[finalValue appendFormat:[NSString stringWithFormat:@"%@%d",currentCharStr,count]];
}
}];
Objective C solution
- deepak.hebbar December 11, 2014