0% found this document useful (0 votes)
47 views20 pages

Cse 1200 33-48

Uploaded by

Ashikur Rahman
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)
47 views20 pages

Cse 1200 33-48

Uploaded by

Ashikur Rahman
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/ 20

Heaven's light is our guide

Rajshahi University of Engineering & Technology


Department of Computer Science & Engineering

LAB REPORT
Course Code: CSE 1200
Course Name: Competitive Programming Sessional
Lab No: 04
Lab Subject: Platform Problem Solving
Date of Lab: 9 Sep 2023

Submitted by: Submitted to:


Name: Fariha Anjum Aupy Rizoan Toufiq
Roll: 2103078 Assistant Professor
Section: B Department of Computer Science &
Series: 21 Engineering, RUET

Suhrid Shakhar Ghosh


Assistant Professor
Department of Computer Science &
Engineering, RUET

Date of Submission: 23 Sep 2023


Problem No: 33
Problem Name: Number Spiral
Summary of the problem: A number spiral of five layer is given. The task is to find out the
number in row y and column x.

Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--) {
long long y, x;
cin >> y >> x;
long long layer = max(y, x);
long long start = layer * layer - (layer - 1);
if (layer % 2 == 0) {
cout << start + y - 1 - (x - 1) << endl;
} else {
cout << start + x - 1 - (y - 1) << endl;
}
}
return 0;
}
Input:
3
23
11
42
Output:
8
1
15

Screenshot of Accepted Bar:


Comments: This C++ program takes an integer 't' as input, representing the number of test
cases. For each test case, it reads two integers 'y' and 'x', which represent coordinates on a
grid. The program calculates the layer in which the point (x, y) lies, finds the starting value of
that layer, and then calculates the position of the point within that layer. The position is
determined based on whether the layer is even or odd. Finally, the program prints the
position of the point (x, y) within the grid's layer for each test case. Essentially, this code
computes the positions of points in a spiral grid pattern.

Problem No: 34
Problem Name: Circle In Square
Summary of the problem: A circle is placed perfectly into a square. The term perfectly placed
the circle doesn't have any overlapping part with the square. The task is to find the empty
area of the square which is not covered by the circle.

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
double r;
double sa;
double ca;
double a;
int t;
cin >> t;
for (int i = 1; i <= t; i++)
{
cin >> r;
ca = 2 * acos(0.0) * r * r;
sa = 4 * r * r;
a = sa - ca;
cout<<"Case "<<i<<": "<<fixed<<setprecision(2)<<a<<endl;
}
return 0;
}
Input:
3
20
30.091
87.0921
Output:
Case 1: 343.36
Case 2: 777.26
Case 3: 6511.05

Screenshot of Accepted Bar:

Comments: This code takes as input the number of test cases and for each case, computes
and displays the difference between the areas of a square and a circle with a given radius. It
then formats and prints the results for each case, ensuring two decimal places of precision,
before terminating with a status code of 0 upon completion.

Problem No: 35
Problem Name: Mike and palindrome
Summary of the problem: The task is to calculate the number of bit strings of length n.

Code:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main()
{
int n, answer = 1;
cin >> n;
for (int i = 0; i < n; i++) {
answer *= 2;
answer %= MOD;
}
cout << answer;
}

Input:
3
Output:
8
Screenshot of Accepted Bar:

Comments: This code calculates and prints the result of 2 raised to the power of 'n' modulo
10^9 + 7, where 'n' is read from the input. It efficiently computes the power using a loop and
updates the result modulo 10^9 + 7 in each iteration, ensuring the final output remains within
the specified modulus.

Problem No: 36
Problem Name: Large Division
Summary of the problem: Given two integers, a and b, the task is to check whether a is
divisible by b or not. We know that an integer a is divisible by an integer b if and only if there
exists an integer c such that a = b * c.

Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
long int t;
cin >> t;
long int i, j,len, b, d, k;
string a;

for (i=1; i<=t; i++) {


cin>>a>> b;
d = 0;
if (a[0]=='-') j=1;
else j=0;
len = a.length();
for (; j<len; j++) {

d = d*10 + (a[j]-'0');
d = d%b;
}

cout << "Case " << i << ": ";


if (d) cout << "not divisible" << endl;
else cout << "divisible" << endl;

}
return 0;
}

Input:
6
101 101
0 67
-101 101
7678123668327637674887634 101
11010000000000000000 256
-202202202202000202202202 -101

Output:
Case 1: divisible
Case 2: divisible
Case 3: divisible
Case 4: not divisible
Case 5: divisible
Case 6: divisible

Screenshot of Accepted Bar:

Comments: This code reads an integer 't' representing the number of test cases. For each test
case, it takes a string 'a' and an integer 'b'. It calculates whether the integer representation of
'a' is divisible by 'b' and prints "divisible" or "not divisible" accordingly. It does this by iterating
through the characters in the string 'a', converting them to an integer, and checking for
divisibility with 'b'. The program then prints the result for each test case in the format "Case i:
divisible" or "Case i: not divisible," where 'i' represents the test case number.
Problem No: 37
Problem Name: Power
Summary of the problem: For given integers 𝑚 and 𝑛, the task is to compute 𝑚𝑛 (mod
1,000,000,007). Here, 𝐴 (mod 𝑀) is the remainder when 𝐴 is divided by 𝑀.

Code:
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long modpow(long long a, long long b){
long long ans = 1;
while (b > 0){
if (b % 2 == 1)
{
ans = ans * a % MOD;
}
a = a * a % MOD;
b /= 2;
}
return ans;
}
int main(){
long long m, n;
cin >> m >> n;
cout << modpow(m, n) << endl;
}
Input:
23
Output:
8

Input:
58
Output:
390625

Screenshot of Accepted Bar:


Comments: This code efficiently calculates and prints the result of 'm' raised to the power of
'n' modulo 10^9 + 7, where 'm' and 'n' are read from the input. It uses the binary
exponentiation technique (`modpow` function) to compute the power in logarithmic time,
reducing the risk of overflow by performing modulo operations after each multiplication.

Problem No: 39
Problem Name: Beautiful Year
Summary of the problem: The task is to find the smallest year greater than a given year in
which all the digits are unique (no repeated digits). For instance, 2013 is such a year, as it
comes after 1987 and has distinct digits (2, 0, 1, and 3).

Code:
#include<iostream>
using namespace std;
int main()
{
int year, a, b, c,d;
cin>>year;;
while(1)
{
year++;
a=year/1000;
b=(year/100)%10;
c=(year/10)%10;
d=year%10;
if(a!=b && a!=c && a!=d && b!=c && b!=d && c!=d)
break;

}
cout<<year<<endl;
return 0;

}
Input:
1987
Output:
2013
Input:
2013
Output:
2014

Screenshot of Accepted Bar:


Comments: This code finds the next year after the input year where all four digits in the year
(thousands, hundreds, tens, and units) are distinct. It starts with the input year and
increments it until it finds such a year, breaking the loop when all four digits are distinct.
Finally, it prints the found year.

Problem No: 40
Problem Name: Least Common Multiple
Summary of the problem: The task is to find the least common multiple (LCM) of given n
integers.

Code:
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
int r;
while (b != 0) r = a % b, a = b, b = r;
return a;
}

int lcm(int a, int b)


{
return a/gcd(a, b)*b;
}

int main()
{
int n, i, a, b;
cin>>n;
cin>>a>>b;
a = lcm(a, b);
for (i = 2; i < n; i++) {
cin>>b;
a = lcm(a, b);
}
cout<<a<<endl;
return 0;
}
Input:
3
346
Output:
12
Input:
4
1235
Output:
30

Screenshot of Accepted Bar:

Comments: This code calculates the least common multiple (LCM) of 'n' integers provided as
input. It first defines functions 'gcd' (greatest common divisor) and 'lcm' (least common
multiple) using Euclidean algorithms. Then, it reads 'n' and the initial two numbers 'a' and 'b'.
It computes the LCM of 'a' and 'b' and updates 'a'. After that, it iteratively reads the remaining
'n-2' integers, calculates their LCM with the running result 'a', and updates 'a' accordingly.
Finally, it prints the LCM of all 'n' integers as the output.

Problem No: 41
Problem Name: Raising Bacteria
Summary of the problem: The problem involves starting with an empty box and performing
morning and night actions. In the morning, you can add bacteria, and at night, existing
bacteria double. The objective is to reach a specific number of bacteria, 'x,' and the challenge
is to calculate the minimum initial number of bacteria needed to achieve this goal.

Code:

#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
int count = 0;
while (x > 0)
{
if (x % 2 == 1) count++;
x /= 2;
}

cout << count << endl;

return 0;
}
Input:
5
Output:
2
Input:
8
Output:
1

Screenshot of Accepted Bar:

