0% found this document useful (0 votes)
65 views52 pages

Ilovepdf Merged

This document contains details of a student named Aarnav Singh including their registration number and the course Design and Analysis of Algorithms. It includes a bonafide certificate, index of program objectives completed by the student and date, and the program code for various algorithms problems solved by the student.

Uploaded by

Aarnav Singh
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)
65 views52 pages

Ilovepdf Merged

This document contains details of a student named Aarnav Singh including their registration number and the course Design and Analysis of Algorithms. It includes a bonafide certificate, index of program objectives completed by the student and date, and the program code for various algorithms problems solved by the student.

Uploaded by

Aarnav Singh
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/ 52

Design and Analysis of Algorithms (21CSC204J)

B.Tech, Second Year, 4th Semester

Name – Aarnav Singh


Registration No. - RA2211030030019

FACULTY OF ENGINEERING & TECHNOLOGY


SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
DELHI NCR CAMPUS, MODINAGAR
SIKRI KALAN, DELHI MEERUT ROAD, DIST-GHAZIABAD-201204

EVEN SEMESTER (2023-2024)


BONAFIDE CERTIFICATE

Certified to be the bonafide record of work done by AARNAV


SINGH(RA221130030019) of 4th semester 2nd year B.Tech degree course
in SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, NCR
Campus of
Department of Computer Science & Engineering, in Design and Analysis of
Algorithms, during the academic year 2023-24.

SIGNATURE SIGNATURE

Mr. Abhilash (AB) Dr. Avneesh Vashishta


Assistant Professor HOD (CSE)
Dept. of Computer Science & Engineering

Signature of Internal Examiner Signature of Internal Examiner

Submitted for university examination held on / / at SRM IST,NCR


Campus.
INDEX

S.No. Objective Date Page No. Sign Remarks


1 Find the Summation of number 15-Jan-24 4
2 Calculate Remainder when B^N! / R 15-Jan-24 5

3 Count the no. of each type of coin 15-Jan-24 6

4 Ganesan has a String S …. 22-Jan-24 7

5 Find the Pigeon with drone’s mapping app 22-Jan-24 8

6 Find the no. of Pongal’s each contestant ate 22-Jan-24 9

7 Create a program to check your yen math 29-Jan-24 10

8 Find the total number of Students in Trust 29-Jan-24 11

9 Find Min. No. of hours of training 29-Jan-24 12

10 Create Palindrome by removing letters 12- Feb-24 13

11 Recently Aarush has become …. 12- Feb-24 14

12 Find min. no. of char. to make palindrome 12- Feb-24 15

13 Teja has given a permutation …. 19- Feb-24 16

14 Devika is addicted to meat ! .... 19- Feb-24 17

15 There are N banks in the city…. 19- Feb-24 18

16 Max. No. of Tickets could Vaanavan get 26- Feb-24 19

17 Maximal number of orders can be accepted 26- Feb-24 20

18 A Stealing got into a matches …. 26- Feb-24 21

19 Shiv has given a rebus of form …. 4- Mar-24 22

20 Chef started watching a movie …. 4- Mar-24 23

21 Find no. of sub strings are palindrome 4- Mar-24 24

22 Nowadays the one-way traffic …. 11- Mar-24 25

23 Chef monocarp has just put n …. 11- Mar-24 26

24 We call two numbers x and y …. 11- Mar-24 27

25 There is a chessboard of size …. 1- Apr-24 28

26 Little X has n distinct integers …. 1- Apr-24 29

27 Those days, many boys use …. 1- Apr-24 30

28 Find the no. of ordered Pairs 8- Apr-24 31

29 Securitas ID on the National …. 8- Apr-24 32

30 Mano went Shopping and..... 8- Apr-24 33


PROGRAM

1.

#include <iostream>
using namespace std;
int main() {
int t;
long long width, height, area;
scanf("%d", &t);

for (int case_number=1; case_number <=t; case_number++)


{
scanf("%lld %lld", &width, &height);
area = (width * height) / 2;
printf("%lld\n", area);
}
return 0; }
2.

#include<bits/stdc++
.h> using namespace
std; int main()
{
int t; cin>>t; while(t--){
int b,n,r; cin>>b>>n>>r; int
z=1; for(int i=1;i<=n;i++){
z=z*i;
}
int res; res=pow(b,z); cout<<res%r<<endl;
}
return 0; cout<<"if(n%2==1)";
}
3.

#include <iostream>
using namespace std;

