Thursday, July 1, 2021

Published July 01, 2021 by Anonymous with 0 comment

Count of unique subsequences from given number which are power of 2

import java.io.*;

import java.util.*;

public class main {

  

    

    

    static HashSet<String> allUniqueSubSeq

        = new HashSet<>();

  

    

    

    static boolean checkpower(int n)

    {

        if ((n & (n - 1)) == 0) {

            return true;

        }

        return false;

    }

  

    

    

    

    static void uniqueSubSeq(String S, int N, String ans,

                             int index)

    {

  

        

        if (index == N) {

  

            if (ans.length() != 0)

  

                

                

                if (checkpower(

                        Integer.parseInt(ans.trim()))) {

  

                    

                    

                    allUniqueSubSeq.add(ans);

                }

            return;

        }

  

        

        

        uniqueSubSeq(S, N, ans + S.charAt(index),

                     index + 1);

  

        

        

        uniqueSubSeq(S, N, ans, index + 1);

    }

  

    

    

    

    static int Countsubsequneces(String S, int N)

    {

        

        uniqueSubSeq(S, N, "", 0);

  

        

        

        return allUniqueSubSeq.size();

    }

  

    

    public static void main(String[] args)

    {

  

        

        String S = "1216389";

        int N = S.length();

  

        

        System.out.println(Countsubsequneces(S, N));

    }

}

Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment