Related Articles

# Check if a numeric string can be split into substrings having difference between consecutive numbers equal to K

• Difficulty Level : Hard
• Last Updated : 07 Sep, 2021

Given a numeric string S consisting of N digits and a positive integer K, the task is to check if the given string can be split into more than one substrings with difference between consecutive substrings equalt to K.

Examples:

Input: S = “8642”, K = 2
Output: Yes
Explanation: Split the given string as {“8”, “6”, “4”, “2”}. Now, the difference between the consecutive substrings is K(= 2).

Input: S = “1009896”, K = 0
Output: No

Approach: The given problem can be solved by generating all possible substring of the given string and check if the concatenation of any subset of the generated substring is equal to the given string S and the consecutive difference of the number as a substring is K, then print Yes. Otherwise, print No. Follow the below steps to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to check if a numeric string// can be split into substrings such that// the difference between the consecutive// substrings is Kvoid isPossible(string s, int K){    bool valid = false;    long firstx = -1;     // Stores the size of the string    int n = s.length();     // Iterate over the range [1, N] and    // try each possible starting number    for (int i = 1; i <= n / 2; ++i) {         long x = stol(s.substr(0, i));        firstx = x;         // Convert the number to string        string test = to_string(x);         // Build up a sequence        // starting with the number        while (test.length() < s.length()) {            x -= K;            test += to_string(x);        }         // Compare it with the        // original string s        if (test == s) {            valid = true;            break;        }    }     // Print the result    cout << ((valid == true) ? "Yes " : "No");} // Driver Codeint main(){    string S = "8642";    int K = 2;    isPossible(S, K);     return 0;}

## Python3

 # python 3 program for the above approach # Function to check if a numeric string# can be split into substrings such that# the difference between the consecutive# substrings is Kdef isPossible(s,K):    valid = False    firstx = -1     # Stores the size of the string    n = len(s)     # Iterate over the range [1, N] and    # try each possible starting number    for i in range(1,n//2+1,1):        x = int(s[0:i])        firstx = x         # Convert the number to string        test = str(x)         # Build up a sequence        # starting with the number        while (len(test) < len(s)):            x -= K            test += str(x)         # Compare it with the        # original string s        if (test == s):            valid = True            break     # Print the result    print("Yes") if valid == True else print("No") # Driver Codeif __name__ == '__main__':    S = "8642"    K = 2    isPossible(S, K)         # This code is contributed by ipg2016107.

## Javascript


Output:
Yes

Time Complexity: O(N2)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up