diff --git a/Data Structure Algo/C++/sam_and_subtrings.cpp.txt b/Data Structure Algo/C++/sam_and_subtrings.cpp.txt new file mode 100644 index 00000000..a8a44a26 --- /dev/null +++ b/Data Structure Algo/C++/sam_and_subtrings.cpp.txt @@ -0,0 +1,44 @@ +/** Sam and substrings + https://www.hackerrank.com/challenges/sam-and-substrings/problem +*/ + +#include + +using namespace std; + +/* + * Complete the 'substrings' function below. + * + * The function is expected to return an INTEGER. + * The function accepts STRING n as parameter. + */ + +long long substrings(string n) { + long long sum = 0; + long curelem; + long mod = 1000000007; + long long ones = 1; + for (long i = n.size () -1 ; i >=0; --i) { + curelem = n[i] - '0'; + sum += ((curelem * (i + 1) * ones)% 1000000007) ; + ones = ((ones * 10) + 1) % mod; + sum %= 1000000007; + } + return sum; +} + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + string n; + getline(cin, n); + + long long result = substrings(n); + + fout << result << "\n"; + + fout.close(); + + return 0; +} \ No newline at end of file