1

I have been trying the question on hacker rank in which addition of all the substring of a string is taken into account. In doing so our answer exceeds the limit of NSInteger, NSUInteger, long, very very long, double.

So in this case i tried

- (NSDecimalNumber *) substrings:(NSString *)n {

    NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    if ([n rangeOfCharacterFromSet:notDigits].location == NSNotFound) {

        NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
        f.numberStyle = NSNumberFormatterNoStyle;
        NSDecimalNumber *sum = [NSDecimalNumber decimalNumberWithString:@"0"];
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i<n.length; i++) {

            for (NSInteger j = 1; j<=n.length-i; j++) {

                NSString *stringInRange = [n substringWithRange:NSMakeRange(i, j)];
                [arr addObject:stringInRange];
                NSDecimalNumber *myNumber1 = [NSDecimalNumber decimalNumberWithString:stringInRange];
                sum = [sum decimalNumberByAdding:myNumber1];
            }
        }
        return sum;
    } else {

        return nil;
    }
}

But still, I am getting the wrong answer of addition. I want to know in such cases where test cases include much heavier value than the limits, what API should we use to perform arithmetic operation?

What I have tried is a correct approach or not?

Himan Dhawan
  • 894
  • 5
  • 23
  • Think you might have to do your calculation in String by the character array, the return also have to be in String – Tj3n Aug 15 '18 at 07:57
  • How can we do arithmetic with string? – Himan Dhawan Aug 15 '18 at 07:57
  • Back to your elementary calculation skill, plus each from last character and if it exceed 10, add 1 to the prior character? That way you can do calculation with very very long number string also – Tj3n Aug 15 '18 at 09:20

1 Answers1

0

If you need to operate with big integer values in Objective-C you can use JKBigInteger. For example, you can multiply 2 big integers and convert result to NSString with JKBigInteger. Here you can see how to use this library:

JKBigInteger *bigInteger1 = [[JKBigInteger alloc] initWithString:@"2"];
JKBigInteger *bigInteger2 = [[JKBigInteger alloc] initWithString:@"3"];
JKBigInteger* bigInteger3 = [bigInteger1 add:bigInteger2];
NSLog(@"%@", [bigInteger3 stringValue]); // output is 5

And I guess you can get values that you need with this function:

-(JKBigInteger*)sumOfSubValues:(JKBigInteger*)value {
    JKBigInteger* currentValue = [[JKBigInteger alloc] initWithString:value.stringValue];
    JKBigInteger* result = [[JKBigInteger alloc] initWithString:currentValue.stringValue];
    NSUInteger digitsCount = currentValue.stringValue.length - 1;
    JKBigInteger* const tenValue = [[JKBigInteger alloc] initWithString:@"10"];
    while (digitsCount != 0) {
        JKBigInteger* const firstDigit = [[JKBigInteger alloc] initWithString:[currentValue.stringValue substringToIndex:1]];
        JKBigInteger* const firstDigitPowerBase = [tenValue pow:(unsigned int)digitsCount];
        JKBigInteger* const firstDigitPower = [firstDigit multiply:firstDigitPowerBase];
        JKBigInteger* const diff = [currentValue subtract:firstDigitPower];
        currentValue = diff;
        digitsCount--;
        result = [result add:currentValue];
    }
    return result;
}

You can test it with this code snippet:

JKBigInteger* result = [self sumOfSubValues:[[JKBigInteger alloc] initWithString:@"1234"]];
NSLog(@"%@", result.stringValue); // output is "1506" (1234 + 234 + 34 + 4)
Roman Podymov
  • 4,168
  • 4
  • 30
  • 57
  • I don't want to use any library , I want to use native API's to achieve this. – Himan Dhawan Aug 16 '18 at 06:26
  • @HimanDhawan Could you please provide an example of number that you want to handle with your code? – Roman Podymov Aug 16 '18 at 07:25
  • @HimanDhawan In such case you need to use custom solution. JKBigInteger is a stable library and provides all functions that you need. What do you want to do with such big integer number? Multiply? Divide? – Roman Podymov Aug 16 '18 at 07:38
  • I have to submit my solution online , so I cannot add library to code. There test cases include a number of 101 digits. – Himan Dhawan Aug 16 '18 at 07:40
  • @HimanDhawan But what do you want to do with such big integer numbers? Just print it with NSLog? – Roman Podymov Aug 16 '18 at 07:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178112/discussion-between-roman-podymov-and-himan-dhawan). – Roman Podymov Aug 16 '18 at 07:43