0% found this document useful (0 votes)
31 views

Computer Science & Engineering Computer Science & Engineering

The document summarizes an experiment on dynamic programming. It has two aims: a) finding the number of ways to construct an array with different consecutive positions, and b) calculating the minimum number of operations needed to equalize the number of chocolates each colleague receives. It includes the C++ code and output for solving these two problems using dynamic programming approaches. The learning outcome is that the student learned about dynamic programming techniques like memoization and tabulation to solve problems with overlapping subproblems more efficiently.

Uploaded by

lostinspace981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Computer Science & Engineering Computer Science & Engineering

The document summarizes an experiment on dynamic programming. It has two aims: a) finding the number of ways to construct an array with different consecutive positions, and b) calculating the minimum number of operations needed to equalize the number of chocolates each colleague receives. It includes the C++ code and output for solving these two problems using dynamic programming approaches. The learning outcome is that the student learned about dynamic programming techniques like memoization and tabulation to solve problems with overlapping subproblems more efficiently.

Uploaded by

lostinspace981
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.1

Student Name: Aditya Singh UID: 21BCS2466


Branch: CSE Section/Group: 611/B
Semester: 5th Date of Performance: 12/10/23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314

1. Aim: Dynamic Programming: Implement the problems based on


Dynamic Programming.

2. Objective: a) Your goal is to find the number of ways to


construct an array such that consecutive positions contain
different values.

b) Christy is interning at HackerRank. One day she has to


distribute some chocolates to her colleagues. She is biased
towards her friends and plans to give them more than the others.
One of the program managers hears of this and tells her to make
sure everyone gets the same number.

To make things difficult, she must equalize the number of


chocolates in a series of operations. For each operation, she can
give 1,2 or 5 pieces to all but one colleague. Everyone who gets
a piece in a round receives the same number of pieces.

Given a starting distribution, calculate the minimum number of


operations needed so that every colleague has the same number
of pieces.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. DBMS script and output:


a)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;

while(t--){
ll n,k,x;
cin>>n>>k>>x;
vector<ll> path(n+1),dp(n+1);
path[1]=1;
if(x==1)
dp[1]=1;
else
dp[1]=0;
for(ll i=2;i<=n;i++){
path[i]=(path[i-1]*(k-1))%MOD;
dp[i]=(path[i-1]-dp[i-1]+MOD)%MOD;
}
cout<<dp[n]<<endl;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return 0;
}

Result:

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

string ltrim(const string &);


string rtrim(const string &);
vector<string> split(const string &);

int equal(vector<int> arr) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int mn = INT_MAX;
for (int i = 0; i < arr.size(); i++) mn = min(mn, arr[i]);
int k, ope, ans = INT_MAX;
for (int j = 0; j < 5; j++){
ope = 0;
for (int i = 0; i < arr.size(); i++){
k = arr[i] - mn;
ope += (k / 5) + (k % 5) / 2 + (k % 5) % 2;
}
ans = min(ans, ope);
mn--;
}
return ans;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string t_temp;
getline(cin, t_temp);

int t = stoi(ltrim(rtrim(t_temp)));

for (int t_itr = 0; t_itr < t; t_itr++) {


string n_temp;
getline(cin, n_temp);

int n = stoi(ltrim(rtrim(n_temp)));

string arr_temp_temp;
getline(cin, arr_temp_temp);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

vector<string> arr_temp = split(rtrim(arr_temp_temp));

vector<int> arr(n);

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


int arr_item = stoi(arr_temp[i]);

arr[i] = arr_item;
}

int result = equal(arr);

fout << result << "\n";


}

fout.close();

return 0;
}

string ltrim(const string &str) {


string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {


string s(str);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str) {


vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {


tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

return tokens;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Result:

Learning Outcome
• Learned about dynamic programmming
• Dynamic programming is especially useful for problems that
exhibit overlapping subproblems, where the same
subproblem is solved multiple times
• Dynamic programming can be categorized into two main
approaches: top-down (memoization) and bottom-up
(tabulation). and searching for specific characters or
substrings within the string

You might also like