Wednesday, December 7, 2022
HomeSoftware DevelopmentDepend of Subsequences forming Palindrome with Binary representations

Depend of Subsequences forming Palindrome with Binary representations


  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

vector<string> convert(vector<int> arr,

                       int n)

{

  

    vector<string> v;

    int i, x;

    for (i = 0; i < n; i++) {

        x = arr[i];

        string s;

  

        

        whereas (x) {

  

            int r = x % 2;

            s += to_string(r);

            x /= 2;

        }

  

        

        

        reverse(s.start(), s.finish());

  

        v.push_back(s);

    }

    return v;

}

  

bool palindrome(string a, int i, int j)

{

    whereas (i < j) {

  

        

        

        

        if (a[i] != a[j])

            return false;

  

        i++;

        j--;

    }

  

    

    

    return true;

}

  

void countSubsequences(vector<string> arr,

                       int i, string s,

                       int& depend)

{

    

    

    if (i == arr.dimension()) {

        int l = s.size();

  

        

        

        if (l > 0 && palindrome(s, 0, l - 1)) {

  

            depend++;

        }

    }

    else {

  

        

        

        countSubsequences(arr, i + 1, s, depend);

  

        

        s += (arr[i]);

  

        

        

        countSubsequences(arr, i + 1,

                          s, depend);

    }

    return;

}

  

int remedy(vector<int>& arr)

{

    int depend = 0, i = 0;

    vector<string> v = convert(arr, arr.dimension());

  

    

    countSubsequences(v, i, "", depend);

    return depend;

}

  

int fundamental()

{

    vector<int> arr = { 4, 9, 3, 15, 23 };

  

    

    

    int depend = remedy(arr);

  

    cout << depend;

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments