0% found this document useful (0 votes)
52 views7 pages

Work Through

The document contains multiple programming problems related to calculating maximum profits from transactions in cities, identifying numbers with exactly four divisors, and determining the winner in a game based on segments of zeros in a binary string. Each problem includes input formats, constraints, and sample outputs. The solutions involve using data structures like adjacency lists and depth-first search for graph traversal, as well as efficient divisor counting methods.

Uploaded by

Athrav Chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

Work Through

The document contains multiple programming problems related to calculating maximum profits from transactions in cities, identifying numbers with exactly four divisors, and determining the winner in a game based on segments of zeros in a binary string. Each problem includes input formats, constraints, and sample outputs. The solutions involve using data structures like adjacency lists and depth-first search for graph traversal, as well as efficient divisor counting methods.

Uploaded by

Athrav Chougule
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

class Result {

/*
* Complete the 'maximumProfit' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER ARRAY val
* 2. INTEGER ARRAY A
* 3. INTEGER ARRAY B
*/

public static int maximumProfit(List<Integer> val, List<Integer> A,


List<Integer> B) {
// Write your code here
int n = val.size();
int maxProfit = 0;

Map<Integer, List<Integer>> adjList = new HashMap<>();


for (int i = 0; i < n; i++) {
adjList.put(i, new ArrayList<>());
}
for (int i = 0; i < A.size(); i++) {
adjList.get(A.get(i) - 1).add(B.get(i) - 1);
}

// Perform a Depth-First Search (DFS) to explore reachable cities


for (int start = 0; start < n; start++) {
maxProfit = Math.max(maxProfit, dfs(val, adjList, start, -1, 0)); // -1 is to
avoid considering buying in the same city
}

return maxProfit;

private static int dfs(List<Integer> val, Map<Integer, List<Integer>>


adjList, int currCity, int prevCity, int profit) {
// Base case: reached a city where we cannot sell (previously bought or no
outgoing roads)
if (prevCity == currCity || adjList.get(currCity).isEmpty()) {
return profit;
}

int maxProfitFromCurr = profit;


for (int nextCity : adjList.get(currCity)) {
// Check if profit is higher selling in this reachable city
int potentialProfit = val.get(nextCity) - val.get(currCity) + profit;
maxProfitFromCurr = Math.max(maxProfitFromCurr, dfs(val, adjList, nextCity,
currCity, potentialProfit));
}

return maxProfitFromCurr;
}
}
nsdfjgvuidsejawodgtkbbinhjohjiojiojiotjiorjgioh

class Result {

/*
* Complete the 'maximumProfit' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER ARRAY val
* 2. INTEGER ARRAY A
* 3. INTEGER ARRAY B
*/

public static int maximumProfit(List<Integer> val, List<Integer> A,


List<Integer> B) {
// Write your code here
int n = val.size();
int maxProfit = 0;

Map<Integer, List<Integer>> adjList = new HashMap<>();


for (int i = 0; i < n; i++) {
adjList.put(i, new ArrayList<>());
}
for (int i = 0; i < A.size(); i++) {
adjList.get(A.get(i) - 1).add(B.get(i) - 1);
}

// Perform a Depth-First Search (DFS) to explore reachable cities


for (int start = 0; start < n; start++) {
maxProfit = Math.max(maxProfit, dfs(val, adjList, start, -1, 0)); // -1 is to
avoid considering buying in the same city
}
return maxProfit;

private static int dfs(List<Integer> val, Map<Integer, List<Integer>>


adjList, int currCity, int prevCity, int profit) {
// Base case: reached a city where we cannot sell (previously bought or no
outgoing roads)
if (prevCity == currCity || adjList.get(currCity).isEmpty()) {
return profit;
}

int maxProfitFromCurr = profit;


for (int nextCity : adjList.get(currCity)) {
// Check if profit is higher selling in this reachable city
int potentialProfit = val.get(nextCity) - val.get(currCity) + profit;
maxProfitFromCurr = Math.max(maxProfitFromCurr, dfs(val, adjList, nextCity,
currCity, potentialProfit));
}

return maxProfitFromCurr;
}
}
Description
Problem Statement

Abhinav, was studying about Quatro Divisors. These are numbers that have exactly 4
divisors. Now he has been given homework, based on that. His teacher wants him to
find the sum of divisors of all integers in the array that have exactly 4 divisors.

If there are no such numbers than return 0.

Input Format

The first line contains an integer, denoting the number of integers to be operated
upon.
The subsequent lines are the integers of the array.
Constraints

The size of the array : N : 1<=N<=104 .


The array elements : 1<=arr[i]<=105 .
Output Format

The output is an integer, denoting the sum of all the divisors of all the Quatro
Numbers.
Evaluation Parameters

Sample Input
3
21
4
7
Sample Output
32
Explanation
21 has 4 divisors : 1,3,7,21
4 has 3 divisors : 1,2,4

7 has 2 divisors : 1,7

Hence the answer is 1+3+7+21 = 32

Execution time limit


Default

1st ======
problem

Description
Problem Statement

There are n cities and m one-directional roads connecting the city A[i] to city
B[i]. We are in the panner-making business. Each city has set its own price of
paneer. You can buy or sell paneer in the city i for vali rupees. You have to buy
paneer from some city u and sell the paneer in another city v. The profit that you
make is calculated as the selling price minus buying price. Return an integer
denoting the maximum profit you can make. NOTE: In all, it is mandatory to buy only
1 paneer and it is mandatory to sell that paneer in some other city that can be
travelled from it.

Input Format

The first line contains n the number of cities.


The next n lines contain the elements of the array val.
The next line contains an integer m.
The next m lines contain the array of elements A.
The next line again contains the integer m.
The next m lines contain the array of elements B.
Constraints

2 <= n <= 105


1 <= m <= 105
1 <= Ai < Bi <= n
Output Format

Return an integer denoting the maximum profit possible.


Evaluation Parameters

Sample Input
4
2
3
1
5
3
1
1
2
3
2
3
4
Sample Output
3
Explanation
The maximum profit of 3 rupees can be obtained, as follows:

At city 1, buy paneer for 2 rupees


Traverse Road 1 to get to city 2
Traverse Road 3 to get to city 4
At city 4, sell the paneer for 5 rupees

Execution time limit


Default

3rd
=================================

To solve the problem, we need to break down the binary string into segments of
zeros, calculate the number of divisors for each segment length, and then simulate
the turn-based game between Marc and Rex where Marc goes first.

Here's a step-by-step approach to solving this:

Identify Segments of Zeros:

Traverse the string to find contiguous segments of zeros.


Calculate Number of Divisors:

For each segment length, calculate the number of divisors.


Sort Divisors in Descending Order:

Sort the divisors in descending order because Marc will take the highest available
score first.
Simulate the Game:

Alternately assign the values to Marc and Rex, starting with Marc.
Determine the Winner:

Compare the final scores of Marc and Rex to determine the winner.
Here is the Java code implementing this logic:

java
Copy code
import java.util.*;

class Result {

public static String findWinner(String s) {


List<Integer> segmentLengths = new ArrayList<>();
int n = s.length();
int count = 0;

// Step 1: Identify segments of zeros


for (int i = 0; i < n; i++) {
if (s.charAt(i) == '0') {
count++;
} else {
if (count > 0) {
segmentLengths.add(count);
}
count = 0;
}
}
if (count > 0) {
segmentLengths.add(count);
}

// Step 2: Calculate number of divisors for each segment length


List<Integer> scores = new ArrayList<>();
for (int length : segmentLengths) {
scores.add(countDivisors(length));
}

// Step 3: Sort the scores in descending order


Collections.sort(scores, Collections.reverseOrder());

// Step 4: Simulate the game


int marcScore = 0;
int rexScore = 0;
for (int i = 0; i < scores.size(); i++) {
if (i % 2 == 0) {
marcScore += scores.get(i);
} else {
rexScore += scores.get(i);
}
}

// Step 5: Determine the winner


if (marcScore > rexScore) {
return "MARC";
} else {
return "REX";
}
}

private static int countDivisors(int n) {


int count = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i * i == n) {
count += 1;
} else {
count += 2;
}
}
}
return count;
}

public static void main(String[] args) {


System.out.println(findWinner("10000001001")); // Output: MARC
}
}
Explanation of the code:

Identifying Segments:

We loop through the string to find contiguous segments of zeros and store their
lengths in segmentLengths.
Calculating Divisors:

For each segment length, we calculate the number of divisors using the
countDivisors function. This function counts the divisors efficiently by checking
up to the square root of the number.
Sorting Scores:

We sort the divisor counts in descending order to ensure that Marc always gets the
highest available score first.
Simulating the Game:

We simulate the turn-based game by alternately adding the scores to Marc's and
Rex's totals.
Determining the Winner:

We compare the final scores to determine and return the winner.


This method ensures we accurately determine the winner based on the rules provided.

You might also like