Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum splits in a binary string such that every substring is a power of 4 or 6.

  • Difficulty Level : Hard
  • Last Updated : 23 Apr, 2021

Given a string S composed of 0 and 1. Find the minimum splits such that the substring is a binary representation of the power of 4 or 6 with no leading zeros. Print -1 if no such partitioning is possible. 
Examples: 
 

Input: 100110110
Output: 3
The string can be split into a minimum of 
three substrings 100(power of 4), 110
(power of 6) and 110(power of 6).

Input : 00000
Output : -1
0 is not a power of  4 or 6.

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A simple solution is to split the string recursively at different indices and check if each split is a power of 4 or 6. Start with index 0 and split str[0] from other string. If it is a power of 4 or 6 then call recursively for index 1 and perform the same operation. When an entire string is split check if a total number of partitions are minimum so far or not. Then split str[0..1], check if it is the power of 4 or 6 and then call recursively for rest string. Compare partitions with minimum so far at the end of string traversal. This approach will be exponential in time.
An efficient solution is to use Dynamic Programming. A 1-D dp table is created in which dp[i] stores minimum number of partitions required to split string str[i..n-1] into substrings that are power of 4 or 6. Suppose we are at index i and str[i..j] is power of 4 or 6, then minimum number of partitions will be minimum number of partitions to split str[j+1..n-1] plus one partition to split str[i..j] from string, that is dp[j+1] + 1. Hence the recurrence relation for (j!=(n-1)) and (dp[j + 1]!=-1) will be: 
 



dp[i] = min(dp[i], dp[j + 1] + 1)

Implementation: 
 

C++




// CPP program for Minimum splits in a
//string such that substring is a power of 4 or 6.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find if given number
// is power of another number or not.
bool isPowerOf(long val, int base)
{
 
    // Divide given number repeatedly
    // by base value.
    while (val > 1) {
        if (val % base != 0)
            return false; // not a power
        val /= base;
    }
 
    return true;
}
 
// Function to find minimum number of
// partitions of given binary string
// so that each partition is power of 4 or 6.
int numberOfPartitions(string binaryNo)
{
    int i, j, n = binaryNo.length();
 
    // Variable to store integer value of
    // given binary string partition.
    long val;
 
    // DP table to store results of
    // partitioning done at differentindices.
    int dp[n];
 
    // If the last digit is 1, hence 4^0=1 and 6^0=1
    dp[n - 1] = ((binaryNo[n - 1] - '0') == 0) ? -1 : 1;
 
    // Fix starting position for partition
    for (i = n - 2; i >= 0; i--) {
        val = 0;
 
        // Binary representation
        // with leading zeroes is not allowed.
        if ((binaryNo[i] - '0') == 0) {
            dp[i] = -1;
            continue;
        }
 
        dp[i] = INT_MAX;
 
        // Iterate for all different partitions starting from i
        for (j = i; j < n; j++) {
 
            // Find integer value of current
            // binary partition.
            val = (val * 2) + (long)(binaryNo[j] - '0');
 
            // Check if the value is a power of 4 or 6 or not
            // apply recurrence relation
            if (isPowerOf(val, 4) || isPowerOf(val, 6)) {
                if (j == n - 1) {
                    dp[i] = 1;
                }
                else {
                    if (dp[j + 1] != -1)
                        dp[i] = min(dp[i], dp[j + 1] + 1);
                }
            }
        }
 
        // If no partitions are possible, then
        // make dp[i] = -1 to represent this.
        if (dp[i] == INT_MAX)
            dp[i] = -1;
    }
 
    return dp[0];
}
 
// Driver code
int main()
{
    string binaryNo = "100110110";
    cout << numberOfPartitions(binaryNo);
    return 0;
}

Java




// Java program for Minimum splits
// in a string such that substring
// is a power of 4 or 6.
import java.io.*;
 
class GFG
{
    static boolean isPowerOf(long val,
                             int base)
{
 
    // Divide given number
    // repeatedly by base value.
    while (val > 1)
    {
        if (val % base != 0)
            return false; // not a power
        val /= base;
    }
 
    return true;
}
 
// Function to find minimum
// number of partitions of
// given binary string so that
// each partition is power
// of 4 or 6.
static int numberOfPartitions(String binaryNo)
{
    int i, j, n = binaryNo.length();
 
    // Variable to store integer
    // value of given binary
    // string partition.
    long val;
 
    // DP table to store results
    // of partitioning done at
    // differentindices.
    int dp[] = new int[n];
 
    // If the last digit is 1,
    // hence 4^0=1 and 6^0=1
    dp[n - 1] = (((binaryNo.charAt(n - 1) -
                               '0') == 0) ?
                                   -1 : 1);
 
    // Fix starting position
    // for partition
    for (i = n - 2; i >= 0; i--)
    {
        val = 0;
 
        // Binary representation
        // with leading zeroes
        // is not allowed.
        if ((binaryNo.charAt(i) - '0') == 0)
        {
            dp[i] = -1;
            continue;
        }
 
        dp[i] = Integer.MAX_VALUE;
 
        // Iterate for all different
        // partitions starting from i
        for (j = i; j < n; j++)
        {
 
            // Find integer value of
            // current binary partition.
            val = (val * 2) +
                  (long)(binaryNo.charAt(j) - '0');
 
            // Check if the value is a
            // power of 4 or 6 or not
            // apply recurrence relation
            if (isPowerOf(val, 4) ||
                isPowerOf(val, 6))
            {
                if (j == n - 1)
                {
                    dp[i] = 1;
                }
                else
                {
                    if (dp[j + 1] != -1)
                        dp[i] = Math.min(dp[i],
                                         dp[j + 1] + 1);
                }
            }
        }
 
        // If no partitions are possible,
        // then make dp[i] = -1 to
        // represent this.
        if (dp[i] == Integer.MAX_VALUE)
            dp[i] = -1;
    }
 
    return dp[0];
}
 
// Driver code
public static void main (String[] args)
{
    String binaryNo = "100110110";
    System.out.println(numberOfPartitions(binaryNo));
}
}
 
// This code is contributed
// by shiv_bhakt.

Python 3




# Python 3 program for Minimum
# splits in a string such that
# substring is a power of 4 or 6.
 
import sys
 
# Function to find if given number
# is power of another number or not.
def isPowerOf(val, base):
 
    # Divide given number repeatedly
    # by base value.
    while (val > 1):
        if (val % base != 0):
            return False # not a power
        val //= base
 
    return True
 
# Function to find minimum number of
# partitions of given binary string
# so that each partition is power of 4 or 6.
def numberOfPartitions(binaryNo):
 
    n = len(binaryNo)
 
    # DP table to store results of
    # partitioning done at differentindices.
    dp = [0] * n
 
    # If the last digit is 1, hence 4^0=1 and 6^0=1
    if ((ord(binaryNo[n - 1]) - ord('0')) == 0) :
        dp[n - 1] = -1
    else:
        dp[n - 1] = 1
 
    # Fix starting position for partition
    for i in range( n - 2, -1, -1):
        val = 0
 
        # Binary representation
        # with leading zeroes is not allowed.
        if ((ord(binaryNo[i]) - ord('0')) == 0):
            dp[i] = -1
            continue
 
        dp[i] = sys.maxsize
 
        # Iterate for all different partitions starting from i
        for j in range(i, n):
 
            # Find integer value of current
            # binary partition.
            val = (val * 2) + (ord(binaryNo[j]) - ord('0'))
 
            # Check if the value is a power of 4 or 6 or not
            # apply recurrence relation
            if (isPowerOf(val, 4) or isPowerOf(val, 6)):
                if (j == n - 1):
                    dp[i] = 1
                 
                else :
                    if (dp[j + 1] != -1):
                        dp[i] = min(dp[i], dp[j + 1] + 1)
 
        # If no partitions are possible, then
        # make dp[i] = -1 to represent this.
        if (dp[i] == sys.maxsize):
            dp[i] = -1
 
    return dp[0]
 
# Driver code
if __name__ == "__main__":
     
    binaryNo = "100110110"
    print(numberOfPartitions(binaryNo))
     
# This code is contributed by Ita_c.   

C#




// C# program for Minimum splits
// in a string such that substring
// is a power of 4 or 6.
 
using System;
  
