You are on page 1of 5

Experiment 3.

1
Student Name:FAIZAN FAROOQ UID 21BCS8580
Branch: BE-CSE Section/Grou IoT-638B
Semester:5 Date of Performance:15-10-2023
Subject Name: Advance Programming Lab
Subject Code: 21CSP-314

1. Aim:
1.WAP to construct the array
2.WAP to minima the operation on array
2. Objective:
1. Your goal is to find the number of ways to construct an array such that consecutive positions
contain different values. Specifically, we want to construct an array with elements such that each
element between and , inclusive. We also want the first and last elements of the array to be and .
Given , and , find the number of ways to construct such an array. Since the answer may be large,
only find it modulo
2. 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 pieces to all but one colleague. Everyone who gets a piece in a round
receives the same number of pieces.

3. Script and Output:


Program 1:-
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007;

void print(long arr[2][2]) {


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << i << ' ' << j << ' ' << arr[i][j] << endl;
}
}
}

long countArray(int n, int k, int x) {


long ways[2][2];
ways[0][0] = 1;
ways[0][1] = 0;
bool fillSecond = true;
for (int i = 0; i < n-1; i++) {
ways[fillSecond][0] = (ways[!fillSecond][1] * (k - 1)) % MOD;
ways[fillSecond][1] = (ways[!fillSecond][1] * (k - 2) + ways[!f
illSecond][0]) % MOD;
fillSecond = !fillSecond;
}

long answer;
if (x == 1) {
answer = (ways[fillSecond][1] * (k - 1)) % MOD;
}
else {
answer = (ways[fillSecond][1] * (k - 2) + ways[fillSecond][0])
% MOD;
}
return answer;
}

int main() {
int n;
int k;
int x;
cin >> n >> k >> x;
long answer = countArray(n, k, x);
cout << answer << endl;
return 0;
}
Output:-
Program 2:-
#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) {
vector<int> possibilities(5, 0);
int minimum = *min_element(arr.begin(), arr.end());

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


for (int k : arr) {
int diff = k - minimum;
int stepsRequired = diff / 5 + (diff % 5) / 2 + ((diff
% 5) % 2) / 1;
possibilities[i] += stepsRequired;
}
minimum--;
}
return *min_element(possibilities.begin(), possibilities.end())
;
}
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);

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);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspac
e))).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;
}

Output:-

You might also like