Pages

Friday, May 23, 2025

First Occurence using Recursion in C++

 #include <iostream>

#include <vector>
using namespace std;

int firstOccur(vector<int> arr, int i, int target) {
    if(i==arr.size()) {
        return -1;
    }


    if(arr[i] == target)
    {
        return i;
    }


    return firstOccur(arr, i+1, target);
}

int main() {
    vector<int> arr = {1, 2, 3, 3, 3, 4};
    cout << firstOccur(arr, 0, 4);
    return 0;
}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...