Thursday, July 8, 2021

CSES Coin Combinations 1

 Problem:

Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ways you can produce a money sum x using the available coins.

For example, if the coins are {2,3,5} and the desired sum is 9, there are 8 ways:

  • 2+2+5
  • 2+5+2
  • 5+2+2
  • 3+3+3
  • 2+2+2+3
  • 2+2+3+2
  • 2+3+2+2
  • 3+2+2+2

Input

The first input line has two integers n and x: the number of coins and the desired sum of money.

The second line has n distinct integers c1,c2,,cn: the value of each coin.

Output

Print one integer: the number of ways modulo 109+7.

Constraints

  • 1n100
  • 1x106
  • 1ci106

Example

Input:
3 9
2 3 5


Output:
8


Full Explanation:



CODE:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n=0,x=0;
    cin>>x>>n;

    int dice[x];

    for(int i=0 ; i<x ; i++){
        cin>>dice[i];
    }

    long long value[n+1];

    value[0] = 1;
    for(int i=1;i<=n;i++){

        value[i] = 0;

        for (auto c:dice){
            if(i-c >=0){
                value[i]+=(value[i-c]%1000000007);
            }
        }
    }

    cout<<value[n]%(1000000007);

    return 0;
}

Friday, June 18, 2021

CSES : Increasing Subsequence - dynamic Programming

 Problem Statement:

You are given an array containing n integers. Your task is to determine the longest increasing subsequence in the array, i.e., the longest subsequence where every element is larger than the previous one.

A subsequence is a sequence that can be derived from the array by deleting some elements without changing the order of the remaining elements.

Input

The first line contains an integer n: the size of the array.

After this there are n integers x1,x2,,xn: the contents of the array.

Output

Print the length of the longest increasing subsequence.

Constraints

  • 1n2105
  • 1xi109

Example

Input:
8
7 3 5 3 6 2 9 8


Output:
4


Full Video Explanation:

Youtube Video Explanation


Code:(C++)


#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> dp;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    auto it = lower_bound(dp.begin(), dp.end(), x);
    if (it == dp.end()) {
      dp.push_back(x);
    } else {
      *it = x;
    }
  }
  cout << dp.size() << endl;
}

CSES Coin Combinations 1

 Problem: Consider a money system consisting of  n n  coins. Each coin has a positive integer value. Your task is to calculate the number of...