Given a string S of size N and containing digits in the range [0-9], the task is to print the count of all the unique subsequences of a string that are the power of 2.
Examples:
Input: S = "1216389"
Output: 5
Explanation:
All the possible unique subsequences that are power of 2 are: {1, 2, 16, 128, 8}Input: S = "12"
Output: 2
Explanation:
All the possible unique subsequences that are power of 2 are: {1, 2}.
Approach: The problem can be solved by generating all the subsequences of the string S and then checking if the number is the power of 2 or not. Follow the steps below to solve the problem:
- Initialize a set say, allUniqueSubSeq to store the subsequence, which is a power of 2.
- Define a recursive function, say uniqueSubSeq(S, ans, index), and perform the following steps:
- Base Case: If the index is equal to N, then if, (int)ans, is the power of 2, then insert ans into the set allUniqueSubSeq.
- Otherwise, there are two cases:
- Finally, after performing the above steps, call the function, uniqueSubSeq(S, "", 0) and then print the allUniqueSubSeq.size() as the answer.
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Set to store subsequences that are
// power of 2
unordered_set<string> allUniqueSubSeq;
// Function to check whether the number
// is power of 2 or not
bool checkpower(int n)
{
if ((n & (n - 1)) == 0)
{
return true;
}
return false;
}
// Auxiliary recursive Function to find
// all the unique subsequences of a string
// that are the power of 2
void uniqueSubSeq(string S, int N, string ans,
int index)
{
// If index is equal to N
if (index == N)
{
if (ans.length() != 0)
// If the number is
// power of 2
if (checkpower(stoi(ans)))
{
// Insert the String
// in the set
allUniqueSubSeq.insert(ans);
}
return;
}
// Recursion call, if the S[index]
// is inserted in ans
uniqueSubSeq(S, N, ans + S[index], index + 1);
// Recursion call, if S[index] is
// not inserted in ans
uniqueSubSeq(S, N, ans, index + 1);
}
// Function to find count of all the unique
// subsequences of a string that are the
// power of 2
int Countsubsequneces(string S, int N)
{
// Function Call
uniqueSubSeq(S, N, "", 0);
// Return the length of set
// allUniqueSubSeq
return allUniqueSubSeq.size();
}
// Driver Code
int main()
{
// Given Input
string S = "1216389";
int N = S.length();
// Function call
cout << Countsubsequneces(S, N);
return 0;
}
// This code is contributed by maddler
// Java program for the above approach
import java.io.*;
import java.util.*;
public class main {
// Set to store subsequences that are
// power of 2
static HashSet<String> allUniqueSubSeq
= new HashSet<>();
// Function to check whether the number
// is power of 2 or not
static boolean checkpower(int n)
{
if ((n & (n - 1)) == 0) {
return true;
}
return false;
}
// Auxiliary recursive Function to find
// all the unique subsequences of a string
// that are the power of 2
static void uniqueSubSeq(String S, int N, String ans,
int index)
{
// If index is equal to N
if (index == N) {
if (ans.length() != 0)
// If the number is
// power of 2
if (checkpower(
Integer.parseInt(ans.trim()))) {
// Insert the String
// in the set
allUniqueSubSeq.add(ans);
}
return;
}
// Recursion call, if the S[index]
// is inserted in ans
uniqueSubSeq(S, N, ans + S.charAt(index),
index + 1);
// Recursion call, if S[index] is
// not inserted in ans
uniqueSubSeq(S, N, ans, index + 1);
}
// Function to find count of all the unique
// subsequences of a string that are the
// power of 2
static int Countsubsequneces(String S, int N)
{
// Function Call
uniqueSubSeq(S, N, "", 0);
// Return the length of set
// allUniqueSubSeq
return allUniqueSubSeq.size();
}
// Driver Code
public static void main(String[] args)
{
// Given Input
String S = "1216389";
int N = S.length();
// Function call
System.out.println(Countsubsequneces(S, N));
}
}
# Python program for the above approach
# Set to store subsequences that are
# power of 2
allUniqueSubSeq = set()
# Function to check whether the number
# is power of 2 or not
def checkpower(n):
if(n & (n-1) == 0):
return True
return False
# Auxiliary recursive Function to find
# all the unique subsequences of a string
# that are the power of 2
def uniqueSubSeq(S, N, ans, index):
# If index is equal to N
if (index == N):
if (len(ans) != 0):
# If the number is
# power of 2
if(checkpower(int(ans))):
allUniqueSubSeq.add(ans)
return
# Recursion call, if the S[index]
# is inserted in ans
uniqueSubSeq(S, N, ans+S[index], index+1)
# Recursion call, if the S[index]
# is not inserted in ans
uniqueSubSeq(S, N, ans, index+1)
# Function to find count of all the unique
# subsequences of a string that are
# the power of 2
def countSubsequences(S, N):
# Function call
uniqueSubSeq(S, N, "", 0)
# Return the length of set
# allUniqueSubSeq
return len(allUniqueSubSeq)
# Driver code
if __name__ == '__main__':
# Given Input
S = "1216389"
N = len(S)
# Function call
print(countSubsequences(S, N))
# This code is contributed by MuskanKalra1
using System;
using System.Collections.Generic;
public class GFG {
// Set to store subsequences that are
// power of 2
static HashSet<String> allUniqueSubSeq
= new HashSet<String>();
// Function to check whether the number
// is power of 2 or not
static bool checkpower(int n)
{
if ((n & (n - 1)) == 0) {
return true;
}
return false;
}
// Auxiliary recursive Function to find
// all the unique subsequences of a string
// that are the power of 2
static void uniqueSubSeq(String S, int N, String ans,
int index)
{
// If index is equal to N
if (index == N) {
if (ans.Length != 0)
// If the number is
// power of 2
if (checkpower(
int.Parse(ans))) {
// Insert the String
// in the set
allUniqueSubSeq.Add(ans);
}
return;
}
// Recursion call, if the S[index]
// is inserted in ans
uniqueSubSeq(S, N, ans + S[index],
index + 1);
// Recursion call, if S[index] is
// not inserted in ans
uniqueSubSeq(S, N, ans, index + 1);
}
// Function to find count of all the unique
// subsequences of a string that are the
// power of 2
static int Countsubsequeneces(String S, int N)
{
// Function Call
uniqueSubSeq(S, N, "", 0);
// Return the length of set
// allUniqueSubSeq
return allUniqueSubSeq.Count;
}
// Driver Code
static public void Main()
{
String S = "1216389";
int N = S.Length;
// Function call
Console.WriteLine(Countsubsequeneces(S, N));
}
}
// This code is contributed by maddler.
<script>
// Javascript program for above approach
// Set to store subsequences that are
// power of 2
const allUniqueSubSeq
= new Set();
// Function to check whether the number
// is power of 2 or not
function checkpower(n) {
if ((n & (n - 1)) == 0) {
return true;
}
return false;
}
// Auxiliary recursive Function to find
// all the unique subsequences of a string
// that are the power of 2
function uniqueSubSeq(S, N, ans, index) {
// If index is equal to N
if (index == N) {
if (ans.length != 0)
// If the number is
// power of 2
if (checkpower(parseInt(ans.trim()))) {
// Insert the String
// in the set
allUniqueSubSeq.add(ans);
}
return;
}
// Recursion call, if the S[index]
// is inserted in ans
uniqueSubSeq(S, N, ans + S.charAt(index),
index + 1);
// Recursion call, if S[index] is
// not inserted in ans
uniqueSubSeq(S, N, ans, index + 1);
}
// Function to find count of all the unique
// subsequences of a string that are the
// power of 2
function Countsubsequneces(S, N) {
// Function Call
uniqueSubSeq(S, N, "", 0);
// Return the length of set
// allUniqueSubSeq
return allUniqueSubSeq.size;
}
// Driver Code
// Given Input
let S = "1216389";
let N = S.length;
// Function call
document.write(Countsubsequneces(S, N));
// This code is contributed by Hritik
</script>
Output
5
Time Complexity: O(2N)
Auxiliary Space: O(2N)