Open In App

Find a sequence of distinct integers whose Harmonic Mean is N

Last Updated : 09 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find a sequence of distinct integers whose Harmonic Mean is N itself. In case such a sequence doesn’t exist, print -1.

Examples:

Input: N = 5
Output: {3, 4, 5, 6, 20}
Explanation: Given sequence is the correct answer because 5/(1/3​+1/4+1/5​+1/6​+1/20)​=5. Note that there are other possible sequences as well. For example – {2, 6, 12, 20, 5}.

Input: N=3
Output: {2, 3, 6}

Approach: To solve the problem follow the below observations:

Observations:

  • For N = 1, the sequence {1} satisfies the conditions.
  • For N = 2, there is no possible sequence satisfying the given conditions. So, we’ll consider the case for N > 2.
  • We know that 1/X(X+1) = 1/X – 1/(X+1). Using this fact, we can see that the sequence {1*2, 2*3, …, (N-1)*N, N} would satisfy the required conditions. (Reason: {1*2, 2*3, …, (N-1)*N, N} = {2, 6, 12, 20…..(N-1)*N, N}.
  • The harmonic mean of this sequence:
    • N/(1/(1*2) + 1/(2*3) + 1/(3*4) +…+ 1/(N-1)*N) + 1/N)
    • N/(1-1/2 + 1/2-1/3 + 1/3-1/4) +…+ 1/(N-1)-1/N + 1/N)
    • N/1 = N
  • However, for the cases when N can be represented as N=X*(X-1), for some integer X, it can be seen that the above observation fails as the sequence would contain a duplicate.

For example, for N=6 (6=2*3), the sequence would be {2, 6, 12, 20, 30, 6}. Here 6 is repeated, but we want all distinct integers in the sequence.

  • Suppose the sequence from observation 1 is {A1, A2, A3….AN}. To avoid repeating numbers when N is even (as N = X*(X-1) will be possible only when N is even), we’ll print the sequence {2, 2*A1, 2*A2,….,2*A(N-2), 2*(N-1)}.

