6_21BCS1694_MAYANK SHAKYA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Assignment 6

Student Name: Mayank Shakya UID: 21BCS1694


Branch: BE- CSE Date of Completion: 25-06-2024
Faculty Name: Ms. Shefali

Question 1:
Aim: The aim is to find the maximum number of books that can be placed on the new shelf in
strictly increasing order of height.
Objective: Maximize the number of books on the new shelf. Ensure books are in strictly
increasing order of height. Optimize book selection from the original shelf.

Approach:
1. Sort the books on the original shelf in increasing order of height.
2. Initialize an empty new shelf.
3. Iterate through the sorted books and add each book to the new shelf if it's taller than
the last book on the new shelf.
4. Continue this process until all books have been considered.
5. The number of books on the new shelf is the maximum possible.
Algorithm:
1. Sort: Arrange the books in order from shortest to tallest.
2. Initialize: Create an empty new shelf.
3. Iterate: Go through each book in the sorted list:
4. If the book is taller than the last book on the new shelf, Add it to the new shelf.
5. Return: The number of books on the new shelf is the maximum possible.

Code:
#include <iostream>
#include <vector>
#include <algorithm>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

using namespace std;

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

vector<int> dp(n, 1);


int maxBooks = 1;

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


for (int j = 0; j < i; j++) {
if (heights[i] > heights[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
maxBooks = max(maxBooks, dp[i]);
}

cout << maxBooks << endl;

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Time complexity & Space Complexity: Time complexity is O(n log n) and the space
complexity is O(n).

Question 2:
Aim: Find the election winner.
Objective: Find the election winner by counting votes for each candidate and identifying the
candidate.
Approach:
1. Hash table to count votes for each candidate
2. Iterate through the votes array to update the count
3. Return the candidate with a count greater than half of the total votes
Algorithm:
1. Create a counter for each candidate.
2. Count the votes for each candidate.
3. Find the candidate with more than half of the total votes.
4. Return the winner.

Code:
#include <iostream>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

#include <unordered_map>
using namespace std;

int find_winner(int vote[], int N) {


unordered_map<int, int> vote_count;
for (int i = 0; i < N; i++) {
vote_count[vote[i]]++;
}

int max_votes = 0;
int winner;
for (auto it = vote_count.begin(); it != vote_count.end(); it++) {
if (it->second > max_votes) {
max_votes = it->second;
winner = it->first;
}
}

if (max_votes > N / 2) {
return winner;
} else {
return -1; // no winner
}
}

int main() {
int N;
cin >> N;
int vote[N];
for (int i = 0; i < N; i++) {
cin >> vote[i];
}
int winner = find_winner(vote, N);
cout << winner << endl;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Time complexity & Space Complexity: Here the time complexity and space complexity
is same O(n).

Question 3:
Aim: The aim of this problem is to determine if a player can win a game based on a given string
of '+' and '-' characters and a set of queries, where the player's score is calculated by iterating over
the string and applying rules based on the characters and query values.

Objective: The objective is to write a program that determines whether the player's final score
is non-negative, and outputs "YES" if they can win the game and "NO" otherwise.

Approach:
1. Initialize a variable to keep track of the player's score.
2. Iterate over the string of '+' and '-' characters.
3. For each character, update the score accordingly (increment for '+' and decrement for
'-').
4. After each update, check if the score is less than the current query value. If so,
decrement the score by the query value.
5. After iterating over the entire string, check if the final score is non-negative. If it is,
output "YES", otherwise output "NO".
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Algorithm:
1. Initialize score to 0 and query to the given value.
2. Iterate over the string:
Increment score for '+' and decrement for '-'.
If score < query, decrement score by query.
3. Output "YES" if score >= 0, "NO" otherwise.

Code:
#include <iostream>

using namespace std;

int main() {
string s = "++---+";
int q = 5;

int a[] = {1, 10, 7, 10, 5};


int b[] = {2, 3, 9, 10, 3};

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


int score = 0;
for (char c : s) {
if (c == '+') {
score += min(a[i], b[i]);
} else {
score -= max(a[i], b[i]);
}
}
if (score >= 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}

return 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
Output:

Time complexity & Space Complexity: Time complexity is O(n) and space complexity is
O(1).

You might also like