Open In App

Sum of i * countDigits(i)^countDigits(i) for all i in range [L, R]

Last Updated : 24 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers L, R which signify a range [L, R], the task is to find the sum i * countDigits(i)countDigits(i) for all i ? [L, R] where countDigits(i) is the count of digits in i.
Examples: 
 

Input: L = 8, R = 11 
Output: 101 
Explanation: 
For the given numbers, we need to find the values for all the numbers in the range [L, R]. 
=> 8 * 11 + 9 * 11 + 10 * 22 + 11 * 22 
=> 8 + 9 + 40 + 44 
=> 101
Input: L = 98, R = 102 
Output: 8969 
Explanation: 
For the given numbers, we need to find the values for all the numbers in the range [L, R]. 
=> 98 * 22 + 99 * 22 + 100 * 33 + 101 * 33 + 102 * 33 
=> 8969 
 

 

Approach: The idea is to break the range into the segments according to the number of digits. That is, every segment contains numbers with same number of digits: 
 

[1 - 9], [10 - 99], [100 - 999], [1000 - 9999], [10000 - 99999] ...

From the above segments, clearly, both [L, R] of each segment have the same lengths. Therefore, the required sum for the segment is: 
 

countDigits(L)countDigits(L) * (L + R) * (R - L + 1) / 2

Proof: 
 

  • Let [L, R] = [10, 14] where L and R are of the same length i.e. 2.
  • Therefore, the sum for the segment [L, R] will be: 
     
10 * 22 + 11 * 22 + 12 * 22 + 13 * 22 + 14 * 22
  •  
  • On taking 22 common: 
     
22 * (10 + 11 + 12 + 13 + 14) 
=> totalDigitstotalDigits * (Sum of AP)
  • Sum of AP = (no of terms / 2) * (first term + last term) i.e. (R – L + 1) * (L + R) / 2.
  • Therefore, the required sum is: 
     
countDigits(L)countDigits(L) * (L + R) * (R - L + 1) / 2

Below is the implementation of the above approach: 
 

C++




// C++ program to find the required sum
 
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
 
// Function to return the required sum
int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
 
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = max(l, a);
        int R = min(r, b);
 
        // If the range is valid
        if (L <= R) {
 
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += pow(i, i) * (sum % MOD);
            res %= MOD;
        }
 
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
 
// Driver code
int main()
{
    int l = 98, r = 102;
    cout << rangeSum(l, r);
    return 0;
}


Java




// Java program to find the required sum
import java.util.*;
 
class GFG{
static final int MOD = 1000000007;
  
// Function to return the required sum
static int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
  
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = Math.max(l, a);
        int R = Math.min(r, b);
  
        // If the range is valid
        if (L <= R) {
  
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += Math.pow(i, i) * (sum % MOD);
            res %= MOD;
        }
  
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
  
// Driver code
public static void main(String[] args)
{
    int l = 98, r = 102;
    System.out.print(rangeSum(l, r));
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python 3 program to find the required sum
  
MOD = 1000000007
  
# Function to return the required sum
def rangeSum(l, r):
 
    a = 1
    b = 9
    res = 0
  
    # Iterating for all the number
    # of digits from 1 to 10
    for i in range(1, 11):
        L = max(l, a)
        R = min(r, b)
  
        # If the range is valid
        if (L <= R):
  
            # Sum of AP
            sum = (L + R) * (R - L + 1) // 2
            res += pow(i, i) * (sum % MOD)
            res %= MOD
  
        # Computing the next minimum and maximum
        # numbers by for the (i+1)-th digit
        a = a * 10
        b = b * 10 + 9
    return res
  
# Driver code
if __name__ == "__main__":
     
    l = 98
    r = 102
    print(rangeSum(l, r))
     
# This code is contributed by chitranayal


C#




// C# program to find the required sum
using System;
 
class GFG{
static readonly int MOD = 1000000007;
   
// Function to return the required sum
static int rangeSum(int l, int r)
{
    int a = 1, b = 9, res = 0;
   
    // Iterating for all the number
    // of digits from 1 to 10
    for (int i = 1; i <= 10; i++) {
        int L = Math.Max(l, a);
        int R = Math.Min(r, b);
   
        // If the range is valid
        if (L <= R) {
   
            // Sum of AP
            int sum = (L + R) * (R - L + 1) / 2;
            res += (int)Math.Pow(i, i) * (sum % MOD);
            res %= MOD;
        }
   
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
   
// Driver code
public static void Main(String[] args)
{
    int l = 98, r = 102;
    Console.Write(rangeSum(l, r));
}
}
  
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// Javascript program to find the required sum
 
MOD=1000000007
 
// Function to return the required sum
function rangeSum(l, r)
{
    var a = 1, b = 9, res = 0;
 
    // Iterating for all the number
    // of digits from 1 to 10
    for (var i = 1; i <= 10; i++) {
        var L = Math.max(l, a);
        var R = Math.min(r, b);
 
        // If the range is valid
        if (L <= R) {
 
            // Sum of AP
            var sum = (L + R) * (R - L + 1) / 2;
            res += Math.pow(i, i) * (sum % MOD);
            res %= MOD;
        }
 
        // Computing the next minimum and maximum
        // numbers by for the (i+1)-th digit
        a = a * 10;
        b = b * 10 + 9;
    }
    return res;
}
 
// Driver code
var l = 98, r = 102;
document.write( rangeSum(l, r));
 
// This code is contributed by noob2000.
</script>


Output: 

8969

 

Time Complexity: O(10)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads