Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given a boolean Matrix of size N*M, A cell of the matrix is called "Good" if it is completely surrounded by the cells containing '1' i.e. each adjacent cell (which shares a common edge) contains '1'. Your task is to find the number of such cells.
See the below example for a better understandingFirst line of input contains two space- separated integers N and M. Next N lines of input contain M space- separated integers depicting the values of the matrix.
Constraints:-
3 <= N, M <= 500
0 <= Matrix[][] <= 1Print the number of good cells.Sample Input:-
3 3
1 1 0
1 1 1
1 1 1
Sample Output:-
1
Explanation:-
Only cell at position 1, 1 is good
Sample Input:-
5 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Sample Output:-
3
Explanation:-
(1, 2), (2, 1) and (3, 2) are good cells, I have written this Solution Code: // mat is the matrix/ 2d array
// n,m are dimensions
function goodCell(mat, n, m) {
// write code here
// do not console.log
// return the answer as a number
let cnt = 0;
for (let i = 1; i < n - 1; i++) {
for (let j = 1; j < m - 1; j++) {
if (mat[i - 1][j] == 1 && mat[i + 1][j] == 1 && mat[i][j - 1] == 1 && mat[i][j + 1] == 1) {
cnt++;
}
}
}
return cnt
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean Matrix of size N*M, A cell of the matrix is called "Good" if it is completely surrounded by the cells containing '1' i.e. each adjacent cell (which shares a common edge) contains '1'. Your task is to find the number of such cells.
See the below example for a better understandingFirst line of input contains two space- separated integers N and M. Next N lines of input contain M space- separated integers depicting the values of the matrix.
Constraints:-
3 <= N, M <= 500
0 <= Matrix[][] <= 1Print the number of good cells.Sample Input:-
3 3
1 1 0
1 1 1
1 1 1
Sample Output:-
1
Explanation:-
Only cell at position 1, 1 is good
Sample Input:-
5 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Sample Output:-
3
Explanation:-
(1, 2), (2, 1) and (3, 2) are good cells, I have written this Solution Code: N, M= list(map(int,input().split()))
mat =[]
for i in range(N):
List =list(map(int,input().split()))[:M]
mat.append(List)
count =0
for i in range(1,N-1):
for j in range(1,M-1):
if (mat[i][j-1] == 1 and mat[i][j+1] == 1 and mat[i-1][j] == 1 and mat[i+1][j] == 1):
count +=1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean Matrix of size N*M, A cell of the matrix is called "Good" if it is completely surrounded by the cells containing '1' i.e. each adjacent cell (which shares a common edge) contains '1'. Your task is to find the number of such cells.
See the below example for a better understandingFirst line of input contains two space- separated integers N and M. Next N lines of input contain M space- separated integers depicting the values of the matrix.
Constraints:-
3 <= N, M <= 500
0 <= Matrix[][] <= 1Print the number of good cells.Sample Input:-
3 3
1 1 0
1 1 1
1 1 1
Sample Output:-
1
Explanation:-
Only cell at position 1, 1 is good
Sample Input:-
5 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Sample Output:-
3
Explanation:-
(1, 2), (2, 1) and (3, 2) are good cells, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1000001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n,m;
cin>>n>>m;
int a[n][m];
FOR(i,n){
FOR(j,m){
cin>>a[i][j];}}
int sum=0,sum1=0;;
FOR1(i,1,n-1){
FOR1(j,1,m-1){
if(a[i-1][j]==1 && a[i+1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1){
sum++;
}
}
}
out1(sum);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean Matrix of size N*M, A cell of the matrix is called "Good" if it is completely surrounded by the cells containing '1' i.e. each adjacent cell (which shares a common edge) contains '1'. Your task is to find the number of such cells.
See the below example for a better understandingFirst line of input contains two space- separated integers N and M. Next N lines of input contain M space- separated integers depicting the values of the matrix.
Constraints:-
3 <= N, M <= 500
0 <= Matrix[][] <= 1Print the number of good cells.Sample Input:-
3 3
1 1 0
1 1 1
1 1 1
Sample Output:-
1
Explanation:-
Only cell at position 1, 1 is good
Sample Input:-
5 4
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
Sample Output:-
3
Explanation:-
(1, 2), (2, 1) and (3, 2) are good cells, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m= sc.nextInt();
int a[][]= new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=sc.nextInt();}}
int cnt=0;
for(int i=1;i<n-1;i++){
for(int j=1;j<m-1;j++){
if(a[i-1][j]==1 && a[i+1][j]==1 && a[i][j-1]==1 && a[i][j+1]==1){
cnt++;
}
}
}
System.out.print(cnt);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Complete the function <code>checkCanIVote</code>
<ol>
<li>Takes 2 arguments</li>
<li>1st argument <code>time</code>, which is the number of milliseconds after the function will resolve or reject</li>
<li>Second argument is the <code>age</code> upon (also a number) which you will use to return the string based on logic mentioned below</li>
<li>The function returns a promise, which will have 2 functions as arguments <code>resolve</code> and <code>reject</code> like any other promise.</li>
<li>The <code>resolve</code> function should be called with the argument <code>"You can vote"</code> after x milliseconds if <code>age</code> is greater than or equal to 18</li>
<li>The <code>reject</code> function should be called with the argument with "You can not vote" after x milliseconds if <code>age</code> less than 18</li>
</ol>
Note:- You only have to implement the function, in the example it
shows how your implemented question will be ran.Function will take two arguments
1) 1st argument will be a number which tells after how much milliseconds promise will be resolved or rejected.
2) 2nd argument will be a number (age)Function returns a promise which resolves to "You can vote" or rejects to "You can not vote".
If age >= 18 resolves to "You can vote" else rejects to "You can not vote".checkCanIVote(200, 70). then(data=>{
console. log(data) // prints 'You can vote'
}).catch((err)=>{
console.log(err) // does not do anything
})
checkCanIVote(200, 16). then(data=>{
console. log(data) // does not do anything
}).catch((err)=>{
console.log(err) // prints 'You can not vote'
}), I have written this Solution Code: function checkCanIVote(number, dat) {
return new Promise((res,rej)=>{
if(dat >= 18){
setTimeout(()=>{
res('You can vote')
},number)
}else{
setTimeout(()=>{
rej('You can not vote')
},number)
}
})
// return the output using return keyword
// do not console.log it
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int[] arr1=new int[5];
for(int i=0;i< arr1.length;i++)
arr1[i]=sc.nextInt();
Main m=new Main();
System.out.println(m.sumMaxMin(arr1));
}
public static int sumMaxMin(int []array){
int max =array[0];
int min=array[0];
int sum1=0;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
sum1= max +(min);
return sum1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, I have written this Solution Code:
function sumMaxMin(a,b,c,d,e){
// write code here
// return the output , do not use console.log here
return Math.max(a,b,c,d,e) + Math.min(a,b,c,d,e)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code: public static long SumOfDivisors(long N){
long sum=0;
long c=(long)Math.sqrt(N);
for(long i=1;i<=c;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){sum+=N/i;}
}
}
return sum;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
def SumOfDivisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 1
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num/i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result); , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this question, you will be adding a new method called <code>squareAndSort</code> to the <code>Array prototype</code>. This means that any array created in the future will have this method available to it. This method will be called on the <code>array with number and string data type elements</code>.
The method should <code>filter</code> the <code>number datatype</code> from the <code>string datatype</code> elements and <code>return</code> a <code>sorted squared array in ascending order</code> of the remaining number element array.The squareAndSort method will be called on an array with elements of number and string datatypes.The squareAndSort method should return an array with elements of number datatype.const numbers = [5,2,1,"four",4];
console. log(numbers. squareAndSort()); // [ 1, 4, 16, 25 ], I have written this Solution Code: Array.prototype.squareAndSort = function() {
const filteredArray = this.filter(element => typeof element === "number");
const squaredArray = filteredArray.map(number => number ** 2);
const sortedArray = squaredArray.sort((a, b) => a - b);
return sortedArray;
};, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton loves EVEN numbers.
You are given two integers N and M. Generate 5 unique even numbers for Newton between N and M (excluding both).The first and only line of input contains integer N and integer M.
Constraints
-10<sup>3</sup> ≤ N ≤ M ≤ 10<sup>3</sup>
10 ≤ M-NThe only line of output contains 5 singly spaced integers satisfying the constraints.Sample Input
0 20
Sample Output
2 6 8 18 14, I have written this Solution Code: N, M = map(int, input().split())
start = N
if N % 2 != 0:
start = N+1
else:
start = N+2
print(f'{start} {start+2} {start+4} {start+6} {start+8}'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: n=int(input())
li = list(map(int,input().strip().split()))
sum=0
for i in li:
if i>0:
sum+=i
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine().trim();
char[] express = s.toCharArray();
StringBuilder ans = new StringBuilder("");
Stack<Character> stack = new Stack<>();
for(int i=0; i<express.length; i++){
if(express[i]>=65 && express[i]<=90){
ans.append(express[i]);
}else if(express[i]==')'){
while(stack.peek()!='('){
ans.append(stack.peek());
stack.pop();
}
stack.pop();
}else if(stack.empty() || express[i]=='(' || precedence(express[i])>=precedence(stack.peek())){
stack.push(express[i]);
}else{
while(!stack.empty() && stack.peek()!='(' && precedence(stack.peek())<=express[i]){
ans.append(stack.peek());
stack.pop();
}
stack.push(express[i]);
}
}
while(!stack.empty()){
ans.append(stack.peek());
stack.pop();
}
System.out.println(ans);
}
static int precedence(char operator){
int precedenceValue;
if(operator=='+' || operator=='-')
precedenceValue = 1;
else if(operator=='*' || operator=='/')
precedenceValue = 2;
else if(operator == '^')
precedenceValue = 3;
else
precedenceValue = -1;
return precedenceValue;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: string=input()
stack=list()
preced={'+':1,'-':1,'*':2,'/':2,'^':3}
for i in string:
if i!=')':
if i=='(':
stack.append(i)
elif i in ['*','+','/','-','^']:
while(stack and stack[-1]!='(' and preced[i]<=preced[stack[-1]]):
print(stack.pop(),end="")
stack.append(i)
else:
print(i,end="")
else:
while(stack and stack[-1]!='('):
print(stack.pop(),end="")
stack.pop()
while(stack):
print(stack.pop(),end=""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
// The main function to convert infix expression
//to postfix expression
void infixToPostfix(string s)
{
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
for(int i = 0; i < l; i++)
{
// If the scanned character is an operand, add it to output string.
if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))
ns+=s[i];
// If the scanned character is an ‘(‘, push it to the stack.
else if(s[i] == '(')
st.push('(');
// If the scanned character is an ‘)’, pop and to output string from the stack
// until an ‘(‘ is encountered.
else if(s[i] == ')')
{
while(st.top() != 'N' && st.top() != '(')
{
char c = st.top();
st.pop();
ns += c;
}
if(st.top() == '(')
{
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else{
while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while(st.top() != 'N')
{
char c = st.top();
st.pop();
ns += c;
}
cout << ns << endl;
}
//Driver program to test above functions
int main()
{
string exp;
cin>>exp;
infixToPostfix(exp);
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: static void verticalFive(){
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
}
static void horizontalFive(){
System.out.print("* * * * *");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: def vertical5():
for i in range(0,5):
print("*",end="\n")
#print()
def horizontal5():
for i in range(0,5):
print("*",end=" ")
vertical5()
print(end="\n")
horizontal5(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Morgan was opening some boxes when she found one bearing a message “For Morgan, Dad loves you 3000". She wants to open the box but she needs to solve the question to open it. She asks Peter to do that. Help Peter and Morgan solve the question so that they can see what's inside the box.
Given two arrays Arr1 and Arr2 of size N1 and N2. Your task is to find the sum of all elements that are common to both arrays. If there are no common elements the output would be 0.
Note: The arrays may contain duplicate elements. However, you need to sum only unique elements that are common to both arrays.The first line of input contains N1 and N2 separated by a space. The second line contains N1 space separated elements of Arr1. The third line contains N2 space separated elements of Arr2.
Constraints:
1 <= N1, N2 <= 10<sup>6</sup>
1 <= Arr1[i], Arr2[i] <= 1000000000Print the sum of common elements.Sample Input:
5 6
1 2 3 4 5
2 3 4 5 6 7
Sample Output:
14
Explanation:- Common elements = 2, 3, 4 , 5
sum= 2 + 3 + 4 + 5 = 14
Sample Input:-
3 3
1 2 3
4 5 6
Sample Output:-
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc = new FastReader();
int n1 = sc.nextInt();
int n2 = sc.nextInt();
HashSet<Integer> hs = new HashSet<>();
long sum = 0l;
for(int i=0;i<n1;i++){
int curr = sc.nextInt();
hs.add(curr);
}
for(int i=0;i<n2;i++){
int sl = sc.nextInt();
if(hs.contains(sl)){
sum+=sl;
hs.remove(sl);
}
}
System.out.println(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Morgan was opening some boxes when she found one bearing a message “For Morgan, Dad loves you 3000". She wants to open the box but she needs to solve the question to open it. She asks Peter to do that. Help Peter and Morgan solve the question so that they can see what's inside the box.
Given two arrays Arr1 and Arr2 of size N1 and N2. Your task is to find the sum of all elements that are common to both arrays. If there are no common elements the output would be 0.
Note: The arrays may contain duplicate elements. However, you need to sum only unique elements that are common to both arrays.The first line of input contains N1 and N2 separated by a space. The second line contains N1 space separated elements of Arr1. The third line contains N2 space separated elements of Arr2.
Constraints:
1 <= N1, N2 <= 10<sup>6</sup>
1 <= Arr1[i], Arr2[i] <= 1000000000Print the sum of common elements.Sample Input:
5 6
1 2 3 4 5
2 3 4 5 6 7
Sample Output:
14
Explanation:- Common elements = 2, 3, 4 , 5
sum= 2 + 3 + 4 + 5 = 14
Sample Input:-
3 3
1 2 3
4 5 6
Sample Output:-
0, I have written this Solution Code: N1,N2=(map(int,input().split()))
arr1=set(map(int,input().split()))
arr2=set(map(int,input().split()))
arr1=list(arr1)
arr2=list(arr2)
arr1.sort()
arr2.sort()
i=0
j=0
sum1=0
while i<len(arr1) and j<len(arr2):
if arr1[i]==arr2[j]:
sum1+=arr1[i]
i+=1
j+=1
elif arr1[i]>arr2[j]:
j+=1
else:
i+=1
print(sum1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Morgan was opening some boxes when she found one bearing a message “For Morgan, Dad loves you 3000". She wants to open the box but she needs to solve the question to open it. She asks Peter to do that. Help Peter and Morgan solve the question so that they can see what's inside the box.
Given two arrays Arr1 and Arr2 of size N1 and N2. Your task is to find the sum of all elements that are common to both arrays. If there are no common elements the output would be 0.
Note: The arrays may contain duplicate elements. However, you need to sum only unique elements that are common to both arrays.The first line of input contains N1 and N2 separated by a space. The second line contains N1 space separated elements of Arr1. The third line contains N2 space separated elements of Arr2.
Constraints:
1 <= N1, N2 <= 10<sup>6</sup>
1 <= Arr1[i], Arr2[i] <= 1000000000Print the sum of common elements.Sample Input:
5 6
1 2 3 4 5
2 3 4 5 6 7
Sample Output:
14
Explanation:- Common elements = 2, 3, 4 , 5
sum= 2 + 3 + 4 + 5 = 14
Sample Input:-
3 3
1 2 3
4 5 6
Sample Output:-
0, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
signed main()
{
int n,m;
cin>>n>>m;
set<int> ss,yy;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
ss.insert(a);
}
for(int i=0;i<m;i++)
{
int a;
cin>>a;
if(ss.find(a)!=ss.end()) yy.insert(a);
}
int sum=0;
for(auto it:yy)
{
sum+=it;
}
cout<<sum<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.The first line contains the number of test cases T. Each of the next T lines contains two integers N and M which is size of matrix.
Constraints:
1 <= T <= 100
1 <= N, M <= 100For each testcase in a new line, print the required answer, i.e, number of possible ways to place knights.Sample Input:
3
2 2
2 3
4 5
Sample Output:
12
26
312
Explanation:
Test Case 1: We can place a black and a white knight in 12 possible ways such that none of them attacks each other., I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.BigInteger;
class Main{
public static void main (String[] args) throws IOException {
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
int T = Integer.parseInt(s);
for (int t=0; t<T; t++) {
s = reader.readLine();
int index = s.indexOf(' ');
long N = Integer.parseInt(s.substring(0,index));
long M = Integer.parseInt(s.substring(index+1));
if (N < M) {
long temp = N;
N = M;
M = temp;
}
long answer;
if (M == 1) {
answer = 0;
} else if (M == 2) {
if (N == 2) {
answer = 0;
} else if (N == 3) {
answer = 4;
} else {
answer = 2*N*M-8;
}
} else if (M == 3) {
if (N == 3) {
answer = 16;
} else {
answer = 4*N*M-20;
}
} else {
answer = (N-4)*(8*M-12)+2*(4*M-6)+2*(6*M-10);
}
BigInteger all = BigInteger.valueOf(N*M);
BigInteger result = all.multiply(all.add(BigInteger.ONE.negate())).add(BigInteger.valueOf(-answer));
output.append(result).append("\n");
}
System.out.print(output);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.The first line contains the number of test cases T. Each of the next T lines contains two integers N and M which is size of matrix.
Constraints:
1 <= T <= 100
1 <= N, M <= 100For each testcase in a new line, print the required answer, i.e, number of possible ways to place knights.Sample Input:
3
2 2
2 3
4 5
Sample Output:
12
26
312
Explanation:
Test Case 1: We can place a black and a white knight in 12 possible ways such that none of them attacks each other., I have written this Solution Code: t = int(input())
for i in range(t):
n, m = input().split()
n = int(n)
m = int(m)
if n < m:
n, m = m, n
if n == 1 or m == 1:
print(n * (n - 1))
else:
b = n * m
print((b * (b - 1)) - (4 * (((n - 1) * (m - 2)) + ((m - 1) * (n - 2))))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.The first line contains the number of test cases T. Each of the next T lines contains two integers N and M which is size of matrix.
Constraints:
1 <= T <= 100
1 <= N, M <= 100For each testcase in a new line, print the required answer, i.e, number of possible ways to place knights.Sample Input:
3
2 2
2 3
4 5
Sample Output:
12
26
312
Explanation:
Test Case 1: We can place a black and a white knight in 12 possible ways such that none of them attacks each other., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
int t;cin>>t;while(t--){
long n,m,c,count=0,len=0;
cin>>n>>m;
c=n*m;
count=c*(c-1);
if(m>1 && n>1){
len+=(2*(m-2))*(n-1);
len+=(2*(n-2))*(m-1);
len*=2;}
/*for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i+1<n && j+2<m)
len++;
if(i+1<n && j-2>=0)
len++;
if(i-1>=0 && j+2<m)
len++;
if(i-1>=0 && j-2>=0)
len++;
if(i+2<n && j+1<m)
len++;
if(i+2<n && j-1>=0)
len++;
if(i-2>0 && j+1<m)
len++;
if(i-2>=0 && j-1>=0)
len++;
}
}*/
count-=len;
cout<<count<<endl;
}
//code
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer A, find the value of A + A<sup>2</sup> + A<sup>3</sup> +. . .+ A<sup>1000</sup>. As the answer can be large, output answer modulo 10<sup>9</sup>+7.The first and the only line of input contains a single integer A.
Constraints
1 <= A <= 10<sup>9</sup>Output a single integer, value of sum modulo 10<sup>9</sup>+7.Sample Input
1
Sample Output
1000
Explanation: 1 + 1<sup>2</sup> + 1<sup>3</sup> +. . + 1<sup>1000</sup> = 1000
Sample Input
157
Sample Output
731862470
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long n=Long.parseLong(br.readLine());
double sum=0;
long m=n;
for(int i=1;i<=1000;i++){
sum=sum+m;
m=(m*n)%1000000007;
}
System.out.println(((long)(sum))%1000000007);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer A, find the value of A + A<sup>2</sup> + A<sup>3</sup> +. . .+ A<sup>1000</sup>. As the answer can be large, output answer modulo 10<sup>9</sup>+7.The first and the only line of input contains a single integer A.
Constraints
1 <= A <= 10<sup>9</sup>Output a single integer, value of sum modulo 10<sup>9</sup>+7.Sample Input
1
Sample Output
1000
Explanation: 1 + 1<sup>2</sup> + 1<sup>3</sup> +. . + 1<sup>1000</sup> = 1000
Sample Input
157
Sample Output
731862470
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int a; cin>>a;
int cur = 1;
int ans = 0;
For(i, 1, 1001){
cur *= a;
cur %= MOD;
ans += cur;
ans %= MOD;
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer A, find the value of A + A<sup>2</sup> + A<sup>3</sup> +. . .+ A<sup>1000</sup>. As the answer can be large, output answer modulo 10<sup>9</sup>+7.The first and the only line of input contains a single integer A.
Constraints
1 <= A <= 10<sup>9</sup>Output a single integer, value of sum modulo 10<sup>9</sup>+7.Sample Input
1
Sample Output
1000
Explanation: 1 + 1<sup>2</sup> + 1<sup>3</sup> +. . + 1<sup>1000</sup> = 1000
Sample Input
157
Sample Output
731862470
, I have written this Solution Code: n = int(input())
if n == 1:
print('1000')
else:
print((n*(n**1000-1)//(n-1))%(10**9+7)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For an integer N, your task is to calculate sum of first N natural numbers.<b>User Task:</b>
Since this will be a functional problem, you don't have to worry about input. You just have to complete the function <b>sum()</b> which takes the integer N as a parameter.
Constraints:
1 <= N < = 100000000Print the sum of first N natural numbers.Sample Input:-
5
Sample Output:-
15
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code:
static void sum(int N){
long x=N;
x=x*(x+1);
x=x/2;
System.out.print(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A</b> of <b>N</b> integers. Your teacher has asked you to find the maximum quotient of the array. Quotient of an array A of size N can be calculated in the following way:
1) Let the sum of the N elements of array A be <b>X</b>.
2) Let pre[] be another array of size N in which for every index i (1 <= i <= N), <b>pre<sub>i</sub></b> gives us the sum of elements <b>A<sub>1</sub> + A<sub>2</sub> +. . + A<sub>i</sub></b>.
3) Quotient of the array A will be equal to the <b>summation of the floored value of X/pre<sub>i</sub> over every value of i from 1 to N</b>.
You can swap any two elements of the array A. FInd the <b>maximum</b> possible quotient of array A.First line of the input contains a single integer N.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>8</sup>Print the maximum possible quotient of array A.Sample Input:
3
1 1 3
Sample Output:
8
Explaination:
Array pre[] = [1, 2, 5]
Sum X of array A = 5
Quotient of array A = 5/1 + 5/2 + 5/5 = 5 + 2 + 1 = 8
It can be proved that their is no greater quotient of array A possible., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
long sum= 0;
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
sum += arr[i];
}
Arrays.sort(arr);
long currSum = 0;
long ans = 0;
for(int i=0; i<n; i++){
currSum += arr[i];
ans += sum/currSum;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A</b> of <b>N</b> integers. Your teacher has asked you to find the maximum quotient of the array. Quotient of an array A of size N can be calculated in the following way:
1) Let the sum of the N elements of array A be <b>X</b>.
2) Let pre[] be another array of size N in which for every index i (1 <= i <= N), <b>pre<sub>i</sub></b> gives us the sum of elements <b>A<sub>1</sub> + A<sub>2</sub> +. . + A<sub>i</sub></b>.
3) Quotient of the array A will be equal to the <b>summation of the floored value of X/pre<sub>i</sub> over every value of i from 1 to N</b>.
You can swap any two elements of the array A. FInd the <b>maximum</b> possible quotient of array A.First line of the input contains a single integer N.
The second line contains N space seperated integers A<sub>i</sub>
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>8</sup>Print the maximum possible quotient of array A.Sample Input:
3
1 1 3
Sample Output:
8
Explaination:
Array pre[] = [1, 2, 5]
Sum X of array A = 5
Quotient of array A = 5/1 + 5/2 + 5/5 = 5 + 2 + 1 = 8
It can be proved that their is no greater quotient of array A possible., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using T = pair<int, int>;
typedef long long ll;
const int mod = 1e9 + 7;
const int INF = 1e9;
void solve(){
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for(auto &i : a) cin >> i, sum += i;
int ans = 0;
int cur = 0;
sort(all(a));
for(int i = 0; i < n; i++){
cur += a[i];
ans += sum/cur;
}
cout << ans;
}
signed main(){
fast
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
if(i != t) cout << endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to input three numbers from user and find maximum between three numbers using ternary operatorThe first line of the input contains three numbers a, b and c.
<b>Constraints<b>
1<= a, b, c <= 1e9Print the maximum NumberSample Input
12 14 15
Sample Output
15, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int a=Integer.parseInt(s[0]);
int b=Integer.parseInt(s[1]);
int c=Integer.parseInt(s[2]);
int max = (a>b) ? a : (b>c) ? b : c;
System.out.print(max);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to input three numbers from user and find maximum between three numbers using ternary operatorThe first line of the input contains three numbers a, b and c.
<b>Constraints<b>
1<= a, b, c <= 1e9Print the maximum NumberSample Input
12 14 15
Sample Output
15, I have written this Solution Code: a,b,c=map(int,input().split())
print(max(a,b,c)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to input three numbers from user and find maximum between three numbers using ternary operatorThe first line of the input contains three numbers a, b and c.
<b>Constraints<b>
1<= a, b, c <= 1e9Print the maximum NumberSample Input
12 14 15
Sample Output
15, I have written this Solution Code: #include <stdio.h>
int main()
{
int num1, num2, num3, max;
scanf("%d%d%d", &num1, &num2, &num3);
max = (num1 > num2 && num1 > num3) ? num1 :
(num2 > num3) ? num2 : num3;
printf("%d", max);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: def minDistanceCoveredBySara(N):
if N%4==1 or N%4==2:
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: static int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 ≤ N ≤ 2000
1 ≤ A<sub>i</sub> ≤ B<sub>i</sub> ≤ 2000
0 ≤ C<sub>i</sub> ≤ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer — the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: l = [0]*2001
k = []
n = int(input())
for i in range(n):
a,b,c = map(int,input().split())
k.append([b,(a,c)])
k.sort()
for b,aa in k:
a = aa[0]
c = aa[1]
cnt = 0
for i in range(b,a-1,-1):
if l[i]:
cnt+=1
if cnt>=c:
continue
else:
for i in range(b,a-1,-1):
if not l[i]:
l[i]=1
cnt+=1
if cnt==c:
break
print(sum(l)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 ≤ N ≤ 2000
1 ≤ A<sub>i</sub> ≤ B<sub>i</sub> ≤ 2000
0 ≤ C<sub>i</sub> ≤ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer — the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: //Author: Xzirium
//Time and Date: 00:28:35 28 December 2021
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t clk;
clk = clock();
//-----------------------------------------------------------------------------------------------------------//
READV(N);
vector<pair<pll,ll>> Z;
FORI(i,0,N)
{
READV(a);
READV(b);
READV(c);
Z.pb({{b,a},c});
}
sort(Z.begin(),Z.end());
ordered_set1<ll> unused;
FORI(i,1,2001)
{
unused.insert(i);
}
ll ans=0;
FORI(i,0,N)
{
ll a=Z[i].fi.se;
ll b=Z[i].fi.fi;
ll c=Z[i].se;
ll curr=b-a+1-(unused.order_of_key(a-1)-unused.order_of_key(b));
while(curr<c)
{
ans++;
curr++;
auto it=unused.lower_bound(b);
unused.erase(it);
}
}
cout<<ans<<endl;
//-----------------------------------------------------------------------------------------------------------//
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] and a number K where K is not greater than the size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
<b>Note:</b>
Do Not Use sort() STL function, Use heap data structure.The input line contains T, denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers N and K. Second-line contains N space-separated integer denoting elements of the array.
<b>Constraints:</b>
1 <= T <= 50
1 <= N <= 10000
1 <= K <= N
1 <= arr[i] <= 10<sup>6</sup>Corresponding to each test case, print the kth smallest element in a new line.Sample Input
1
6 3
7 10 4 3 20 15
Sample Output
7
<b>Explanation:</b>
Sorted array: 3 4 7 10 15 20, 7 is the third element, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void sort(int arr[])
{
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
static void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
sort(arr);
System.out.println(arr[k-1]);
arr=null;
System.gc();
}
sc.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] and a number K where K is not greater than the size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
<b>Note:</b>
Do Not Use sort() STL function, Use heap data structure.The input line contains T, denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers N and K. Second-line contains N space-separated integer denoting elements of the array.
<b>Constraints:</b>
1 <= T <= 50
1 <= N <= 10000
1 <= K <= N
1 <= arr[i] <= 10<sup>6</sup>Corresponding to each test case, print the kth smallest element in a new line.Sample Input
1
6 3
7 10 4 3 20 15
Sample Output
7
<b>Explanation:</b>
Sorted array: 3 4 7 10 15 20, 7 is the third element, I have written this Solution Code: for _ in range(int(input())):
s,k = map(int,input().split())
lis=list(map(int,input().split()))
lis.sort()
print(lis[k-1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] and a number K where K is not greater than the size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct.
<b>Note:</b>
Do Not Use sort() STL function, Use heap data structure.The input line contains T, denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers N and K. Second-line contains N space-separated integer denoting elements of the array.
<b>Constraints:</b>
1 <= T <= 50
1 <= N <= 10000
1 <= K <= N
1 <= arr[i] <= 10<sup>6</sup>Corresponding to each test case, print the kth smallest element in a new line.Sample Input
1
6 3
7 10 4 3 20 15
Sample Output
7
<b>Explanation:</b>
Sorted array: 3 4 7 10 15 20, 7 is the third element, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n, k;
cin >> n >> k;
priority_queue<int> s;
for(int i = 1; i <= n; i++){
int p; cin >> p;
s.push(-p);
}
int ans = 0;
while(k--){
ans = -s.top();
s.pop();
}
cout << ans << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String [] str=br.readLine().trim().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(str[i]);
}
Arrays.sort(a);
int size=a[n-1]+1;
int c[]=new int[size];
for(int i=0;i<size;i++) c[i]=0;
for(int i=0;i<n;i++) c[a[i]]++;
int max=0,freq=c[1];
for(int i=2;i<size;i++){
if(freq<=c[i]){
freq=c[i];
max=i;
}
}
System.out.println(max);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: n = int(input())
a = [int(x) for x in input().split()]
freq = {}
for x in a:
if x not in freq:
freq[x] = 1
else:
freq[x] += 1
mx = max(freq.values())
rf = sorted(freq)
for i in range(len(rf) - 1, -1, -1):
if freq[rf[i]] == mx:
print(rf[i])
break, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
int mx = 0, id = -1;
for(int i = 1; i <= 100; i++){
if(a[i] >= mx)
mx = a[i], id = i;
}
cout << id;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
import java.math.BigInteger;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger sum;
String ip1 = sc.next();
String ip2 = sc.next();
BigInteger a = new BigInteger(ip1);
BigInteger b = new BigInteger(ip2);
sum = a.add(b);
System.out.println(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-02-03 02:46:30
**/
#include <bits/stdc++.h>
#define NX 105
#define MX 3350
using namespace std;
const int mod = 998244353;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
typedef long long INT;
const int pb = 10;
const int base_digits = 9;
const int base = 1000000000;
const int DIV = 100000;
struct bigint {
vector<int> a;
int sign;
bigint() : sign(1) {}
bigint(INT v) { *this = v; }
bigint(const string &s) { read(s); }
void operator=(const bigint &v) { sign = v.sign, a = v.a; }
void operator=(INT v) {
sign = 1;
if (v < 0) sign = -1, v = -v;
for (; v > 0; v = v / base) a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) ||
carry;
i++) {
if (i == (int)res.a.size()) res.a.push_back(0);
res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry) res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int)v.a.size() || carry; i++) {
res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry) res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int)a.size() || carry; i++) {
if (i == (int)a.size()) a.push_back(0);
INT cur = a[i] * (INT)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> DIVmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((INT)base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0) r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const { return DIVmod(*this, v).first; }
bigint operator%(const bigint &v) const { return DIVmod(*this, v).second; }
void operator/=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = (int)a.size() - 1, rem = 0; i >= 0; i--) {
INT cur = a[i] + rem * (INT)base;
a[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0) v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (INT)base) % v;
return m * sign;
}
void operator+=(const bigint &v) { *this = *this + v; }
void operator-=(const bigint &v) { *this = *this - v; }
void operator*=(const bigint &v) { *this = *this * v; }
void operator/=(const bigint &v) { *this = *this / v; }
bool operator<(const bigint &v) const {
if (sign != v.sign) return sign < v.sign;
if (a.size() != v.a.size()) return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i]) return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const { return v < *this; }
bool operator<=(const bigint &v) const { return !(v < *this); }
bool operator>=(const bigint &v) const { return !(*this < v); }
bool operator==(const bigint &v) const { return !(*this < v) && !(v < *this); }
bool operator!=(const bigint &v) const { return *this < v || v < *this; }
void trim() {
while (!a.empty() && !a.back()) a.pop_back();
if (a.empty()) sign = 1;
}
bool isZero() const { return a.empty() || (a.size() == 1 && !a[0]); }
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
INT longValue() const {
INT res = 0;
for (int i = a.size() - 1; i >= 0; i--) res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) { return b.isZero() ? a : gcd(b, a % b); }
friend bigint lcm(const bigint &a, const bigint &b) { return a / gcd(a, b) * b; }
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-') sign = -sign;
pos++;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++) x = x *
pb +
s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream &operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream &operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1) stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int)v.a.size() - 2; i >= 0; --i) stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
vector<INT> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int)p.size(); i++) p[i] = p[i - 1] * pb;
vector<int> res;
INT cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int)a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while (!res.empty() && !res.back()) res.pop_back();
return res;
}
typedef vector<INT> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++) a2[i] += a1[i];
for (int i = 0; i < k; i++) b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int)a1b1.size(); i++) r[i] -= a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) r[i] -= a2b2[i];
for (int i = 0; i < (int)r.size(); i++) res[i + k] += r[i];
for (int i = 0; i < (int)a1b1.size(); i++) res[i] += a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a5 = convert_base(this->a, base_digits, 5);
vector<int> b5 = convert_base(v.a, base_digits, 5);
vll a(a5.begin(), a5.end());
vll b(b5.begin(), b5.end());
while (a.size() < b.size()) a.push_back(0);
while (b.size() < a.size()) b.push_back(0);
while (a.size() & (a.size() - 1)) a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int)c.size(); i++) {
INT cur = c[i] + carry;
res.a.push_back((int)(cur % DIV));
carry = (int)(cur / DIV);
}
res.a = convert_base(res.a, 5, base_digits);
res.trim();
return res;
}
inline bool isOdd() { return a[0] & 1; }
};
int main() {
bigint n, m;
cin >> n >> m;
cout << n + m << "\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: n,m = map(int,input().split())
print(n+m) , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. Find the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.The first line of input contains N, the size of the array.
The second line of input contains N space seperated integers A<sub>i</sub>.
Constraints:
1 <= N <= 10<sup>5</sup>
-10<sup>9</sup> <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.Sample Input:
5
3 -2 4 -1 4
Sample Output:
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
long arr[] = new long[n];
long pre[] =new long[n];
long sum=0;
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(st.nextToken());
sum+=arr[i];
pre[i]=sum;
}
int cnt=0;
for(int i=0;i<pre.length;i++){
if(i==0){
if(0>=(pre[pre.length-1]-pre[i]))cnt++;
continue;
}else if(i==pre.length-1){
if(pre[pre.length-2]>=0)cnt++;
continue;
}
if(pre[i-1]>=(pre[pre.length-1]-pre[i])){
cnt++;
}
}
System.out.print(cnt);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. Find the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.The first line of input contains N, the size of the array.
The second line of input contains N space seperated integers A<sub>i</sub>.
Constraints:
1 <= N <= 10<sup>5</sup>
-10<sup>9</sup> <= A<sub>i</sub> <= 10<sup>9</sup>Print the number of indexes i (1 <= i <= N) such that the sum of the elements to its right is atmost equal to the sum of elements to its left.Sample Input:
5
3 -2 4 -1 4
Sample Output:
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
using T = pair<int, int>;
signed main(){
int n;
cin >> n;
vector<int> a(n + 1), pre(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
int sum = pre.back();
int ans = 0;
for(int i = 1; i <= n; i++){
if(pre[i - 1] >= sum - pre[i]) ans++;
}
cout << ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.print(nonRepeatChar(s));
}
static int nonRepeatChar(String s){
char count[] = new char[256];
for(int i=0; i< s.length(); i++){
count[s.charAt(i)]++;
}
for (int i=0; i<s.length(); i++) {
if (count[s.charAt(i)]==1){
return i;
}
}
return -1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: from collections import defaultdict
s=input()
d=defaultdict(int)
for i in s:
d[i]+=1
ans=-1
for i in range(len(s)):
if(d[s[i]]==1):
ans=i
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-10 12:51:16
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
int firstUniqChar(string s) {
map<char, int> charCount;
int len = s.length();
for (int i = 0; i < len; i++) {
charCount[s[i]]++;
}
for (int i = 0; i < len; i++) {
if (charCount[s[i]] == 1)
return i;
}
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
cin>>str;
cout<<firstUniqChar(str)<<"\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph, the task is to detect if there is a cycle in the undirected graph or not.The first line of input contains two integers N and M which denotes the no of vertices and no of edges in the graph respectively.
Next M lines contain space-separated integers u and v denoting that there is a directed edge from u to v.
Constraints:
1 <= N, M <= 1000
0 <= u, v <= N-1, u != v
There are no self loops or multiple edges.
Print 'Yes' if there is a cycle in the graph otherwise print 'No'Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output 1:
Yes
Explanation:
There is a cycle with nodes 0, 1, 2, 3
Sample Input 2:
4 3
0 1
1 2
2 3
Sample Output 2:
No
Explanation:
There is no cycle in this graph, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static ArrayList<Integer> ar[];
static boolean st[];
static boolean status=false;
public static void main (String[] args) throws Exception {
BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
try{
String sa[]=rd.readLine().split(" ");
int n=Integer.parseInt(sa[0]);
int e=Integer.parseInt(sa[1]);
st=new boolean[n+1];
ar=new ArrayList[n+1];
for(int i=0;i<n;i++)
ar[i]=new ArrayList<Integer>();
for(int i=0;i<e;i++)
{
sa=rd.readLine().split(" ");
int u=Integer.parseInt(sa[0]);;
int v=Integer.parseInt(sa[1]);;
ar[u].add(v);
ar[v].add(u);
}
dfs(0);
if(status)
System.out.println("Yes");
else
System.out.println("No");
}
catch(Exception e)
{
if(status)
System.out.println("Yes");
else
System.out.println("Yes");
}
}
static int p=-1;
static void dfs(int k)
{
try{
Stack <Integer> st1=new Stack<>();
int l=0;
st1.add(k);
while(!st1.empty())
{
if(!st1.empty())
l=st1.pop();
if(st[l])
{
status=true;
return;
}
st[l]=true;
for(int i=0;i<ar[l].size();i++)
{
if(!st[ar[l].get(i)])
st1.push(ar[l].get(i));
}
}
}
catch(Exception e)
{
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph, the task is to detect if there is a cycle in the undirected graph or not.The first line of input contains two integers N and M which denotes the no of vertices and no of edges in the graph respectively.
Next M lines contain space-separated integers u and v denoting that there is a directed edge from u to v.
Constraints:
1 <= N, M <= 1000
0 <= u, v <= N-1, u != v
There are no self loops or multiple edges.
Print 'Yes' if there is a cycle in the graph otherwise print 'No'Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output 1:
Yes
Explanation:
There is a cycle with nodes 0, 1, 2, 3
Sample Input 2:
4 3
0 1
1 2
2 3
Sample Output 2:
No
Explanation:
There is no cycle in this graph, I have written this Solution Code:
def dfs(adjList,vis,start,prev):
vis[start]=True
for i in adjList[start]:
if i == prev:
continue
if vis[i]:
return True
else:
dfs(adjList,vis,i,start)
return False
np=list(map(int,input().rstrip().split()))
n=np[0]
e=np[1]
adjList = [[] for i in range(n+1)]
for i in range(e):
edge = list(map(int,input().rstrip().split()))
adjList[edge[0]].append(edge[1])
adjList[edge[1]].append(edge[0])
vis = [False for i in range(n+1)]
flag=0
for i in range(n):
if not vis[i]:
if dfs(adjList,vis,i,0):
flag=1
break
if flag:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph, the task is to detect if there is a cycle in the undirected graph or not.The first line of input contains two integers N and M which denotes the no of vertices and no of edges in the graph respectively.
Next M lines contain space-separated integers u and v denoting that there is a directed edge from u to v.
Constraints:
1 <= N, M <= 1000
0 <= u, v <= N-1, u != v
There are no self loops or multiple edges.
Print 'Yes' if there is a cycle in the graph otherwise print 'No'Sample Input 1:
4 5
0 1
1 2
2 3
3 0
0 2
Sample Output 1:
Yes
Explanation:
There is a cycle with nodes 0, 1, 2, 3
Sample Input 2:
4 3
0 1
1 2
2 3
Sample Output 2:
No
Explanation:
There is no cycle in this graph, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
vector<int> g[N];
int vis[N];
bool flag = 0;
void dfs(int u, int p){
vis[u] = 1;
for(auto i: g[u]){
if(i == p) continue;
if(vis[i] == 1)
flag = 1;
if(vis[i] == 0) dfs(i, u);
}
vis[u] = 2;
}
signed main() {
IOS;
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 0; i < n; i++){
if(vis[i]) continue;
dfs(i, n);
}
if(flag)
cout << "Yes";
else
cout << "No";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. Your task is to print the number of prime numbers before that number.The first line of the number of test cases T.
Next T lines contains the value of N.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 1000Print the number of primes numbers before that number.Sample Input 1:
3
10
19
4
Sample Output 1:
4
8
2, I have written this Solution Code: n = 1000
arr = [True for i in range(n+1)]
i = 2
while i*i <= n:
if arr[i] == True:
for j in range(i*2, n+1, i):
arr[j] = False
i +=1
arr2 = [0] * (n+1)
for i in range(2,n+1):
if arr[i]:
arr2[i] = arr2[i-1] + 1
else:
arr2[i] = arr2[i-1]
x = int(input())
for i in range(x):
y = int(input())
print(arr2[y]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. Your task is to print the number of prime numbers before that number.The first line of the number of test cases T.
Next T lines contains the value of N.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 1000Print the number of primes numbers before that number.Sample Input 1:
3
10
19
4
Sample Output 1:
4
8
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
vector<bool> sieve(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
int main() {
vector<bool> prime = sieve(1e5 + 1);
vector<int> prefix(1e5 + 1, 0);
for (int i = 1; i <= 1e5; i++) {
if (prime[i]) {
prefix[i] = prefix[i - 1] + 1;
} else {
prefix[i] = prefix[i - 1];
}
}
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
cout << prefix[n] << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code: import math
import os
import random
import re
import sys
def minimumAbsoluteDifference(arr):
diff_arr = []
sort_arr = sorted(arr)
for i in range(0, len(sort_arr)-1):
diff_arr.append(abs(sort_arr[i] - sort_arr[i+1]))
return min(diff_arr)
if __name__ == '__main__':
n=int(input())
n=input().split()
for i in range(len(n)):
n[i]=int(n[i])
print(minimumAbsoluteDifference(n)%(10**9+7)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int arrSize = sc.nextInt();
long arr[]= new long[arrSize];
for(int i = 0; i < arrSize; i++)
arr[i] = sc.nextLong();
System.out.println(minABSDiff(arr, arrSize));
}
static long minABSDiff(long arr[], int arrSize)
{
Arrays.sort(arr);
long ans=Long.MAX_VALUE;
for(int i = 1; i < arrSize; i++){
ans = Math.min(ans,arr[i]-arr[i-1]);}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N, find the minimum absolute difference between any two elements in the array.
We define the absolute difference between two elements ai and aj (where i != j ) is |ai - aj|.First line of input contains a single integer N, next line contains N space separated integers containing values of array A.
Constraints :
2 < = N < = 10^5
0 < = A[i] < = 10^10Print the minimum absolute difference between any two elements of the array.Sample Input :
5
2 9 0 4 5
Sample Input :
1
Sample Input:-
3
1 2 1
Sample Output:-
0, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 20000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long int
signed main()
{
int n;
cin>>n;
int a[n];
FOR(i,n){
cin>>a[i];}
sort(a,a+n);
int ans=LONG_MAX;
FOR1(i,1,n){
ans=min(ans,a[i]-a[i-1]);}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of N integers. The task is to count the number of ways to split given array elements into two disjoint groups, such that XOR of elements of each group is equal.The first line contains a single integer N, denoting the number of elements of the array.
The second line contains N integers.
1 <= N <= 50
1 <= A[i] <= 255Print a single integer denoting the number of ways to split the array into two groups with equal xor.
Note: The output may not fit in INT data typeSample Input:
3
1 2 3
Sample Output:
3
Explanation:
{(1), (2, 3)}, {(2), (1, 3)}, {(3), (1, 2)} are three ways with equal XOR value of two groups.
Sample Input:
4
5 2 3 2
Sample Output:
0, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long int
int a[55];
int countgroup(int n)
{
int xs = 0;
for (int i = 0; i < n; i++)
xs = xs ^ a[i];
// We can split only if XOR is 0. Since
// XOR of all is 0, we can consider all
// subsets as one group.
if (xs == 0)
return (1LL << (n-1)) - 1;
return 0;
}
// Driver Program
signed main()
{
int n; cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
cout << countgroup(n);
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array, your task is to print the minimum size of the set so that at least half of the integers of the array are removed.The first line contains n the size of the array and the next line contains input of the array
<b>Constraints</b>
1 <= n<= 1e5 (n is even)
1 <= arr[i] <= 1e5Return the minimum set size Sample Input :
10
3 3 3 3 5 5 5 2 2 7
Sample Output :
2
Explanation :
Choosing {3, 7} will make the new array [5, 5, 5, 2, 2] which has size 5 (i. e equal to half of the size of the old array).
Possible sets of size 2 are {3, 5}, {3, 2}, {5, 2}.
Choosing set {2, 7} is not possible as it will make the new array [3, 3, 3, 3, 5, 5, 5] which has a size greater than half of the size of the old array.
Sample Input 1:
5
7 7 7 7 7
Sample Output 1:
1
Explanation :
The only possible set you can choose is {7}. This will make the new array empty., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
int arr[] = new int[n];
String[] str = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(str[i]);
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
for (Map.Entry entry : map.entrySet()) {
maxHeap.add((Integer) entry.getValue());
}
long ans = 0;
int h=n/2;
int c=0;
while(ans<h) {
ans += maxHeap.poll();
c++;
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array, your task is to print the minimum size of the set so that at least half of the integers of the array are removed.The first line contains n the size of the array and the next line contains input of the array
<b>Constraints</b>
1 <= n<= 1e5 (n is even)
1 <= arr[i] <= 1e5Return the minimum set size Sample Input :
10
3 3 3 3 5 5 5 2 2 7
Sample Output :
2
Explanation :
Choosing {3, 7} will make the new array [5, 5, 5, 2, 2] which has size 5 (i. e equal to half of the size of the old array).
Possible sets of size 2 are {3, 5}, {3, 2}, {5, 2}.
Choosing set {2, 7} is not possible as it will make the new array [3, 3, 3, 3, 5, 5, 5] which has a size greater than half of the size of the old array.
Sample Input 1:
5
7 7 7 7 7
Sample Output 1:
1
Explanation :
The only possible set you can choose is {7}. This will make the new array empty., I have written this Solution Code: from collections import defaultdict
import numpy as np
n=int(input())
val=np.array([input().strip().split()],int).flatten()
d=defaultdict(int)
l=n
for i in val:
d[i]+=1
a=[]
for i in d:
a.append([d[i],i])
a.sort(reverse=True)
c=0
h=0
for i in a:
c+=1
h+=i[0]
if(h>(l//2-1)):
break
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer array arr. You can choose a set of integers and remove all the occurrences of these integers in the array, your task is to print the minimum size of the set so that at least half of the integers of the array are removed.The first line contains n the size of the array and the next line contains input of the array
<b>Constraints</b>
1 <= n<= 1e5 (n is even)
1 <= arr[i] <= 1e5Return the minimum set size Sample Input :
10
3 3 3 3 5 5 5 2 2 7
Sample Output :
2
Explanation :
Choosing {3, 7} will make the new array [5, 5, 5, 2, 2] which has size 5 (i. e equal to half of the size of the old array).
Possible sets of size 2 are {3, 5}, {3, 2}, {5, 2}.
Choosing set {2, 7} is not possible as it will make the new array [3, 3, 3, 3, 5, 5, 5] which has a size greater than half of the size of the old array.
Sample Input 1:
5
7 7 7 7 7
Sample Output 1:
1
Explanation :
The only possible set you can choose is {7}. This will make the new array empty., I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-12 19:43:00
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int minSetSize(vector<int> &arr) {
unordered_map<int, int> counter;
priority_queue<int> q;
int res = 0, removed = 0;
for (auto a : arr) counter[a]++;
for (auto c : counter) q.push(c.second);
while (removed < arr.size() / 2) {
removed += q.top();
q.pop();
res++;
}
return res;
}
int main() {
#ifdef LOCAL
auto start = std::chrono::high_resolution_clock::now();
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (auto &it : a) {
cin >> it;
}
cout << minSetSize(a) << "\n";
#ifdef LOCAL
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
cerr << "Time taken : " << ((long double)duration.count()) / ((long double)1e9) << "s " << endl;
#endif
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that takes the integer n as a parameter.
</b>Constraints:</b>
1 <= N <= 100
<b>Note:</b>
<i>But there is a catch here given user function has already code in it which may or may not be correct, now you need to figure out these and correct if it is required</i>Print all the even numbers from 1 to n.Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: public static void For_Loop(int n){
for(int i=2;i<=n;i+=2){
System.out.print(i+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long subarraysDivByK(int[] A, int k)
{
long ans =0 ;
int rem;
int[] freq = new int[k];
for(int i=0;i<A.length;i++)
{
rem = A[i]%k;
ans += freq[(k - rem)% k] ;
freq[rem]++;
}
return ans;
}
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int [] a = new int [n];
for(int i=0; i<n; i++)
a[i] = Integer.parseInt(input[i]);
System.out.println(subarraysDivByK(a, k));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: def countKdivPairs(A, n, K):
freq = [0] * K
for i in range(n):
freq[A[i] % K]+= 1
sum = freq[0] * (freq[0] - 1) / 2;
i = 1
while(i <= K//2 and i != (K - i) ):
sum += freq[i] * freq[K-i]
i+= 1
if( K % 2 == 0 ):
sum += (freq[K//2] * (freq[K//2]-1)/2);
return int(sum)
a,b=input().split()
a=int(a)
b=int(b)
arr=input().split()
for i in range(0,a):
arr[i]=int(arr[i])
print (countKdivPairs(arr,a, b)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
fast();
int n;
cin>>n;
int a;
int k;
cin>>k;
int fre[k];
FOR(i,k){
fre[i]=0;}
FOR(i,n){
cin>>a;
fre[a%k]++;
}
int ans=(fre[0]*(fre[0]-1))/2;
for(int i=1;i<=(k-1)/2;i++){
ans+=fre[i]*fre[k-i];
}
if(k%2==0){
ans+=(fre[k/2]*(fre[k/2]-1))/2;
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of n elements, check whether a given element x is present in arr[] or not.The first line contains a single integer N.
next line contains N space-separated integer arr[i]
next line contains a single integer target(k)
Constraints
1<=N<=10^5
1<=A[i]<=10^9Determine whether given number is present in array or not.Sample Input 1:
8
1 12 13 14 22 45 67 11123
14
Sample Output 1:
1
Explanation : 14 present at fourth index, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = sc.nextInt();
}
int count =0;
int x = sc.nextInt();
for(int i=0; i<n; i++){
if(arr[i]==x){
System.out.print(1);
count++;
}
}
if(count==0){
System.out.print(0);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of n elements, check whether a given element x is present in arr[] or not.The first line contains a single integer N.
next line contains N space-separated integer arr[i]
next line contains a single integer target(k)
Constraints
1<=N<=10^5
1<=A[i]<=10^9Determine whether given number is present in array or not.Sample Input 1:
8
1 12 13 14 22 45 67 11123
14
Sample Output 1:
1
Explanation : 14 present at fourth index, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int n,x;
cin>>n;
int arr[n];
for(int &i:arr)cin>>i;
cin>>x;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)? cout <<0:cout <<1;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer n, you have to print a Inverted half pyramid of size n.The first line contains a single Integer n.
<b>Constraints</b>
1 ≤ n ≤ 100Print inverted half pyramid of size n.Sample 1:
Input:
3
Output:
1 2 3
1 2
1
Explanation:
Inverted half pyramid of size 3., I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
assert n>=1&&n<=100 : "Input not valid";
// loop for rows
for(int i = n; i >= 1; i--) {
// loop for columns
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: John and Olivia once start fighting when Olivia sees John talking with another girl. So, you tell them to play the game of love in which you give them an array A of size N. Now, the game will play in turns in which a player must decrease the value at the smallest index (with a non-zero value) in A by x (x > 0). The player who cannot make a move will lose the game.
You being a loyal friend of Olivia, wants her to win. So, tell Olivia whether she should move first or second to win this game.
Assume both players will play optimally at every step of the game.The first line contains a single integer T (1 ≤ T ≤ 1000) — the number of test cases.
The first line of each test case contains a single integer N (1 ≤ N ≤ 100) — the size of the array A on which the game is to be played.
.
The second line contains n integers A<sub>1</sub>, A<sub>2</sub>, …, A<sub>N</sub> (1 ≤ A<sub>i</sub> ≤ 10<sup>9</sup>).For each test case, print on a new line whether Olivia should move "first" or "second" (without quotes) to win the game.Sample Input:
2
3
2 1 3
2
1 1
Sample Output:
first
second
Sample Explanation:
For the first test case, Olivia will remove 2 from the 1st index, then John has to remove 1 from the 2nd index, and finally, Olivia will remove 3 from the 3rd index.
For the second test case, John has to remove 1 from the 1st index, then Olivia removes 1 from the 2nd index., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> v(n);
bool ok=1;
for(auto &i:v) cin >> i;
int ans=1;
for(auto i:v){
if(i!=1) ok=0;
if(i==1) ans^=1;
else break;
}
if(!ok){
if(ans){
cout << "first\n";
}else{
cout << "second\n";
}
}else{
if(n&1){
cout << "first\n";
}else{
cout << "second\n";
}
}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Recently Ultron invented a new structure that he has called Xor Grid. It is an infinitely large grid G filled with numbers based on these three rules:
1. G[1, c] = 1 for all c ≥ 1
2. G[r, 1] = r for all r ≥ 1
3. G[r, c] = G[r - 1, c] xor G[r, c - 1] for all r > 1 and c > 1
Now Ultron is wondering, what are the xor sums of all the numbers within some specific rectangles of the Xor Grid?
<b>Note :</b>
Xor sum refers to successive xor operations on integers. For example, xor sum of integers x1, x2, x3, ., xn will be (. ((x1 xor x2) xor x3).. xor xn).The input contains four integers r1, r2, c1 and c2, where (r1, c1), (r2, c2) denotes the coordinates of top- left and bottom- right cells of the rectangle
<b>Constraints :</b>
1 ≤ r1 ≤ r2 ≤ 10^18
1 ≤ c1 ≤ c2 ≤ 10^18Output a single integer ― the xor sum of all the numbers within G[r1. r2, c1. c2].Sample Input 1:-
1 2 1 4
Sample Output 1:-
0
Sample Input 2:-
1 1 71 93
Sample Output 2:-
1
<b>Explanation:-</b>
Matrix:-
1 1 1 1 1 1. .. ..
2 3 2 3 2 3. .. ..
3 0 2 1 3 0. .. ..
Required sum = 1^1^1^1^2^3^2^3 = 0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
XorGrid solver = new XorGrid();
int testCount = 1;
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class XorGrid {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long r1 = in.readLong();
long r2 = in.readLong();
long c1 = in.readLong();
long c2 = in.readLong();
long ans = 0;
for (int i = 0; i < 62; i++) {
ans ^= f(r2, c2, i) ^ f(r1 - 1, c2, i);
ans ^= f(r2, c1 - 1, i) ^ f(r1 - 1, c1 - 1, i);
}
out.println(ans);
}
long f(long r, long c, int i) {
r += (1L << 62) - (1L << i) + 1;
c += (1L << i);
if ((r & c) != 0)
return 1L << i;
return 0;
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
try {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0)
return -1;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return buf[curChar++];
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
if (c == -1) throw new RuntimeException();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += (c - '0');
c = read();
} while (!isSpaceChar(c));
return negative ? (-res) : (res);
}
public String next() {
return readString();
}
public String readString() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Recently Ultron invented a new structure that he has called Xor Grid. It is an infinitely large grid G filled with numbers based on these three rules:
1. G[1, c] = 1 for all c ≥ 1
2. G[r, 1] = r for all r ≥ 1
3. G[r, c] = G[r - 1, c] xor G[r, c - 1] for all r > 1 and c > 1
Now Ultron is wondering, what are the xor sums of all the numbers within some specific rectangles of the Xor Grid?
<b>Note :</b>
Xor sum refers to successive xor operations on integers. For example, xor sum of integers x1, x2, x3, ., xn will be (. ((x1 xor x2) xor x3).. xor xn).The input contains four integers r1, r2, c1 and c2, where (r1, c1), (r2, c2) denotes the coordinates of top- left and bottom- right cells of the rectangle
<b>Constraints :</b>
1 ≤ r1 ≤ r2 ≤ 10^18
1 ≤ c1 ≤ c2 ≤ 10^18Output a single integer ― the xor sum of all the numbers within G[r1. r2, c1. c2].Sample Input 1:-
1 2 1 4
Sample Output 1:-
0
Sample Input 2:-
1 1 71 93
Sample Output 2:-
1
<b>Explanation:-</b>
Matrix:-
1 1 1 1 1 1. .. ..
2 3 2 3 2 3. .. ..
3 0 2 1 3 0. .. ..
Required sum = 1^1^1^1^2^3^2^3 = 0, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
long long f(long long r, long long c, int i) {
r += (1ll << 62) - (1ll << i) + 1;
c += (1ll << i);
if (r & c) return (1ll << i);
return 0;
}
int main() {
long long r1, r2, c1, c2;
scanf("%lld %lld %lld %lld"
, &r1, &r2, &c1, &c2);
r1--;
c1--;
long long ans = 0;
for (int i = 0; i < 60; i++) {
ans ^= f(r2, c2, i);
ans ^= f(r1, c2, i);
ans ^= f(r2, c1, i);
ans ^= f(r1, c1, i);
}
printf("%lld\n"
, ans);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Main {
public static void main (String[] args)
throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String str = read.readLine();
System.out.println(maxProduct(str));
}
public static long maxProduct(String str) {
StringBuilder sb = new StringBuilder(str);
int x = sb.length();
int[] dpl = new int[x];
int[] dpr = new int[x];
modifiedOddManacher(sb.toString(), dpl);
modifiedOddManacher(sb.reverse().toString(), dpr);
long max=1;
for(int i=0;i<x-1;i++)
max=Math.max(max, (1+(dpl[i]-1)*2L)*(1+(dpr[x-(i+1)-1]-1)*2L));
return max;
}
private static void modifiedOddManacher(String str, int[] dp){
int x = str.length();
int[] center = new int[x];
for(int l=0,r=-1,i=0;i<x;i++){
int radius = (i > r) ? 1 : Math.min(center[l+(r-i)], r-i+1);
while(i-radius>=0 && i+radius<x && str.charAt(i-radius)==str.charAt(i+radius)) {
dp[i+radius] = radius+1;
radius++;
}
center[i] = radius--;
if(i+radius>r){
l = i-radius;
r = i+radius;
}
}
for(int i=0, max=1;i<x;i++){
max = Math.max(max, dp[i]);
dp[i] = max;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, I have written this Solution Code: s=input()
n = len(s)
hlen = [0]*n
center = right = 0
for i in range(n):
if i < right:
hlen[i] = min(right - i, hlen[2*center - i])
while 0 <= i-1-hlen[i] and i+1+hlen[i] < len(s) and s[i-1-hlen[i]] == s[i+1+hlen[i]]:
hlen[i] += 1
if right < i+hlen[i]:
center, right = i, i+hlen[i]
left = [0]*n
right = [0]*n
for i in range(n):
left[i+hlen[i]] = max(left[i+hlen[i]], 2*hlen[i]+1)
right[i-hlen[i]] = max(right[i-hlen[i]], 2*hlen[i]+1)
for i in range(1, n):
left[~i] = max(left[~i], left[~i+1]-2)
right[i] = max(right[i], right[i-1]-2)
for i in range(1, n):
left[i] = max(left[i-1], left[i])
right[~i] = max(right[~i], right[~i+1])
print(max(left[i-1]*right[i] for i in range(1, n))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
#define S second
#define F first
#define int long long
/////////////
int v1[100001]={};
int v2[100001]={};
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
string s;
cin>>s;
n=s.length();
vector<int> d1(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
int c=0;
for(int i=0;i<n;++i)
{
int x=2*d1[i]-1;
int j=i+d1[i]-1;
while(v1[j]<x&&j>=i)
{
v1[j]=x;
x-=2;
++c;
--j;
}
}
for(int i=1;i<n;++i)
{
v1[i]=max(v1[i],v1[i-1]);
}
for(int i=n-1;i>=0;--i)
{
int x=2*d1[i]-1;
int j=i-d1[i]+1;
while(v2[j]<x&&j<=i)
{
v2[j]=x;
x-=2;
++j;
++c;
}
}
for(int i=n-2;i>=0;--i)
{
v2[i]=max(v2[i],v2[i+1]);
}
int ans=0;
for(int i=1;i<n;++i)
{
ans=max(ans,v1[i-1]*v2[i]);
}
cout<<ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: They say friendship is greater than love. Why not play the famous game "FLAMES".
The rules are super simple. Given two strings (all lowercase), remove all the letters that are common to both the strings from both the strings. You cannot erase a character in first string whichever corresponding same character in other string not exist.
For example, in the case
String 1: saumya
String 2: ansh
You can remove only 1 'a' and 1 's' from both the strings.
Remaining strings are:
String 1: umya
String 2: nh
Now all you need to do is find the sum of the remaining strings length % 6.
Output:
If obtained value is 1, output "Friends"
If obtained value is 2, output "Love"
If obtained value is 3, output "Affection"
If obtained value is 4, output "Marriage"
If obtained value is 5, output "Enemy"
If obtained value is 0, output "Siblings"You will be given two strings on different lines.
Constraints
1 <= Length of both the strings <= 100000Output a single string, the result of FLAMES test.Sample Input:-
saumya
ansh
Sample Output:-
Siblings
Explanation:-
after deleting characters :-
str1 = umya
str2 = nh
sum = 4+2
sum%6=0
, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
String str2 = br.readLine();
countCommonCharacters(str1, str2);
}
static void countCommonCharacters(String s1, String s2) {
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for (int i = 0; i < s1.length(); i++)
arr1[s1.codePointAt(i) - 97]++;
for (int i = 0; i < s2.length(); i++)
arr2[s2.codePointAt(i) - 97]++;
int lenToCut = 0;
for (int i = 0; i < 26; i++) {
int leastOccurrence = Math.min(arr1[i], arr2[i]);
lenToCut += (2 * leastOccurrence);
}
System.out.println(findRelation((s1.length() + s2.length() - lenToCut) % 6));
}
static String findRelation(int value) {
switch (value) {
case 1:
return "Friends";
case 2:
return "Love";
case 3:
return "Affection";
case 4:
return "Marriage";
case 5:
return "Enemy";
default:
return "Siblings";
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: They say friendship is greater than love. Why not play the famous game "FLAMES".
The rules are super simple. Given two strings (all lowercase), remove all the letters that are common to both the strings from both the strings. You cannot erase a character in first string whichever corresponding same character in other string not exist.
For example, in the case
String 1: saumya
String 2: ansh
You can remove only 1 'a' and 1 's' from both the strings.
Remaining strings are:
String 1: umya
String 2: nh
Now all you need to do is find the sum of the remaining strings length % 6.
Output:
If obtained value is 1, output "Friends"
If obtained value is 2, output "Love"
If obtained value is 3, output "Affection"
If obtained value is 4, output "Marriage"
If obtained value is 5, output "Enemy"
If obtained value is 0, output "Siblings"You will be given two strings on different lines.
Constraints
1 <= Length of both the strings <= 100000Output a single string, the result of FLAMES test.Sample Input:-
saumya
ansh
Sample Output:-
Siblings
Explanation:-
after deleting characters :-
str1 = umya
str2 = nh
sum = 4+2
sum%6=0
, I have written this Solution Code: name1 = input().strip().lower()
name2 = input().strip().lower()
listA = [0]*26
listB = [0]*26
for i in name1:
listA[ord(i)-ord('a')] = listA[ord(i)-ord('a')] + 1
for i in name2:
listB[ord(i)-ord('a')] = listB[ord(i)-ord('a')] + 1
count = 0
for i in range(0,26):
count = count+abs(listA[i]-listB[i])
res = ["Siblings","Friends","Love","Affection", "Marriage", "Enemy"]
print(res[count%6]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: They say friendship is greater than love. Why not play the famous game "FLAMES".
The rules are super simple. Given two strings (all lowercase), remove all the letters that are common to both the strings from both the strings. You cannot erase a character in first string whichever corresponding same character in other string not exist.
For example, in the case
String 1: saumya
String 2: ansh
You can remove only 1 'a' and 1 's' from both the strings.
Remaining strings are:
String 1: umya
String 2: nh
Now all you need to do is find the sum of the remaining strings length % 6.
Output:
If obtained value is 1, output "Friends"
If obtained value is 2, output "Love"
If obtained value is 3, output "Affection"
If obtained value is 4, output "Marriage"
If obtained value is 5, output "Enemy"
If obtained value is 0, output "Siblings"You will be given two strings on different lines.
Constraints
1 <= Length of both the strings <= 100000Output a single string, the result of FLAMES test.Sample Input:-
saumya
ansh
Sample Output:-
Siblings
Explanation:-
after deleting characters :-
str1 = umya
str2 = nh
sum = 4+2
sum%6=0
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 10000001
int main(){
string s1,s2;
cin>>s1>>s2;
string a[6];
a[1]= "Friends";
a[2]= "Love";
a[3]="Affection";
a[4]= "Marriage";
a[5]= "Enemy";
a[0]= "Siblings";
int b[26],c[26];
for(int i=0;i<26;i++){b[i]=0;c[i]=0;}
for(int i=0;i<s1.length();i++){
b[s1[i]-'a']++;
}
for(int i=0;i<s2.length();i++){
c[s2[i]-'a']++;
}
int sum=0;
for(int i=0;i<26;i++){
sum+=abs(b[i]-c[i]);
}
cout<<a[sum%6];
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
public static int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;break;}
ch*=(long)3;
}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
def Ifpossible(x,y) :
result = y-x
ans = 0
ch = 1
while ans<result :
ans+=ch
if ans==result:
return 1;
ch*=3
return 0;
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code: n=1
index=0
li=[]
while n!=0:
n=int(input())
li.append(n)
index=index+1
#li = list(map(int,input().strip().split()))
for i in range(0,len(li)-1):
print(li[i],end=" ")
print(li[len(li)-1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=100001;
int a;
for(int i=0;i<n;i++){
a=sc.nextInt();
System.out.print(a+" ");
if(a==0){break;}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n;
while(cin >> n){
cout << n << " ";
if(n == 0) break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String s=reader.readLine();
long c=0;
for(int i=1;i<s.length();i=i+2){
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u')
c++;
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: def check(c):
if c=='a' or c=='e' or c=='i' or c=='o' or c=='u':
return True
else:
return False
n=int(input())
s=input()
j=0
cnt=0
ans=0
for i in range(0,n):
j=i+1
if j%2==0:
if check(s[i]):
#=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
cnt+=1
print(cnt)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
bool check(char c){
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){return true;}
else{
return false;
}
}
int main() {
int n; cin>>n;
string s;
cin>>s;
int j=0;
int cnt=0;
int ans=0;
for(int i=0;i<n;i++){
if(i&1){
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
cnt++;
}}
}
cout<<cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array <b>A[]</b> of size <b>N</b>. A triplet (i, j, k) is said to be special if they satisfy below two conditions:
<b>1. i < j < k</b>
<b>2. A<sub>i</sub> < A<sub>j</sub> < A<sub>k</sub></b>
<b>V<sub>(A<sub>i</sub>,A<sub>j</sub>,A<sub>k</sub>)</sub>: A<sub>i</sub> + (A<sub>j</sub> * A<sub>k</sub>)</b>
You need to find the maximum possible value V<sub>(A<sub>i</sub>,A<sub>j</sub>,A<sub>k</sub>)</sub> which can be obtained from possible triplets satisfying above two conditions.The input line contains T, denoting the number of testcases. Each testcase contains Two lines. First line contains size of array. Second line contains elements of array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
Sum of N for all test cases is less than 10^5For each testcase you need find the maximum possible value in new line otherwise print "<b>-1</b>" if no such triplet is found.Sample Input:
2
4
5 20 11 19
4
1 2 2 2
Sample Output:
214
-1
Explanation:
Testcase 1: 5 11 19 forms special triplet fulfilling above conditions, thus giving maximum value 5 + (11*19) = 214.
Testcase 2: None of triplets satisfy the given conditions thus, maximum value is -1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int[] maxLeft(int[] a){
int N = a.length;
int[] res = new int[N];
TreeSet<Integer> ts = new TreeSet<>();
for(int i=0; i<N; i++){
int curr = a[i];
Integer maxLeftVal = ts.lower(curr);
if(maxLeftVal==null){
res[i] = -1;
}else{
res[i] = maxLeftVal;
}
ts.add(curr);
}
return res;
}
static int[] maxRight(int[] a){
int N = a.length;
int[] res = new int[N];
res[N-1] = -1;
int maxSeenIndexTillNow = N-1;
for(int i=N-2; i>=0; i--){
int curr = a[i];
if(curr<a[maxSeenIndexTillNow]){
res[i] = maxSeenIndexTillNow;
}else{
res[i] = -1;
maxSeenIndexTillNow = i;
}
}return res;
}
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int t=0; t<T; t++){
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
String[] strArr = br.readLine().split(" ");
for(int i=0; i<N; i++){
arr[i] = Integer.parseInt(strArr[i]);
}
int[] valOfA = maxLeft(arr);
int[] valOfK = maxRight(arr);
long vMax = -1;
for(int i=0; i<N; i++){
int vA = valOfA[i];
int vK = valOfK[i];
if(vA==-1 || vK==-1){
}else{
long valA = vA;
long valK = arr[vK];
long valJ = arr[i];
long currVmax = vA + (valJ*valK);
vMax = Math.max(vMax, currVmax);
}
}System.out.println(vMax);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array <b>A[]</b> of size <b>N</b>. A triplet (i, j, k) is said to be special if they satisfy below two conditions:
<b>1. i < j < k</b>
<b>2. A<sub>i</sub> < A<sub>j</sub> < A<sub>k</sub></b>
<b>V<sub>(A<sub>i</sub>,A<sub>j</sub>,A<sub>k</sub>)</sub>: A<sub>i</sub> + (A<sub>j</sub> * A<sub>k</sub>)</b>
You need to find the maximum possible value V<sub>(A<sub>i</sub>,A<sub>j</sub>,A<sub>k</sub>)</sub> which can be obtained from possible triplets satisfying above two conditions.The input line contains T, denoting the number of testcases. Each testcase contains Two lines. First line contains size of array. Second line contains elements of array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
Sum of N for all test cases is less than 10^5For each testcase you need find the maximum possible value in new line otherwise print "<b>-1</b>" if no such triplet is found.Sample Input:
2
4
5 20 11 19
4
1 2 2 2
Sample Output:
214
-1
Explanation:
Testcase 1: 5 11 19 forms special triplet fulfilling above conditions, thus giving maximum value 5 + (11*19) = 214.
Testcase 2: None of triplets satisfy the given conditions thus, maximum value is -1, I have written this Solution Code: #include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll getLowValue(set<ll>& lowValue, ll& n)
{
auto it = lowValue.lower_bound(n);
--it;
return (*it);
}
ll maxTriplet(ll arr[], ll n)
{
ll maxSuffArr[n + 1];
maxSuffArr[n] = 0;
for (int i = n - 1; i >= 0; --i)
maxSuffArr[i] = max(maxSuffArr[i + 1], arr[i]);
ll ans = -1;
set<ll> lowValue;
lowValue.insert(INT_MIN);
for (int i = 0; i < n - 1; ++i) {
if (maxSuffArr[i + 1] > arr[i]) {
ans = max(ans, getLowValue(lowValue,arr[i]) + arr[i] * maxSuffArr[i + 1]);
lowValue.insert(arr[i]);
}
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
ll arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout << maxTriplet(arr, n)<<endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. You have to print the maximum in every <b>K-sized subarray</b> from the left to the right in the array A.
More formally, you have to print N - K + 1 integers X<sub>1</sub>, X<sub>2</sub>, ..., X<sub>N-K+1</sub> such that X<sub>i</sub> (1 <= i <= N - K + 1) is the maximum element in the subarray A<sub>i</sub>, A<sub>i+1</sub>, ..., A<sub>i+K-1</sub>.1. The first line contains an integer N, denoting the size of the array.
2. The second line has N space-separated integers of the array A.
3. The third line contains integer K, denoting the size of the sliding window
<b>Constraints :</b>
1 ≤ N ≤ 10<sup>5</sup>
-10<sup>4</sup> ≤ A[i] ≤ 10<sup>4</sup>
1 ≤ K ≤ NPrint the max of K numbers for each position of sliding windowSample Input:-
8
1 3 -1 -3 5 3 6 7
3
Sample Output:-
3 3 5 5 6 7
Explanation:-
Window position Max
- - - -
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Sample Input:-
1
1
1
Sample Output:-
1 , I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(bu.readLine());
String s[]=bu.readLine().split(" ");
int a[]=new int[n],i;
PriorityQueue<int[]> pq=new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0]<o2[0]) return 1;
else return -1;
}});
for(i=0;i<n;i++) a[i]=Integer.parseInt(s[i]);
int k=Integer.parseInt(bu.readLine());
for(i=0;i<k;i++) pq.add(new int[]{a[i],i});
sb.append(pq.peek()[0]+" ");
for(i=k;i<n;i++)
{
pq.add(new int[]{a[i],i});
while(!pq.isEmpty() && pq.peek()[1]<=i-k) pq.poll();
sb.append(pq.peek()[0]+" ");
}
System.out.println(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. You have to print the maximum in every <b>K-sized subarray</b> from the left to the right in the array A.
More formally, you have to print N - K + 1 integers X<sub>1</sub>, X<sub>2</sub>, ..., X<sub>N-K+1</sub> such that X<sub>i</sub> (1 <= i <= N - K + 1) is the maximum element in the subarray A<sub>i</sub>, A<sub>i+1</sub>, ..., A<sub>i+K-1</sub>.1. The first line contains an integer N, denoting the size of the array.
2. The second line has N space-separated integers of the array A.
3. The third line contains integer K, denoting the size of the sliding window
<b>Constraints :</b>
1 ≤ N ≤ 10<sup>5</sup>
-10<sup>4</sup> ≤ A[i] ≤ 10<sup>4</sup>
1 ≤ K ≤ NPrint the max of K numbers for each position of sliding windowSample Input:-
8
1 3 -1 -3 5 3 6 7
3
Sample Output:-
3 3 5 5 6 7
Explanation:-
Window position Max
- - - -
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Sample Input:-
1
1
1
Sample Output:-
1 , I have written this Solution Code: N = int(input())
A = [int(x) for x in input().split()]
K = int(input())
def print_max(a, n, k):
max_upto = [0 for i in range(n)]
s = []
s.append(0)
for i in range(1, n):
while (len(s) > 0 and a[s[-1]] < a[i]):
max_upto[s[-1]] = i - 1
del s[-1]
s.append(i)
while (len(s) > 0):
max_upto[s[-1]] = n - 1
del s[-1]
j = 0
for i in range(n - k + 1):
while (j < i or max_upto[j] < i + k - 1):
j += 1
print(a[j], end=" ")
print()
print_max(A, N, K), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. You have to print the maximum in every <b>K-sized subarray</b> from the left to the right in the array A.
More formally, you have to print N - K + 1 integers X<sub>1</sub>, X<sub>2</sub>, ..., X<sub>N-K+1</sub> such that X<sub>i</sub> (1 <= i <= N - K + 1) is the maximum element in the subarray A<sub>i</sub>, A<sub>i+1</sub>, ..., A<sub>i+K-1</sub>.1. The first line contains an integer N, denoting the size of the array.
2. The second line has N space-separated integers of the array A.
3. The third line contains integer K, denoting the size of the sliding window
<b>Constraints :</b>
1 ≤ N ≤ 10<sup>5</sup>
-10<sup>4</sup> ≤ A[i] ≤ 10<sup>4</sup>
1 ≤ K ≤ NPrint the max of K numbers for each position of sliding windowSample Input:-
8
1 3 -1 -3 5 3 6 7
3
Sample Output:-
3 3 5 5 6 7
Explanation:-
Window position Max
- - - -
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Sample Input:-
1
1
1
Sample Output:-
1 , I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
vector<int> maxSwindow(vector<int>& arr, int k) {
vector<int> result;
deque<int> Q(k); // store the indices
// process the first window
for(int i = 0; i < k; i++) {
while(!Q.empty() and arr[i] >= arr[Q.back()])
Q.pop_back();
Q.push_back(i);
}
// process the remaining elements
for(int i = k; i < arr.size(); i++) {
// add the max of the current window
result.push_back(arr[Q.front()]);
// remove the elements going out of the window
while(!Q.empty() and Q.front() <= i - k)
Q.pop_front();
// remove the useless elements
while(!Q.empty() and arr[i] >= arr[Q.back()])
Q.pop_back();
// add the current element in the deque
Q.push_back(i);
}
result.push_back(arr[Q.front()]);
return result;
}
int main()
{
int k, n, m;
cin >> n;
vector<int> nums, res;
for(int i =0; i < n; i++){
cin >> m;
nums.push_back(m);
}
cin >> k;
res = maxSwindow(nums,k);
for(auto i = res.begin(); i!=res.end(); i++)
cout << *i << " ";
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry wants to rescue Fleur's sister Gabrielle in the underwater task. For that he needs to get ahead of all the people in the two magical queues.
Our small Tono and Solo are busy casting spells alternatively to remove people from the two magical queues. But since they are highly competitive, both of them wish to be the last person to remove people from the queue.
We can consider the removal of people as game played between Tono and Solo with the following rules:
<li> The turn goes alternatively. Tono starts the game.
<li> In each turn, the player first chooses a queue, then removes 3*X people from the queue, and then adds X people to the other queue (X > 0).
<li> The player who is unable to make a move loses.
<li> Both the players play with the most optimal strategy.
Since this game can take a lot of time to complete, Harry can declare the last person to remove people from the queue directly. Can you please help Harry accomplish the task, and save Gabrielle by being on time?The first line of the input contains an integer T denoting the number of test cases.
The next T lines contains two integers L1 and L2, denoting the initial number of people standing in the two queues.
Constraints
1 <= T <= 100
1 <= L1, L2 <= 200000For each test case, output "Tono" (without quotes) if Tono wins the game, else output "Solo" (without quotes) in a new line.Sample Input
2
0 1
0 2
Sample Output
Solo
Solo
Explanation
For both test cases, since Tono cannot make any move in the first turn itself, she loses.
Sample Input
1
1 4
Sample Output
Tono
Explanation
Tono removes 3 people from second queue, and adds 1 in the first queue. The queues now have 2 and 1 people respectively. Since Solo cannot make a move now, she loses., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.text.*;
public class Main{
void pre() throws Exception{}
void solve(int TC) throws Exception {
pn(win(ni(), ni())?"Tono":"Solo");
}
boolean win(int l1, int l2){
if(l1%2 == 0)return Math.abs(l1-l2) > 2;
else return Math.abs(l1-l2) > 1;
}
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
void exit(boolean b){if(!b)System.exit(0);}
static void dbg(Object... o){System.err.println(Arrays.deepToString(o));}
final long IINF = (long)1e17;
final int INF = (int)1e9+2;
DecimalFormat df = new DecimalFormat("0.00000000000000");
double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-9;
static boolean multipleTC = true, memory = true, fileIO = false;
FastReader in;PrintWriter out;
void run() throws Exception{
long ct = System.currentTimeMillis();
if (fileIO) {
in = new FastReader("");
out = new PrintWriter("");
} else {
in = new FastReader();
out = new PrintWriter(System.out);
}
int T = multipleTC? ni():1;
pre();
for (int t = 1; t <= T; t++) solve(t);
out.flush();
out.close();
System.err.println(System.currentTimeMillis() - ct);
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start();
else new Main().run();
}
int[][] make(int n, int e, int[] from, int[] to, boolean f){
int[][] g = new int[n][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = to[i];
if(f)g[to[i]][--cnt[to[i]]] = from[i];
}
return g;
}
int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){
int[][][] g = new int[n][][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 1};
if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, -1};
}
return g;
}
int[][] make(int n, int[] par, boolean f){
int[][] g = new int[n][];
int[] cnt = new int[n];
for(int x:par)cnt[x]++;
if(f)for(int i = 1; i< n; i++)cnt[i]++;
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]];
for(int i = 1; i< n-1; i++){
g[par[i]][--cnt[par[i]]] = i;
if(f)g[i][--cnt[i]] = par[i];
}
return g;
}
int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object... o){for(Object oo:o)out.print(oo+" ");}
void pn(Object... o){
if(o.length == 0)out.println("");
for(int i = 0; i< o.length; i++){
out.print(o[i]);
out.print((i+1 == o.length?"\n":" "));
}
}
void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str;
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.