Comments: This code takes an input integer 'x' from the user and counts the number of '1's in
its binary representation. It initializes a count variable to 0, then repeatedly divides 'x' by 2
while checking if the remainder is 1 (which corresponds to a '1' in binary). If it is, it increments
the count. Finally, it prints the count, which represents the number of '1's in the binary form
of 'x'.

Problem No: 42
Problem Name: Balanced Array
Summary of the problem: Given a positive even integer n, we need to construct an array of
length n with specific conditions: the first half of the array contains distinct even numbers, the
second half contains distinct odd numbers, and the sum of the first half equals the sum of the
second half. Multiple valid solutions may exist, and it's not guaranteed that a solution exists
for every input. It is needed to provide answers for ‘t’ independent test cases.

Code:
#include<iostream>
using namespace std;
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
if(n%4==0)
{
cout<<"YES\n";
for(int i=1;i<=n/2;i++)cout<<i*2<<' ';
for(int i=1;i<n/2;i++)cout<<i*2-1<<' ';
cout<<n+n/2-1<<'\n';
}
else cout<<"NO\n";
}
}
Input:
5
2
4
6
8
10
Output:
NO
YES
2415
NO
YES
2 4 6 8 1 3 5 11
NO

Screenshot of Accepted Bar:

Comments: This C++ program takes the number of test cases and for each case, checks if the
input number is divisible by 4. If it is divisible, it prints "YES" and constructs an array where
the first half consists of even numbers and the second half contains odd numbers. If not
divisible by 4, it prints "NO."

Problem No: 43
Problem Name: Two Sets
Summary of the problem: The problem requires determining whether it is possible to divide a
given set of n numbers into two separate sets of equal sum. If such a division is possible, the
program should output "YES" and provide an example of how to create the two sets, including
the number of elements in each set and the elements themselves; otherwise, it should output
"NO."

Code:
#include <iostream>

using namespace std;

int N;

int main() {
cin >> N;
if (N % 4 == 1 || N % 4 == 2)
cout << "NO" << endl;
else if (N % 4 == 3) {
cout << "YES" << endl;
cout << N / 2 << endl;
for (int i = 2; i <= N / 2; i += 2)
cout << i << " " << N - i << " ";
cout << N << endl << N / 2 + 1 << endl;
for (int i = 1; i <= N / 2; i += 2)
cout << i << " " << N - i << " ";
} else {
cout << "YES" << endl;
cout << N / 2 << endl;
for (int i = 2; i <= N / 2; i += 2)
cout << i << " " << N - i + 1 << " ";
cout << endl << N / 2 << endl;
for (int i = 1; i <= N / 2; i += 2)
cout << i << " " << N - i + 1 << " ";
}

return 0;
}

Input:
7
Output:
YES
3
257
4
1634

Screenshot of Accepted Bar:

Comments: This C++ code takes an integer N as input, representing the number of elements.
It then checks if it's possible to divide these numbers into two sets of equal sum. If N modulo
4 is 1 or 2, it outputs "NO" because it's not possible to divide the numbers equally. If N
modulo 4 is 3, it outputs "YES" and provides an example of how to create two sets by iterating
through the numbers and printing them in pairs such that their sums are equal. Finally, if N
modulo 4 is 0, it also outputs "YES" and provides an example with a different pairing strategy.
Problem No: 44
Problem Name: Search II
Summary of the problem: Given a sequence of n integers S and a sequence of different q
integers T. Write a program which outputs C, the number of integers in T which are also in the
set S.
Code:
#include <iostream>
using namespace std;
int main()
{
long long n, q, s[100005], t, i, min, max, mid, c = 0;
cin >> n;

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


cin >> s[i];

cin >> q;
while (q--)
{
cin >> t;
min = 0;
max = n - 1;

while (min <= max) {


mid = (min + max) / 2;
if (s[mid] == t) {
c++;
break;
} else if (s[mid] > t) {
max = mid - 1;
} else if (s[mid] < t) {
min = mid + 1;
}
}
}
cout << c << endl;
return 0;
}
Input:
5
12345
3
341
Output:
3
Screenshot of Accepted Bar:

Comments: This C++ program takes as input an array of 'n' integers, followed by 'q' queries.
For each query 't', it performs binary search on the sorted array 's' to determine how many
elements in the array match the query. The count 'c' is incremented for each match found.
Finally, it prints the total count 'c' of matching elements for all queries. Essentially, the
program efficiently searches for elements in the sorted array and counts their occurrences
among the queries.

Problem No: 45
Problem Name: Distinct Numbers
Summary of the problem: Given a list of n integers, the task is to calculate the number of
distinct values in the list.