int main() {
int cases;
cin >> cases;

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


int p, q, r;
cin >> p >> q >> r;

q = q + (r - 1) / 5;
r = (r - 1) % 5 + 1;

p = p + (q - 1) / 10;
q = (q - 1) % 10 + 1;

cout << p << " " << q << " " << r << endl;
}
return 0; }

4.

#include <iostream>
#include <string>
using namespace std;
int main() {
string s, s2;
cin >> s >> s2;
int z = s.length();
int a[z];
for (int i = 0; i < z - 1; i++) {
a[i] = s[i + 1] - s[i];
}
for (int i = 0; i < z - 2; i++) {
if (a[i] != a[i + 1]) {
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;

5.

#include <iostream>
using namespace std;

#define p1 cout << "Ace, move fast, pigeon is at (" << i << ",0)";
#define p2 cout << "Ace, move fast, pigeon is at (" << (i - i / z) % z << "," << i / z << ")";
#define p3 cout << "No pigeon, try another map, Ace";
#define a continue;
#define f(n) for (int i = 0; i < z; i++)
#define while1 while ((scanf("%c", &s[i])) != EOF)
int main() {
string s1;
cin >> s1;
int z = s1.size();
f(n) {
if (s1[i] == 'P') {
p1
return 0;
}
}
int i = 0, cnt = 0;
char s[10000];
while1 {
if (s[i] == 'P') {
cnt = 1;
break;
}
i++;
}
if (cnt == 1) p2
else p3
}

6.

#include <iostream>
using namespace std;
int factors(int num, int l) {
int c1 = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0 && i > l) {
c1++;
}
}
return c1;
}
int main() {
int t, j;
cin >> t;
for (j = 1; j <= t; j++) {
int p, l, q, i, c = 0, sp;
cin >> p >> l;
q = p - l;
printf("Line %d: ", j);
sp = factors(q, l);
for (i = 1; i <= q; i++) {
if (q % i == 0 && i > l) {
printf("%d", i);
if (c < sp - 1) {
printf(" ");
}
c++;
}
}
if (c == 0) {
printf("impossible");
}
printf("\n");
}
return 0;
}
7.

#include <iostream>
using namespace std;

int main() {

int items; int a,j,cnt=0;


cin>>a>>items; int c[items]; string
s[items]; for(j=0;j<items;j++){
cin>>s[j]>>c[j]; if(c[j]<a){
cout<<"I can afford "<<s[j]<<endl; a=a-c[j];
}
else{
cnt++
;
cout<<"I can't afford "<<s[j]<<endl;
}
}
if(cnt==items)
cout<<"I need more Yen!";
else cout<<a;
return 0; cout<<"for(i=1;i<=yen;i++) int i,j;";
}
8.

#include <iostream>
#include <cstring>
using namespace std;

int main() {
int n, i, j, t;
cin >> n;
for (i = 0; i < n; i++) {
cout << "Line " << i + 1 << ":" << endl;
cin >> t;
int sum = 0;
for (j = 0; j < t; j++) {
char name[100];
cin >> name;
if (strcmp(name, "donate") == 0) {
int donation;
cin >> donation;
sum += donation;
} else {
cout << sum << endl;
}
}
}
return 0;
}
9.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=(int)1e6+6,mod=(int)0;
int a[N];
long long sum[N];
int main(){
int tc;
cin>>tc;
for(int tt=1;tt<=tc;++tt){
int n,p;
cin>>n>>p;
for(int j=0;j<n;++j)
cin>>a[j];
sort(a,a+n);
int i;
for(i=0;i<n;i++) sum[i+1]=sum[i]+a[i];
long long res=1e18;
for(int j=p-1;j<n;++j){
long long s=sum[j+1]-sum[j-(p-1)];
long long cost=(LL)a[j]*p-s;
res=min(res,cost);
}
cout<<res<<'\n';
}
}
10.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1e9;
const int N = 62;
char word[N];
ll dp[N][N];
long long calculate(int s,int e)
{
if (s > e)
return 0;
if (s ==e)
return 1; ll &p = dp[s][e];
if (p != -1)
return p; p = 0;
if (word[s] == word[e]) p = 1 + calculate(s+1, e-1);
p += (calculate (s+1, e) + calculate (s, e-1) - calculate (s+1, e-1));
return p;
}
int main ()
{
ll res;
cin>>word;
memset (dp, -1, sizeof (dp));
res = calculate (0, strlen(word)-1);
printf ("%lld\n", res);
return 0;
}
11.

#include<bits/stdc++.h>
using namespace std;
int h,q,v,e;
string str;
map<int,int> f;
double puzzle(int u,int mx) {return (f[u]<=mx)?mx:(0.5*(puzzle(u<<1,max(mx,f[u]-
f[u<<1]))+puzzle(u<<1|1,max(mx,f[u]-f[u<<1|1]))));}
int main(){
cin>>h>>q;
while (q--){
cin>>str;
if (str[0]=='a'){
scanf("%d %d",&v,&e);
while (v) f[v]+=e,v>>=1;
}
else printf("%.2lf\n",puzzle(1,0));
}
return 0;
}
12.

#include <iostream>
#include <string>
using namespace std;
void garbage() {
cout << "int go(int f, int s) cin >> a; ";
}
int findMinInsertions(string str, int l, int h) {
if (l > h) return INT_MAX;
if (l == h) return 0;
if (l == h - 1) return (str[l] == str[h]) ? 0 : 1;
return (str[l] == str[h]) ?
findMinInsertions(str, l + 1, h - 1) :
(min(findMinInsertions(str, l, h - 1), findMinInsertions(str, l + 1, h)) + 1);
}
int main() {
string s;
cin >> s;
cout << findMinInsertions(s, 0, s.length() - 1);
return 0;
}
13.

#include <iostream>
using namespace std;

int a[300010], n, p[300010];

void update(int t, int l, int r, int x) {


cout << "int query(int t, int l, int r, int L, int R) cin >> x;";
}

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
p[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= min(n, i + 5); j++) {
if (a[i] * 2 - a[j] > 0 && a[i] * 2 - a[j] <= n && p[a[i] * 2 - a[j]] < i)
return puts("YES"), 0;
if (a[j] * 2 - a[i] > 0 && a[j] * 2 - a[i] <= n && p[a[j] * 2 - a[i]] > j)
return puts("YES"), 0;
}
}
puts("NO");
return 0;
}
14.

