Pages

Wednesday, June 4, 2025

Find all Occurences using Recursion in C++

 #include <iostream>

using namespace std;


void allOccurences(int arr[], int key, int i, int n) {

    if(i == n) {

        return;

    }

    

    if(arr[i] == key) {

        cout << i << " ";

    }

    

    allOccurences(arr, key, i+1, n);

}


int main() {

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

    int n = sizeof(arr)/sizeof(int);

    int key = 2; 

    allOccurences(arr, key, 0, n);


    return 0;

}

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...