Code:
#include <bits/stdc++.h>
#include <set>
using namespace std;
int main()
{
int n;
cin >> n;
set<int> distinctValues;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
distinctValues.insert(x);
}
cout << distinctValues.size() << endl;
return 0;
}

Input:
5
23223
Output:
2

Screenshot of Accepted Bar:


Comments: This C++ code reads an integer 'n' representing the number of integers in a list. It
then uses a `set` data structure to efficiently count the number of distinct values in the list. As
each integer is read, it's inserted into the `distinctValues` set. Since sets automatically store
unique elements only, it ensures that duplicates are ignored. After processing all input values,
the program prints the size of the set, which represents the count of distinct values in the list.

Problem No: 46
Problem Name: Apartments
Summary of the problem: In this problem, there are 'n' applicants and 'm' free apartments.
Each applicant has a desired apartment size, and they are willing to accept an apartment if its
size is close enough to their desired size. The task is to distribute the apartments in a way that
maximizes the number of applicants who can get an apartment.

Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, m, k;
cin >> n >> m >> k;

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

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

sort(applicants, applicants + n);


sort(apartments, apartments + m);

int ans = 0;
int i = 0, j = 0;
while (i < n && j < m) {
if (abs(applicants[i] - apartments[j]) <= k) {
++ans;
++i;
++j;
} else if (applicants[i] < apartments[j]) {
++i;
} else {
++j;
}
}

cout << ans << endl;

return 0;
}

Input:
435
60 45 80
60
30 60 75
Output:
2

Screenshot of Accepted Bar:

Comments: This C++ code takes as input the number of applicants 'n', the number of available
apartments 'm', and the maximum allowed size difference 'k'. It then reads the sizes of 'n'
applicants and 'm' apartments. The code sorts both arrays in ascending order. It uses two
pointers, 'i' and 'j', to iterate through the sorted arrays and counts successful allocations when
the absolute difference between an applicant's size and an apartment's size is less than or
equal to 'k'. The result, 'ans', represents the number of successful allocations, and it's printed
to the console. The code efficiently matches applicants to apartments while considering the
allowed size difference.
Problem No: 47
Problem Name: Restaurant Customers
Summary of the problem: In this problem, the arrival and leaving times of 'n' customers in a
restaurant are provided. The maximum number of customers present in the restaurant at any
given time is to be determined. The time intervals during which customers are present are
analyzed, and the peak number of customers simultaneously present in the restaurant is
found. The busiest time period in the restaurant is identified based on customer arrivals and
departures.

Code:
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int n;
cin >> n;
pair<int, int> ev[2 * n];
for (int i = 0; i < n; ++i)
{
int a, l;
cin >> a >> l;
ev[2 * i] = {a, 1};
ev[2 * i + 1] = {l, -1};
}
sort(ev, ev + 2 * n);
int curr = 0;
int maxCust = 0;
for (int i = 0; i < 2 * n; ++i)
{
curr += ev[i].second;
maxCust = max(maxCust, curr);
}
cout << maxCust << endl;
return 0;
}

Input:
3
58
24
39
Output:
2

Screenshot of Accepted Bar:


Comments: This C++ code reads the arrival and departure times of customers in a restaurant,
stores them as events, and determines the maximum number of customers present
simultaneously. It sorts the events chronologically, tracks the current customer count, and
updates the maximum count when necessary, ultimately outputting the peak customer count
in the restaurant.

Problem No: 48
Problem Name: Weird Algorithm
Summary of the problem: The problem requires simulating the execution of a given algorithm
for a positive integer 'n.' The algorithm operates as follows: if 'n' is even, it divides it by two; if
'n' is odd, it multiplies it by three and adds one. This process continues until 'n' becomes one.
The task is to produce and print the entire sequence of 'n' values generated by the algorithm,
starting from the initial 'n' and ending with '1'.

Code:
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin >> n;
cout << n << " ";
while (n > 1)
{
if (n & 1)
n = (3 * n) + 1;
else
n >>= 1;
cout << n << " ";
}
return 0;
}
Input:
3
Output:
3 10 5 16 8 4 2 1
Screenshot of Accepted Bar:

Comments: This C++ code implements the Collatz sequence for a positive integer 'n.' It starts
by printing 'n' and then repeatedly applies a rule: if 'n' is odd, it multiplies 'n' by 3 and adds 1;
if 'n' is even, it divides 'n' by 2. The code continues this process and prints each new value of
'n' until 'n' reaches 1, producing the entire Collatz sequence.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy