You are given a positive integer n. Each digit of n has a sign according to the following rules:

  • The most significant digit is assigned a positive sign.
  • Each other digit has an opposite sign to its adjacent digits.

Return the sum of all digits with their corresponding sign.

Example 1:

Input: n = 521 Output: 4 Explanation: (+5) + (-2) + (+1) = 4.

Example 2:

Input: n = 111 Output: 1 Explanation: (+1) + (-1) + (+1) = 1.

Example 3:

Input: n = 886996 Output: 0 Explanation: (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.

Constraints:

  • 1 <= n <= 109

.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}


Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 521
    integer :: i, j, k, l, m, n_digits
    character(len=1) :: digit
    logical :: negative

    ! Count the number of digits in n
    n_digits = 0
    do while (n /= 0)
        n_digits = n_digits + 1
        n = n / 10
    end do

    ! Print the sum of all digits with their corresponding sign
    write (*, '(A)') 'Sum of all digits with their corresponding sign:'
    do i = 1, n_digits
        ! Get the next digit from n
        digit = char(ichar('0') + mod(n, 10))
        n = n / 10

        ! Determine the sign of the current digit
        if (i == 1) then
            ! Most significant digit is always positive
            negative = .false.
        else
            ! Other digits have opposite signs as their adjacent digits
            negative = (mod(i, 2) == 0)
        end if

        ! Print the digit with its sign
        if (negative) then
            write (*, '(A)', advance='no') '-'
        else
            write (*, '(A)', advance='no') '+'
        end if
        write (*, '(A)', advance='no') digit
    end do
    write (*, *)

end program main
❌ Compiled
❌ Executed
❌ Correct
PROGRAM SUM_DIGITS
      IMPLICIT NONE

      INTEGER :: n, sum, digit, i
      CHARACTER(LEN=1) :: sign

      ! Read input
      READ(*,*) n

      ! Initialize sum and sign
      sum = 0
      sign = '+'

      ! Iterate over digits of n
      DO i = 1, LEN_TRIM(n)
         ! Get digit and sign
         digit = INT(n(i:i))
         IF (i == 1) THEN
            sign = '+'
         ELSE
            sign = '-'
         END IF

         ! Add digit to sum
         sum = sum + digit * sign
      END DO

      ! Output result
      WRITE(*,*) sum

      END PROGRAM SUM_DIGITS
🌐 Data from online sources
def sum_of_digits(n: int) -> int:
    total = 0
    positive = True

    while n > 0:
        digit = n % 10
        n //= 10

        if positive:
            total += digit
        else:
            total -= digit

        positive = not positive

    return total

The algorithm is simple and works as follows: 1. Initialize a variable, sum, to store the sum of the digits with their signs, and a boolean variable, positive, to determine whether the current digit should have a positive or negative sign. 2. Iterate through the digits of the input number n by repeatedly dividing it by 10 and checking if it's greater than 0. - Note that we process the digits from the least significant digit to the most significant digit. 3. At each iteration, get the least significant digit (digit) of n by taking the remainder of the division of n by 10. 4. If positive is true, add the digit to the sum; otherwise, subtract it from the sum. 5. Toggle the value of positive for the next iteration. 6. Return the final value of sum.

The algorithm has a time complexity of O(log(n)) because it processes each digit of the input number n.

🌐 Data from online sources
int sumOfDigits(int n) {
    int sum = 0;
    bool positive = true;

    while (n > 0) {
        int digit = n % 10;
        n /= 10;

        if (positive) {
            sum += digit;
        } else {
            sum -= digit;
        }

        positive = !positive;
    }

    return sum;
}

The algorithm is simple and works as follows: 1. Initialize a variable, sum, to store the sum of the digits with their signs, and a boolean variable, positive, to determine whether the current digit should have a positive or negative sign. 2. Iterate through the digits of the input number n by repeatedly dividing it by 10 and checking if it's greater than 0. - Note that we process the digits from the least significant digit to the most significant digit. 3. At each iteration, get the least significant digit (digit) of n by taking the remainder of the division of n by 10. 4. If positive is true, add the digit to the sum; otherwise, subtract it from the sum. 5. Toggle the value of positive for the next iteration. 6. Return the final value of sum.

The algorithm has a time complexity of O(log(n)) because it processes each digit of the input number n.