Open In App

Birthday Gift: Counting integer Arrays with interesting properties

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

Your birthday is coming soon and one of your friends, Alex, is thinking about a gift for you. He knows that you really like integer arrays with interesting properties.

He selected two numbers, and K, and decided to write down on paper all integer arrays of length (in form a[1], a[2], …, a[K]), where every number a[i] is in range from to N, and, moreover, a[i+1] is divisible by a[i] (where 1 < <= K), and give you this paper as a birthday present.

Alex is very patient, so he managed to do this. Now you’re wondering, how many different arrays are written down on this paper? Since the answer can be really large, print it modulo 10000.

Example:

Input: N = 3, K = 2
Output: 5
Explanation: All possible arrays are: [1, 1], [1, 2], [1, 3], [2, 2], [3, 3].

Naïve Solution: The basic idea to solve the problem is as follows:

The naïve solution consists of two nested loops that build all possible arrays and then check whether each array meets the stated constraints (1< i = K and a[i+1] is divisible by a[i]). It returns the count of the number of valid arrays.

Time Complexity: O(10k)
Auxiliary Space: O(1)

Efficient approach: We can solve this problem using below algorithm:

  • Read the values ‘N’ and ‘K’ from the input.
  • Create a helper function called ‘counter’ to count the number of valid arrays of length 2. Set the ‘count’ variable to 0.
    • If ‘K’ is 1, return ‘N’ since ‘N’ arrays of length 1 exist between 1 and ‘N’.
    • If ‘K’ is 2, use the ‘counter’ function to determine how many valid arrays of length 2 there are.
    • If ‘K’ is greater than 2, find ‘mid’ by dividing ‘K’ by 2.
  • Run the ‘count’ function iteratively with the arguments ‘N’ and ‘K – mid’ to get the count of arrays containing ‘K – mid’ elements.
  • Run the ‘counter’ function recursively with the arguments ‘N’ and ‘mid’ to get the count of arrays containing ‘mid’ elements.
  • Add the counts from the two recursive calls and remove 1 to eliminate the overlap when the last element of the first subproblem and the first element of the second subproblem are the same.
  • Return the final count modulo 10000 as a response.

Below is the implementation of the above idea:

C++




// C++ code for the above approach
#include <iostream>
using namespace std;
 
// Helper function to count the number
// of valid arrays of length 'k'
int counter(int n, int k)
{
    int cnt = 0;
    if (k == 1)
        return n;
    else {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (j % i == 0)
                    cnt++;
            }
        }
    }
    return cnt;
}
 
// Recursive function to count the
// arrays
int count(int n, int k)
{
    if (k == 1)
        return n;
    if (k == 2) {
        return counter(n, k);
    }
    int mid = k / 2;
    int x = count(n, k - mid);
    int y = counter(n, mid);
    return x + y - 1;
}
 
int main()
{
    // Input values for 'n' and 'k'
    int n = 3;
    int k = 2;
 
    // Calculate and print the number
    // of different arrays
    cout << count(n, k) << endl;
 
    return 0;
}
 
// This code is contributed by Sakshi


Java




// Java code for the above approach:
import java.util.*;
 
class Main {
    public static void main(String args[])
    {
       
        // Input values for 'n' and 'k'
        int n = 3;
        int k = 2;
 
        // Calculate and print the number
        // of different arrays
        System.out.println(count(n, k));
    }
 
    // Helper function to count the number
    // of valid arrays of length 'k'
    static int counter(int n, int k)
    {
        int cnt = 0;
        if (k == 1)
            return n;
        else {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (j % i == 0)
                        cnt++;
                }
            }
        }
        return cnt;
    }
 
    // Recursive function to count the
    // arrays
    static int count(int n, int k)
    {
        if (k == 1)
            return n;
        if (k == 2) {
            return counter(n, k);
        }
        int mid = k / 2;
        int x = count(n, k - mid);
        int y = counter(n, mid);
        return x + y - 1;
    }
}


Python3




def count(n, k):
    def counter(n, k):
        cnt = 0
        if k == 1:
            return n
        else:
            for i in range(1, n + 1):
                for j in range(1, n + 1):
                    if j % i == 0:
                        cnt += 1
            return cnt
 
    if k == 1:
        return n
    if k == 2:
        return counter(n, k)
     
    mid = k // 2
    x = count(n, k - mid)
    y = counter(n, mid)
    return x + y - 1
 
if __name__ == "__main__":
    n = 3
    k = 2
 
    # Calculate and print the number of different arrays
    print(count(n, k))


C#




using System;
 
class Program {
    // Helper function to count the number
    // of valid arrays of length 'k'
    static int Counter(int n, int k)
    {
        int cnt = 0;
        if (k == 1) {
            return n;
        }
        else {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (j % i == 0) {
                        cnt++;
                    }
                }
            }
        }
        return cnt;
    }
 
    // Recursive function to count the arrays
    static int Count(int n, int k)
    {
        if (k == 1) {
            return n;
        }
        if (k == 2) {
            return Counter(n, k);
        }
        int mid = k / 2;
        int x = Count(n, k - mid);
        int y = Counter(n, mid);
        return x + y - 1;
    }
 
    static void Main()
    {
        // Input values for 'n' and 'k'
        int n = 3;
        int k = 2;
 
        // Calculate and print the number
        // of different arrays
        Console.WriteLine(Count(n, k));
    }
}


Javascript




// JavaScript code for the above approach
 
// Helper function to count the number
// of valid arrays of length 'k'
function counter(n, k) {
    let cnt = 0;
    if (k === 1) {
        return n;
    } else {
        for (let i = 1; i <= n; i++) {
            for (let j = 1; j <= n; j++) {
                if (j % i === 0) {
                    cnt++;
                }
            }
        }
    }
    return cnt;
}
 
// Recursive function to count the arrays
function count(n, k) {
    if (k === 1) {
        return n;
    }
    if (k === 2) {
        return counter(n, k);
    }
    const mid = Math.floor(k / 2);
    const x = count(n, k - mid);
    const y = counter(n, mid);
    return x + y - 1;
}
 
// Input values for 'n' and 'k'
const n = 3;
const k = 2;
 
// Calculate and print the number of different arrays
console.log(count(n, k));
 
// This code is contributed by Abhinav Mahajan (abhinav_m22).


Output

5










Time Complexity: O(n^2 * log(k))
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads