Ass 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

ASSIGNMENT 6

Name: Rajneesh Verma


UID: 23BCS80093
Course: Mastering in Data Structure And Algorithm
Faculty: Er. Jyoti Arora Incharge
Problem 1. Too Many Books
Luke has a long bookshelf filled with n books numbered from 1 to n, and the ith book has the height
ai. He recently bought a new shelf to keep some of his books since the first one was almost full. To
add books to his new shelf, he walks across the first bookshelf from left to right, i.e., starting from
book 1 towards book n. For each book in the shelf, he can do one of the following operations:
o Leave the book as it is and move on to the next one.
o Take the book out and place it on the new shelf at the leftmost available space.
To make his new shelf look good, he wants the books in the new shelf to be in strictly increasing order
of height from left to right. In other words, if the new shelf has m books numbered from 1 (leftmost)
to m (rightmost), then for any i > 1, the height of the ith book should be strictly greater than the height
of the book. Help him find out the maximum number of books that he can place on his new shelf.
Input Format
 The first line of the input contains a single integer n – the number of books.
 The second line of the input contains n integers a1, a2, …, an – the height of the books where
ai represents the height of the ith book from the left.
Output Format
 Print a single integer, the maximum number of books he can place on the new shelf satisfying
the given conditions.
Constraints
 1 <= n <= 104
 0 <= ai <= 108
Sample Testcase 0
Testcase Input
8 10 9 2 5 3 7 101 18
Testcase Output
4
Explanation
Some of the ways to pick out the maximum number of books are [2, 5, 7, 18], [2,5, 7, 101], and [2, 3,
7, 18]. We can see that the maximum number of books that can be placed on the new shelf is 4.
Sample Testcase 1
Testcase Input
6999999
Testcase Output
1
Explanation
We can pick only one of the books because we need them in strictly increasing order of heights.

Code:
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
// Read the number of books
int n = scanner.nextInt();
int[] heights = new int[n];
// Read the heights of the books
for (int i = 0; i < n; i++) {
heights[i] = scanner.nextInt();
}
// Close the scanner
scanner.close();
// Find the length of the longest increasing subsequence
int maxLength = lengthOfLIS(heights);
// Print the result
System.out.println(maxLength);
}
public static int lengthOfLIS(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
// Array to store the increasing subsequence
ArrayList<Integer> lis = new ArrayList<>();
for (int height : heights) {
// Find the position where the current height should be placed
int pos = Collections.binarySearch(lis, height);
// If the element is not found in the list, binarySearch returns (-(insertion point) - 1)
if (pos < 0) {
pos = -(pos + 1);
}
// If pos is equal to the size of the list, it means the height is greater than any element in the list
if (pos == lis.size()) {
lis.add(height);
} else {
// Otherwise, replace the element at the position with the current height
lis.set(pos, height);
}
}
// The size of the list represents the length of the longest increasing subsequence
return lis.size();
}}

Output:
Problem 2. Election Winner
You are in a city where there are N people who can vote for their preferred candidates in the
elections. Everyone in the city casts their votes. You are given an array, vote[] where vote[i]
represents the candidate the ith person has voted for.
You are asked to find the winner of the election. A candidate wins if he strictly gets more than half of
the total votes polled.
Note: There always exists a winner of the election.
Input Format
The first line contains N (number of voters).
The next line contains N space-separated integers.
Output Format
You have to print which candidate won the election.
Constraints
1<=N<=105
Sample Testcase 0
Testcase Input
42212
Testcase Output
2
Explanation
3 people have voted for the candidate with number 2 which is greater than half of the total votes
polled so he wins.
Sample Testcase 1
Testcase Input
3323
Testcase Output
3
Explanation
2 people have voted for the candidate with number 3 which is greater than half of the total votes
polled so he wins.
Code:
import java.io.*;
import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
// Read the number of voters
int n = scanner.nextInt();
int[] votes = new int[n];
// Read the votes
for (int i = 0; i < n; i++) {
votes[i] = scanner.nextInt();
}
// Close the scanner
scanner.close();
// Find the candidate who got more than half of the votes
int winner = findMajorityCandidate(votes);
// Print the result
System.out.println(winner);
}
public static int findMajorityCandidate(int[] votes) {
int candidate = -1;
int count = 0;
// Phase 1: Find a candidate
for (int vote : votes) {
if (count == 0) {
candidate = vote;
}
if (vote == candidate) {
count++;
} else {
count--;
}
}
// Phase 2: Verify the candidate
count = 0;
for (int vote : votes) {
if (vote == candidate) {
count++;
}
}
return candidate;
}}
Output:
Problem 3. Plus-Minus Matchup
You are a software developer tasked with creating a video game for a new console that only has two
buttons, each with a number written on it. The game is composed of n rounds, and for each round, a
symbol appears on the screen, which is either + (plus) or - (minus). The player must press one of the
two buttons on the controller once, and their score will increase by the number on the button they
pressed if the symbol was +, and decrease by the same number if the symbol was -
The goal of the game is for the player to reach a score of 0. For Each test case You have to write a
function that takes in the number of rounds, the symbols for each round, and the numbers on the
buttons of a controller, and determines whether the game is winnable using that controller.
Your function should take in the following inputs:
 n: an integer, the number of rounds
 s: a string of length n, where si is the symbol that will appear on the screen in the i-th round. It
is guaranteed that s contains only the characters + and -.
 aj: an integer, the number on the first button of the controller
 bj: an integer, the number on the second button of the controller
For each test case your function should return a boolean value: Yes if the game is winnable using the
controller, and No otherwise.
Input Format
The first line contains a single integer n (1≤n≤2⋅105) — the number of rounds.
The second line contains a string s of length n— where si is the symbol that will appear on the screen
in the i-th round. It is guaranteed that s contains only the characters ++ and --.
The third line contains an integer q (1≤q≤105) — the number of controllers.
The following q lines contain two integers aj and bj each (1≤aj,bj≤109) — the numbers on the buttons
of controller j.
Output Format
Output q lines. On line j print YES if the game is winnable using controller j, otherwise print NO
Constraints
1<=n<=2*10^5
1<=q<=10^5
1<=ai,bi<=10^9
Sample Testcase 0
Testcase Input
8 +-+---+- 5 2 1 10 3 7 9 10 10 5 3
Testcase Output
YES NO NO NO YES
Explanation
For the first round there is a possible sequence on which we can get zero, one possible way to get
score 0 using the first controller is by pressing the button with number 1 in rounds 1, 2, 4, 5, 6 and 8,
and pressing the button with number 2 in rounds 3 and 7. For the second round there is no possible
sequence so that answer is zero. For the third round there is no possible sequence so that answer is
zero. For the fourth round there is no possible sequence so that answer is zero. For the fifth test case
there is a possible solution.
Sample Testcase 1
Testcase Input
20 +-----+--+--------+- 2 1000000000 99999997 250000000 1000000000
Testcase Output
NO YES
Explanation
For the first round there is no possible combination so the answer is No. For the second round there
is possible combination so the answer is Yes
Code:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class GameController {
public static boolean isWinnable(int n, String s, int aj, int bj) {
Set<Integer> possibleScores = new HashSet<>();
possibleScores.add(0);
for (int i = 0; i < n; i++) {
char currentSymbol = s.charAt(i);
Set<Integer> nextPossibleScores = new HashSet<>();
for (int score : possibleScores) {
if (currentSymbol == '+') {
nextPossibleScores.add(score + aj);
nextPossibleScores.add(score + bj);
} else if (currentSymbol == '-') {
nextPossibleScores.add(score - aj);
nextPossibleScores.add(score - bj);
}
}
possibleScores = nextPossibleScores;
}
return possibleScores.contains(0);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = sc.next();
int q = sc.nextInt();
for (int j = 0; j < q; j++) {
int aj = sc.nextInt();
int bj = sc.nextInt();
boolean winnable = isWinnable(n, s, aj, bj);
System.out.println(winnable ? "YES" : "NO");
}
sc.close();
}
}
Output:

You might also like