class GFG
{
    static bool isPowerOf(long val, int b)
{
  
    // Divide given number
    // repeatedly by base value.
    while (val > 1)
    {
        if (val % b != 0)
            return false; // not a power
        val /= b;
    }
  
    return true;
}
  
// Function to find minimum
// number of partitions of
// given binary string so that
// each partition is power
// of 4 or 6.
static int numberOfPartitions(string binaryNo)
{
    int i, j, n = binaryNo.Length;
  
    // Variable to store integer
    // value of given binary
    // string partition.
    long val;
  
    // DP table to store results
    // of partitioning done at
    // differentindices.
    int[] dp = new int[n];
  
    // If the last digit is 1,
    // hence 4^0=1 and 6^0=1
    dp[n - 1] = (((binaryNo[n - 1] -
                               '0') == 0) ?
                                   -1 : 1);
  
    // Fix starting position
    // for partition
    for (i = n - 2; i >= 0; i--)
    {
        val = 0;
  
        // Binary representation
        // with leading zeroes
        // is not allowed.
        if ((binaryNo[i] - '0') == 0)
        {
            dp[i] = -1;
            continue;
        }
  
        dp[i] = int.MaxValue;
  
        // Iterate for all different
        // partitions starting from i
        for (j = i; j < n; j++)
        {
  
            // Find integer value of
            // current binary partition.
            val = (val * 2) +
                  (long)(binaryNo[j] - '0');
  
            // Check if the value is a
            // power of 4 or 6 or not
            // apply recurrence relation
            if (isPowerOf(val, 4) ||
                isPowerOf(val, 6))
            {
                if (j == n - 1)
                {
                    dp[i] = 1;
                }
                else
                {
                    if (dp[j + 1] != -1)
                        dp[i] = Math.Min(dp[i],
                                         dp[j + 1] + 1);
                }
            }
        }
  
        // If no partitions are possible,
        // then make dp[i] = -1 to
        // represent this.
        if (dp[i] == int.MaxValue)
            dp[i] = -1;
    }
  
    return dp[0];
}
  
// Driver code
public static void Main ()
{
    string binaryNo = "100110110";
    Console.Write(numberOfPartitions(binaryNo));
}
}

Javascript




<script>
 
// Javascript program for Minimum splits in a
//string such that substring is a power of 4 or 6.
 
// Function to find if given number
// is power of another number or not.
function isPowerOf(val, base)
{
 
    // Divide given number repeatedly
    // by base value.
    while (val > 1) {
        if (val % base != 0)
            return false; // not a power
        val /= base;
    }
 
    return true;
}
 
// Function to find minimum number of
// partitions of given binary string
// so that each partition is power of 4 or 6.
function numberOfPartitions(binaryNo)
{
    var i, j, n = binaryNo.length;
 
    // Variable to store integer value of
    // given binary string partition.
    var val;
 
    // DP table to store results of
    // partitioning done at differentindices.
    var dp = Array(n);
 
    // If the last digit is 1, hence 4^0=1 and 6^0=1
    dp[n - 1] = ((binaryNo[n - 1] - '0') == 0) ? -1 : 1;
 
    // Fix starting position for partition
    for (i = n - 2; i >= 0; i--) {
        val = 0;
 
        // Binary representation
        // with leading zeroes is not allowed.
        if ((binaryNo[i] - '0') == 0) {
            dp[i] = -1;
            continue;
        }
 
        dp[i] = 1000000000;
 
        // Iterate for all different partitions starting from i
        for (j = i; j < n; j++) {
 
            // Find integer value of current
            // binary partition.
            val = (val * 2) + (binaryNo[j] - '0');
 
            // Check if the value is a power of 4 or 6 or not
            // apply recurrence relation
            if (isPowerOf(val, 4) || isPowerOf(val, 6)) {
                if (j == n - 1) {
                    dp[i] = 1;
                }
                else {
                    if (dp[j + 1] != -1)
                        dp[i] = Math.min(dp[i], dp[j + 1] + 1);
                }
            }
        }
 
        // If no partitions are possible, then
        // make dp[i] = -1 to represent this.
        if (dp[i] == 1000000000)
            dp[i] = -1;
    }
 
    return dp[0];
}
 
// Driver code
var binaryNo = "100110110";
document.write( numberOfPartitions(binaryNo));
 
// This code is contributed by itsok.
</script>

Output:

 3

Time Complexity: O(n^2*log(x)), x = largest power of 4 or 6 obtainable from input string. 
Auxiliary Space: O(n)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!