![]() |
VOOZH | about |
Harmonic series is inverse of a arithmetic progression. In general, the terms in a harmonic progression can be denoted as 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d) .... 1/(a + nd).
As Nth term of AP is given as ( a + (n – 1)d). Hence, Nth term of harmonic progression is reciprocal of Nth term of AP, which is 1/(a + (n – 1)d), where "a" is the 1st term of AP and "d" is a common difference.
Method #1: Simple approach
Sum is 2.283333
Time Complexity : O(n) ,as we are traversing once in array.
Auxiliary Space : O(1) ,no extra space needed.
Method #2: Using recursion
2.7178571428571425 2.9289682539682538
Time Complexity : O(n), as we are recursing for n times.
Auxiliary Space : O(n), due to recursive stack space, since n extra space has been taken.