Nishant Mishra
Nishant Mishra
Problem Understanding:
To solve this problem, we need to check whether all types of brackets
mentioned are properly closed and in the correct order. If all brackets are closed
correctly and in the right order, we return true. Otherwise, we return false.
Here is Algorithm:
Code:
class Solution {
public boolean isValid(String s) {
while(true) {
if(s.contains("()")) {hone
s = s.replace("()", "");
} else if(s.contains("{}")) {
s = s.replace("{}", "");
} else if(s.contains("[]")) {
s = s.replace("[]", "");
} else {
return s.isEmpty();
}
}
}
}
Result:
true
false
Problem Understanding:
We need to find the total sum of the largest group of numbers in the list that are
next to each other. We look at each number one by one, keeping track of two
important numbers: the highest sum of a group of numbers that ends at the
current number, and the highest sum of any group of numbers we've seen so far.
Finally, we get the total sum of the largest group of numbers we found.
Here is Algorithm:
1. Start with two variables, currSum and maxSum, both initialized
to the smallest possible value.
2. Start iterating through the array.
3. Add each element of the array to currSum.
4. If currSum becomes negative, reset it to 0 and update tempStart
to the current index.
5. If currSum becomes greater than maxSum, update maxSum to
currSum and update start and end indices.
6. After itrating through the array, return an array containing
maxSum, start, and end.
7. Use these indices to print the subarray with the largest sum and
the sum itself.
Code:
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
class Solution {
public int maxSubArray(int[] nums) {
int currSum = 0;
int maxSum = Integer.MIN_VALUE;
if(currSum < 0) {
currSum = 0;
}
maxSum = Math.max(currSum, maxSum);
} return maxSum;
}
}
Output:
Subarray is [4, -1, 2, 1]
Largest sub array Sum is 6
Problem Understanding:
In a gaming tournament, players compete in multiple games and earn
points. We need to find out the top three players based on their total
points and allocate prize money accordingly. In this problem, we
begin by sorting the players based on their total points, from highest
to lowest. Then, we display the names of the top 3 players. Finally,
we print out the prize money allocated to the top 3 players.
Here is Algorithm:
1. Start the program.
2. Ask the user how many players are in the tournament.
3. Read the number of players provided by the user.
4. If the number of players is less than 1, display "Invalid number of
players. Exiting..." and stop the program.
5. Prepare to store the players' names and points in a list.
6. For each player:
• Ask for the player's name.
• Ask for the player's total points.
• Store the player's name and points in the list.
7. Sort the list of players in descending order based on their points.
8. Ask the user for the prizes for the top three positions.
9. Read and store the prizes for the top three positions entered by the user.
10.Display "Top 3 Players, Points, and Prizes:".
11.For each of the top three players or until the end of the list:
• Get the player's name and points from the list.
• Display the position (1st, 2nd, or 3rd), player's name, points, and
prize.
12.End the program.
Code:
import java.util.*;
}
}
Result:
Enter total number of players: 5
Enter player 1 name: Alice
Enter total points for player Alice: 2400
Enter player 2 name: BOB
Enter total points for player BOB: 3500
Enter player 3 name: CHARLIE
Enter total points for player CHARLIE: 2000
Enter player 4 name: DAVE
Enter total points for player DAVE: 4000
Enter player 5 name: EVE
Enter total points for player EVE: 1500
Enter prize for position 1: 5000
Enter prize for position 2: 3000
Enter prize for position 3: 2000