A specific sort of recursion called implicit recursion occurs when a function calls itself without making an explicit recursive call. This can occur when a function calls another function, which then calls the original code once again and starts a recursive execution of the original function. This can sometimes be difficult to spot and can lead to unintended behavior if not handled carefully.
Example of the implicit recursion:
We will use implicit recursion to find the second-largest elements from the array:
C++14
#include <bits/stdc++.h>
using namespace std;
int findLargest(vector< int > numbers) {
int largest = numbers[0];
for ( int number : numbers) {
if (number > largest) {
largest = number;
}
}
return largest;
}
int findSecondLargest(vector< int > numbers) {
numbers.erase( remove (numbers.begin(), numbers.end(), findLargest(numbers)), numbers.end());
return findLargest(numbers);
}
int main() {
vector< int > numbers = {1, 2, 3, 4, 5};
int secondLargest = findSecondLargest(numbers);
cout << secondLargest << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
class GFG {
public static int findLargest(List<Integer> numbers)
{
int largest = numbers.get( 0 );
for ( int number : numbers) {
if (number > largest) {
largest = number;
}
}
return largest;
}
public static int
findSecondLargest(List<Integer> numbers)
{
final int largest = findLargest(numbers);
numbers = numbers.stream()
.filter(n -> n != largest)
.collect(Collectors.toList());
return findLargest(numbers);
}
public static void main(String[] args)
{
List<Integer> numbers
= Arrays.asList( 1 , 2 , 3 , 4 , 5 );
int secondLargest = findSecondLargest(numbers);
System.out.println(secondLargest);
}
}
|
Python3
def find_largest(numbers):
largest = numbers[ 0 ]
for number in numbers:
if number > largest:
largest = number
return largest
def find_second_largest(numbers):
numbers.remove(find_largest(numbers))
return find_largest(numbers)
numbers = [ 1 , 2 , 3 , 4 , 5 ]
second_largest = find_second_largest(numbers)
print (second_largest)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int findLargest(List< int > numbers)
{
int largest = numbers[0];
for ( int i = 1; i < numbers.Count; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}
public static int findSecondLargest(List< int > numbers)
{
int largest = findLargest(numbers);
numbers.Remove(largest);
return findLargest(numbers);
}
public static void Main( string [] args)
{
List< int > numbers = new List< int >{ 1, 2, 3, 4, 5 };
int secondLargest = findSecondLargest(numbers);
Console.WriteLine(secondLargest);
}
}
|
Javascript
function findLargest(numbers) {
let largest = numbers[0];
for (let number of numbers) {
if (number > largest) {
largest = number;
}
}
return largest;
}
function findSecondLargest(numbers)
{
numbers = numbers.filter(n => n !== findLargest(numbers));
return findLargest(numbers);
}
let numbers = [1, 2, 3, 4, 5];
let secondLargest = findSecondLargest(numbers);
console.log(secondLargest);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
In this case, the find_second_largest( It’s crucial to remember that explicit recursion could also be used to accomplish this example, which would make the code simpler to read and comprehend. ) method calls the find_largest() function via implicit recursion to locate the second-largest number in a provided list of numbers. Implicit recursion can be used in this way to get the second-largest integer without having to write any more code, which is a handy use.