ALL QUESTIONS FOR JUSPAY
ALL QUESTIONS FOR JUSPAY
// Printing value
System.out.println(maxWeightCell(N, Edge));
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
scanner.close();
}
public static int findNearestMeetingCell(int N, int[] edge, int C1, int C2) {
// Get distances from both cells using BFS
int[] distFromC1 = bfs(N, edge, C1);
int[] distFromC2 = bfs(N, edge, C2);
return meetingCell;
}
while (!queue.isEmpty()) {
int current = queue.poll();
int next = edge[current];
return dist;
}
}
OPERATION ON TREE
class LockingTree {
int[] parent;
int[] locked;
ArrayList<Integer>[] child;
public LockingTree(int[] parent) {
this.parent=parent;
this.locked=new int[parent.length];
this.child = new ArrayList[parent.length];
for(int i=0;i<child.length;i++){
child[i] = new ArrayList<>();
}
for(int i=1;i<parent.length;i++){
child[parent[i]].add(i);
}
}
import java.util.*;
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
List<Integer> Edge = Arrays.asList(1, 2, 0, -1);
// Function Call
long ans = largestSumCycle(N, Edge);
System.out.println(ans);
}
}
Example
s=’|**|*|’
n=2
startIndex=[0,0]
endIndex=[4,5]
For the first pair of indices (0,4) the substrings is “|**|*” . There are 2 stars between a pair of bars
For the second pair of indices (0,5) the substring is “|**|*|” and there are 2+1=3 stars in between the
bars.
Both of the answers are returned to the array [2,3].
Constraints
First line contains a string S. The next line contains an integer n , the no.of elements in startIndex and endIndex.
Each line i of the n subsequent lines contains an integer of startIndex. Each line i of the n subsequent lines contains
an integer of endindex.
Sample Input
*|*| → s=”*|*|”
1 → size of startindex[] and endIndex[] is 1.
0 → startindex = 0
2 → endindex = 2
Sample output:
Explanation :
The substring from index = 0 to index = 2 is “*|*” . there is no consecutive pair of bars in this string.
import java.util.*;
class Main
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = scn.next();
int n = scn.nextInt();
int startIndex[] = new int[n];
int endIndex[] = new int[n];
for(int i = 0; i < n; i++)
{
startIndex[i] = scn.nextInt();
}
for(int i = 0; i < n; i++)
{
endIndex[i] = scn.nextInt();
}
int len = str.length();
int counter[] = new int[len];
int count = 0;
int lastIdx = -1;
Arrays.fill(counter, -1);
Stack<Integer> st = new Stack<>();
for(int i = 0; i < len; i++)
{
char ch = str.charAt(i);
if(ch == '|')
{
while(!st.empty())
{
int idx = st.pop();
counter[idx] = i;
}
}
st.push(i);
}
Note : Damon is a beast, so no human being will be left after Damon drinks everyone’s blood. But Stephan always
comes early in the town.
Input Format:
Second line with a string with n characters, denoting the one digit power in every blood.
Output Format:
Constraints:
Sample input:
6
093212
Sample output
9
Explanation:
Stephan riches the town, drinks the blood with power 9. Now Damon cannot reach 9 by drinking all the other
bloods.
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String str=sc.next();
char arr[]=str.toCharArray();
int a[]=new int[arr.length];
for(int i=0;i<a.length;i++)
a[i]=Integer.parseInt(arr[i]+"");
Arrays.sort(a);
int sum=0;
for(int i=0;i<a.length;i++) sum=sum+a[i]; int sumA=0; int sumB=sum;
ArrayList subsetA=new ArrayList(); for(int i=a.length-1;i>=0;i--)
{
sumA=sumA+a[i];
sumB=sumB-a[i];
subsetA.add(a[i]);
if(sumA>sumB)
break;
}
Iterator itr=subsetA.iterator();
while(itr.hasNext())
{
System.out.print((Integer)itr.next());
}
}
}
Duplicates
The principal has a problem with repetitions. Everytime someone sends the same email twice he becomes angry
and starts yelling. His personal assistant filters the mails so that all the unique mails are sent only once, and if
there is someone sending the same mail again and again, he deletes them. Write a program which will see the list
of roll numbers of the student and find how many emails are to be deleted.
Input Format:
Output Format:
Constraints:
Sample input:
6
133433
Sample output
3
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
TreeSet list=new TreeSet<>();
for(int i=0;i<n;i++)
list.add(sc.nextInt());
System.out.println(Math.abs(n-list.size()));
}
}
SET MATRIX ZERO :
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
class Solution {
public void setZeroes(int[][] matrix) {
boolean[][] flag = new boolean[matrix.length][matrix[0].length];
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix[0].length;j++)
{
if(matrix[i][j] == 0 && flag[i][j] == false)
{
flag[i][j] = true;
for(int a=0;a<matrix[0].length;a++)
{
if(matrix[i][a] != 0)
{
flag[i][a] = true;
matrix[i][a] = 0;
}
}
for(int a=0;a<matrix.length;a++)
{
if(matrix[a][j] != 0)
{
flag[a][j] = true;
matrix[a][j] = 0;
}
}
}
}
}
}
}
The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and
going backward) for which the stock price was less than or equal to the price of that day.
For example, if the prices of the stock in the last four days is [7,2,1,2] and the price of the stock today is 2,
then the span of today is 4 because starting from today, the price of the stock was less than or
equal 2 for 4 consecutive days.
Also, if the prices of the stock in the last four days is [7,34,1,2] and the price of the stock today is 8, then
the span of today is 3 because starting from today, the price of the stock was less than or
equal 8 for 3 consecutive days.
int next(int price) Returns the span of the stock's price given that today's price is price.
class StockSpanner {
Stack<Pair> stack;
public StockSpanner() {
this.stack = new Stack<>();
}
class Pair{
int price;
int count;
public Pair(int n, int c)
{
this.price = n;
this.count = c;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/
private void dfs(int[] edges, int curr, int time, int[] visited, boolean[] inPath)
{
if (curr == -1) {
return;
}
if (inPath[curr]) {
maxCycle = Math.max(maxCycle, time - visited[curr]);
return;
}
if (visited[curr] > 0) {
return;
}
visited[curr] = time;
inPath[curr] = true;