Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Sam and substrings problem solution

YASH PAL, 31 July 202425 January 2026

In this HackerRank Sam and substrings problem solution, Samantha and Sam are playing a numbers game. Given a number as a string, no leading zeros, determine the sum of all integer values of substrings of the string.

Given an integer as a string, sum all of its substrings cast as integers. As the number may become large, return the value modulo 109 + 7.

Function Description

Complete the substrings function in the editor below.

substrings has the following parameter(s):

  • string n: the string representation of an integer
HackerRank Sam and substrings problem solution

HackerRank Sam and substrings problem solution in Python.

def solution(n):
    s = 0
    prev_sum = 0
    
    for i, d in enumerate(n):
        s_ = prev_sum * 10 + (i + 1) * int(d)
        s += s_
        prev_sum = s_
    return s % (10 ** 9 + 7)

n = input()

print(solution(n))


Sam and substrings problem solution in Java.

import java.io.*;

public class Solution {
    
    private final static int MOD = 1000000007;
    
    public static void main(String[] args) throws IOException {
        
        int[] balls = strNumToArr((new BufferedReader(new InputStreamReader(System.in))).readLine());
        
        int n = balls.length;
        for(int i = n - 2; i >= 0; --i){
            balls[i] = (int)((balls[i+1] + (((long)balls[i])*(n - i))%MOD)%MOD);
        }
        
        int pow = 1;
        int total = 0;
        for(int i = 0; i < n; ++i){
            total = (int)((total + (((long)balls[i])*pow)%MOD)%MOD);
            pow = (int)((pow*10L)%MOD);
        }
        
        System.out.print(total);
    }
    
    private static int[] strNumToArr(String str){
        int n = str.length();
        int[] ar = new int[n];
        for(char c : str.toCharArray()){
            ar[--n] = c - '0';
        }
        return ar;
    }    
}


Problem solution in C++.

#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;

int main() {
    string s;
    cin >> s;
    long long pw[s.length() + 1];
    pw[0] = 1;
    for (int i = 1; i <= (int)s.length(); ++i) {
        pw[i] = pw[i - 1] * 10 % mod;
    }
    long long sum[s.length() + 1];
    sum[0] = pw[0];
    for (int i = 1; i <= (int)s.length(); ++i) {
        sum[i] = sum[i - 1] + pw[i];
        sum[i] %= mod;
    }
    long long ans = 0;
    int n = s.length();
    for (int i = 0; i < n; ++i) {
        ans += (s[i] - '0') * (i + 1) * sum[n - 1 - i];
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}


Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MOD 1000000007
#define MAX_SIZE 200000
int main() {

    unsigned long long one = 1;
    unsigned long long sum = 0;
    int len = 0;
    unsigned char input[MAX_SIZE];
    memset(input, 0, MAX_SIZE);
    scanf("%s", input);
    len = strlen(input);
    for(int i = len-1; i >= 0; i--)
    {
        sum = ( sum + (input[i] - '0') * (i + 1) * one ) % MOD;
        one = (one * 10 + 1) % MOD;
    }
    printf("%llu", sum);
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes