Birthday Gift: Counting integer Arrays with interesting properties
Last Updated :
19 Oct, 2023
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, N and K, and decided to write down on paper all integer arrays of length K (in form a[1], a[2], …, a[K]), where every number a[i] is in range from 1 to N, and, moreover, a[i+1] is divisible by a[i] (where 1 < i <= 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++
#include <iostream>
using namespace std;
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;
}
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()
{
int n = 3;
int k = 2;
cout << count(n, k) << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
public static void main(String args[])
{
int n = 3 ;
int k = 2 ;
System.out.println(count(n, 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;
}
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
print (count(n, k))
|
C#
using System;
class Program {
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;
}
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()
{
int n = 3;
int k = 2;
Console.WriteLine(Count(n, k));
}
}
|
Javascript
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;
}
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;
}
const n = 3;
const k = 2;
console.log(count(n, k));
|
Time Complexity: O(n^2 * log(k))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...