Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int a=sc.nextInt();
int b=sc.nextInt();
ArrayList<Integer> arr=new ArrayList();
arr.add(a);
for(int j=1;j<b+1;j++)
{ int decrease=0;
int increse=0;
int mul=0;
int len=arr.size();
for(int e=0;e<len;e++)
{
decrease=arr.get(e)-3;
if(!arr.contains(decrease))
{
arr.add(decrease);
}
increse=arr.get(e)+3;
if(!arr.contains(increse))
{
arr.add(increse);
}
mul=arr.get(e)*2;
if(!arr.contains(mul))
{
arr.add(mul);
}
}
}
System.out.println(arr.size());
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: T = int(input())
for t in range(T):
n, k = map(int, input().strip().split())
s = set()
s.add(n)
l = list(s)
while(k):
for i in range(len(l)):
s.add(l[i]-3)
s.add(l[i]+3)
s.add(l[i]*2)
l = list(s)
k -= 1
print(len(s)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
set<int> ss;
int yy;
void solve(int a,int x)
{
ss.insert(a);
if(yy<=x) return;
solve((a+3LL),x+1);
solve((a-3LL),x+1);
solve((a*2LL),x+1);
}
signed main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int a,b;
cin>>a>>b;
ss.clear();
yy=b+1;
ss.insert(a);
solve(a,1);
cout<<ss.size()<<endl;
}
}, 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, your task is to print all subarrays of the given array in the order given as:-
First, print all the subarrays starting from the first element of the array in increasing order of their size. Then go for the second element and print all of its subarrays in increasing order and so on.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>PrintSubarrays</b> that takes the integer N and the array Arr as parameters.
Constraints:-
1 <= N <= 100
1 <= Arr[i] <= 100000Print all the subarray in a new line in the order mentioned above.Sample input:-
3
1 2 3
Sample Output:-
1
1 2
1 2 3
2
2 3
3
Sample Input:-
2
2 4
Sample Output:-
2
2 4
4, I have written this Solution Code: num = int(input())
arr = list(map(int,input().split()))
for i in range(0,num):
for j in range(i,num):
for k in range(i,j+1):
print (arr[k],end=" ")
print ("\n",end=""), 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, your task is to print all subarrays of the given array in the order given as:-
First, print all the subarrays starting from the first element of the array in increasing order of their size. Then go for the second element and print all of its subarrays in increasing order and so on.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>PrintSubarrays</b> that takes the integer N and the array Arr as parameters.
Constraints:-
1 <= N <= 100
1 <= Arr[i] <= 100000Print all the subarray in a new line in the order mentioned above.Sample input:-
3
1 2 3
Sample Output:-
1
1 2
1 2 3
2
2 3
3
Sample Input:-
2
2 4
Sample Output:-
2
2 4
4, I have written this Solution Code: static void PrintSubarrays(int Arr[], int N){
solve(Arr,N,0,0);
}
static void solve(int a[],int n, int cnt, int e){
if(e==n){cnt++;e=cnt;}
if(cnt==n){return;}
for(int i=cnt;i<=e;i++){
System.out.print(a[i]+" ");
}
System.out.println();
solve(a,n,cnt,e+1);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code: n=int(input())
x=n/3
if n%3==2:
x+=1
print(int(x)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int ans = n/3;
if(n%3==2){ans++;}
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int x=n/3;
if(n%3==2){
x++;}
cout<<x;}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int len = Integer.parseInt(br.readLine());
char[] str = new char[len];
for(int i = 0; i < len; i++){
if(i%2 == 0){
str[i] = 'a';
} else{
str[i] = 'b';
}
}
System.out.println(str);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, 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>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s(n,'a');
for(int i=1;i<n;i+=2)
s[i]='b';
cout<<s;
#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 positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: a="ab"
inp = int(input())
print(a*(inp//2)+a[0:inp%2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws 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]);
long p = Long.parseLong(str[1]);
long[] arr = new long[n];
long l = 0;
long mid = 0;
long cnt = 0;
long max = 1000000000000L;
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(input[i]);
}
while (l < max) {
mid = (l + max)/2;
cnt = 0;
for (int i = 0; i < n; i++) {
cnt += mid / arr[i];
}
if (cnt < p) {
l = mid + 1;
} else {
max = mid;
}
}
System.out.println(l);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: def noofdish(B,t):
a=0
for x in B:
a += t//x
return a
def findans(A,l,r,ans,p):
if(l>r):
print(ans)
else:
m=(l+r)//2
tdish=noofdish(A,m)
if tdish >=p:
findans(A,l,m-1,m,p)
else:
findans(A,m+1,r,ans,p)
n,p=[int(x) for x in input().split()]
A=[int(x) for x in input().split()]
findans(A,1,(p//n +1)*10,(p//n +1)*10,p), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara's team participated in an eating competition at FoodFest in which they are asked to eat at least P dishes.
Sara's team consists of N members where a member <b>i</b> (1 <= i <= N ) takes <b>Arr[i]</b> minutes to finish a single dish. Your task is to find the minimum time to finish at least P dishes.
Note: A member can eat a dish any number of times.First line of input contains two integers N and P. The next line of input contains N space separated integers depicting the values of Array.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 10
1 <= P <= 1000000000000Print the minimum time taken to eat at least P dishes.Sample Input:-
4 10
1 2 3 4
Sample Output:-
6
Explanation:-
1st member will eat 6 dishes
2nd member will eat 3 dishes
3rd member will eat 2 dishes
4th member will eat 1 dishes
total = 12
Sample Input:-
8 8
1 1 1 1 1 1 1 1
Sample Output:-
1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int p;
int n;
cin>>n>>p;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int l=0,h=1e10;
int mid=0;
int cnt=0;
int s,y;
while(l<h){
mid=(l+h);
mid/=2;
cnt=0;
for(int i=0;i<n;i++){
s=a[i];
y=2;
cnt+=mid/a[i];
}
// cout<<l<<" "<<h<<" "<<cnt<<endl;
if(cnt<p){l=mid+1;}
else{
h=mid;
}
}
cout<<l;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total 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));
String[] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int t=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
int sum=0;
for(int i=1;i<n;i++){
int dif=arr[i]-arr[i-1];
if(dif>t){
sum=sum+t;
}else{
sum=sum+dif;
}
}
sum+=t;
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: n , t = [int(x) for x in input().split() ]
l= [int(x) for x in input().split() ]
c = 0
for i in range(len(l)-1):
if l[i+1] - l[i]<=t:
c+=l[i+1] - l[i]
else:
c+=t
c+=t
print(c), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Arpit Gupta has brought a toy for his valentine. She is playing with that toy which runs for T seconds when winded.If winded when the toy is already running, from that moment it will run for T seconds (not additional T seconds) For example if T is 10 and toy has run for 5 seconds and winded at this moment then in total it will run for 15 seconds.
Arpit's Valentine winds the toy N times.She winds the toy at t[i] seconds after the first time she winds it.How long will the toy run in total?First Line of input contains two integers N and T
Second Line of input contains N integers, list of time Arpit's Valentine has wound the toy at.
Constraints
1 <= N <= 100000
1 <= T <= 1000000000
1 <= t[i] <= 1000000000
t[0] = 0Print a single integer the total time the toy has run.Sample input 1
2 4
0 3
Sample output 1
7
Sample input 2
2 10
0 5
Sample output 2
15
Explanation:
Testcase1:-
at first the toy is winded at 0 it will go till 4 but it again winded at 3 making it go for more 4 seconds
so the total is 7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long n,t;
cin>>n>>t;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long cur=t;
long ans=t;
for(int i=1;i<n;i++){
ans+=min(a[i]-a[i-1],t);
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array and Q queries. Your 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 the frontUser 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 the integer to be added and the maximum size of array as parameter.
<b>dequeue()</b>:- that takes the queue as parameter.
<b>displayfront()</b> :- that takes the queue as parameter.
Constraints:
1 <= Q(Number of queries) <= 10<sup>3</sup>
<b> Custom Input:</b>
First line of input should contains two integer number of queries Q and the size of the array N. Next Q lines contains any of the given three operations:-
enqueue x
dequeue
displayfrontDuring a dequeue operation if queue is empty you need to print "Queue is empty", during enqueue operation if the maximum size of array is reached you need to print "Queue is full" and during displayfront operation you need to print the element which is at the front and if the queue is empty you need to print "Queue is empty".
Note:-Each msg or element is to be printed on a new line
Sample Input:-
8 2
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
enqueue 5
Sample Output:-
Queue is empty
2
2
4
Queue is full
Explanation:-here size of given array is 2 so when last enqueue operation perfomed the array was already full so we display the msg "Queue is full".
Sample input:
5 5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: public static void enqueue(int x,int k)
{
if (rear >= k) {
System.out.println("Queue is full");
}
else {
a[rear] = x;
rear++;
}
}
public static void dequeue()
{
if (rear <= front) {
System.out.println("Queue is empty");
}
else {
front++;
}
}
public static void displayfront()
{
if (rear<=front) {
System.out.println("Queue is empty");
}
else {
int x = a[front];
System.out.println(x);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 ≤ N ≤ 2000
1 ≤ A<sub>i</sub> ≤ B<sub>i</sub> ≤ 2000
0 ≤ C<sub>i</sub> ≤ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer — the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: l = [0]*2001
k = []
n = int(input())
for i in range(n):
a,b,c = map(int,input().split())
k.append([b,(a,c)])
k.sort()
for b,aa in k:
a = aa[0]
c = aa[1]
cnt = 0
for i in range(b,a-1,-1):
if l[i]:
cnt+=1
if cnt>=c:
continue
else:
for i in range(b,a-1,-1):
if not l[i]:
l[i]=1
cnt+=1
if cnt==c:
break
print(sum(l)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Anya owns N triplets of integers (A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub>), for each i from 1 to N. She asks you to find a sequence of integers which satisfies the following conditions:
1. The sequence contains at least C<sub>i</sub> distinct integers from the closed interval [A<sub>i</sub>, B<sub>i</sub>], for each i from 1 to N.
2. Out of all sequences satisfying the first condition, choose a sequence with the minimum possible number of elements.
For simplicity, she asks you to just print the length of such a sequence.The first line of the input contains a single integer N denoting the number of triplets.
Then N lines follow, where the i<sup>th</sup> line contains three integers A<sub>i</sub>, B<sub>i</sub>, C<sub>i</sub> for each i from 1 to N.
<b> Constraints: </b>
1 ≤ N ≤ 2000
1 ≤ A<sub>i</sub> ≤ B<sub>i</sub> ≤ 2000
0 ≤ C<sub>i</sub> ≤ B<sub>i</sub> - A<sub>i</sub> + 1Print a single integer — the minimum possible sequence length.Sample Input 1:
1
1 3 3
Sample Output 1:
3
Sample Explanation 1:
Since there are only 3 elements in the closed interval [1,3], and we need to take 3 of them, clearly the smallest possible length is 3.
Sample Input 2:
2
1 3 1
3 5 1
Sample Output 2:
1
Sample Explanation 2:
We can take the sequence consisting of a single element {3}., I have written this Solution Code: //Author: Xzirium
//Time and Date: 00:28:35 28 December 2021
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t clk;
clk = clock();
//-----------------------------------------------------------------------------------------------------------//
READV(N);
vector<pair<pll,ll>> Z;
FORI(i,0,N)
{
READV(a);
READV(b);
READV(c);
Z.pb({{b,a},c});
}
sort(Z.begin(),Z.end());
ordered_set1<ll> unused;
FORI(i,1,2001)
{
unused.insert(i);
}
ll ans=0;
FORI(i,0,N)
{
ll a=Z[i].fi.se;
ll b=Z[i].fi.fi;
ll c=Z[i].se;
ll curr=b-a+1-(unused.order_of_key(a-1)-unused.order_of_key(b));
while(curr<c)
{
ans++;
curr++;
auto it=unused.lower_bound(b);
unused.erase(it);
}
}
cout<<ans<<endl;
//-----------------------------------------------------------------------------------------------------------//
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array 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: 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: 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: You are given an array A of size N, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
Sample Output
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));
String [] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int target = Integer.parseInt(input[1]);
input = br.readLine().split(" ");
int [] arr = new int[n];
for(int i =0; i<n; i++){
arr[i] = Integer.parseInt(input[i]);
}
Arrays.sort(arr);
int pointerLeft = 0;
int pointerRight = n-1;
boolean value = false;
while(pointerLeft<pointerRight){
if(arr[pointerLeft] + arr[pointerRight] == target){
System.out.println("1");
value = true;
break;
}
else if(arr[pointerLeft]+arr[pointerRight]<target){
pointerLeft++;
}
else{
pointerRight--;
}
}
if(value == false){
System.out.println("0");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
Sample Output
1
, I have written this Solution Code: st = set()
n, s = map(int, input().split())
l = list(map(int, input().split()))
stt = set(l)
found = False
for i in l:
if s - i in stt:
print(1)
found = True
break
if not found:
print(0), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N, and you are also given a sum. You need to find if two numbers in A exists such that their sum is equal to the given sum. If yes, print 1, else print 0.The first line contains N denoting the size of the array A and target sum. The second line contains N elements of the array. The third line contains element sum.
Constraints:
1 <= N <= 100000
1 <= A[i] <= 1000000Print 1, if there is an occurrence of the sum, else print 0.Sample Input
10 14
1 2 3 4 5 6 7 8 9 10
Sample Output
1
Explanation
10 + 4 = 14, so pair exists
Sample Input
5 9
1 2 3 4 5
Sample Output
1
, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int dp[sz];
signed main()
{
int n,k;
cin>>n>>k;
int A[n];
set<int> ss;
int ch=0;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
A[i]=a;
int p=k-a;
if(ss.find(p)!=ss.end()) ch=1;
ss.insert(a);
}
cout<<ch<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a natural number N, your task is to print all of its unique divisors in a sorted order.Input contains a single integer N.
Constraints:-
1 < = N < = 1000000000Print the unique divisors of N from smallest to largest.Sample Input:-
6
Sample Output:-
1 2 3 6
Sample Input:-
30
Sample Input:-
1 2 3 5 6 10 15 30, 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));
long num = Long.parseLong(br.readLine());
ArrayList<Long> arr = new ArrayList<Long>();
for(long i=1L;i<=Math.sqrt(num);i++)
{
if(num % i == 0L)
{
if (num/i == i){
arr.add(i);
}
else {
arr.add(i);
arr.add(num/i);
}
}
}
Collections.sort(arr);
for(Long number: arr){
System.out.print(number+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a natural number N, your task is to print all of its unique divisors in a sorted order.Input contains a single integer N.
Constraints:-
1 < = N < = 1000000000Print the unique divisors of N from smallest to largest.Sample Input:-
6
Sample Output:-
1 2 3 6
Sample Input:-
30
Sample Input:-
1 2 3 5 6 10 15 30, I have written this Solution Code: import math
n = int(input())
l=[]
for i in range(1,int(math.sqrt(n))+1):
if (n%i==0):
if(n//i==i):
l.append(i)
else:
l.append(i)
l.append(n//i)
l.sort()
print(*l), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a natural number N, your task is to print all of its unique divisors in a sorted order.Input contains a single integer N.
Constraints:-
1 < = N < = 1000000000Print the unique divisors of N from smallest to largest.Sample Input:-
6
Sample Output:-
1 2 3 6
Sample Input:-
30
Sample Input:-
1 2 3 5 6 10 15 30, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#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;
vector<int> v;
int x=sqrt(n);
for(int i=1;i<=x;i++){
if(n%i==0){v.EB(i);
if(i*i!=n){
v.EB(n/i);
}}
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
out1(v[i]);
}
}
, 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: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args)throws IOException
{
StringBuilder out=new StringBuilder();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int test=Integer.parseInt(br.readLine());
while(test-->0)
{
int n=Integer.parseInt(br.readLine());
String s=br.readLine();
int c1=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1') c1++;
}
if(c1%4==0) out.append("1\n");
else if(c1==s.length() && (c1/2)%2==0) out.append("1\n");
else
out.append("0\n");
}
System.out.print(out);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: T=int(input())
for i in range(T):
n=int(input())
a=input()
count_1=0
for i in a:
if i=='1':
count_1+=1
if count_1%2==0 and ((count_1)//2)%2==0:
print('1')
else:
print('0'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main() {
int t;
cin >> t;
for(int i=0; i<t; i++) {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for(int j=0; j<n; j++) {
if(s[j] == '1') cnt++;
}
if(cnt % 4 == 0) cout << 1 << "\n";
else cout << 0 << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, 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 t= Integer.parseInt(br.readLine());
int max = 1000001;
boolean isNotPrime[] = new boolean[max];
ArrayList<Integer> arr = new ArrayList<Integer>();
isNotPrime[0] = true; isNotPrime[1] = true;
for (int i=2; i*i <max; i++) {
if (!isNotPrime[i]) {
for (int j=i*i; j<max; j+= i) {
isNotPrime[j] = true;
}
}
}
for(int i=2; i<max; i++) {
if(!isNotPrime[i]) {
arr.add(i);
}
}
while(t-- > 0) {
String str[] = br.readLine().trim().split(" ");
int l = Integer.parseInt(str[0]);
int r = Integer.parseInt(str[1]);
System.out.println(primeRangeSum(l,r,arr));
}
}
static long primeRangeSum(int l , int r, ArrayList<Integer> arr) {
long sum = 0;
for(int i=l; i<=r;i++) {
sum += arr.get(i-1);
}
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
pri = []
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
pri.append(p)
return pri
N = int(input())
X = []
prim = SieveOfEratosthenes(1000000)
for i in range(1,len(prim)):
prim[i] = prim[i]+prim[i-1]
for i in range(N):
nnn = input()
X.append((int(nnn.split()[0]),int(nnn.split()[1])))
for xx,yy in X:
if xx==1:
print(prim[yy-1])
else:
print(prim[yy-1]-prim[xx-2])
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, 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;
vector<int> v;
v.push_back(0);
for(int i = 2; i < N; i++){
if(a[i]) continue;
v.push_back(i);
for(int j = i*i; j < N; j += i)
a[j] = 1;
}
int p = 0;
for(auto &i: v){
i += p;
p = i;
}
int t; cin >> t;
while(t--){
int l, r;
cin >> l >> r;
cout << v[r] - v[l-1] << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice's power is currently known to be an integer X. It is also known that her power doubles every second. For example, if Alice's power was currently 20, then after 2 seconds her power would have grown to 80.
Your task is to find out Alice's power after N seconds.The input consists of a single line containing two space-separated integers X and N.
<b>Constraints:</b>
1 ≤ X ≤ 1000
1 ≤ N ≤ 10Print a single integer – the power of Alice after N seconds.Sample Input 1:
5 2
Sample Output 1:
20
Sample Explanation 1:
Alice's power after 1 second will be 5*2 = 10. After 2 seconds it will be 10*2 = 20.
Sample Input 2:
4 3
Sample Output 2:
32, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if(s==null){
System.exit(0);
}
StringTokenizer st = new StringTokenizer(s, " ");
int power = Integer.parseInt(st.nextToken());
int multiple = Integer.parseInt(st.nextToken());
int res = power;
for(int i = 1;i<=multiple;i++){
res = res*2;
}
System.out.println(res);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice's power is currently known to be an integer X. It is also known that her power doubles every second. For example, if Alice's power was currently 20, then after 2 seconds her power would have grown to 80.
Your task is to find out Alice's power after N seconds.The input consists of a single line containing two space-separated integers X and N.
<b>Constraints:</b>
1 ≤ X ≤ 1000
1 ≤ N ≤ 10Print a single integer – the power of Alice after N seconds.Sample Input 1:
5 2
Sample Output 1:
20
Sample Explanation 1:
Alice's power after 1 second will be 5*2 = 10. After 2 seconds it will be 10*2 = 20.
Sample Input 2:
4 3
Sample Output 2:
32, I have written this Solution Code: #include <iostream>
using namespace std;
int main()
{
int x, n;
cin >> x >> n;
cout << x*(1 << n);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alice's power is currently known to be an integer X. It is also known that her power doubles every second. For example, if Alice's power was currently 20, then after 2 seconds her power would have grown to 80.
Your task is to find out Alice's power after N seconds.The input consists of a single line containing two space-separated integers X and N.
<b>Constraints:</b>
1 ≤ X ≤ 1000
1 ≤ N ≤ 10Print a single integer – the power of Alice after N seconds.Sample Input 1:
5 2
Sample Output 1:
20
Sample Explanation 1:
Alice's power after 1 second will be 5*2 = 10. After 2 seconds it will be 10*2 = 20.
Sample Input 2:
4 3
Sample Output 2:
32, I have written this Solution Code: x,n = map(int,input().split())
print(x*(2**n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: import java.util.*;
class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int a=sc.nextInt();
int b=sc.nextInt();
ArrayList<Integer> arr=new ArrayList();
arr.add(a);
for(int j=1;j<b+1;j++)
{ int decrease=0;
int increse=0;
int mul=0;
int len=arr.size();
for(int e=0;e<len;e++)
{
decrease=arr.get(e)-3;
if(!arr.contains(decrease))
{
arr.add(decrease);
}
increse=arr.get(e)+3;
if(!arr.contains(increse))
{
arr.add(increse);
}
mul=arr.get(e)*2;
if(!arr.contains(mul))
{
arr.add(mul);
}
}
}
System.out.println(arr.size());
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: T = int(input())
for t in range(T):
n, k = map(int, input().strip().split())
s = set()
s.add(n)
l = list(s)
while(k):
for i in range(len(l)):
s.add(l[i]-3)
s.add(l[i]+3)
s.add(l[i]*2)
l = list(s)
k -= 1
print(len(s)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given 2 numbers a, b.
You can perform at most b steps
In one step :
1- increase a by 3
2- decrease a by 3
3- multiply a by 2
Find number of distinct numbers we can make after performing at most b operations on a.The first line contains the number of tests T.
For each test case:
Input two integers a and b.
0 < T <= 100
1 <= a <= 100000
1 <= b <= 9Print answer on a separate line for each test caseSample Input
2
5 1
30 2
Sample Output
4
11
For test 1:-
In 0 steps, 5 can be formed
In 1 steps 2, 8, 10 can be formed
For test 2:-
in 0 step :- 30
in 1 step- 27 33 60
in 2 step:- 24, 30, 54, 30, 36, 66, 57 63 120
total unique number = 11, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
set<int> ss;
int yy;
void solve(int a,int x)
{
ss.insert(a);
if(yy<=x) return;
solve((a+3LL),x+1);
solve((a-3LL),x+1);
solve((a*2LL),x+1);
}
signed main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int a,b;
cin>>a>>b;
ss.clear();
yy=b+1;
ss.insert(a);
solve(a,1);
cout<<ss.size()<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function replaceArray(arr, n) {
// write code here
// do not console.log
// return the new array
const newArr = []
newArr[0] = arr[0] * arr[1]
newArr[n-1] = arr[n-1] * arr[n-2]
for(let i= 1;i<n-1;i++){
newArr[i] = arr[i-1] * arr[i+1]
}
return newArr
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: n = int(input())
X = [int(x) for x in input().split()]
lst = []
for i in range(len(X)):
if i == 0:
lst.append(X[i]*X[i+1])
elif i == (len(X) - 1):
lst.append(X[i-1]*X[i])
else:
lst.append(X[i-1]*X[i+1])
for i in lst:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long b[n],a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=1;i<n-1;i++){
b[i]=a[i-1]*a[i+1];
}
b[0]=a[0]*a[1];
b[n-1]=a[n-1]*a[n-2];
for(int i=0;i<n;i++){
cout<<b[i]<<" ";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(a[0]*a[1]+" ");
for(int i=1;i<n-1;i++){
System.out.print(a[i-1]*a[i+1]+" ");
}
System.out.print(a[n-1]*a[n-2]);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: static int LikesBoth(int N, int A, int B){
return (A+B-N);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: def LikesBoth(N,A,B):
return (A+B-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: int LikesBoth(int N,int A, int B){
return (A+B-N);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: int LikesBoth(int N,int A, int B){
return (A+B-N);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., 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());
if(n==1){
System.out.println(2);
}else{
int after = afterPrime(n);
int before = beforePrime(n);
if(before>after){
System.out.println(n+after);
}
else{System.out.println(n-before);}
}
}
public static boolean isPrime(int n)
{
int count=0;
for(int i=2;i*i<n;i++)
{
if(n%i==0)
count++;
}
if(count==0)
return true;
else
return false;
}
public static int beforePrime(int n)
{
int c=0;
while(true)
{
if(isPrime(n))
return c;
else
{
n=n-1;
c++;
}
}
}
public static int afterPrime(int n)
{
int c=0;
while(true)
{
if(isPrime(n))
return c;
else
{
n=n+1;
c++;
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., I have written this Solution Code: from math import sqrt
def NearPrime(N):
if N >1:
for i in range(2,int(sqrt(N))+1):
if N%i ==0:
return False
break
else: return True
else: return False
N=int(input())
i =0
while NearPrime(N-i)==False and NearPrime(N+i)==False:
i+=1
if NearPrime(N-i) and NearPrime(N+i):print(N-i)
elif NearPrime(N-i):print(N-i)
elif NearPrime(N+i): print(N+i), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., 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>
/////////////
bool isPrime(int n){
if(n<=1)
return false;
for(int i=2;i*i<=n;++i)
if(n%i==0)
return false;
return true;
}
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
if(n==1)
cout<<"2";
else{
int v1=n,v2=n;
while(isPrime(v1)==false)
--v1;
while(isPrime(v2)==false)
++v2;
if(v2-n==n-v1)
cout<<v1;
else{
if(v2-n<n-v1)
cout<<v2;
else
cout<<v1;
}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 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 n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] val = new int[n];
for(int i=0; i<n; i++){
val[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] freq = new int[n];
for(int i=0; i<n; i++){
freq[i] = Integer.parseInt(st.nextToken());
}
int k = Integer.parseInt(br.readLine());
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (val[j] < val[i]) {
int temp = val[i];
val[i] = val[j];
val[j] = temp;
int temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;
}
}
}
int element=0;
for(int i=0; i<n; i++){
for(int j=0; j<freq[i]; j++){
element++;
int value = val[i];
if(element==k){
System.out.print(value);
break;
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: def myFun():
n = int(input())
arr1 = list(map(int,input().strip().split()))
arr2 = list(map(int,input().strip().split()))
k = int(input())
arr = []
for i in range(n):
arr.append((arr1[i], arr2[i]))
arr.sort()
c = 0
for i in arr:
k -= i[1]
if k <= 0:
print(i[0])
return
myFun()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define inf 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;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// 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 N;ll K;
cin>>N;
int c=0;
pair<int, ll> A[N];
for(int i=0;i<N;++i){
cin >> A[i].first ;
}
for(int i=0;i<N;++i){
cin >> A[i].second ;
}
cin>>K;
sort(A, A+N);
for(int i=0;i<N;++i){
K -= A[i].second;
if(K <= 0){
cout << A[i].first << endl;;
break;
}
}
#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: Create a class Teacher with name, age, and salary attributes, where salary must be a private attribute that cannot be accessed outside the class.
Print the details of the teacher including salary.The first line contains the name, the second line contains the age and the third line contains the salary of the teacher.
<b>Constraints:</b>
20<=age<=80Prints the details of the teacher in the following format:
Name:
Age:
Salary:Sample Input:
raj
22
20000
Sample Output:
Name: raj
Age: 22
Salary: 20000, I have written this Solution Code: class Teacher():
def __init__(self, name, age, salary):
self.name = name
self.age = age
# private variable
self.__salary = salary
def show_details(self):
print("Name:", self.name)
print("Age:", self.age)
#access private attribute inside the class
print("Salary:", self.__salary)
name=input()
age=int(input())
salary=int(input())
teacher = Teacher(name,age,salary)
teacher.show_details(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Create a class Teacher with name, age, and salary attributes, where salary must be a private attribute that cannot be accessed outside the class.
Print the details of the teacher including salary.The first line contains the name, the second line contains the age and the third line contains the salary of the teacher.
<b>Constraints:</b>
20<=age<=80Prints the details of the teacher in the following format:
Name:
Age:
Salary:Sample Input:
raj
22
20000
Sample Output:
Name: raj
Age: 22
Salary: 20000, I have written this Solution Code: import java.io.*;
import java.util.*;
class Teacher{
String name;
int age;
private int salary;
Teacher(String name , int age , int salary){
this.name = name;
this.age = age;
this.salary = salary;
}
public void details(){
System.out.println("Name: "+this.name);
System.out.println("Age: "+ this.age);
System.out.print("Salary: "+this.salary);
}
}
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int age = sc.nextInt();
int salary = sc.nextInt();
Teacher obj = new Teacher (name , age , salary);
obj.details();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For creating a memory collage of the platform for Christmas festival, we want to fetch all those posts that were made in the Christmas week of the year 2020. Write a query to fetch all the posts made between the dates 1 December 2020 to 2 January 2021, both dates included.
<schema>[{'name': 'post', 'columns': [{'name': 'id', 'type': 'int'}, {'name': 'username', 'type': 'varchar(24)'}, {'name': 'post_title', 'type': 'varchar (72)'}, {'name': 'post_description', 'type': 'text'}, {'name': 'datetime_created', 'type': 'datetime'}, {'name': 'number_of_likes', 'type': 'int'}, {'name': 'photo', 'type': 'blob'}]}]</schema>nannannan, I have written this Solution Code: SELECT * FROM post WHERE datetime_created BETWEEN '2020-12-01' AND '2021-01-02';, In this Programming Language: SQL, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N which is sorted. You are also given another integer array B of size M which may or may not be sorted but all the elements of B are distinct. Intersection of two arrays A and B is a sorted array C which contains only distinct elements and all elements of C are present in both A and B. Find the intersection of A and B.
Note: There is atleast one common element in A and B.First line contains an integer N.
Next line contains N space separated integers denoting elements of array A.
Next line contains an integer M.
Next line contains M space separated integers denoting elements of array B.
Constraints
1 <= N <= 10^5
0 <= Ai <= 10^5
1 <= M <= 10^5
0 <= Bi <= 10^5Print K space separated integers denoting the elements of array C - intersection of arrays A and B.Sample Input 1:
3
1 2 3
3
2 1 3
Output
1 2 3
Explanation:
1,2 and 3 are present in both arrays.
Sample Input 2:
3
1 1 2
4
2 3 4 5
Output
2
Explanation:
Only element 2 is common in both A and B., I have written this Solution Code: def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
n=int(input())
lst1 = list(map(int,input().strip().split()))
n2=int(input())
lst2 = list(map(int,input().strip().split()))
l=(intersection(lst1, lst2))
l.sort()
for i in l:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array A of size N which is sorted. You are also given another integer array B of size M which may or may not be sorted but all the elements of B are distinct. Intersection of two arrays A and B is a sorted array C which contains only distinct elements and all elements of C are present in both A and B. Find the intersection of A and B.
Note: There is atleast one common element in A and B.First line contains an integer N.
Next line contains N space separated integers denoting elements of array A.
Next line contains an integer M.
Next line contains M space separated integers denoting elements of array B.
Constraints
1 <= N <= 10^5
0 <= Ai <= 10^5
1 <= M <= 10^5
0 <= Bi <= 10^5Print K space separated integers denoting the elements of array C - intersection of arrays A and B.Sample Input 1:
3
1 2 3
3
2 1 3
Output
1 2 3
Explanation:
1,2 and 3 are present in both arrays.
Sample Input 2:
3
1 1 2
4
2 3 4 5
Output
2
Explanation:
Only element 2 is common in both A and B., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
static boolean binary_search(int[] a, int n, int tar) {
int l = 0;
int r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] == tar) {
return true;
}
if (a[mid] < tar)l = mid + 1;
else r = mid - 1;
}
return false;
}
public static void main (String args[]) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int a[] = new int[n];
String line = br.readLine();
String[] strs = line.trim().split("\\s+");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(strs[i]);
}
int m=Integer.parseInt(br.readLine());
int b[] = new int[m];
line = br.readLine();
strs = line.trim().split("\\s+");
for (int i = 0; i < m; i++) {
b[i] = Integer.parseInt(strs[i]);
}
ArrayList<Integer> ans = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
if (binary_search(a, n, b[i]))ans.add(b[i]);
}
Collections.sort(ans);
for (int i = 0; i < ans.size(); i++)
System.out.print(ans.get(i) + " ");
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7An array containing numbers.Print true if such triplet exists summing to 7 else print falseSample input:-
[2, 1, 5, 1, 0]
[1, 6]
Sample output:-
true
false
Explanation:-
1+5+1 = 7
no 3 consecutive numbers so false, I have written this Solution Code: function lucky_sevens(arr) {
// if less than 3 elements then this challenge is not possible
if (arr.length < 3) {
console.log(false)
return;
}
// because we know there are at least 3 elements we can
// start the loop at the 3rd element in the array (i=2)
// and check it along with the two previous elements (i-1) and (i-2)
for (let i = 2; i < arr.length; i++) {
if (arr[i] + arr[i-1] + arr[i-2] === 7) {
console.log(true)
return;
}
}
// if loop is finished and no elements summed to 7
console.log(false)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)., 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 input[]=br.readLine().split("\\s");
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(input[i]);
}
System.out.print(implementMergeSort(a,0,n-1));
}
public static long implementMergeSort(int arr[], int start, int end)
{
long count=0;
if(start<end)
{
int mid=start+(end-start)/2;
count +=implementMergeSort(arr,start,mid);
count +=implementMergeSort(arr,mid+1,end);
count +=merge(arr,start,end,mid);
}
return count;
}
public static long merge(int []a,int start,int end,int mid)
{
int i=start;
int j=mid+1;
int k=0;
int len=end-start+1;
int c[]=new int[len];
long inv_count=0;
while(i<=mid && j<=end)
{
if(a[i]<=a[j])
{
c[k++]=a[i];
i++;
}
else
{
c[k++]=a[j];
j++;
inv_count +=(mid-i)+1;
}
}
while(i<=mid)
{
c[k++]=a[i++];
}
while(j<=end)
{
c[k++]=a[j++];
}
for(int l=0;l<len;l++)
a[start+l]=c[l];
return inv_count;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
long long _mergeSort(int arr[], int temp[], int left, int right);
long long merge(int arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
long long mergeSort(int arr[], int array_size)
{
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
long long _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
long long merge(int arr[], int temp[], int left,
int mid, int right)
{
int i, j, k;
long long inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
signed main()
{
int n;
cin>>n;
int a[n];
unordered_map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
if(m.find(a[i])==m.end()){
m[a[i]]=i;
}
}
cout<<mergeSort(a,n);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: def minDistanceCoveredBySara(N):
if N%4==1 or N%4==2:
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: static int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: 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 an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., 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());
if(n==1){
System.out.println(2);
}else{
int after = afterPrime(n);
int before = beforePrime(n);
if(before>after){
System.out.println(n+after);
}
else{System.out.println(n-before);}
}
}
public static boolean isPrime(int n)
{
int count=0;
for(int i=2;i*i<n;i++)
{
if(n%i==0)
count++;
}
if(count==0)
return true;
else
return false;
}
public static int beforePrime(int n)
{
int c=0;
while(true)
{
if(isPrime(n))
return c;
else
{
n=n-1;
c++;
}
}
}
public static int afterPrime(int n)
{
int c=0;
while(true)
{
if(isPrime(n))
return c;
else
{
n=n+1;
c++;
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., I have written this Solution Code: from math import sqrt
def NearPrime(N):
if N >1:
for i in range(2,int(sqrt(N))+1):
if N%i ==0:
return False
break
else: return True
else: return False
N=int(input())
i =0
while NearPrime(N-i)==False and NearPrime(N+i)==False:
i+=1
if NearPrime(N-i) and NearPrime(N+i):print(N-i)
elif NearPrime(N-i):print(N-i)
elif NearPrime(N+i): print(N+i), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, find the closest prime number to N. If there are multiple print the smaller one.The input contains a single integer N.
Constraints:
1 <= N <= 1000000000Print the closest prime to N.Sample Input 1
12
Sample Output 1
11
Explanation: Closest prime to 12 is 11 and 13 smaller of which is 11.
Sample Input 2
17
Sample Output 2
17
Explanation: Closest prime to 17 is 17., 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>
/////////////
bool isPrime(int n){
if(n<=1)
return false;
for(int i=2;i*i<=n;++i)
if(n%i==0)
return false;
return true;
}
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
if(n==1)
cout<<"2";
else{
int v1=n,v2=n;
while(isPrime(v1)==false)
--v1;
while(isPrime(v2)==false)
++v2;
if(v2-n==n-v1)
cout<<v1;
else{
if(v2-n<n-v1)
cout<<v2;
else
cout<<v1;
}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. Your task is to print the number of prime numbers before that number.The first line of the number of test cases T.
Next T lines contains the value of N.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 1000Print the number of primes numbers before that number.Sample Input 1:
3
10
19
4
Sample Output 1:
4
8
2, I have written this Solution Code: n = 1000
arr = [True for i in range(n+1)]
i = 2
while i*i <= n:
if arr[i] == True:
for j in range(i*2, n+1, i):
arr[j] = False
i +=1
arr2 = [0] * (n+1)
for i in range(2,n+1):
if arr[i]:
arr2[i] = arr2[i-1] + 1
else:
arr2[i] = arr2[i-1]
x = int(input())
for i in range(x):
y = int(input())
print(arr2[y]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a number n. Your task is to print the number of prime numbers before that number.The first line of the number of test cases T.
Next T lines contains the value of N.
<b>Constraints</b>
1 <= T <= 100
1 <= N <= 1000Print the number of primes numbers before that number.Sample Input 1:
3
10
19
4
Sample Output 1:
4
8
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
vector<bool> sieve(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
int main() {
vector<bool> prime = sieve(1e5 + 1);
vector<int> prefix(1e5 + 1, 0);
for (int i = 1; i <= 1e5; i++) {
if (prime[i]) {
prefix[i] = prefix[i - 1] + 1;
} else {
prefix[i] = prefix[i - 1];
}
}
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
cout << prefix[n] << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and an integer D. Your task is to rotate the array D times in a circular manner from the right to left direction. Consider the examples for better understanding:-
Try to do without creating another arrayUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>rotate()</b> that takes the array, size of the array, and the integer d as a parameter.
Constraints:
1 <= T <= 25
2 <= N <= 10^4
1<=D<=10^5
1 <= A[i] <= 10^5For each test case, you just need to rotate the array by D times. The driver code will prin the rotated array in a new line.Sample Input:
2
8
4
1 2 3 4 5 6 7 8
10
3
1 2 3 4 5 6 7 8 9 10
Sample Output:
5 6 7 8 1 2 3 4
4 5 6 7 8 9 10 1 2 3
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
After the first rotation, the array becomes 2 3 4 5 6 7 8 1
After the second rotation, the array becomes 3 4 5 6 7 8 1 2
After the third rotation, the array becomes 4 5 6 7 8 1 2 3
After the fourth rotation, the array becomes 5 6 7 8 1 2 3 4
Hence the final result: 5 6 7 8 1 2 3 4, I have written this Solution Code: public static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static void rotate(int arr[], int n, int d){
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;
boolean win=true;
while (win) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: During this period of social distancing, Tono can't help herself remember the N beautiful numbers that exist. The numbers, without a doubt, are 1 to N. Beautiful numbers are extremely expensive, the cost of buying the i-th beautiful number is cost[i].
But there is one thing that only Tono can do, she can increment the values of all the numbers she possess at any point of time by 1, except for N that turns to 1. For example if she currently possess 2, 4, 7 (N=7) she can change them to 3, 5, 1. The cost of this magic is X.
There are infinite numbers of each kind. Help Tono find the minimum cost for possessing all the numbers.
Note: She can buy any number at any point of time, and use the magic at any point of time as well. The magic can be used any number of times, but the cost incurred every time is X.The first line of the input contains two numbers N and X.
The next line contains N integers, the cost of the N beautiful numbers, cost[1], cost[2],...,cost[N].
Constraints
1 <= N <= 2000
1 <= X <= 1000000000 (10^9)
1 <= cost[i] <= 1000000000 (10^9)The only line of the output should contain the minimum cost for possessing all the beautiful numbers.Sample Input
2 10
2 15
Sample Output
14
Explanation: First Tono buys number 1, then use the magic, then buy the number 1. (2+10+2=14), 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 int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// 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 dp[2005][2005];
signed main()
{
fast
int n, x; cin>>n>>x;
int a[n];
int ans = INF;
for(int i=0; i<n; i++){
cin>>a[i];
dp[i][0]=a[i];
}
for(int i=0; i<n; i++){
for(int j=1; j<n; j++){
dp[i][j]=min(dp[i][j-1], a[(i+j)%n]);
}
}
for(int i=0; i<n; i++){
int val = 0;
for(int j=0; j<n; j++){
val += dp[j][i];
}
val += x*i;
ans = min(ans, val);
}
cout<<ans;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and D, your task is to find the numbers between 1 to N which contains the digit D at least 1 time.Input contains only two integers N and D.
Constraints:-
1 < = N < = 100000
1 < = D < = 9Print all the numbers from 1 to N which contains the digit D in it separated by space in non decreasing order.Sample Input:-
20 5
Sample Output:-
5 15
Sample Input:-
15 1
Sample Output:-
1 10 11 12 13 14 15, I have written this Solution Code: /**
* author: tourist1256
* created: 2022-07-08 04:16:54
**/
#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(int l = LLONG_MIN, int r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
int32_t main() {
auto start = std::chrono::high_resolution_clock::now();
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d;
cin >> n >> d;
int cnt = 0;
function<int(int, int)> f = [&](int d, int m) {
while (d) {
int x = d % 10;
d /= 10;
if (x == m) {
return 1;
}
}
return 0;
};
for (int i = 1; i <= n; i++) {
if (f(i, d)) {
cout << i << " ";
}
}
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\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and D, your task is to find the numbers between 1 to N which contains the digit D at least 1 time.Input contains only two integers N and D.
Constraints:-
1 < = N < = 100000
1 < = D < = 9Print all the numbers from 1 to N which contains the digit D in it separated by space in non decreasing order.Sample Input:-
20 5
Sample Output:-
5 15
Sample Input:-
15 1
Sample Output:-
1 10 11 12 13 14 15, I have written this Solution Code: def index(st, ch):
for i in range(len(st)):
if (st[i] == ch):
return i;
return -1
def printNumbers(n, d):
# Converting d to character
st = "" + str(d)
ch = st[0]
l = []
# Loop to check each digit one by one.
for i in range(0, n + 1, 1):
# initialize the string
st = ""
st = st + str(i)
# checking for digit
if (i == d or index(st, ch) >= 0):
# print(i, end = " ")
l.append(i)
for k in range(0, len(l) - 1):
print(l[k], end=" ")
print(l[len(l) - 1])
# Driver code
if __name__ == '__main__':
li = list(map(int, input().strip().split()))
n = li[0]
d = li[1]
printNumbers(n, d), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and D, your task is to find the numbers between 1 to N which contains the digit D at least 1 time.Input contains only two integers N and D.
Constraints:-
1 < = N < = 100000
1 < = D < = 9Print all the numbers from 1 to N which contains the digit D in it separated by space in non decreasing order.Sample Input:-
20 5
Sample Output:-
5 15
Sample Input:-
15 1
Sample Output:-
1 10 11 12 13 14 15, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int d=sc.nextInt();
printnumber(n,d);
}
static boolean ispresent(int n,int d){
while(n>0){
if(n%10==d)
break;
n=n / 10;
}
return n>0;
}
static void printnumber(int n,int d){
for(int i=0;i<=n;i++){
if(i==d||ispresent(i,d)){
System.out.print(i+" ");
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer N. Find the number of ways to fill a NX2 grid with red and blue colours such that:
1. There is at least one square of each colour.
2. Both colours form a connected component, i.e. it is possible to travel between any two squares of the same colour, only by moving through squares of this colour. You can move from one square to other if they share a side. You cannot move from a square to other if they share a corner.
Since the answer can be very large, find the answer modulo 998244353.The first line contains an integer T (1 <= T <= 10^5) - the number of test cases.
Next T lines contain the value of N (1 <= N <= 10^18) for T test cases.For each test case, print the answer modulo 998244353 in a separate line.Sample Input 1:
2
1
2
Sample Output 2:
2
12
Explanation:
For testcase 1, possible solutions are [R B] and [B R]., I have written this Solution Code:
import java.io.*;
class Main
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
long n=Long.parseLong(bu.readLine()),M=998244353;
long ans=(n%M)*((n*2-1)%M)*2%M;
sb.append(ans+"\n");
}
System.out.print(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer N. Find the number of ways to fill a NX2 grid with red and blue colours such that:
1. There is at least one square of each colour.
2. Both colours form a connected component, i.e. it is possible to travel between any two squares of the same colour, only by moving through squares of this colour. You can move from one square to other if they share a side. You cannot move from a square to other if they share a corner.
Since the answer can be very large, find the answer modulo 998244353.The first line contains an integer T (1 <= T <= 10^5) - the number of test cases.
Next T lines contain the value of N (1 <= N <= 10^18) for T test cases.For each test case, print the answer modulo 998244353 in a separate line.Sample Input 1:
2
1
2
Sample Output 2:
2
12
Explanation:
For testcase 1, possible solutions are [R B] and [B R]., I have written this Solution Code: t = int(input())
m = 998244353
for i in range(t):
n = int(input())
print(((2*n)*(2*n - 1))%m), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an integer N. Find the number of ways to fill a NX2 grid with red and blue colours such that:
1. There is at least one square of each colour.
2. Both colours form a connected component, i.e. it is possible to travel between any two squares of the same colour, only by moving through squares of this colour. You can move from one square to other if they share a side. You cannot move from a square to other if they share a corner.
Since the answer can be very large, find the answer modulo 998244353.The first line contains an integer T (1 <= T <= 10^5) - the number of test cases.
Next T lines contain the value of N (1 <= N <= 10^18) for T test cases.For each test case, print the answer modulo 998244353 in a separate line.Sample Input 1:
2
1
2
Sample Output 2:
2
12
Explanation:
For testcase 1, possible solutions are [R B] and [B R]., I have written this Solution Code: //*** Author: ShlokG ***//
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
typedef long long int ll;
typedef unsigned long long int ull;
#define pb push_back
#define endl "\n"
const ll MAX=1e18+5;
ll mod=1e9+7;
ll mod1=998244353;
const int infi = 1e9;
const ll INF=1e18;
void precompute(){
}
void TEST_CASE(){
ll n;
cin >> n;
ll n1=2*n;
n1%=mod1;
ll n2=n1-1+mod1;
n2%=mod1;
cout << (n1*n2)%mod1 << endl;
}
signed main(){
fast;
//freopen ("INPUT.txt","r",stdin);
//freopen ("OUTPUT.txt","w",stdout);
int test=1,TEST=1;
precompute();
cin >> test;
while(test--){
//cout << "Case #" << TEST++ << ": ";
TEST_CASE();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: John and Olivia once start fighting when Olivia sees John talking with another girl. So, you tell them to play the game of love in which you give them an array A of size N. Now, the game will play in turns in which a player must decrease the value at the smallest index (with a non-zero value) in A by x (x > 0). The player who cannot make a move will lose the game.
You being a loyal friend of Olivia, wants her to win. So, tell Olivia whether she should move first or second to win this game.
Assume both players will play optimally at every step of the game.The first line contains a single integer T (1 ≤ T ≤ 1000) — the number of test cases.
The first line of each test case contains a single integer N (1 ≤ N ≤ 100) — the size of the array A on which the game is to be played.
.
The second line contains n integers A<sub>1</sub>, A<sub>2</sub>, …, A<sub>N</sub> (1 ≤ A<sub>i</sub> ≤ 10<sup>9</sup>).For each test case, print on a new line whether Olivia should move "first" or "second" (without quotes) to win the game.Sample Input:
2
3
2 1 3
2
1 1
Sample Output:
first
second
Sample Explanation:
For the first test case, Olivia will remove 2 from the 1st index, then John has to remove 1 from the 2nd index, and finally, Olivia will remove 3 from the 3rd index.
For the second test case, John has to remove 1 from the 1st index, then Olivia removes 1 from the 2nd index., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> v(n);
bool ok=1;
for(auto &i:v) cin >> i;
int ans=1;
for(auto i:v){
if(i!=1) ok=0;
if(i==1) ans^=1;
else break;
}
if(!ok){
if(ans){
cout << "first\n";
}else{
cout << "second\n";
}
}else{
if(n&1){
cout << "first\n";
}else{
cout << "second\n";
}
}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Gian and Suneo want their heights to be equal so they asked Doraemon's help. Doraemon gave a big light to both of them but the both big lights have different speed of magnifying. Let's assume the big light given to Gian can increase height of a person by v1 m/s and that of Suneo's big light is v2 m/s.
At the end of each second Doraemon check if their heights are equal or not.
Given initial height of Gian and Suneo, your task is to check whether the height of Gian and Suneo will become equal at some point or not, assuming they both started at the same time.First line takes the input of integer h1(height of gian), h2(height of suneo), v1(speed of Gian's big light) and v2(speed of Suneo's big light) as parameter.
<b>Constraints:-</b>
1 <b>≤</b> h2 < h1<b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v1 <b>≤</b> 10<sup>4</sup>
1 <b>≤</b> v2 <b>≤</b> 10<sup>4</sup>complete the function EqualOrNot and return a boolean True if their height will become equal at some point (as seen by Doraemon) else print False
Sample input:-
4 2 2 4
Sample output:-
Yes
Explanation:-
height of Gian goes as- 4 6 8 10. .
height of Suneo goes as:- 2 6 10..
at the end of 1 second their height will become equal.
Sample Input:-
5 4 1 6
Sample Output:
No, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
bool EqualOrNot(int h1, int h2, int v1,int v2){
if (v2>v1&&(h1-h2)%(v2-v1)==0){
return true;
}
return false;
}
int main(){
int n1,n2,v1,v2;
cin>>n1>>n2>>v1>>v2;
if(EqualOrNot(n1,n2,v1,v2)){
cout<<"Yes";}
else{
cout<<"No";
}
}
, In this Programming Language: C++, 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.