Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
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: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
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));
int size = Integer.parseInt(br.readLine());
String line[] = br.readLine().split(" ");
long a[] = new long[size + 1];
for(int i = 1 ; i <= size; i++)
a[i] = Long.parseLong(line[i - 1]);
long value1 = maxofsubarray(a,size);
long value2 = maxofsubarrayBreakingatNegativeVal(a, size);
System.out.print(Math.max(value1, value2));
}
static long maxofsubarray(long[] a , int size)
{
long currsum = 0 , maxsum = 0, maxEl = 0;
for(int i = 1 ; i <= size ; i++)
{
currsum += a[i];
if(currsum < 0) currsum =0;
if(currsum > maxsum)
{
maxsum = currsum; maxEl = Math.max(maxEl, a[i]);
}
} return (maxsum - maxEl);
}
static long maxofsubarrayBreakingatNegativeVal(long[] a , int size)
{
long currsum = 0 , maxsum = 0, maxEl = 0, maxofall = 0;
for(int i = 1 ; i <= size ; i++)
{
maxEl = Math.max(maxEl, a[i]);
currsum += a[i];
if(currsum - maxEl < 0)
{
currsum =0; maxEl = 0;
}
if(currsum - maxEl > maxsum)
{
maxofall = currsum - maxEl; maxsum = maxofall;
}
}
return (maxofall);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
0, I have written this Solution Code: def maxSumTill(arr,n):
currSum = 0
maxSum = 0
maxEle = 0
maxi = 0
for i in range(1,n+1):
maxEle = max(maxEle,arr[i])
currSum += arr[i]
if currSum-maxEle < 0:
currSum = 0
maxEle =0
if currSum-maxEle > maxSum:
maxi = currSum - maxEle
maxSum = maxi
return maxi
def maxOfSum(arr,n):
currSum = 0
maxSum = 0
maxEle = 0
for i in range(1,n+1):
currSum += arr[i]
if currSum < 0:
currSum = 0
if currSum > maxSum:
maxSum = currSum
maxEle = max(maxEle, arr[i])
return maxSum - maxEle
n = int(input())
arr = list(map(int,input().split()))
arr = [0]+arr
val1 = maxSumTill(arr,n)
val2 = maxOfSum(arr,n)
print(max(val1,val2))
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono and Solo are one of the best programmers on the planet. Solo knows how to solve the problem on maximum subarray sum using the Kadane algorithm (don't you?). Now, Tono gives Solo a trickier problem to solve. In this problem, Solo can choose any non- empty subarray, the value of the subarray will be the <b>(sum of all the elements of the subarray - largest element of the subarray)</b>. Can you find the maximum value of any subarray?The first line of the input contains a single integer N, the size of the array.
The second line of the input contains N elements A[1], A[2],. , A[N].
Constraints
1 <= N <= 100000
-30 <= A[i] <= 30Output a single integer, the value of the subarray having maximum value.Sample Input
7
5 2 5 3 -30 -30 6
Sample Output
10
Explanation: Solo chooses the subarray [5, 2, 5, 3]. The value of the subarray is 10.
Sample Input
3
-10 6 -15
Sample Output
0, 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 = 1e5+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
int arr[N];
int n;
int kadane(){
int mx = 0;
int cur = 0;
For(i, 0, n){
cur += arr[i];
mx = max(mx, cur);
if(cur < 0)
cur = 0;
}
return mx;
}
void play(int x){
For(i, 0, n){
if(arr[i]==x){
arr[i]=-1000000000;
}
}
}
void solve(){
cin>>n;
For(i, 0, n){
cin>>arr[i];
assert(arr[i]>=-30 && arr[i]<=30);
}
int ans = 0;
for(int i=30; i>=1; i--){
ans = max(ans, kadane()-i);
play(i);
}
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 a queue of integers and N queries. Your the task is to perform these operations:-
enqueue:-this operation will add an element to your current queue.
dequeue:-this operation will delete the element from the starting of the queue
displayfront:-this operation will print the element presented at front
Note:-if queue is empty than dequeue operation will do nothing, and 0 will be printed as a front element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes integer to be added as a parameter.
<b>dequeue()</b>:- that takes no parameter.
<b>displayfront()</b> :- that takes no parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in displayfront() in which you require to print the element at front of your queue in a new line, if the queue is empty you just need to print 0.Sample Input:-
7
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
Sample Output:-
0
2
2
4
Sample input:
5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: class Queue
{
private Node front, rear;
private int currentSize;
class Node {
Node next;
int val;
Node(int val) {
this.val = val;
next = null;
}
}
public Queue()
{
front = null;
rear = null;
currentSize = 0;
}
public boolean isEmpty()
{
return (currentSize <= 0);
}
public void dequeue()
{
if (isEmpty())
{
}
else{
front = front.next;
currentSize--;
}
}
//Add data to the end of the list.
public void enqueue(int data)
{
Node oldRear = rear;
rear = new Node(data);
if (isEmpty())
{
front = rear;
}
else
{
oldRear.next = rear;
}
currentSize++;
}
public void displayfront(){
if(isEmpty()){
System.out.println("0");
}
else{
System.out.println(front.val);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. The letters are numbered from 1 to |S| from left to right. You have to perform M operations. In each operation, you will be given a number P<sub>i</sub>. You need to reverse the substring from P<sub>i</sub> to |S| - P<sub>i</sub> + 1. Print the final string after all the operations.
It is guaranteed that 2*P<sub>i</sub> <= |S|
|S| denotes the length of string SThe first line contains a string S. Second line contains a single integer M. Third line contains M space separated integers denoting the value of P<sub>i</sub>.
1 <= |S| <= 100000
1 <= M <= 100000
1 <= P<sub>i</sub> <= ceil(|S|/2)Print a single line containing the final string after all the operationsSample Input:
abcdef
3
1 2 3
Sample Output:
fbdcea
Explanation:
After 1st step, S = fedcba
After 2nd step, S = fbcdea
After 3rd step, S = fbdcea, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static String reverseString(String str)
{
StringBuilder s=new StringBuilder(str);
int n=s.length();
for(int i=0;i<n/2;i++)
{
char a=s.charAt(i);
s.setCharAt(i,s.charAt(n-i-1));
s.setCharAt(n-i-1,a);
}
return s.toString();
}
public static void main (String[] args) {
Scanner scan=new Scanner(System.in);
String s=scan.next();
int q=scan.nextInt();
String t="";
int[] a=new int[(int)Math.ceil(s.length()/2.0)];
while(q-->0)
{
t="";
int p=scan.nextInt();
a[p-1]=(a[p-1]==0)?1:0;
}
char[] arr=s.toCharArray();
for(int i=1;i<a.length;i++)
{
a[i]=a[i]+a[i-1];
a[i]=(a[i]==2)?0:a[i];
}
for(int i=0;i<a.length;i++)
{
if(a[i]==1)
{
char c=arr[i];
arr[i]=arr[arr.length-i-1];
arr[arr.length-i-1]=c;
}
}
s=String.valueOf(arr);
System.out.println(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. The letters are numbered from 1 to |S| from left to right. You have to perform M operations. In each operation, you will be given a number P<sub>i</sub>. You need to reverse the substring from P<sub>i</sub> to |S| - P<sub>i</sub> + 1. Print the final string after all the operations.
It is guaranteed that 2*P<sub>i</sub> <= |S|
|S| denotes the length of string SThe first line contains a string S. Second line contains a single integer M. Third line contains M space separated integers denoting the value of P<sub>i</sub>.
1 <= |S| <= 100000
1 <= M <= 100000
1 <= P<sub>i</sub> <= ceil(|S|/2)Print a single line containing the final string after all the operationsSample Input:
abcdef
3
1 2 3
Sample Output:
fbdcea
Explanation:
After 1st step, S = fedcba
After 2nd step, S = fbcdea
After 3rd step, S = fbdcea, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
string s; cin >> s;
int m; cin >> m;
int n = s.length();
for(int i = 1; i <= m; i++){
int x; cin >> x;
a[x]++;
a[n-x+2]--;
}
for(int i = 1; i <= (n-1)/2 + 1; i++){
a[i] += a[i-1];
if(a[i]&1)
swap(s[i-1], s[n-i]);
}
cout << s;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S and an integer K, your task is to find the lexicographically maximum string after performing at most K swaps.The first line of input contains a single string S. The next line of input contains the value of K.
Constraints:-
1 <= |S| <= 9
S contains digits from 1 to 9
1 <= K <= 4Print the lexicographically maximum string after performing at most K swaps.Sample Input:-
132
2
Sample Output:-
321
Sample Input:-
254
1
Sample Output:-
524, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static String maxString;
public static void solve(char charArr[], int swaps, int len, int index) {
if(swaps == 0 || index == len) {
return;
}
char maxChar = charArr[index];
for(int i = index; i < len; i++) {
if(charArr[i] > maxChar) {
maxChar = charArr[i];
}
}
if(maxChar != charArr[index]) {
swaps--;
}
for(int i = len - 1; i >= index; i--) {
if(charArr[i] == maxChar) {
charArr = swap(charArr, i, index);
String str = new String(charArr);
if(Integer.parseInt(maxString) < Integer.parseInt(str)) {
maxString = str;
}
solve(charArr, swaps, len, index + 1);
charArr = swap(charArr, i, index);
}
}
}
public static char[] swap(char charArr[], int i, int j) {
char temp = charArr[i];
charArr[i] = charArr[j];
charArr[j] = temp;
return charArr;
}
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
String str = sc.next();
int swaps = sc.nextInt();
char charArr[] = str.toCharArray();
maxString = str;
solve(charArr, swaps, charArr.length, 0);
System.out.println(maxString);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S and an integer K, your task is to find the lexicographically maximum string after performing at most K swaps.The first line of input contains a single string S. The next line of input contains the value of K.
Constraints:-
1 <= |S| <= 9
S contains digits from 1 to 9
1 <= K <= 4Print the lexicographically maximum string after performing at most K swaps.Sample Input:-
132
2
Sample Output:-
321
Sample Input:-
254
1
Sample Output:-
524, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void doit(string &A, int moves, string &ans) {
if(moves == 0) {
ans = max(ans, A);
return;
}
int n = A.length();
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
swap(A[i], A[j]);
doit(A, moves-1, ans);
swap(A[i], A[j]);
}
}
}
string solve(string A, int B) {
string ans = A;
doit(A, B, ans);
return ans;
}
int main(){
string s;
cin>>s;
int n;
cin>>n;
cout<<solve(s,n);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: /**
* author: tourist1256
* created: 2022-06-14 14:26:47
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
const int mod = 1e9 + 7;
vector<int> fac, sz, dp;
vector<vector<int>> adj;
struct Node {
int data;
Node *left = NULL, *right = NULL;
Node(int val) : data(val) {}
};
int power(int x, int y) {
x %= mod;
if (!x) return x;
int res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
y >>= 1;
x = (x * x) % mod;
}
return res;
}
int inverse(int x) {
return power(x, mod - 2);
}
Node *insert(Node *root, int val) {
if (!root) {
Node *tmp = new Node(val);
return tmp;
}
if (root->data < val)
root->right = insert(root->right, val);
else
root->left = insert(root->left, val);
return root;
}
void inorder(Node *root, Node *par) {
if (!root)
return;
inorder(root->left, root);
inorder(root->right, root);
if (par != NULL) {
sz[par->data] += sz[root->data];
}
int val = fac[sz[root->data] - 1];
if (root->left != NULL) {
val = (val * inverse(fac[sz[root->left->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->left->data]) % mod;
}
if (root->right != NULL) {
val = (val * inverse(fac[sz[root->right->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->right->data]) % mod;
}
dp[root->data] = (dp[root->data] * val) % mod;
}
int numOfWays(vector<int> &nums) {
int n = nums.size();
fac.assign(n + 10, 1);
for (int i = 2; i <= n; i++)
fac[i] = (fac[i - 1] * i) % mod;
sz.assign(n + 10, 1);
dp.assign(n + 10, 1);
adj.assign(n + 1, vector<int>());
Node *root = new Node(nums[0]);
for (int i = 1; i < n; i++)
insert(root, nums[i]);
inorder(root, NULL);
dp[nums[0]]--;
dp[nums[0]] += mod;
dp[nums[0]] %= mod;
return dp[nums[0]];
}
int32_t main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
debug(a);
cout << numOfWays(a) << "\n";
}, 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 integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}, 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 integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, 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;
unordered_map<long long,int> m;
long k;
cin>>k;
long long a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;
}
if(m.find(k)!=m.end()){
cout<<1<<endl;
}
else{
cout<<-1<<endl;
}
}
}
, 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 integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return 1
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def position(n,arr,x):
return binary_search(arr,0,n-1,x)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: // arr is they array to search from
// x is target
function binSearch(arr, x) {
// write code here
// do not console.log
// return the 1 or -1
let l = 0;
let r = arr.length - 1;
let mid;
while (r >= l) {
mid = l + Math.floor((r - l) / 2);
// If the element is present at the middle
// itself
if (arr[mid] == x)
return 1;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
r = mid - 1;
// Else the element can only be present
// in right subarray
else
l = mid + 1;
}
// We reach here when element is not
// present in array
return -1;
}, 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 in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader(){
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1){
if (c == '\n')break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do{
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)return -ret;return ret;
}
public long nextLong() throws IOException{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.'){
while ((c = read()) >= '0' && c <= '9'){
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)buffer[0] = -1;
}
private byte read() throws IOException{
if (bufferPointer == bytesRead)fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException{
if (din == null)return;
din.close();
}
}
public static void main (String[] args) throws IOException{
Reader sc = new Reader();
int m = sc.nextInt();
int n = sc.nextInt();
int[][] arr = new int[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
arr[i][j] = sc.nextInt();
}
}
int max_row_index = 0;
int j = n - 1;
for (int i = 0; i < m; i++) {
while (j >= 0 && arr[i][j] == 1) {
j = j - 1;
max_row_index = i;
}
}
System.out.println(max_row_index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a boolean matrix of size N*M in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: r, c = list(map(int, input().split()))
max_count = 0
max_r = 0
for i in range(r):
count = input().count("1")
if count > max_count:
max_count = count
max_r = i
print(max_r), 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 in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, 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 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int a[max1][max1];
signed main()
{
int n,m;
cin>>n>>m;
FOR(i,n){
FOR(j,m){cin>>a[i][j];}}
int cnt=0;
int ans=0;
int res=0;
FOR(i,n){
cnt=0;
FOR(j,m){
if(a[i][j]==1){
cnt++;
}}
if(cnt>res){
res=cnt;
ans=i;
}
}
out(ans);
}
, 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 in which each row is sorted your task is to print the index of the row containing maximum 1's. If multiple answer exist print the smallest one.First line contains two space separated integers denoting values of N and M. Next N lines contains M space separated integers depicting the values of the matrix.
Constraints:-
1 < = M, N < = 1000
0 < = Matrix[][] < = 1Print the smallest index (0 indexing) of a row containing the maximum number of 1's.Sample Input:-
3 5
0 1 1 1 1
0 0 0 1 1
0 0 0 1 1
Sample Output:-
0
Sample Input:-
4 4
0 1 1 1
1 1 1 1
0 0 1 1
1 1 1 1
Sample Output:-
1, I have written this Solution Code: // mat is the matrix/ 2d array
// n,m are dimensions
function max1Row(mat, n, m) {
// write code here
// do not console.log
// return the answer as a number
let j, max_row_index = 0;
j = m - 1;
for (let i = 0; i < n; i++)
{
// Move left until a 0 is found
let flag = false;
// to check whether a row has more 1's than previous
while (j >= 0 && mat[i][j] == 1)
{
j = j - 1; // Update the index of leftmost 1
// seen so far
flag = true;//present row has more 1's than previous
}
// if the present row has more 1's than previous
if (flag)
{
max_row_index = i; // Update max_row_index
}
}
if (max_row_index == 0 && mat[0][m - 1] == 0)
return -1;
return max_row_index;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a square character grid of side length N (where N is odd). You have to find whether the given grid is magical or not. A grid is said to be magical if it contains the same character on each element in its diagonal. Moreover, all the elements that are not present on the diagonal have to have the same character (which is different than the one on the diagonal).The first line contains integer N.
Each of the next N lines contain N lowercase English latin characters.
Constraints:
3 <= N <= 500
N is odd.Print "YES" if the given grid is magical, else print "NO", without the quotes.Sample Input:
3
aba
bab
aba
Sample Output:
YES
Explaination:
All characters on its diagonal are the same.
All the characters other than the diagonal are same.
The characters on the diagonal and the rest of the grid do not match.
Thus, it is a magical grid., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
set<char> d, r;
vector<string> s(n);
for(int i = 1; i <= n; i++){
cin >> s[i - 1];
for(int j = 1; j <= n; j++){
if(i == j or i + j == n + 1){
d.insert(s[i - 1][j - 1]);
}
else r.insert(s[i - 1][j - 1]);
}
}
if(d.size() == r.size() && d.size() == 1 && *d.begin() != *r.begin()) cout << "YES";
else cout << "NO";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement <code>getObjKeys</code> which only takes one argument which will be a object.
The function should return all they keys present in object as a string where elements are seperated by a ', '. (No nested objects)(Use JS Built in function)Function will take one argument which will be an objectFunction will is string which contain all the keys from the input object seperated by a ', 'const obj = {email:"akshat. sethi@newtonschool. co", password:"123456"}
const keyString = getObjKeys(obj)
console. log(keyString) // prints email, password, I have written this Solution Code: function getObjKeys(obj){
return Object.keys(obj).join(",")
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String StrInput[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(StrInput[0]);
int s = Integer.parseInt(StrInput[1]);
int arr[] = new int[n];
String StrInput2[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(StrInput2[i]);
}
int sum = arr[0];
int startingindex = 0;
int endingindex = 1;
int j = 0;
int i;
for(i=1;i<=n;i++)
{
if(sum < s && arr[i] != 0)
{
sum += arr[i];
}
while(sum > s && startingindex < i-1)
{
sum -= arr[startingindex];
startingindex++;
}
if(sum == s)
{
endingindex = i+1;
if(arr[0] == 0)
{
System.out.print(startingindex+2 + " " + endingindex);
}
else
{
System.out.print(startingindex+1 + " "+ endingindex);
}
break;
}
if(i == n && sum < s)
{
System.out.print(-1);
break;
}
}
}
catch(Exception e)
{
System.out.print(-1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==k){cout<<1<<" "<<i+1;return 0;}
if(m.find(sum-k)!=m.end()){
cout<<m[sum-k]+2<<" "<<i+1;
return 0;
}
m[sum]=i;
}
cout<<-1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: def sumFinder(N,S,a):
currentSum = a[0]
start = 0
i = 1
while i <= N:
while currentSum > S and start < i-1:
currentSum = currentSum - a[start]
start += 1
if currentSum == S:
return (start+1,i)
if i < N:
currentSum = currentSum + a[i]
i += 1
return(-1)
N, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = sumFinder(N,S,a)
if(ans==-1):
print(ans)
else:
print(ans[0],ans[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code:
#include <iostream>
using namespace std;
int Dishes(int N, int T){
return T-N;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
printf("%d",Dishes(n,k));
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, 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: You are given weights and values of N items, put some or all of these items in a knapsack of capacity W weight to get the maximum total value in the knapsack. Note that we have at most one quantity of each item.
In other words, given two integer arrays val[0..(N-1)] and wt[0..(N-1)] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
The first line consists of N the number of items.
The second line consists of W, the maximum capacity of the knapsack.
In the next line are N space separated positive integers denoting the values of the N items,
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ v[i] ≤ 1000For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.Input:
2
3
4
1 2 3
4 5 1
3
3
1 2 3
4 5 6
Output:
3
0, I have written this Solution Code: import java.util.*;
import java.io.*;
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public class Main {
private static int solve(int cpacity, int values[], int weights[], int i,int dp[][]) {
if (i == 0) {
if (weights[0]<=cpacity) {
return values[0];
}else{
return 0;
}
}
if (dp[i][cpacity] !=-1) {
return dp[i][cpacity];
}
int include = 0;
if (weights[i]<=cpacity) {
include = values[i] + solve(cpacity-weights[i], values, weights, i-1,dp);
}
int exclude = solve(cpacity, values, weights, i-1,dp);
return dp[i][cpacity] = Math.max(include, exclude);
}
public static void main(String[] args)throws IOException {
Reader sc = new Reader();
int t = sc.nextInt();
int dp[][] = new int[1000][1001];
while (t-- > 0) {
int n = sc.nextInt();
int maxCarry = sc.nextInt();
int values[] = new int[n];
int weights[] = new int[n];
for (int i = 0; i < n; i++) {
values[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
weights[i] = sc.nextInt();
}
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
System.out.println(solve(maxCarry, values, weights,n-1,dp));
}
sc.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given weights and values of N items, put some or all of these items in a knapsack of capacity W weight to get the maximum total value in the knapsack. Note that we have at most one quantity of each item.
In other words, given two integer arrays val[0..(N-1)] and wt[0..(N-1)] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
The first line consists of N the number of items.
The second line consists of W, the maximum capacity of the knapsack.
In the next line are N space separated positive integers denoting the values of the N items,
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ v[i] ≤ 1000For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.Input:
2
3
4
1 2 3
4 5 1
3
3
1 2 3
4 5 6
Output:
3
0, I have written this Solution Code: // A Dynamic Programming based solution for 0-1 Knapsack problem
#include<bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int t; cin >> t;
while(t--){
int n, w; cin >> n >> w;
int wt[n+1], val[n+1];
for(int i = 0; i < n; i++)
cin >> val[i];
for(int i = 0; i < n; i++)
cin >> wt[i];
cout << knapSack(w, wt, val, n) << endl;
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int count=search(a,0,n-1);
System.out.println(count);
}
}
public static int search(int[] a,int l,int h){
while(l<=h){
int mid=l+(h-l)/2;
if ((mid==h||a[mid+1]==0)&&(a[mid]==1))
return mid+1;
if (a[mid]==1)
l=mid+1;
else h=mid-1;
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 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 = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] == 1)
l = m;
else
h = m;
}
cout << l << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: c=int(input())
for x in range(c):
size=int(input())
s=input()
print(s.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a queue of integers and N queries. Your the task is to perform these operations:-
enqueue:-this operation will add an element to your current queue.
dequeue:-this operation will delete the element from the starting of the queue
displayfront:-this operation will print the element presented at front
Note:-if queue is empty than dequeue operation will do nothing, and 0 will be printed as a front element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes integer to be added as a parameter.
<b>dequeue()</b>:- that takes no parameter.
<b>displayfront()</b> :- that takes no parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in displayfront() in which you require to print the element at front of your queue in a new line, if the queue is empty you just need to print 0.Sample Input:-
7
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
Sample Output:-
0
2
2
4
Sample input:
5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: class Queue
{
private Node front, rear;
private int currentSize;
class Node {
Node next;
int val;
Node(int val) {
this.val = val;
next = null;
}
}
public Queue()
{
front = null;
rear = null;
currentSize = 0;
}
public boolean isEmpty()
{
return (currentSize <= 0);
}
public void dequeue()
{
if (isEmpty())
{
}
else{
front = front.next;
currentSize--;
}
}
//Add data to the end of the list.
public void enqueue(int data)
{
Node oldRear = rear;
rear = new Node(data);
if (isEmpty())
{
front = rear;
}
else
{
oldRear.next = rear;
}
currentSize++;
}
public void displayfront(){
if(isEmpty()){
System.out.println("0");
}
else{
System.out.println(front.val);
}
}
}
, 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: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, 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 str1 = br.readLine();
String[] str2 = str1.split(" ");
int[] arr = new int[n];
for(int i = 0; i < n; ++i) {
arr[i] = Integer.parseInt(str2[i]);
}
PermuteTheArray(arr, n);
}
static void PermuteTheArray(int A[], int n)
{
int []arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
int mini = n, maxi = 0;
for(int i = 0; i < n; i++)
{
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
if (maxi - mini == i)
System.out.print(1+" ");
else
System.out.print(0+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, I have written this Solution Code: def find_permutations(arr):
max_ind = -1
min_ind = 10000000;
n = len(arr)
index_of = {}
for i in range(n):
index_of[arr[i]] = i + 1
for i in range(1, n + 1):
max_ind = max(max_ind, index_of[i])
min_ind = min(min_ind, index_of[i])
if (max_ind - min_ind + 1 == i):
print(1,end = " ")
else:
#print( "i = ",i)
print(0,end = " ")
n = int(input())
arr = list(map(int, input().split()))
find_permutations(arr), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, 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 1001
#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);
}
pair<int,int> ans[100005];
signed main(){
// freopen("ou.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t;t=1;
while(t--){
int n;
cin>>n;
int a[n+10];
int b[n+10];
FOR(i,n){
cin>>a[i];
b[a[i]]=i;
}
ans[1].first=b[1];
ans[1].second=b[1];
for(int i=2;i<=n;i++){
ans[i].first=min(ans[i-1].first,b[i]);
ans[i].second=max(ans[i-1].second,b[i]);
}
for(int i=1;i<=n;i++){
if(ans[i].second-ans[i].first+1==i){cout<<1<<" ";}
else{
cout<<0<<" ";
}
}
END;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array of integers arr of size n, a window of size k. Your task is to find the sum of the maximum element from each window.The first line contains the input of n, k ie the size of the array, and the size of the window.
The next line contains the input of the array.
<b>Constraints</b>
1 <= k < n <= 10<sup>5</sup>
1 <= arr[i] <= 10<sup>5</sup>Print the single line containing the maximum sum.Sample Input 1:
5 3
1 2 3 4 5
Sample Output 1:
12
Sample Input 2:
6 2
2 3 1 7 8 3
Sample Output 2:
29, 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 srr[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(srr[0]);
int k = Integer.parseInt(srr[1]);
String ksrr[] = br.readLine().trim().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] =Integer.parseInt(ksrr[i]);
}
long sum=0,ans=0;
for(int i=0;i<=n-k;i++){
int maxval=0;
for(int j=i;j<i+k;j++){
if(j>=n) break;
maxval=Math.max(maxval,arr[j]);
}
ans+=maxval;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array of integers arr of size n, a window of size k. Your task is to find the sum of the maximum element from each window.The first line contains the input of n, k ie the size of the array, and the size of the window.
The next line contains the input of the array.
<b>Constraints</b>
1 <= k < n <= 10<sup>5</sup>
1 <= arr[i] <= 10<sup>5</sup>Print the single line containing the maximum sum.Sample Input 1:
5 3
1 2 3 4 5
Sample Output 1:
12
Sample Input 2:
6 2
2 3 1 7 8 3
Sample Output 2:
29, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-18 01:39:40
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
int sumWindow(vector<int> a, int n, int k) {
int result = 0;
for (int i = 0; i < n - k + 1; i++) {
int windowMax = INT_MIN;
for (int j = i; j < i + k; j++) {
windowMax = max(windowMax, a[j]);
}
debug(windowMax);
result += windowMax;
}
return result;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k, sum;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int result = sumWindow(a, n, k);
cout << result << "\n";
debug(result);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Uttu broke his phone. He can get it repaired by spending X rupees or he can buy a new phone by spending Y rupees. Uttu wants to spend as little money as possible. Find out if it is better to get the phone repaired or to buy a new phone.The first line contains a single integer T - the number of test cases. Then the test cases follow.
The first and only line of each test case contains two space- separated integers X and Y - the cost of getting the phone repaired and the cost of buying a new phone.
<b>Constraints</b>
1 ≤ T ≤ 1000
1 ≤ X, Y ≤ 10<sup>4</sup>For each test case,
- output REPAIR if it is better to get the phone repaired.
- output NEW PHONE if it is better to buy a new phone.
- output ANY if both the options have the same price.
You may print each character of REPAIR, NEW PHONE and ANY in uppercase.Sample Input
3
100 1000
10000 5000
3000 3000
Sample Output
REPAIR
NEW PHONE
ANY, 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
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
auto start = std::chrono::high_resolution_clock::now();
int tt;
cin >> tt;
while (tt--) {
int x, y;
cin >> x >> y;
if (x > y) {
cout << "NEW PHONE\n";
} else if (y > x) {
cout << "REPAIR\n";
} else {
cout << "ANY\n";
}
}
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;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified list.<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>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
int[] arr = new int[n];
String str[] = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
arr = sortedSquares(arr);
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public static int[] sortedSquares(int[] A) {
int[] nums = new int[A.length];
int k=A.length-1;
int i=0, j=A.length-1;
while(i<=j){
if(Math.abs(A[i]) <= Math.abs(A[j])){
nums[k--] = A[j]*A[j];
j--;
}
else{
nums[k--] = A[i]*A[i];
i++;
}
}
return nums;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: t = int(input())
for i in range(t):
n = int(input())
for i in sorted(map(lambda j:int(j)**2,input().split())):
print(i,end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: def Race(A,B,C):
if abs(C-A) ==abs(C-B):
return 'D'
if abs(C-A)>abs(C-B):
return 'S'
return 'N'
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code:
char Race(int A, int B, int C){
if(abs(C-A)==abs(C-B)){return 'D';}
if(abs(C-A)>abs(C-B)){return 'S';}
else{
return 'N';}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Naruto and Sasuke are on a S- Rank mission. Now They got bored and thought of competing in a race against each other in a horizontal plane. They decides a common spot C at which both of them will try to reach. Whoever reaches first wins the race, both of them run at the same speed.
Given initial positions of Naruto and Sasuke as A and B recpectively.
you need to tell which of them will win the race. If Naruto wins print "N" ( without the quotes ), if Sasuke wins print "S" ( without the quotes ).
if both of them reach the common spot at the same time, print "D" (for draw, without the quotes ).<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>Race</b> that takes the integer A (initial position of Naruto) and B (initial position of Sasuke) and C (position of common spot) as parameter.
Constraints
1 <= A, B, C <= 100Return the character according to the given scenario.Sample Input
1 2 3
Sample Output
S
Sample Input
1 3 2
Sample Output
D, I have written this Solution Code: static char Race(int A,int B,int C){
if(Math.abs(C-A)==Math.abs(C-B)){return 'D';}
if(Math.abs(C-A)>Math.abs(C-B)){return 'S';}
else{
return 'N';}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The Dilmo game is a two-player game played on a rectangular grid of cells. Each cell can be claimed by either player, but once a player claims a cell, none of the cells in the same row or column can be claimed by either player. Players take turns claiming cells, and the game ends when neither player can make a move. The player who can't make a move loses. Alice and Bob decide to play the game. Given the initial state of the grid and the fact that Alice goes first, write a program to predict the winner of the game, assuming that both players play optimally.
Optimally means they choose the most strategic move at each turn to increase their chances of winning the game.The first line consists of two space-separated integers k and l, the number of rows and columns in the matrix.
The following k lines consist of l space-separated integers each either 0 or 1.
<b>Constraints</b>
1 ≤ k, l ≤ 50Print Single string, Alice or Bob. (case sensitive ).Input:
2 2
0 0
0 0
Output:
Bob
Explanation :
One possible optimal case can be Alice choosing (1,1) now Bob can choose only (2,2). Alice didn't have any cells to choose from according to the given condition, therefore Bob wins., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
const int N = 51;
int n, m;
int a[N][N];
int main()
{
int t;
t=1;
while(t--)
{
cin >> n >> m;
set< int > r, c;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j] == 1)
r.insert(i), c.insert(j);
}
}
int k=n-r.size();
int l=m-c.size();
int mn = min(k,l);
if(mn % 2)
cout << "Alice" << endl;
else
cout << "Bob" << 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 equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: def equationSum(N) :
re=2*N*(N+1)
re=re-3*N
return re, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: static long equationSum(long N)
{
return 2*N*(N+1) - 3*N;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>User task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code:
#include <iostream>
using namespace std;
int Dishes(int N, int T){
return T-N;
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
printf("%d",Dishes(n,k));
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sheldon and Leonard are gone for lunch but none of them have money so they decided to wash dishes. In total, they washed T dishes from which N dishes are washed by Leonard. Now Leonard wants to know the number of dishes Sheldon washed. Help him to find it.The first line of the input contains N and T
Constraints:-
1 <= N <= T <= 1000Return the number of dishes Sheldon washed.Sample Input:-
3 6
Sample Output:-
3
Sample Input:-
2 4
Sample Output:-
2, I have written this Solution Code: nan, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two objects of two persons <code>rohanInfo</code> of Rohan and <code>gauriInfo</code> of Gauri which have property <code>height</code> and <code>weight</code> . Now using the concept of Object destructuring , destructure their properties of <code>height</code> and <code>weight</code> into the mentioned variables and then use those variable to write code which return the array that contains two values , maximum height and maximum weight among the two objects.Function will take two arguments, both of them are objects of two different person,both of them contains two properties which are <code>height</code> and <code>weight</code>
function returns an array having two numbers, maximum <code>height</code> and maximum <code>weight</code> from the given objects in that order.const mohan={height:180, weight:60};
const radha={height:100, weight:120};
const answer = calculateMax(mohan, radha) // returns an array of two numbers
console.log(answer) // prints [ 180, 120 ]
const rahul={height:10, weight:20};
const ritika={height:30, weight:40};
const answer = calculateMax(rahul, ritika) // returns an array of two numbers
console.log(answer) // prints [ 30, 40 ], I have written this Solution Code: function calculateMax(rohanInfo,gauriInfo) {
const{height:rohanHeight,weight:rohanweight}=rohanInfo;
const{height:gauriHeight,weight:gauriweight}=gauriInfo;
return [Math.max(rohanHeight,gauriHeight),Math.max(rohanweight,gauriweight)];
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
int n=Integer.parseInt(br.readLine().trim());
if(n%3==1)
System.out.println("Dead");
else
System.out.println("Alive");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: n = int(input().strip())
if n%3 == 1:
print("Dead")
else:
print("Alive"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#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);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
if(n%3==1)
cout<<"Dead";
else
cout<<"Alive";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, 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));
br.readLine();
String[] line = br.readLine().split(" ");
int happyBalloons = 0;
for(int i=1;i<=line.length;++i){
int num = Integer.parseInt(line[i-1]);
if(num%2 == i%2 ){
happyBalloons++;
}
}
System.out.println(happyBalloons);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, I have written this Solution Code: x=int(input())
arr=input().split()
for i in range(0,x):
arr[i]=int(arr[i])
count=0
for i in range(0,x):
if(arr[i]%2==0 and (i+1)%2==0):
count+=1
elif (arr[i]%2!=0 and (i+1)%2!=0):
count+=1
print (count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, 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(ll 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 n; cin>>n;
int ans = 0;
For(i, 1, n+1){
int a; cin>>a;
if(i%2 == a%2)
ans++;
}
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: Design a system that takes big URLs like “http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id. So your program should take an integer id and generate a 6 character long URL.
A URL character can be one of the following
A lower case alphabet [‘a’ to ‘z’], total 26 characters
An upper case alphabet [‘A’ to ‘Z’], total 26 characters
A digit [‘0′ to ‘9’], total 10 characters
There are total 26 + 26 + 10 = 62 possible characters.
So the task is to convert an integer (database id) to a base 62 number where digits of 62 base are "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"The first line contains T denoting the number of test cases.
The next T lines contain a single integer N.
1 <= T <= 100000
1 <= N <= 1000000000For each test case, in a new line, print the shortened string.Sample Input:
1
12345
Sample Output:
dnh
Explanation:
Try to convert 12345 in base-62, replace the integer with the corresponding character, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static StringBuffer URL(int n)
{
StringBuffer c =new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
StringBuffer url = new StringBuffer();
while (n > 0)
{
url.append(c.charAt(n % 62));
n = n / 62;
}
return url.reverse();
}
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int l= Integer.parseInt(br.readLine());
for(int i=0;i<l;i++){
int n = Integer.parseInt(br.readLine());
System.out.println(URL(n));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Design a system that takes big URLs like “http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id. So your program should take an integer id and generate a 6 character long URL.
A URL character can be one of the following
A lower case alphabet [‘a’ to ‘z’], total 26 characters
An upper case alphabet [‘A’ to ‘Z’], total 26 characters
A digit [‘0′ to ‘9’], total 10 characters
There are total 26 + 26 + 10 = 62 possible characters.
So the task is to convert an integer (database id) to a base 62 number where digits of 62 base are "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"The first line contains T denoting the number of test cases.
The next T lines contain a single integer N.
1 <= T <= 100000
1 <= N <= 1000000000For each test case, in a new line, print the shortened string.Sample Input:
1
12345
Sample Output:
dnh
Explanation:
Try to convert 12345 in base-62, replace the integer with the corresponding character, I have written this Solution Code: t=int(input())
a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
while(t>0):
s=""
n=int(input())
while(n>0):
r=n%62
s=a[r]+s
n=n//62
print(s)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Design a system that takes big URLs like “http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id. So your program should take an integer id and generate a 6 character long URL.
A URL character can be one of the following
A lower case alphabet [‘a’ to ‘z’], total 26 characters
An upper case alphabet [‘A’ to ‘Z’], total 26 characters
A digit [‘0′ to ‘9’], total 10 characters
There are total 26 + 26 + 10 = 62 possible characters.
So the task is to convert an integer (database id) to a base 62 number where digits of 62 base are "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"The first line contains T denoting the number of test cases.
The next T lines contain a single integer N.
1 <= T <= 100000
1 <= N <= 1000000000For each test case, in a new line, print the shortened string.Sample Input:
1
12345
Sample Output:
dnh
Explanation:
Try to convert 12345 in base-62, replace the integer with the corresponding character, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define endl '\n'
#define pi pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
void solve(){
int n; cin >> n;
string t = "";
while(n){
int c = n%62;
t += s[c];
n /= 62;
}
reverse(t.begin(), t.end());
cout << t << endl;
}
void testcases(){
int tt = 1;
cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton School Coding Contest usually starts at 21:00 IST and lasts for 100 minutes.
You are given an integer K between 0 and 100 (inclusive). Print the time K minutes after 21:00 in the HH:MM format, where HH denotes the hour on the 24- hour clock and MM denotes the minute. If the hour or the minute has just one digit, append a 0 to the beginning to represent it as a 2- digit integer.The input consists of a single integer.
K
<b>Constraints</b>
K is an integer between 0 and 100 (inclusive).Print the time K minutes after 21:00 in the format specified in the Problem Statement.<b>Sample Input 1</b>
63
<b>Sample Output 1</b>
22:03
<b>Sample Input 2</b>
45
<b>Sample Output 2</b>
21:45
<b>Sample Input 3</b>
100
<b>Sample Output 3</b>
22:40, I have written this Solution Code: #include <iostream>
#include <iomanip>
using namespace std;
int main() {
int X;
cin >> X;
int H = X < 60 ? 21 : 22;
int M = X % 60;
cout << H << ':' << setw(2) << setfill('0') << M << '\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 N flags, initially set to 0. Now you have to perform two operations on them:
1. Increase(F) by 1: flag F is increased by 1.
2. max_flag: all flags are set to a maximum value of any flag.
A non-empty array arr[] will be given of size M. This array represents consecutive operations:
a) If arr[K] = F, such that 1 <= F <= N then operation K is Increase(F).
b) If arr[K] = N+1 then operation K is max_flag.
The goal is to calculate the value of every flag after all operations.The input line contains T, denoting the number of test cases.
Each test case contains two lines.
The first line contains an integer N, the number of flags, and an integer M, the size of the array 'arr'.The second line contains elements of the array 'arr' separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N, M <= 10^5
1 <= arr[i] <= N+1
Sum of N and M for each test case is less than or equal to 10^5For each testcase you need to print the updated array after all operations in new line.Sample Input:
1
5 7
3 4 4 6 1 4 4
Sample Output:
3 2 2 4 2
<b>Explanation:</b>
Testcase 1:
the values of the flags after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2), I have written this Solution Code: t=int(input())
while t>0:
t-=1
n,m=map(int,input().split())
a=map(int,input().split())
b=[0]*(n+1)
for i in a:
if i==n+1:
v=max(b)
for i in range(1,n+1):
b[i]=v
else:b[i]+=1
for i in range(1,n+1):
print(b[i],end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N flags, initially set to 0. Now you have to perform two operations on them:
1. Increase(F) by 1: flag F is increased by 1.
2. max_flag: all flags are set to a maximum value of any flag.
A non-empty array arr[] will be given of size M. This array represents consecutive operations:
a) If arr[K] = F, such that 1 <= F <= N then operation K is Increase(F).
b) If arr[K] = N+1 then operation K is max_flag.
The goal is to calculate the value of every flag after all operations.The input line contains T, denoting the number of test cases.
Each test case contains two lines.
The first line contains an integer N, the number of flags, and an integer M, the size of the array 'arr'.The second line contains elements of the array 'arr' separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N, M <= 10^5
1 <= arr[i] <= N+1
Sum of N and M for each test case is less than or equal to 10^5For each testcase you need to print the updated array after all operations in new line.Sample Input:
1
5 7
3 4 4 6 1 4 4
Sample Output:
3 2 2 4 2
<b>Explanation:</b>
Testcase 1:
the values of the flags after each consecutive operation will be:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 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 = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
memset(a, 0, sizeof a);
int n, m;
cin >> n >> m;
int mx = 0, flag = 0;
for(int i = 1; i <= m; i++){
int p; cin >> p;
if(p == n+1){
flag = mx;
}
else{
a[p] = max(a[p], flag) + 1;
mx = max(mx, a[p]);
}
}
for(int i = 1; i <= n; i++){
a[i] = max(a[i], flag);
cout << a[i] << " ";
}
cout << 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 A[] of size N. The array contains positive and negative elements both. You need to count number of subsequences that has product of its elements as positive. The single positive element of the array will also contribute in the count.
<b>Note:</b> Since the answer can be huge you need to take count as the modulo of (10^9 + 7).The first line of each test case contains N size of array. Second-line contains elements of array separated by space.
<b>Constraints:</b>
1 <= N <= 10^5
-10^6 <= A[i] <= 10^6For each test case you need to print the count of subsequences which has a product of elements as positive.Sample Input 1
4
-1 -2 0 -1
Sample Output 1
3
Sample Input 2
1
5
Sample Output 2
1
Explanation:
Testcase1: The positive product subsequences are {-1, -2}, {-2, -1}, {-1, -1} so a total of 3., I have written this Solution Code: import math
m=int(math.pow(10,9))+7
def power(n):
ans=1
for i in range(0,n):
ans=(ans*2)%m
return ans
def cntSubSeq(arr, n):
pos_count = 0;
neg_count = 0
for i in range (n):
if (arr[i] > 0) :
pos_count += 1
if (arr[i] < 0):
neg_count += 1
result = power(pos_count)
if (neg_count > 0):
result = (result *power(neg_count-1))%m
result -= 1
return result
n=int(input())
arr=input().split()
for i in range(0,n):
arr[i]=int(arr[i])
print (int(cntSubSeq(arr,n))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N. The array contains positive and negative elements both. You need to count number of subsequences that has product of its elements as positive. The single positive element of the array will also contribute in the count.
<b>Note:</b> Since the answer can be huge you need to take count as the modulo of (10^9 + 7).The first line of each test case contains N size of array. Second-line contains elements of array separated by space.
<b>Constraints:</b>
1 <= N <= 10^5
-10^6 <= A[i] <= 10^6For each test case you need to print the count of subsequences which has a product of elements as positive.Sample Input 1
4
-1 -2 0 -1
Sample Output 1
3
Sample Input 2
1
5
Sample Output 2
1
Explanation:
Testcase1: The positive product subsequences are {-1, -2}, {-2, -1}, {-1, -1} so a total of 3., I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static final int MOD = 1000000007;
static long getCount(int arr[], int n) {
long cntPrevPos = 0, cntPrevNeg = 0;
if(arr[0] < 0) {
cntPrevNeg = 1;
}
else if(arr[0] > 0) {
cntPrevPos = 1;
}
for(int i=1; i<n; i++) {
long cntCurrPos = 0, cntCurrNeg = 0;
if(arr[i] < 0) {
cntCurrNeg = 1 + cntPrevPos + cntPrevNeg;
cntCurrNeg %= MOD;
cntCurrPos = cntPrevNeg + cntPrevPos;
cntCurrPos %= MOD;
}
else if(arr[i] > 0) {
cntCurrNeg = cntPrevNeg + cntPrevNeg;
cntCurrNeg %= MOD;
cntCurrPos = 1 + cntPrevPos + cntPrevPos;
cntCurrPos %= MOD;
}
else
continue;
cntPrevNeg = cntCurrNeg;
cntPrevPos = cntCurrPos;
}
return cntPrevPos;
}
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().split(" ");
int arr[] = new int[n];
for(int i=0; i<n; i++)
arr[i] = Integer.parseInt(str[i]);
System.out.println(getCount(arr, n));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String num = br.readLine();
int N = Integer.parseInt(num);
String result = "";
if(N < 1000 && N > 0 && N == 1){
result = "AC";
} else {
result = "WA";
}
System.out.println(result);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: N=int(input())
if N==1:
print("AC")
else:
print("WA"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: //Author: Xzirium
//Time and Date: 03:04:29 27 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);
if(N==1)
{
cout<<"AC"<<endl;
}
else
{
cout<<"WA"<<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 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: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
int[] a=new int[n];
int counter=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]%2!=0) counter++;
}
String ans="NO";
if((a[0]%2!=0)&&(a[n-1]%2!=0)&&(n%2!=0))
ans="YES";
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., I have written this Solution Code:
def fun(n,arr):
if n%2==0:
if arr[0]%2==1 and arr[n-1]%2==1:
for i in range(1,n):
if arr[i]%2==1 and arr[i-1]%2==1:
return "YES"
else:
return "NO"
else:
if arr[0]%2==1 and arr[n-1]%2==1:
return "YES"
else:
return "NO"
n=int(input())
arr=[int(i) for i in input().split()]
print(fun(n,arr)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Siddharth, a great entrepreneur, chooses a person as an intern if he/she good at maths. Thus Sid gives a simple maths problem to the candidate based on which he decides to who is gonna get the offer. Below is the description of the problem asked by Sid
Given arr <b>A[]</b> containing <b>N</b> positive integers. The task is to check whether it is possible to divide the given array into an <b>odd number of non-empty subarrays each of odd length such that each sub-array starts and ends with an odd number</b>. If it is possible to divide the array then print "<b>YES</b>" otherwise "<b>NO</b>".
<b>Note:</b> Don't forget that the complete array is also considered as a subarrayThe first line of each test case contains N size of array. Second-line contains N space-separated elements.
<b>Constraints:</b>
1 <= N <= 999999
0 <= A[i] <= 999999999Print "YES", if it is possible to divide the array. Otherwise, print "NO". Without quotes.Sample Input 1:
5
9 0 3 1 5
Sample Output 1:
YES
Sample Input 2:
3
1 2 0
Sample Output 2:
NO
Explanation:
Testcase 1: Array {9, 0, 3, 1, 5} can be divided as {9, 0, 3}, {1}, {5}., I have written this Solution Code: 'use strict'
function main(input)
{
const inputList = input.split('\n');
const arrSize = Number(inputList[0].split(' ')[0])
const arr = inputList[1].split(' ').
map(x => Number(x))
if(arrSize %2 == 0 || Number(arr[0]) %2 == 0 ||
Number(arr[arrSize-1])%2 == 0)
console.log("NO")
else console.log("YES")
}
main(require("fs").readFileSync("/dev/stdin", "utf8"));, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, 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());
double arr[] = new double[N];
String str[] = br.readLine().trim().split(" ");
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
double resistance=0;
int equResistance=0;
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
for(int i=0;i<N;i++)
{
resistance=resistance+(1/arr[i]);
}
equResistance = (int)Math.floor((1/resistance));
System.out.println(equResistance);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: r = input("")
r = int(r)
n = input("").split()
resistance=0.0
for i in range(0,r):
resistor = float(n[i])
resistance = resistance + (1/resistor)
print(int(1/resistance)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, 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;
double s = 0;
for(int i = 1; i <= n; i++){
double p; cin >> p;
s = s + (1/p);
}
s = 1/s;
cout << floor(s);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have entered the animated world of Pokémon and fortunately you have started your career as a Pokémon Trainer. Your first Pokémon is a Pikachu which is a cherry on top of the cake. Now it is your responsibility to increase your Pikachu's CP (Combat Power) using Power Bics. Consider that initially Cinitial is your Pikachu's CP and you can help him increase its CP to a maximum value, Cfinal.
In order to increase your Pokémon's CP in an efficient way you must make sure that after every increase in CP your Pokémon's CP value has exactly F unique prime factors. What is the maximum number of steps in which you can increase the CP of your Pokémon to the maximum possible (with F prime factors), considering every increase in CP as a step. If it is not possible to increase your Pikachu's CP then print -1.• The first line of the input contains an integer T denoting the number of test cases. The description of
T-test cases follows:
• The first line of each test case contains three space-separated integers, Cinitial, Cfinal and F.
Constraints:-
• 1 <= T <= 5000
• 1 ≤ Cinitial ≤ Cfinal ≤ 10^6
• 0 ≤ F ≤ 10For each test case, print an integer which is the maximum number of steps to increase Pokémon's CP.Sample Input:-
3
3 5 1
3 6 2
3 10 3
Sample Output:-
2
1
-1, 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 t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
int[] ci=new int[t];
int[] cf=new int[t];
int[] f=new int[t];
int count=0;
for(int i=0;i<t;i++){
String[] line = br.readLine().split(" ");
ci[i] = Integer.parseInt(line[0]);
cf[i]=Integer.parseInt(line[1]);
f[i]=Integer.parseInt(line[2]);
if(cf[i]>count)
count=cf[i];
}
boolean[] isPrime=new boolean[count+1];
for(int i=2;i<=count;i++){
isPrime[i]=true;
}
for(int i=2;i<=count;i++){
if(isPrime[i]){
for(int j=2*i;j<=count;j+=i){
isPrime[j]=false;
}
}
}
int[] a=new int[count+1];
for(int j=2;j<=count;j++){
for(int i=1;i*i<=j;i++){
if(j%i==0){
if(j/i==i){
if(isPrime[i]){
a[j]++;
}
}
else{
if(isPrime[i]){
a[j]++;
}
if(isPrime[j/i]){
a[j]++;
}
}
}
}
}
for(int i=0;i<t;i++){
int coun=0;
for(int j=ci[i]+1;j<=cf[i];j++){
if(a[j]==f[i]){
coun++;
}
}
if(coun==0)
sb.append(-1+"\n");
else
sb.append(coun+"\n");
}
System.out.print(sb.toString());
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have entered the animated world of Pokémon and fortunately you have started your career as a Pokémon Trainer. Your first Pokémon is a Pikachu which is a cherry on top of the cake. Now it is your responsibility to increase your Pikachu's CP (Combat Power) using Power Bics. Consider that initially Cinitial is your Pikachu's CP and you can help him increase its CP to a maximum value, Cfinal.
In order to increase your Pokémon's CP in an efficient way you must make sure that after every increase in CP your Pokémon's CP value has exactly F unique prime factors. What is the maximum number of steps in which you can increase the CP of your Pokémon to the maximum possible (with F prime factors), considering every increase in CP as a step. If it is not possible to increase your Pikachu's CP then print -1.• The first line of the input contains an integer T denoting the number of test cases. The description of
T-test cases follows:
• The first line of each test case contains three space-separated integers, Cinitial, Cfinal and F.
Constraints:-
• 1 <= T <= 5000
• 1 ≤ Cinitial ≤ Cfinal ≤ 10^6
• 0 ≤ F ≤ 10For each test case, print an integer which is the maximum number of steps to increase Pokémon's CP.Sample Input:-
3
3 5 1
3 6 2
3 10 3
Sample Output:-
2
1
-1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
const int MOD = 1e9 + 7;
int prime[1000007];
int isprime[1000007];
void precompute()
{
isprime[2] = 0;
prime[1] = 0;
isprime[1] = 0;
prime[2] = 1;
for(int i = 2;i <= 1000000;i++)
{
if(isprime[i]) // reverse logic
continue;
prime[i] = 1;
for(int j = (2*i);j <= 1000000;j += i)
{
isprime[j] = 1;
prime[j]++;
}
}
}
int pre[11][1000004];
int main()
{
precompute();
// cout << prime[1];
for(int i = 1;i <= 10;i++)
{
for(int j = 1;j <= 1000000;j++)
{
if(prime[j] == i)
pre[i][j] = 1;
else
pre[i][j] = 0;
}
for(int j = 1;j <= 1000000;j++)
pre[i][j] = pre[i][j] + pre[i][j - 1];
}
int T,x,y,k;
cin >> T;
while(T--)
{
int ans = 0;
cin >> x >> y >> k;
assert(x <= y);
assert(k >= 0 && k <= 10);
if(x == y || k == 0)
{
cout << "-1\n";
continue;
}
ans = pre[k][y] - pre[k][x];
if(ans == 0)
cout << "-1\n";
else
cout << ans << endl;
}
return 0;
}, In this Programming Language: C++, 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: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<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>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is very confused after knowing all about Sirius. To lighten Harry's mood Sirius asks a fun problem to him. Sirius puts K talking skulls equally spaced on the circumference of a circle. Now he enumerates them from 1 to K in clockwise order. Now given skull X and skull Z, he asks Harry to find number of possible Y such that angle XYZ is obtuse(more than 90 degrees).
Note:- X Y and Z must be different.The first line of the input contains single integer T, denoting number of test cases.
Each test case contains three integers K, X and Z.
Constraints
1 <= T <= 10
1 <= K <= 1000000000
1 <= X, Y <= KFor each testcase print number of possible Y in a new line.Sample Input
2
6 1 3
10 1 5
Sample Output
1
3
Explanation:
For first case, 2 is the suitable skull.
For second case, 2, 3 and 4 are the suitable skulls., 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));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
String str[] = in.readLine().trim().split(" ");
int k = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
int z = Integer.parseInt(str[2]);
int cnt = Math.abs(x-z);
int rem = k - cnt;
if(cnt < rem){
if(cnt != 0){
System.out.println(cnt-1);
}
else{
System.out.println(0);
}
}
else if(rem < cnt){
if(rem != 0){
System.out.println(rem-1);
}
else{
System.out.println(0);
}
}
else{
System.out.println(0);
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is very confused after knowing all about Sirius. To lighten Harry's mood Sirius asks a fun problem to him. Sirius puts K talking skulls equally spaced on the circumference of a circle. Now he enumerates them from 1 to K in clockwise order. Now given skull X and skull Z, he asks Harry to find number of possible Y such that angle XYZ is obtuse(more than 90 degrees).
Note:- X Y and Z must be different.The first line of the input contains single integer T, denoting number of test cases.
Each test case contains three integers K, X and Z.
Constraints
1 <= T <= 10
1 <= K <= 1000000000
1 <= X, Y <= KFor each testcase print number of possible Y in a new line.Sample Input
2
6 1 3
10 1 5
Sample Output
1
3
Explanation:
For first case, 2 is the suitable skull.
For second case, 2, 3 and 4 are the suitable skulls., I have written this Solution Code: k = int(input())
for i in range(k):
z = 0
a = input().split()
a = [int(i) for i in a]
rad = 360/a[0]
dia = abs(a[2]-a[1])
ang = rad*dia
if(ang/2 > 90):
z = a[0]- dia - 1
elif(ang/2 < 90):
z = dia - 1
else:
z = 0
print(z)
a.clear(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Harry is very confused after knowing all about Sirius. To lighten Harry's mood Sirius asks a fun problem to him. Sirius puts K talking skulls equally spaced on the circumference of a circle. Now he enumerates them from 1 to K in clockwise order. Now given skull X and skull Z, he asks Harry to find number of possible Y such that angle XYZ is obtuse(more than 90 degrees).
Note:- X Y and Z must be different.The first line of the input contains single integer T, denoting number of test cases.
Each test case contains three integers K, X and Z.
Constraints
1 <= T <= 10
1 <= K <= 1000000000
1 <= X, Y <= KFor each testcase print number of possible Y in a new line.Sample Input
2
6 1 3
10 1 5
Sample Output
1
3
Explanation:
For first case, 2 is the suitable skull.
For second case, 2, 3 and 4 are the suitable skulls., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#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);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int t,k,r,s,a,b;
cin>>t;
while(t--)
{
cin>>k>>r>>s;
b=max(r,s);
a=min(r,s);
if(k%2==0&&(b-a)==k/2)
cout<<0;
else
if((k%2!=0&&(b-a)<=k/2)||(k%2==0&&(b-a)<k/2))
cout<<b-a-1;
else if((b-a)>k/2)
cout<<k-(b-a)-1;
cout<<endl;
}
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given one element K your task is to print the maximum element from the array which is less than or equal to the given element(Floor), and the minimum element from the array which is greater than or equal to the given element(Ceil).<b>In case of Java only</b>
<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>floorAndCeil()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N. The Second line of input contains N space-separated integers depicting the values of the array. The third line of input contains a single integer Q. The next Q line of input contains a single integer the value of K.
Constraints:-
1 <= N <= 100000
1 <= K, Arr[i] <= 1000000000000
1 <= Q <= 10000In a new line Print two space-separated integers depicting the values of Floor and Ceil of the given number. If the floor or ceil of the element does not exist print -1.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
2 2
2 5
6 11
-1 2
15 -1, I have written this Solution Code: n = int(input())
arr = list(map(int,input().split()))
t = int(input())
def findFloor(arr, l, h, x, res):
if(l<=h):
m = l+(h-l)//2
if(arr[m] == x):
return m
if(arr[m] > x):
return findFloor(arr, l, m-1, x, res)
if(arr[m] < x):
res = m
return findFloor(arr, m+1, h, x, res)
else:
return res
def findCeil(arr, l, h, x, res):
if(l<=h):
m = l+(h-l)//2
if(arr[m] == x):
return m
if(arr[m] < x):
return findCeil(arr, m+1, h, x, res)
res = m
return findCeil(arr, l, m-1, x, res)
else:
return res
for _ in range(t):
x = int(input())
f = findFloor(arr, 0, n-1, x, -1)
c = findCeil(arr, 0, n-1, x, -1)
floor = -1
ceil = -1
if(f!=-1):
floor = arr[f]
if(c!=-1):
ceil = arr[c]
print(floor,ceil), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array of N integers a[], and Q queries. For each query, you will be given one element K your task is to print the maximum element from the array which is less than or equal to the given element(Floor), and the minimum element from the array which is greater than or equal to the given element(Ceil).<b>In case of Java only</b>
<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>floorAndCeil()</b> that takes the array a[], integer N and integer k as arguments.
<b>Custom Input</b>
The first line of input contains a single integer N. The Second line of input contains N space-separated integers depicting the values of the array. The third line of input contains a single integer Q. The next Q line of input contains a single integer the value of K.
Constraints:-
1 <= N <= 100000
1 <= K, Arr[i] <= 1000000000000
1 <= Q <= 10000In a new line Print two space-separated integers depicting the values of Floor and Ceil of the given number. If the floor or ceil of the element does not exist print -1.Sample Input:-
5
2 5 6 11 15
5
2
4
8
1
16
Sample Output:-
2 2
2 5
6 11
-1 2
15 -1, I have written this Solution Code: static void floorAndCeil(int a[], int n, int x){
int it = lower(a,n,x);
if(it==0){
if(a[it]==x){
System.out.println(x+" "+x);
}
else{
System.out.println("-1 "+a[it]);
}
}
else if (it==n){
it--;
System.out.println(a[it]+" -1");
}
else{
if(a[it]==x){
System.out.println(x+" "+x);
}
else{
it--;
System.out.println(a[it]+" "+a[it+1]);
}
}
}
static int lower(int a[], int n,int k){
int l=0;
int h=n-1;
int m;
int ans=n;
while(l<=h){
m=l+h;
m/=2;
if(a[m]<k){
l=m+1;
}
else{
h=m-1;
ans=m;
}
}
return ans;
}, 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.