[Code] Intelligent Girl Problem

July 27, 2015


Today, when we were having Tea Break in lunch, my collegue just told me about a website named hackerearth. And I found it there are a lot of funs working with arglorythm and solving problems.

Of course, I registered immediately and tried out a Easy problems, it’s name Intelligent Girl, and I have done with a solution using objective-C. Here is it:

Soumika has a string S and its starting index is 1. The string S consists of characters from 1-9. As she is very intelligent, she wants to test his brother Vinay Tendulkar. She asked her brother Vinay Tendulkar to count the number of even numbered characters ( i.e 2,4,6,8 ) for every index i (1<=i<= S ). For an index i, the result should be calculated from i to the end of the string. As Vinay doesn’t know about programming, he wants you to help him find the solution.

Sample: Input is 574674546476 so the output must be 7 7 7 6 5 5 4 4 3 2 1 1.

Here is my code:

// Intelligent Girl
    NSString *finalString = [[NSString alloc] init];
    for (int i=0; i < [inputString length]; i++) {
        NSRange aRange = NSMakeRange(i, [inputString length]-i);
        NSString *usingString = [inputString substringWithRange:aRange];
        NSMutableArray *stringArray = [NSMutableArray array];
        for (int j = 0; j < [usingString length]; j++) {
            NSRange charRange = NSMakeRange(j, 1);
            NSString *charString = [usingString substringWithRange:charRange];
            [stringArray addObject:charString];
        // count number of even number
        int evenNumberCount = 0;
        for (NSString *charString in stringArray) {
            if ([charString intValue] % 2==0) {
        finalString = [finalString stringByAppendingString:[NSString stringWithFormat:@" %d", evenNumberCount]];
    return finalString;

and implement:

    [self resultWithString:@"574674546476"];

Hope you enjoy the post and found some funs!