Question:
How to find the number of distinct combinations using Java

Summary

We will be implementing a dynamic programming solution to a counting problem. 


Solutions

// Complete this function!


class Geeks {

    public long count(int n) {

        // Add your code here.

        

        int[] cnt = new int[n+1];

        cnt[0] = 1;

        

        for (int i = 3; i <= n; i++) {

            cnt[i] += cnt[i-3];

        }

        for (int i = 5; i <= n; i++) {

            cnt[i] += cnt[i-5];

        }

        for (int i = 10; i <= n; i++) {

            cnt[i] += cnt[i-10];

        }

        

        return cnt[n];

    }

}


Explanation

  • The class Geeks is defined in the code.

  • Definition of the Method: The count method in the Geeks class takes an integer n as input and outputs a long value.

  • Initialization: An array cnt of integers is initialized with a length of n + 1 inside the count method.  


Suggested blogs:

>How to Delete the Node without Head pointer?

>How to make an Array of distinct digits from a mixed array?

>How to check if undirected graph of nodes is a tree or not using Java

>Finding Nth node from end of linked list: Java

>How to find all the possible unique permutations of the array using Java

>How to find the longest subarray with sum divisible by K using Java

>How to print all the paths from the root with a specified sum using Java

>How to return the sum of subarray with maximum sum

>How to return the subarray indexes from left to right using Java

>How to make count consistent across numbers?


Nisha Patel

Nisha Patel

Submit
0 Answers