Sunday, 21 September 2025

VIDEO_4 RECURSION

 #include<iostream>

#include<string>


#include <vector>

#include <numeric> 

using namespace std;

/*this function generates all binary strings of given length without consequitive 1 */

void binarystring(string ans,int n){

    if(ans.length()==n){

        cout<<ans<<endl;

        return;

    }

   

    binarystring(ans+"0",n);

     if(ans==""||ans[(ans.length()-1)]!='1'){

        binarystring(ans+"1",n);

    }

}

void cs( vector<int> v,int arr[],int tr,int n,int idx){

  int sum = accumulate(v.begin(), v.end(), 0);


    if(tr-sum < 0) {

        return;

    }

    if(sum == tr) {

        for(int x : v){

            cout << x << " ";

        }

        cout << endl;

        return;

    }

    for (int i=idx;i<n;i++){

        vector<int> temp = v;       // make a copy of v

        temp.push_back(arr[i]);     // add current element

        cs(temp, arr, tr, n,i); 

        

    }

}

int main(){

    // bs("",4);

     vector<int> v;

     int arr[]={2,3,5};

cs( v,arr,8,3,0);

    // int sum = accumulate(v.begin(), v.end(), 0);


    // cout << "Sum = " << sum; 

}

No comments:

Post a Comment

SDE floyd-warshall algorithm

 // User function template for C++ class Solution {   public:     void floydWarshall(vector<vector<int>> &dist) {         //...