You are on page 1of 12

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

UNIVERSITY INSTITUTE OF ENGINEERING


Department of Computer Science & Engineering

Subject Name: Competitive Coding -I


Subject Code: 20ITP-314

Submitted to: Submitted by:


Er. Jasleen Kaur Name: Anant Kumar Mathur
(E8943) UID: 20BET1071
Section: 20BET-WM-601
Group: B
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

INDEX

Ex. List of Experiments Conduct Viva Record Total Remarks/Signature


No (MM: (MM: 10) (MM: 8) (MM: 30)
12)

1.1 Arrays

1.2
Stacks and Queues

1.3 Linked List

1.4 Searching and Sorting

2.1 Graphs

2.2
Trees

2.3 Strings

3.1 Dynamic Programming

3.2 Backtracking

3.3 Greedy Branch and Bound


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Experiment – 3.3
Student Name: Anant Kumar Mathur UID: 20BET1071
Branch: IT Section/Group: 20BET-WM-601(B)
Semester: 5th Date of Performance: 07-11-2022
Subject Name: Competitive Coding Lab Subject Code: 20ITP-314

Question 1.

1. Aim:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. Code:-

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

string ltrim(const string &);


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

/*
* Complete the 'marcsCakewalk' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts INTEGER_ARRAY calorie as parameter.
*/

long marcsCakewalk(vector<int> calorie) {


sort(calorie.begin(), calorie.end(), greater<int>());
long min = 0;
for(int i = 0; i < calorie.size(); i++)
min += pow(2, i) * calorie[i];
return min;
}

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

string n_temp;
getline(cin, n_temp);

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

string calorie_temp_temp;
getline(cin, calorie_temp_temp);

vector<string> calorie_temp = split(rtrim(calorie_temp_temp));

vector<int> calorie(n);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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


int calorie_item = stoi(calorie_temp[i]);

calorie[i] = calorie_item;
}

long result = marcsCakewalk(calorie);

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>(isspace))).base(),
s.end()
);

return s;
}

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


vector<string> tokens;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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;
}

3. OUTPUT:-
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Question -2

1. Aim:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. Code:

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

string ltrim(const string &);


string rtrim(const string &);

/*
* Complete the 'gridChallenge' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING_ARRAY grid as parameter.
*/

string gridChallenge(vector<string> grid) {


for(int i=0;i<grid.size();i++) {
sort(grid[i].begin(),grid[i].end());
}
for(int i=1;i<grid.size();i++) {
for(int j=0;j<grid.size();j++) {
if(grid[i-1][j]>grid[i][j]) {
return "NO";
}
}

}
return "YES";
}

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

string t_temp;
getline(cin, t_temp);

int t = stoi(ltrim(rtrim(t_temp)));
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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


string n_temp;
getline(cin, n_temp);

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

vector<string> grid(n);

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


string grid_item;
getline(cin, grid_item);

grid[i] = grid_item;
}

string result = gridChallenge(grid);

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;
}

3. OUTPUT :
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcomes:-
1. To become good competitive programmer.
2. By doing coding question on hacker rank it increases problem solving
skills.
3. Learnt the use of Strings in DSA.
4. Competitive coding increases our technical skills.
5. We will be able to solve real world problem based questions
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like