• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Pro Programming

Professional way of Programming: Learn C, C++, Java, Python, Dot Net, Android the professional way

  • Home
  • C MCQs
  • C/C++ Programs
  • Java Programs
  • C#
  • Python
  • MySQL
  • Topics
    • Arrays
    • Strings
    • Link Lists
    • Trees
    • Shapes
  • Projects
  • Articles
  • Games
You are here: Home / Archives for Box Stacking Problem

Box Stacking Problem

Ways to write N as sum of two or more positive integers | Set-2

Leave a Comment


Given a number N, the task is to find the number of ways N can be partitioned, i.e. the number of ways that N can be expressed as a sum of positive integers.

Note: N should also be considered itself a way to express it as a sum of positive integers.
Examples:

Input: N = 5
Output: 7
5 can be partitioned in the following ways:
5
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1

Input: N = 10
Output: 42

This post has been already discussed in Ways to write n as sum of two or more positive integers. In this post, an efficient approach is discussed.

Approach(Using Euler’s recurrence):
If p(n) is the number of partitions of N, then it can be generated by the following generating function:

Using this formula and Euler’s pentagonal number theorem, we can derive the following recurrence relation for p(n): (Check the Wikipedia article for more details)

where k = 1, -1, 2, -2, 3, -3, … and p(n) = 0 for n < 0.

Below is the implementation of above approach:

#include <bits/stdc++.h>

using namespace std;

long long partitions(int n)

{

vector<long long> p(n + 1, 0);

p[0] = 1;

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

int k = 1;

while ((k * (3 * k - 1)) / 2 <= i) {

p[i] += (k % 2 ? 1 : -1) * p[i - (k * (3 * k - 1)) / 2];

if (k > 0)

k *= -1;

else

k = 1 - k;

}

}

return p[n];

}

int main()

{

int N = 20;

cout << partitions(N);

return 0;

}

Time Complexity: O(N√N)
Space Complexity: O(N)



If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :


thumb_up
Be the First to upvote.

Please write to us at [email protected] to report any issue with the above content.


Post navigation










Source link

Filed Under: c programming Tagged With: •   Dynamic Programming, 0 - 1 Knapsack Problem, A Space Optimized DP solution for 0-1 Knapsack Problem, About Us, Advanced Data Structure, Advanced Topics, Algo ▼, Algorithm Paradigms ►, Algorithms, Algorithms-Recursion, All Algorithms, All Data Structures, Amazon, Analysis of Algorithms, Aptitude, Array, Arrays, Backtracking, Bell Numbers, Bellman–Ford Algorithm, BFS, Binary Search, Binary Search Tree, Binary Tree, Binomial Coefficient, Bit Algorithms, Bit Magic, Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person), Bitmasking and Dynamic Programming | Set-2 (TSP), Boolean Parenthesization Problem, Box Stacking Problem, Branch & Bound, Bridge and Torch problem, Burst Balloon to maximize coins, C, C++ Quiz, Campus Ambassador Program, Careers, Check whether row or column swaps produce maximum size binary sub-matrix with all 1s, Choice of Area, Coin Change, Company Prep, Company-wise, Competitive Programming, Compiler Design, Compute nCr % p, Computer Graphics, Computer Networks, Computer Organization, Computer Organization & Architecture, Contact Us, Contests, contribute.geeksforgeeks.org, contributed articles, Core Subjects ►, Count common subsequence in two strings, Count numbers (smaller than or equal to N) with given digit sum, Count of AP (Arithmetic Progression) Subsequences in an array, Count the number of ways to traverse a Matrix, Count ways to increase LCS length of two strings by one, Courses, CPP Functions, CPP-Library, CS Subjects, CS Subjects ▼, CS Subjectwise ►, Cutting a Rod, Data Structures, DBMS, Delannoy Number, Design Patterns, Detect a negative cycle., DFS, Dice Throw, Dictionary, Digit DP | Introduction, Digital Electronics, Divide and Conquer, DS ▼, Egg Dropping Puzzle, Engg. Mathematics, Entringer Number, Eulerian Number, Experienced Interviews, Explore More..., Fibonacci Numbers, Find maximum points which can be obtained by deleting elements from array, Floyd Warshall Algorithm, Friends Pairing Problem, Game Theory, GATE ▼, GATE 2019, GATE CS Corner, GATE CS Notes, GATE Notes, GATE Official Papers, GBlog, Geek of the Month, Geek on the Top, Geeks Classes, Geometric, Geometric Algorithms, Gold Mine Problem, Golomb sequence, Graph, Graph Algorithms, Greedy Algorithms, Hash, Hashing, Heap, HeapSort, Highway Billboard Problem, How to begin with Competitive Programming?, How to prepare for ACM-ICPC?, How to solve a Dynamic Programming Problem ?, HTML & XML, ide.geeksforgeeks.org, Insertion Sort, Internship, Internship Interviews, Internships, Interview ▼, Interview Experiences, ISRO, ISRO CS Exam, Jacobsthal and Jacobsthal-Lucas numbers, Java, Java Collections, Java-Functions, Java.lang package, Java.util Package, JavaScript, K maximum sums of overlapping contiguous sub-arrays, Languages, Languages ►, Languages ▼, Largest divisible pairs subset, Largest Independent Set Problem, Largest rectangular sub-matrix having sum divisible by k, Largest rectangular sub-matrix whose sum is 0, Last Minute Notes, Linked List, LinkedList, Lobb Number, Longest Bitonic Subsequence, Longest dividing subsequence, Longest palindrome subsequence with O(n) space, Longest Palindromic Subsequence, Longest Repeated Subsequence, Machine Learning, Mathematical, Mathematical Algorithms, Matrix, Matrix Chain Multiplication, Matrix Chain Multiplication (A O(N^2) Solution), maximum length Snake sequence, Maximum points from top left of matrix to bottom right and return back, Maximum profit by buying and selling a share at most k times, Maximum sum bitonic subarray, MergeSort, Microprocessor, Microsoft, Minimum cost to buy N kilograms of sweet for M persons, Minimum Cost to make two Numeric Strings Identical, Minimum number of elements which are not part of Increasing or decreasing subsequence in array, Minimum number of increasing subsequences, Minimum number of palindromes required to express N as a sum | Set 1, Minimum number of palindromes required to express N as a sum | Set 2, Minimum odd cost path in a matrix, Mobile Numeric Keypad Problem, Moser-de Bruijn Sequence, Multiple Choice Quizzes, Newman-Conway Sequence, nth Catalan Number, Number of palindromic paths in a matrix, Number Theory, Operating Systems, Optimal Substructure Property, Overlapping Subproblems Property, Painting Fence Algorithm, Palindrome Partitioning, Pattern Searching, Perfect Sum Problem, Permutation Coefficient, PHP, PHP-function, Placement Course, Practice Company Questions, Practice Problems, Print equal sum sets of array (Partition problem) | Set 1, Print Fibonacci sequence using 2 variables, Print Longest Palindromic Subsequence, Printing brackets in Matrix Chain Multiplication Problem, Printing Items in 0/1 Knapsack, Printing Longest Bitonic Subsequence, Printing Matrix Chain Multiplication (A Space Optimized Solution), Privacy Policy, Program Output, Project, Puzzles, Python, Python List, QA - Placement Quizzes, Queue, QuickSort, Quizzes, Quizzes ▼, Randomized Algorithms, Recent Articles, Recommended: Please try your approach on, Rencontres Number, School Programming, Searching, Searching Algorithms, series, Set, Set 2, Set to Array in Java, Skip to content, Software Engineering, Some rights reserved, Sorting, Sorting Algorithms, SQL, Stack, Step by Step Guide for Placement Preparation, STL, Strings, Students ▼, Subjective Questions, Subset Sum Problem, Subset Sum Problem in O(sum) space, Subset with sum divisible by m, Suggest a Topic, Super Ugly Number, Tabulation vs Memoizatation, Technical Scripter, Technical Scripter 2018, Temple Offerings, Testimonials, The painter’s partition problem, Theory of Computation, Tile Stacking Problem, Tiling Problem, Tiling with Dominoes, Top 10 algorithms in Interview Questions, Top Topics, Topic-wise, Topicwise ►, Tree, Tree based DS ►, Tuple, UGC NET CS Paper II, UGC NET CS Paper III, UGC NET Papers, UGC-NET, Ugly Numbers, Unbounded Knapsack, Vertex Cover Problem, Video Tutorials, Videos, Walmart Labs Interview Experience ( On Campus FT + 6 month Internship ), Ways to write n as sum of two or more positive integers, Web Technologies, Web Technology, What’s Difference?, Wikipedia article, Word Break Problem, Word Wrap Problem, Write an Article, Write Interview Experience

Primary Sidebar

Recent Posts

  • How to use optional in java 8 streams
  • 7 Must-Have Mobile Apps to Prepare for Online Interviews
  • Top 10 Most Popular JavaScript Frameworks for Web Development
  • Angular 9 features with examples
  • Popular Programming Languages Supported by AWS
  • Privacy Policy
  • About
  • Contact US

© 2019 ProProgramming
 Privacy Policy About Contact Us