#include <iostream>
using namespace std;
#define f(n) for(n = n; n > 0; --n)
int main() {
int n, r = 0, m = 100, x, y;
cin >> n;
f(n) {
cin >> x >> y;
if (y < m)
m = y;
r += m * x;
}
printf("%d", r);
}
15.

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

#define maxs long long

map<maxs, maxs> a;
maxs i, n, k, x, p;

int main() {
cin >> n;
for (; i < n; i++) {
cin >> x;
k += x;
a[k]++;
p = max(p, a[k]);
}
cout << n – p;
}
16.

#include <iostream>
using namespace std;

int a[3];

int main() {
int n, x, i;
cin >> n;

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


cin >> x;
a[x % 3]++;
}

cout << a[0] / 2 + min(a[1], a[2]) << endl;

return 0;
}
17.

#include<bits/stdc++.h>
using namespace std;
int n,l,z;
pair<int,int> a[500020];
int main(){
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i].second>>a[i].first;
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
if(l<a[i].second)
{
z++;
l=a[i].first;
}
}cout<<z;
return 0;
}
18.

#include <iostream>
using namespace std;

#define res cin >> a >> b; cin >> s >> d;

int n, m, s, a, b, d[11];

int main() {
cin >> n >> m;

while (m--)
cin >> a >> b, d[b] += a;

for (int i = 10; i > 0 && n > 0; i--)


s += i * min(n, d[i]), n -= d[i];

cout << s;

return 0;
}
19.

#include <iostream>
using namespace std;
int p = 1, n, j, a[105];
char c;
int main() {
a[j++] = 1;
while (cin >> c && c != '=') {
if (c == '-')
p--, a[j++] = -1;
if (c == '+')
p++, a[j++] = 1;
}
cin >> n;
for (int i = 0; i < j; i++) {
if (a[i] > 0)
while (p < n && a[i] < n)
a[i]++, p++;
else
while (p > n && a[i] < 0 && a[i] > -n)
a[i]--, p--;
}
if (p != n) {
cout << "Impossible\n";
return 0;
}
cout << "Possible\n";
for (int i = 0; i < j; i++)
cout << (i ? (a[i] < 0 ? "-" : "+") : "") << abs(a[i]) << " ";
cout << "= " << n;
return 0;
}
20.

#include <iostream>
using namespace std;

void solve() {
int x, y;
cin >> x >> y;
cout << (x - y) + y / 2 << "\n";
}

int main() {
int t;
t = 1;
while (t--)
solve();

return 0;
}
21.