The following steps can be used to solve the problem :

  • For N = 1, print 1.
  • For N = 2, print -1.
  • For odd N, print the sequence {1*2, 2*3, …, (N-1)*N, N}.
  • For even N, print the sequence {2, 2*(1*2, 2*3, …, (N-2)(N-1), N-1}.

Following is the code based on the above approach:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find a sequence of
// distinct integers whose
// Harmonic Mean is N
void printSequence(int N)
{
 
    // Sequence is not possible for N=2
    if (N == 2) {
        cout << -1;
        return;
    }
 
    // For N=1, the sequence is {1} itself
    if (N == 1) {
        cout << 1;
        return;
    }
 
    // If N is odd
    if (N % 2) {
 
        // Sequence would be
        // 1*2, 2*3,...,(N-1)*N, N
        for (int i = 1; i <= N - 1; i++) {
            cout << i * (i + 1) << " ";
        }
        cout << N << "\n";
    }
 
    // If N is even
    else {
 
        // Sequence would be
        // 2, 2*(1*2, 2*3,...,(N-2)(N-1), N-1)
        cout << 2 << " ";
        for (int i = 1; i <= N - 2; i++) {
            cout << 2 * i * (i + 1) << " ";
        }
        cout << 2 * (N - 1) << "\n";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
 
    // Function Call
    printSequence(N);
}


Java




// Java Implementation
import java.util.*;
 
public class GFG {
    // Function to find a sequence of
    // distinct integers whose
    // Harmonic Mean is N
    static void printSequence(int N) {
 
        // Sequence is not possible for N=2
        if (N == 2) {
            System.out.println(-1);
            return;
        }
 
        // For N=1, the sequence is {1} itself
        if (N == 1) {
            System.out.println(1);
            return;
        }
 
        // If N is odd
        if (N % 2 != 0) {
 
            // Sequence would be
            // 1*2, 2*3,...,(N-1)*N, N
            for (int i = 1; i <= N - 1; i++) {
                System.out.print(i * (i + 1) + " ");
            }
            System.out.println(N);
        }
 
        // If N is even
        else {
 
            // Sequence would be
            // 2, 2*(1*2, 2*3,...,(N-2)(N-1), N-1)
            System.out.print(2 + " ");
            for (int i = 1; i <= N - 2; i++) {
                System.out.print(2 * i * (i + 1) + " ");
            }
            System.out.println(2 * (N - 1));
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        int N = 5;
 
        // Function Call
        printSequence(N);
    }
}
 
//this code is contributed by uttamdp_10


Python3




#Python Implementation
 
# Function to find a sequence of
# distinct integers whose
# Harmonic Mean is N
def print_sequence(N):
    # Sequence is not possible for N=2
    if N == 2:
        print(-1)
        return
 
    # For N=1, the sequence is {1} itself
    if N == 1:
        print(1)
        return
 
    # If N is odd
    if N % 2 != 0:
        # Sequence would be
        # 1*2, 2*3,...,(N-1)*N, N
        for i in range(1, N):
            print(i * (i + 1), end=" ")
        print(N)
 
    # If N is even
    else:
        # Sequence would be
        # 2, 2*(1*2, 2*3,...,(N-2)(N-1), N-1)
        print(2, end=" ")
        for i in range(1, N - 1):
            print(2 * i * (i + 1), end=" ")
        print(2 * (N - 1))
 
# Driver Code
if __name__ == "__main__":
    N = 5
 
    # Function Call
    print_sequence(N)
 
 # this code is contributed by uttamdp_10


C#




// C# code for the above approach
using System;
 
public class GFG {
    // Function to find a sequence of
    // distinct integers whose
    // Harmonic Mean is N
    static void PrintSequence(int N)
    {
        // Sequence is not possible for N=2
        if (N == 2) {
            Console.WriteLine(-1);
            return;
        }
 
        // For N=1, the sequence is {1} itself
        if (N == 1) {
            Console.WriteLine(1);
            return;
        }
 
        // If N is odd
        if (N % 2 != 0) {
            // Sequence would be
            // 1*2, 2*3,...,(N-1)*N, N
            for (int i = 1; i <= N - 1; i++) {
                Console.Write(i * (i + 1) + " ");
            }
            Console.WriteLine(N);
        }
 
        // If N is even
        else {
            // Sequence would be
            // 2, 2*(1*2, 2*3,...,(N-2)(N-1), N-1)
            Console.Write(2 + " ");
            for (int i = 1; i <= N - 2; i++) {
                Console.Write(2 * i * (i + 1) + " ");
            }
            Console.WriteLine(2 * (N - 1));
        }
    }
 
    // Driver Code
    static public void Main()
    {
        int N = 5;
 
        // Function Call
        PrintSequence(N);
    }
}


Javascript




// JavaScript code for the above approach
 
// Function to find a sequence of
// distinct integers whose
// Harmonic Mean is N
function printSequence(N) {
 
    // Sequence is not possible for N=2
    if (N === 2) {
        console.log(-1);
        return;
    }
 
    // For N=1, the sequence is [1] itself
    if (N === 1) {
        console.log(1);
        return;
    }
 
    // If N is odd
    if (N % 2) {
 
        // Sequence would be
        // 1*2, 2*3,...,(N-1)*N, N
        for (let i = 1; i <= N - 1; i++) {
            process.stdout.write(i * (i + 1) + " ");
        }
        console.log(N);
    }
 
    // If N is even
    else {
 
        // Sequence would be
        // 2, 2*(1*2, 2*3,...,(N-2)(N-1), N-1)
        process.stdout.write(2 + " ");
        for (let i = 1; i <= N - 2; i++) {
            process.stdout.write(2 * i * (i + 1) + " ");
        }
        console.log(2 * (N - 1));
    }
}
 
// Driver Code
const N = 5;
 
// Function Call
printSequence(N);
 
// This code is contributed by prasad264


Output

2 6 12 20 5








Time Complexity: O(N)
Auxiliary Space: O(1)



Similar Reads

Find Harmonic mean using Arithmetic mean and Geometric mean
Given two numbers, first calculate arithmetic mean and geometric mean of these two numbers. Using the arithmetic mean and geometric mean so calculated, find the harmonic mean between the two numbers. Examples: Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500 Arithmetic Mean: Arithmetic Mean 'AM' between two numbers a and b is
5 min read
Program for harmonic mean of numbers
Given an array of elements, find harmonic mean of numbers. Examples: Input : arr[] = {2.0, 1.0} Output : 1.3333 Harmonic mean = 2/(1/2.0 + 1/1.0) = (2 * 2)/3 = 1.333 Input : arr[] = {13.5, 14.5, 14.8, 15.2, 16.1} Output : 14.7707 Harmonic mean is used when average of rates is required, below is the formula.Harmonic mean of n numbers x1, x2, x3, . .
8 min read
Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1
Given an integer N, the task is to find a sequence of N distinct positive integers such that the Greatest Common Divisor of the sequence is 1 and GCD of all possible pairs of elements is greater than 1. Input: N = 4Output: 84 60 105 70Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all possible pair of elements i.e, {(84, 60), (84, 105),
5 min read
Find integers such that A contains exactly X distinct integers greater than A{i]
Given an array A[] of N integers, the task is to print the number of integers i from 1 to N, for which A contains exactly X distinct integers (for X = 0 to N-1) greater than Ai Examples: Input: N = 6, A[] = {2, 7, 1, 8, 2, 8}Output: {2, 1, 2, 1, 0, 0}Explanation: Let us consider X = 2.A[1] = 2: A contains 2 distinct integers greater than 2 (7 and 8
6 min read
Count of ways to generate Sequence of distinct consecutive odd integers with sum N
Given an integer N, the task is to find the total number of ways a sequence can be formed consisting of distinct consecutive odd integers that add up to N. Examples: Input: N = 45Output: 3Explanation: 3 ways to choose distinct consecutive odd numbers that add up to 45 are - {5, 7, 9, 11, 13}, {13, 15, 17} and {45}. Input : N = 20Output : 1Explanati
6 min read
Program to find the Nth Harmonic Number
Given a number N. The task is to find the Nth Harmonic Number.Let the nth harmonic number be Hn.The harmonic series is as follows: H1 = 1 H2 = H1 + 1/2 H3 = H2 + 1/3 H4 = H3 + 1/4 . . . Hn = Hn-1 + 1/n Examples: Input : N = 5 Output : 2.45 Input : N = 9 Output : 2.71786 The idea is to traverse from H1 and then consecutively keep finding H2 from H1,
6 min read
Program to find sum of harmonic series
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
5 min read
Proof: Why the Root Mean Square of two positive numbers is always greater than their Geometric Mean?
This article focuses on discussing the proof of why the root-mean-square of two positive numbers is always greater than their geometric mean. Before getting into the details of the proof, let's first discuss the basic terminologies: Root Mean Square (RMS):Consider some numbers, A1, A2, A3, A4, .... AN, the root-mean-square of these numbers will be
3 min read
Harmonic progression Sum
Given the first element of the progression 'a', common difference between the element 'd' and number of terms in the progression 'n', where [Tex]n, d, a \in [1, 100000] [/Tex]. The task is to generate harmonic progression using the above set of information.Examples: Input : a = 12, d = 12, n = 5 Output : Harmonic Progression : 1/12 1/24 1/36 1/48 1
10 min read
Leibniz harmonic triangle
The Leibniz harmonic triangle is a triangular arrangement of unit fractions in which the outermost diagonals consist of the reciprocals of the row numbers and each inner cell is the cell diagonally above and to the left minus the cell to the left. To put it algebraically, L(r, 1) = 1/r, where r is the number of the row, starting from 1, and c is th
7 min read