Count of unique subsequences from given number which are power of 2
Last Updated :
07 Aug, 2021
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:
- If S[index] is appended in the string ans, then recall the function uniqueSubSeq with parameters S, ans + S[index], and index+1.
- If S[index] is not appended in the string ans, then recall the function uniqueSubSeq with parameters S, ans, and index + 1.
- 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++
// 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
// 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));
}
}
Python3
# 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
C#
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.
JavaScript
<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>
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Similar Reads
Count of unique Subsequences of given String with lengths in range [0, N] Given a string S of length N, the task is to find the number of unique subsequences of the string for each length from 0 to N. Note: The uppercase letters and lowercase letters are considered different and the result may be large so print it modulo 1000000007. Examples: Input: S = "ababd"Output: Num
14 min read
Number of Subsequences with Even and Odd Sum | Set 2 Given an array arr[] of size N. The task is to find the number of subsequences whose sum is even and the number of subsequences whose sum is odd.Examples: Input: arr[] = {1, 2, 2, 3} Output: EvenSum = 7, OddSum = 8 There are 2N-1 possible subsequences. The subsequences with even sum are 1) {1, 3} Su
7 min read
Count Subsequence of 101 in binary form of a number Given a number N, the task is to count the occurrences of 101 subsequences in the binary representation of that number. Examples: Input: N = 10Output: 1Explanation: The binary representation of the number 10 is 1010 and there is only one subsequence 101 which is starting from the left end of binary
5 min read
Number of Subsequences with Even and Odd Sum Given an array, find the number of subsequences whose sum is even and the number of subsequences whose sum is odd. Example: Input: arr[] = {1, 2, 2, 3} Output: EvenSum = 7, OddSum = 8 There are 2^{N}-1 possible subsequences. The subsequences with even sum is 1) {1, 3} Sum = 4 2) {1, 2, 2, 3} Sum = 8
15 min read
Number of subsequences of the form a^i b^j c^k Given a string, count number of subsequences of the form aibjck, i.e., it consists of i âaâ characters, followed by j âbâ characters, followed by k âcâ characters where i >= 1, j >=1 and k >= 1. Note: Two subsequences are considered different if the set of array indexes picked for the 2 sub
15+ min read