#include <stdio.h>
#include <string.h>
int check(char s[], char a[], int x, int y) {
int i, p = 0;
for (i = x; i <= y; i++) {
a[p] = s[i];
p++;
}
a[p] = '\0';
int c = 1;
int j = 0;
while (j <= (strlen(a) / 2)) {
if (a[j] != a[strlen(a) - j - 1]) {
c = 0;
}
j++;
}
return c;
}
int main() {
char s[50];
scanf("%s", s);
char a[50];
int i, j, c = 0;
for (i = 0; i < strlen(s); i++) {
for (j = i; j < strlen(s); j++) {
int b = check(s, a, i, j);
if (b == 1) {
c++;
}
}
}
printf("%d", c);
return 0; }
22.

#include <iostream>
using namespace std;

int s[105], e[105];

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

while (n--) {
int a, b, c;
cin >> a >> b >> c;

if (s[a] || e[b])
res += c, s[b] = e[a] = 1;
else
s[a] = e[b] = 1;

ans += c;
}

cout << min(res, ans - res);

return 0;
}
23.

#include <bits/stdc++.h>
using namespace std;
void hi(){}
int a[500],f[500],n,t;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i]; f[i]=500000;
}
sort(a+1,a+1+n);
for(int i=1;i<=n+n/2;i++)
for(int j=n; j>=1; j--)
f[j]=min(f[j],f[j-1]+abs(a[j]-i));
cout<<f[n]<<endl;
}
return 0;
cout<<"int dp[225][450]; int t[225]; int t;";
}
24.

#include <bits/stdc++.h>
using namespace std;
int i,k,m,n,t,a[60];
int main()
{
scanf("%d",&t);
while(t!=0)
{
cin>>n;
for(i=k=m=0;i<n;i++)
{
cin>>a[i];
if(a[i]&1)m++;} sort(a,a+n); for(i=0;++i<n;)
{
if(a[i]-a[i-1]==1)k++;
}
if(m&1&&!k)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
t--;
}
return 0;
}
25.

#include <iostream>
using namespace std;

int t, n, s;
string a, b;

void as() {
cout << "int T, n, s, x; char a[200010], b[200010];";
}

int main() {
cin >> t;

while (t--) {
s = 0;
cin >> n >> a >> b;

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


if (b[i] == '1' && (a[i] == '0' || a[i - 1] == '1'))
s++;
else if (b[i] == '1' && a[i + 1] == '1') {
s++;
a[i + 1] = '3';
}
}

printf("%d\n", s);
}

return 0;
}
26.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+1;
queue<int> q;
int a,b,num[maxn];
map<ll,ll> A;
void aa() {
}
int main() {
int n;
scanf("%d%d%d",&n,&a,&b);
for(int i=1; i<=n; ++i)
scanf("%d",&num[i]), A[num[i]]++;
for(int i=1; i<=n; ++i)
if(A[num[i]] > 0 && A[a-num[i]] == 0)
q.push(num[i]);
while(!q.empty()) {
int t=q.front();
q.pop();
if(A[t] > 0 && A[a-t] == 0 && A[b-t] == 0) {
puts("NO");
return 0;
}
A[t]--;
A[b-t]--;
if(A[b-t] == 0 && A[a-b+t] > 0)
q.push(a-b+t);
}
puts("YES");
for(int i=1; i<=n; ++i) {
printf("%d ",A[num[i]] > 0 ? 0 : 1);
A[num[i]]--;
}
return 0;
}
27.

#include <iostream>
using namespace std;

void hi() {
int n = 0, i = 0;
int a[100];
printf(n % 2 == 0 ? "CHAT WITH HER!" : "IGNORE HIM!");
n += a[i];
for (n = i = 0; i < 96; i++);
}

int main() {
char a;
cin >> a;
if (a == 'w')
cout << "CHAT WITH HER!";
else if (a == 'x')
cout << "IGNORE HIM!";
else
cout << "CHAT WITH HER!";

return 0;
}
28.

#include <iostream>
using namespace std;

int a[1010], s;
char b;

int main() {
while (cin >> b)
a[(int)b]++;

for (int i = 1; i <= 300; i++)


s += a[i] * a[i];

cout << s;
return 0;
cout<<"string s; cin>>s;";
}
29.

#include <iostream>
using namespace std;
int main() {
char a;
cin >> a;

if (a == 'm' || a == 'Z')
cout << "YES";
else
cout << "NO";
return 0;
cout<<"string cin>>s;";
}
30.

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int t;
cin >> t;

while (t--) {
int n;
cin >> n;
cout << 100 - n << endl;
}

return 0;

You might also like