0% found this document useful (0 votes)
43 views9 pages

Nishant Mishra

The document presents three programming problems with corresponding algorithms and Java code implementations. Problem 1 involves validating a string of brackets, Problem 2 focuses on finding the subarray with the largest sum, and Problem 3 deals with ranking players in a gaming tournament based on their points. Each problem includes a detailed understanding, algorithm steps, and example outputs.

Uploaded by

mnishant55555
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)
43 views9 pages

Nishant Mishra

The document presents three programming problems with corresponding algorithms and Java code implementations. Problem 1 involves validating a string of brackets, Problem 2 focuses on finding the subarray with the largest sum, and Problem 3 deals with ranking players in a gaming tournament based on their points. Each problem includes a detailed understanding, algorithm steps, and example outputs.

Uploaded by

mnishant55555
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/ 9

Name: Nishant Mishra

Phone no: 7597735930


Email: [email protected]
Address: Tak Building, Bapu Nagar, Raila Chouraya, Raila, Bhilwara
(Rajasthan)

Problem:1 (Easy Level)


Write a function that takes a string containing just the characters
‘(‘,’)’,’{‘,’}’,’[‘,’]’, and determines of the input string is valid. An input string is
valid if: Open brackets must be closed by the same type of brackets. Open
brackets must be closed in the correct order.

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:

1. Start an infinite loop.


2. Check if the input string s contains any of the following substrings: "()",
"{}", "[]".
3. If s contains "()", replace all occurrences of "()" with an empty string.
4. If s contains "{}", replace all occurrences of "{}" with an empty string.
5. If s contains "[]", replace all occurrences of "[]" with an empty string.
6. If none of the above substrings are found in s, check if s is empty. If it is,
return true, indicating that the input string is valid.
7. If the loop ends without finding a valid substring, return false.
8. The main method creates an instance of the Solution class and tests the
isValid method with three different input strings: "()", "()[]{}", and "(]"

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

public class Main {


public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isValid("()[]{}"));
System.out.println(solution.isValid("(]"));
}
}

Result:
true
false

Problem:2 ( Moderate level)


Find the sub array with largest sum and return its sum. Here the elements of the
array can be negative and positive mix. Also, the elements can be only positive
so what will be the subarray with largest sum.

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();

int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};


int maxSum = solution.maxSubArray(nums);
System.out.println("Largest subarray sum: " + maxSum);
}
}

class Solution {
public int maxSubArray(int[] nums) {
int currSum = 0;
int maxSum = Integer.MIN_VALUE;

for(int i = 0; i < nums.length; i++) {


currSum = currSum + nums[i];

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:3 (hard level)


Description: In a gaming tournament, players compete in a series if
games. Each game awards points to players based on their
performance. The tournament ranking is based on the total points each
Player accumulates. The goal is to find the out the final rankings of
the players at the end of the tournament and the prize money for the
top three players.

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.*;

public class TournamentRanking {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter total number of players: ");
int n = scanner.nextInt();
if (n < 1) {
System.out.println("Invalid number of players. Exiting...");
return;
}

List<Pair<String, Integer>> players = new ArrayList<>();

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


System.out.print("Enter player " + (i + 1) + " name: ");
String name = scanner.next();
System.out.print("Enter total points for player " + name + ": ");
int points = scanner.nextInt();
players.add(new Pair<>(name, points));
}

Collections.sort(players, new Comparator<Pair<String, Integer>>() {


@Override
public int compare(Pair<String, Integer> a, Pair<String, Integer> b) {
return b.second - a.second;
}
});

int[] prizes = new int[3];


for (int i = 0; i < 3; i++) {
System.out.print("Enter prize for position " + (i + 1) + ": ");
prizes[i] = scanner.nextInt();
}

System.out.println("\nTop 3 Players, Points, and Prizes:");


for (int i = 0; i < Math.min(3, n); i++) {
Pair<String, Integer> player = players.get(i);
System.out.println("Position " + (i + 1) + ": " + player.first + " (" +
player.second + " points) - Prize: " + prizes[i]);
}

}
}

class Pair<T, U> {


T first;
U second;

public Pair(T first, U second) {


this.first = first;
this.second = second;
}
}

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

Top 3 Players, Points, and Prizes:


Position 1: DAVE (4000 points) - Prize: 5000
Position 2: BOB (3500 points) - Prize: 3000
Position 3: Alice (2400 points) - Prize: 2000

You might also like