Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<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>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code:
void patternPrinting(int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a right-angle triangle pattern of consecutive numbers of height N.
See the example for a better understanding.<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>pattern()</b> that takes integer n as a parameter.
Constraint:
1 <= N <= 100Print a right angle triangle of numbers of height N.Sample Input:
5
Sample Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sample Input:
2
Sample Output:
1
1 2, I have written this Solution Code: def patternPrinting(n):
for i in range(1,n+1):
for j in range (1,i+1):
print(j,end=' ')
print()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., 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().trim().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
str = br.readLine().trim().split(" ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(str[i]);
}
HashMap<Integer, Integer> hm = new HashMap<>();
for (int i = 0; i < k; i++) {
if (hm.containsKey(arr[i])) {
int temp = hm.get(arr[i]);
hm.put(arr[i], temp+1);
}
else {
hm.put(arr[i], 1);
}
}
System.out.print(hm.size() +" ");
for (int i = k; i < n; i++) {
int temp = hm.get(arr[i-k]);
if (temp == 1){
hm.remove(arr[i-k]);
}
else {
hm.put(arr[i-k], temp-1);
}
if (hm.containsKey(arr[i])) {
temp = hm.get(arr[i]);
hm.put(arr[i], temp+1);
}
else {
hm.put(arr[i], 1);
}
System.out.print(hm.size() +" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., I have written this Solution Code: n,k=input().split()
n=int(n)
k=int(k)
arr=input().split()
for i in range(0,len(arr)):
arr[i]=int(arr[i])
hs={}
for i in range(0,k):
if arr[i] not in hs:
hs[arr[i]]=1
else:
hs[arr[i]]+=1
print (len(hs),end=' ')
for i in range(k,n):
if(hs[arr[i-k]]==1):
del hs[arr[i-k]]
else:
hs[arr[i-k]]-=1
if arr[i] not in hs:
hs[arr[i]]=1
else:
hs[arr[i]]+=1
print (len(hs),end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N and an integer K. Print the count of distinct numbers in all subarrays of size k in the array A[].The first line of input contains two integers N and K. The next line contains N space separated values of the array A[].
Constraints:-
1 <= N, K <= 100000
1 <= A[] <= 1000000000Print the space separated values denoting counts of distinct numbers in all subarrays of size k in the array A[].Sample Input :
7 4
1 2 1 3 4 2 3
Sample Output:
3 4 4 3
Explanation:-`
First window's elements is:- 1 2 1 3 which contains 3 different elements.
Second window's elements is:- 2 1 3 4 which contains 4 different elements.
Third window's elements is:- 1 3 4 2 which contains 4 different elements.
Fourth window's elements is:- 3 4 2 3 which contains 3 different elements., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unordered_map<long,int> m;
int n,k;
cin>>n>>k;
long a[n];
for(int i=0;i<n;i++){cin>>a[i];}
for(int i=0;i<k;i++){
m[a[i]]++;
}
cout<<m.size()<<" ";
int ans=m.size();
for(int i=k;i<n;i++){
m[a[i-k]]--;
if(m[a[i-k]]==0){ans--;}
m[a[i]]++;
if(m[a[i]]==1){ans++;}
cout<<ans<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers of size N, your task is to find the maximum parity index of this array.
<b>Parity Index is the maximum difference between two indices i and j (1 <= i < j <= N) of an array A such that A<sub>i</sub> < A<sub>j</sub>.</b>The first line contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 < = N < = 10<sup>5</sup>
1 < = Arr[i] < = 10<sup>5</sup>Print the maximum value of <b>j- i</b> under the given condition, if no pair satisfies the condition print -1.Sample Input 1:-
5
1 2 3 4 5
Sample Output 1:-
4
Sample Input 2:-
5
5 4 3 2 1
Sample Output 2:-
-1
<b>Explanation 1:</b>
The maximum difference of j<sub>th</sub> - i<sub>th</sub> index is 4:(4<sub>th</sub> - 0<sub>th</sub>), also arr[4] > arr[0]
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args)throws IOException {
Reader sc = new Reader();
int N = sc.nextInt();
int[] arr = new int[N];
for(int i=0;i<N;i++){
arr[i] = sc.nextInt();
}
int max=0;
if(arr[0]<arr[N-1])
System.out.print(N-1);
else{
for(int i=0;i<N-1;i++){
int j = N-1;
while(j>i){
if(arr[i]<arr[j]){
if(max<j-i){
max = j-i;
} break;
}
j--;
}
if(i==j)
break;
if(j==N-1)
break;
}
if(max==0)
System.out.print("-1");
else
System.out.print(max);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers of size N, your task is to find the maximum parity index of this array.
<b>Parity Index is the maximum difference between two indices i and j (1 <= i < j <= N) of an array A such that A<sub>i</sub> < A<sub>j</sub>.</b>The first line contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 < = N < = 10<sup>5</sup>
1 < = Arr[i] < = 10<sup>5</sup>Print the maximum value of <b>j- i</b> under the given condition, if no pair satisfies the condition print -1.Sample Input 1:-
5
1 2 3 4 5
Sample Output 1:-
4
Sample Input 2:-
5
5 4 3 2 1
Sample Output 2:-
-1
<b>Explanation 1:</b>
The maximum difference of j<sub>th</sub> - i<sub>th</sub> index is 4:(4<sub>th</sub> - 0<sub>th</sub>), also arr[4] > arr[0]
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
/* For a given array arr[],
returns the maximum j – i such that
arr[j] > arr[i] */
int maxIndexDiff(int arr[], int n)
{
int maxDiff;
int i, j;
int *LMin = new int[(sizeof(int) * n)];
int *RMax = new int[(sizeof(int) * n)];
/* Construct LMin[] such that
LMin[i] stores the minimum value
from (arr[0], arr[1], ... arr[i]) */
LMin[0] = arr[0];
for (i = 1; i < n; ++i)
LMin[i] = min(arr[i], LMin[i - 1]);
/* Construct RMax[] such that
RMax[j] stores the maximum value from
(arr[j], arr[j+1], ..arr[n-1]) */
RMax[n - 1] = arr[n - 1];
for (j = n - 2; j >= 0; --j)
RMax[j] = max(arr[j], RMax[j + 1]);
/* Traverse both arrays from left to right
to find optimum j - i. This process is similar to
merge() of MergeSort */
i = 0, j = 0, maxDiff = -1;
while (j < n && i < n)
{
if (LMin[i] < RMax[j])
{
maxDiff = max(maxDiff, j - i);
j = j + 1;
}
else
i = i + 1;
}
return maxDiff;
}
// Driver Code
signed main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int maxDiff = maxIndexDiff(a, n);
cout << maxDiff;
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 integers of size N, your task is to find the maximum parity index of this array.
<b>Parity Index is the maximum difference between two indices i and j (1 <= i < j <= N) of an array A such that A<sub>i</sub> < A<sub>j</sub>.</b>The first line contains a single integer N, next line contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 < = N < = 10<sup>5</sup>
1 < = Arr[i] < = 10<sup>5</sup>Print the maximum value of <b>j- i</b> under the given condition, if no pair satisfies the condition print -1.Sample Input 1:-
5
1 2 3 4 5
Sample Output 1:-
4
Sample Input 2:-
5
5 4 3 2 1
Sample Output 2:-
-1
<b>Explanation 1:</b>
The maximum difference of j<sub>th</sub> - i<sub>th</sub> index is 4:(4<sub>th</sub> - 0<sub>th</sub>), also arr[4] > arr[0]
, I have written this Solution Code: n=int(input())
arr=list(map(int,input().split()))
rightMax = [0] * n
rightMax[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
rightMax[i] = max(rightMax[i + 1], arr[i])
maxDist = -2**31
i = 0
j = 0
while (i < n and j < n):
if (rightMax[j] >= arr[i]):
maxDist = max(maxDist, j - i)
j += 1
else:
i += 1
if maxDist==0:
maxDist=-1
print(maxDist), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a function <code>calculateTotal</code>, which takes two parameter <code>num1</code> and <code>num2</code>, adds them and returns the tax on them by multiplying the sum with <code>tax</code> stored in the<code>this</code> object.
The function <code>solve</code> takes 3 arguments <code>taxObj</code>, <code>x</code> and <code>y</code>. <code>taxObj</code> is a object in the format, <code>{tax: 0.2}</code>, where <code>0.2</code> is the tax rate.
Your task is to complete this function <code>solve</code>, so that it executes the following tasks in the given order -
<ul>
<li>Bind the <code>calculateTotal</code> function to the <code>taxObj</code> object, and call the function using the in-built <code>call()</code> function, with <code>x</code> as the first parameter and <code>y</code> as the second parameter</li>
<li>Bind the <code>calculateTotal</code> function to the <code>taxObj</code> object, and call the function using the in-built <code>apply()</code> function, with <code>x</code> as the first parameter and <code>y</code> as the second parameter</li>
<li>Bind the <code>calculateTotal</code> function to the <code>taxObj</code> object, and store it in a new function <code>boundCalculateTotal</code>. Then return this new function <code>boundCalculateTotal</code></li>
</ul>The function <code>solve</code> takes 3 arguments <code>taxObj</code>, <code>x</code> and <code>y</code>. <code>taxObj</code> is a object in the format, <code>{tax: 0.2}</code>, where <code>0.2</code> is the tax rate. <code>x</code> and <code>y</code> are parameters which are to be used to call the <code>calculateTotal</code> function.
To test your solution, enter <code>num1</code>, <code>num2</code> and the <code>tax rate</code>, seperated by commas. The function will be called internally.
Example: 30,30,0.5The function <code>solve</code> will call the <code>calculateTotal</code> function, twice by binding it to the <code>taxObj</code> object, first with the inbuilt <code>call()</code> function and then with the inbuilt <code>apply()</code> function.
Then it should bind the <code>calculateTotal</code> function to the <code>taxObj</code> object, and store it in a new function <code>boundCalculateTotal</code>. Then the function <code>solve</code> function should return this new function <code>boundCalculateTotal</code>const taxObj = {
tax: 0.1
}
const boundCalculateTotal = solve(taxObj, 10, 20); // prints 3 3
boundCalculateTotal(10, 20); //prints 3, I have written this Solution Code:
function calculateTotal(num1, num2) {
console.log(this.tax * (num1 + num2));
}
function solve(taxObj, x, y){
calculateTotal.call(taxObj, x, y);
calculateTotal.apply(taxObj, [x, y]);
const boundCalculateTotal = calculateTotal.bind(taxObj);
return boundCalculateTotal;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. Over all pairs of (l, r) (1 <= l < r <= n), find the maximum value of max(A<sub>l</sub>, A<sub>l+1</sub>,... , A<sub>r</sub>) * min(A<sub>l</sub>, A<sub>l+1</sub>,... , A<sub>r</sub>).The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Over all pairs of (l, r) (1 <= l < r <= n), print the maximum value of max(A<sub>l</sub>, A<sub>l+1</sub>,. , A<sub>r</sub>) * min(A<sub>l</sub>, A<sub>l+1</sub>,. , A<sub>r</sub>).Sample Input:
4
2 3 2 1
Sample Output:
6
Explaination:
Let l = 1, r = 3
max = 3, min = 2
value = 3*2 = 6.
It can be verified that this is the maximum such value possible., 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());
long[] a= new long[n];
String[] line=br.readLine().split(" ");
for(int i=0;i<n;i++){
a[i]=Long.parseLong(line[i]);
}
int i=0,j=i+1;
long prod=a[i]*a[j];
while(j<n-1){
i++;
j++;
prod=Math.max(prod,(a[i]*a[j]));
}
System.out.println(prod);
}
}, 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. Over all pairs of (l, r) (1 <= l < r <= n), find the maximum value of max(A<sub>l</sub>, A<sub>l+1</sub>,... , A<sub>r</sub>) * min(A<sub>l</sub>, A<sub>l+1</sub>,... , A<sub>r</sub>).The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Over all pairs of (l, r) (1 <= l < r <= n), print the maximum value of max(A<sub>l</sub>, A<sub>l+1</sub>,. , A<sub>r</sub>) * min(A<sub>l</sub>, A<sub>l+1</sub>,. , A<sub>r</sub>).Sample Input:
4
2 3 2 1
Sample Output:
6
Explaination:
Let l = 1, r = 3
max = 3, min = 2
value = 3*2 = 6.
It can be verified that this is the maximum such value possible., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
int ans = 0;
for(int i = 1; i < n; i++){
ans = max(ans, a[i] * a[i - 1]);
}
cout << ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: def For_Loop(n):
string = ""
for i in range(1, n+1):
if i % 2 == 0:
string += "%s " % i
return string
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to print all the even integer from 1 to N.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that take the integer n as a parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here, given user function has already code in it which may or may not be correct, now you need to figure out these and correct them if it is required</i>Print all the even numbers from 1 to n. (print all the numbers in the same line, space-separated)Sample Input:-
5
Sample Output:-
2 4
Sample Input:-
6
Sample Output:-
2 4 6, I have written this Solution Code: public static void For_Loop(int n){
for(int i=2;i<=n;i+=2){
System.out.print(i+" ");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: def profit(C, S):
print(S - C), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Nobita wants to become rich so he came up with an idea, So, he buys some gadgets from the future at a price of C and sells them at a price of S to his friends. Now Nobita wants to know how much he gains by selling all gadget. As we all know Nobita is weak in maths help him to find the profit he getsYou don't have to worry about the input, you just have to complete the function <b>Profit()</b>
<b>Constraints:-</b>
1 <= C <= S <= 1000Print the profit Nobita gets from selling one gadget.Sample Input:-
3 5
Sample Output:-
2
Sample Input:-
9 16
Sample Output:-
7, I have written this Solution Code: static void Profit(int C, int S){
System.out.println(S-C);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: import math
n = int(input())
for i in range(n):
x = int(input())
count = 0
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0:
if (i%2 == 0):
count+=1
if ((x/i) %2 == 0):
count+=1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
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 t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
int count=0;
for(int i=1;i<=Math.sqrt(n);i++){
if(n%i == 0)
{
if(i%2==0) {
count++;
}
if(i*i != n && (n/i)%2==0) {
count++;
}
}
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, the task is to find the number of divisors of N which are divisible by 2.The input line contains T, denoting the number of testcases. First line of each testcase contains integer N
Constraints:
1 <= T <= 50
1 <= N <= 10^9For each testcase in new line, you need to print the number of divisors of N which are exactly divisble by 2Input:
2
9
8
Output
0
3, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n&1){cout<<0<<endl;continue;}
long x=sqrt(n);
int cnt=0;
for(long long i=1;i<=x;i++){
if(!(n%i)){
if(!(i%2)){cnt++;}
if(i*i!=n){
if(!((n/i)%2)){cnt++;}
}
}
}
cout<<cnt<<endl;}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of non- negative integers of size N. The task is to find the maximum possible xor between two numbers present in the array.First line of input contains integer N (length of array). Next line contains N space separated integers which are numbers of array.
Constraints
2 <= N <= 100000
1 <= Arr[i] <= 10^9Print the the maximum possible xor between two numbers present in the array.Sample Input
6
25 10 2 8 5 3
Sample Output
28
Explanation
5^25=28, I have written this Solution Code:
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// Function to return the
// maximum xor
int max_xor(int arr[], int n)
{
int maxx = 0, mask = 0;
set<int> se;
for (int i = 30; i >= 0; i--) {
// set the i'th bit in mask
// like 100000, 110000, 111000..
mask |= (1 << i);
for (int i = 0; i < n; ++i) {
// Just keep the prefix till
// i'th bit neglecting all
// the bit's after i'th bit
se.insert(arr[i] & mask);
}
int newMaxx = maxx | (1 << i);
for (int prefix : se) {
// find two pair in set
// such that a^b = newMaxx
// which is the highest
// possible bit can be obtained
if (se.count(newMaxx ^ prefix)) {
maxx = newMaxx;
break;
}
}
// clear the set for next
// iteration
se.clear();
}
return maxx;
}
// Driver Code
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout << max_xor(arr, n) << endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: John has N candies. He wants to crush all of them. He feels that it would be boring to crush the candies randomly, so he found a method to crush them. He divides these candies into a minimum number of groups such that no group contains more than 3 candies. He crushes one candy from each group. If there are G groups in a single step, then the cost incurred in crushing a single candy for that step is G dollars. After candy from each group is crushed, he takes all the remaining candies and repeats the process until he has no candies left. He hasn't started crushing yet, but he wants to know how much the total cost would be incurred. Can you help him?
You have to answer Q-independent queries.The first line of input contains a single integer, Q denoting the number of queries.
Next, Q lines contain a single integer N denoting the number of candies John has.
<b>Constraints</b>
1 <= Q <= 5 * 10^4
1 <= N <= 10^9Print Q lines containing total cost incurred for each query.Sample Input 1:
1
4
Sample Output 1:
6
<b>Explanation:</b>
Query 1: First step John divides the candies into two groups of 3 and 1 candy respectively. Crushing one-one candy from both groups would cost him 2x2 = 4 dollars. He is now left with 2 candies. He divides it into one group. He crushes one candy for 1 dollar. Now, he is left with 1 candy. He crushes the last candy for 1 dollar. So, the total cost incurred is 4+1+1 = 6 dollars., I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int cost(int n){
if(n == 0) return 0;
int g = (n-1)/3 + 1;
return g*g + cost(n-g);
}
signed main() {
IOS;
clock_t start = clock();
int q; cin >> q;
while(q--){
int n;
cin >> n;
cout << cost(n) << endl;
}
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: John has N candies. He wants to crush all of them. He feels that it would be boring to crush the candies randomly, so he found a method to crush them. He divides these candies into a minimum number of groups such that no group contains more than 3 candies. He crushes one candy from each group. If there are G groups in a single step, then the cost incurred in crushing a single candy for that step is G dollars. After candy from each group is crushed, he takes all the remaining candies and repeats the process until he has no candies left. He hasn't started crushing yet, but he wants to know how much the total cost would be incurred. Can you help him?
You have to answer Q-independent queries.The first line of input contains a single integer, Q denoting the number of queries.
Next, Q lines contain a single integer N denoting the number of candies John has.
<b>Constraints</b>
1 <= Q <= 5 * 10^4
1 <= N <= 10^9Print Q lines containing total cost incurred for each query.Sample Input 1:
1
4
Sample Output 1:
6
<b>Explanation:</b>
Query 1: First step John divides the candies into two groups of 3 and 1 candy respectively. Crushing one-one candy from both groups would cost him 2x2 = 4 dollars. He is now left with 2 candies. He divides it into one group. He crushes one candy for 1 dollar. Now, he is left with 1 candy. He crushes the last candy for 1 dollar. So, the total cost incurred is 4+1+1 = 6 dollars., 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 q = Long.parseLong(br.readLine());
while(q-->0)
{
long N = Long.parseLong(br.readLine());
System.out.println(candyCrush(N,0,0));
}
}
static long candyCrush(long N, long cost,long group)
{
if(N==0)
{
return cost;
}
if(N%3==0)
{
group = N/3;
cost = cost + (group*group);
return candyCrush(N-group,cost,0);
}
else
{
group = (N/3)+1;
cost = cost + (group*group);
return candyCrush(N-group,cost,0);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., 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;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
int ans=0;
int v=0;
for(int i=1;i<n;++i){
if(a[i]>a[i-1])
v=0;
else
++v;
ans=max(ans,v);
}
cout<<ans;
#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 an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., 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 num = Integer.parseInt(br.readLine());
String s= br.readLine();
int[] arr= new int[num];
String[] s1 = s.split(" ");
int ccount = 0, pcount = 0;
for(int i=0;i<(num);i++)
{
arr[i]=Integer.parseInt(s1[i]);
if(i+1 < num){
arr[i+1] = Integer.parseInt(s1[i+1]);}
if(((i+1)< num) && (arr[i]>=arr[i+1]) ){
ccount++;
}else{
if(ccount > pcount){
pcount = ccount;
}
ccount = 0;
}
}
System.out.print(pcount);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of heights of N buildings in a row. You can start from any building and jump to the <b>adjacent</b> right building till the height of the building to the right is less than or equal to the height of your current building. Find the maximum number of jumps you can make.The first line of input contains a single integer N.
The second line of input contains N integers, denoting the array height.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>
1 <= height[i] <= 10<sup>9</sup>Print the maximum number of jumps you can make.Sample Input:-
5
5 4 1 2 1
Sample Output:-
2
<b>Explanation:</b>
We start from building with height 5 then jump right to building with height 4 then again to building with height 1 making a total of 2 jumps., I have written this Solution Code: N = int(input())
arr = iter(map(int,input().strip().split()))
jumps = []
jump = 0
temp = next(arr)
for i in arr:
if i<=temp:
jump += 1
temp = i
continue
temp = i
jumps.append(jump)
jump = 0
jumps.append(jump)
print(max(jumps)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Note:- It is guaranteed that atleast one number is repeated.The first line of the input contains an integer N denoting the number of elements in the array, the next line contains N space separated integers.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9For each test case in new line print the Maximum distance between two occurrences of an elementSample Input
6
1 1 2 2 2 1
Sample Output
5
Explanation:-
The index of two occurrences are:- (1, 2), (1, 6), (2, 6), (3, 4), (3, 5), (4, 5)
it can be seen that the maximum distance is between (1, 6) i. e. 5
Sample Input
5
1 1 1 1 1
Sample Output
4, I have written this Solution Code: import numpy as np
from collections import defaultdict
d=defaultdict(list)
n=input()
a=np.array([input().strip().split()]).flatten()
for i in range(len(a)):
d[a[i]].append(i)
for i in d:
d[i].sort()
ans=0
for i in d:
ans=max(ans,d[i][-1]-d[i][0])
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Note:- It is guaranteed that atleast one number is repeated.The first line of the input contains an integer N denoting the number of elements in the array, the next line contains N space separated integers.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9For each test case in new line print the Maximum distance between two occurrences of an elementSample Input
6
1 1 2 2 2 1
Sample Output
5
Explanation:-
The index of two occurrences are:- (1, 2), (1, 6), (2, 6), (3, 4), (3, 5), (4, 5)
it can be seen that the maximum distance is between (1, 6) i. e. 5
Sample Input
5
1 1 1 1 1
Sample Output
4, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int arrSize = sc.nextInt();
long arr[] = new long[arrSize];
for(int i = 0; i < arrSize; i++)
arr[i] = sc.nextLong();
System.out.println(maxDist(arr, arrSize));
}
static int maxDist(long arr[], int arrSize)
{
int ans = 0;
HashMap<Long, Integer> hMap = new HashMap<>();
for(int i = 0; i < arrSize; i++)
{
if(hMap.containsKey(arr[i]) == true)
ans = Math.max(ans, i-hMap.get(arr[i]));
else
hMap.put(arr[i], i);
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array with repeated elements, the task is to find the maximum distance between two occurrences of an element.
Note:- It is guaranteed that atleast one number is repeated.The first line of the input contains an integer N denoting the number of elements in the array, the next line contains N space separated integers.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9For each test case in new line print the Maximum distance between two occurrences of an elementSample Input
6
1 1 2 2 2 1
Sample Output
5
Explanation:-
The index of two occurrences are:- (1, 2), (1, 6), (2, 6), (3, 4), (3, 5), (4, 5)
it can be seen that the maximum distance is between (1, 6) i. e. 5
Sample Input
5
1 1 1 1 1
Sample Output
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long a[n];
unordered_map<long,int> m;
int ans=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(m.find(a[i])!=m.end()){ans=max(ans,i-m[a[i]]);}
else{
m[a[i]]=i;
}
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function increaseArray(arr, n) {
// write code here
// do not console.log
// return "YES" or "NO"
let cnt = 2;
for (let i = 1; i < n; i++) {
while (cnt <= arr[i] && arr[i] % cnt != 0) {
cnt++;
}
if (cnt > arr[i]) { return "NO" }
cnt++;
}
return "YES"
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: #include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];}
int cnt=2;
for(int i=1;i<n;i++){
while(cnt<=a[i] && a[i]%cnt!=0){
cnt++;}
if(cnt>a[i]){cout<<"NO";return 0;}
cnt++;
}
cout<<"YES";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: n = int(input())
lst = list(map(int, input().split()))
count = 1
ls = []
for i in range(n):
if (lst[i]%count == 0):
lst[i] = count
ls.append(lst[i])
count += 1
change = 0
while( count > lst[i-1] and count <= lst[i]):
if (lst[i]%count == 0) :
lst[i] = count
change = 1
ls.append(lst[i])
count += 1
break
else:
count += 1
if len(ls) == n:
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
boolean flag=true;
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(str[i]);
arr[0]=1;
int j=2;
for(int i=1;i<n;i++){
if(arr[i]%j==0)
j=j+1;
else if((arr[i]%j)!=0 && (arr[i]/j)>=1){
int fight=1;
while(fight==1){
for(int x=j+1;x<=arr[i];x++){
if(arr[i]%x==0){
j=x+1;
fight=0;
break;
}
else
fight=1;
}
}
}
else
flag=false;
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: def two_way_sort(arr, arr_len):
l, r = 0, arr_len - 1
k = 0
while(l < r):
while(arr[l] % 2 != 0):
l += 1
k += 1
while(arr[r] % 2 == 0 and l < r):
r -= 1
if(l < r):
arr[l], arr[r] = arr[r], arr[l]
odd = arr[:k]
even = arr[k:]
odd.sort(reverse = True)
even.sort()
odd.extend(even)
return odd
n = int(input())
lst = input()
arr = lst.split()
for i in range(len(arr)):
arr[i] = int(arr[i])
result = two_way_sort(arr, n)
for i in result:
print(str(i), end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> odd,even;
int a;
for(int i=0;i<n;i++){
cin>>a;
if(a&1){odd.emplace_back(a);}
else{
even.emplace_back(a);
}
}
sort(odd.begin(),odd.end());
sort(even.begin(),even.end());
for(int i=odd.size()-1;i>=0;i--){
cout<<odd[i]<<" ";
}
for(int i=0;i<even.size();i++){
cout<<even[i]<<" ";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: function sortEvenOdd(arr, arrSize)
{
var even = [];
var odd = [];
for(let i = 0; i < arrSize; ++i)
{
if((arr[i]) % 2 === 0)
even.push(arr[i]);
else
odd.push(arr[i]);
}
even.sort((x, y) => (x - y));
odd.sort((x, y) => (x - y));
let res = [];
for(let i = odd.length - 1; i >= 0; i--)
res.push(odd[i]);
for(let i = 0; i < even.length; i++)
res.push(even[i]);
return res;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, 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));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
long[] arr = new long[n];
String[] raw = br.readLine().split("\\s+");
int eCount = 0;
for (int i = 0; i < n; i++) {
long tmp = Long.parseLong(raw[i]);
if (tmp % 2 == 0) arr[i] = tmp;
else arr[i] = -tmp;
}
Arrays.sort(arr);
int k = 0;
while (arr[k] % 2 != 0) {
arr[k] = -arr[k];
k++;
}
for (int i = 0; i < n; i++)
pw.print(arr[i] + " ");
pw.println();
pw.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculators are widely used devices nowadays. It makes calculations easier and faster. A simple calculator consists of the following operators:
1. '+': adding two numbers
2. '- ': subtraction
3. '*': multiplying numbers
4. '/': division
You will be given operator '+' or '- ' or '*' or '/' followed by two operands(integers), you need to perform a mathematical operation based on a given operator.
If '/' operator is used, print the integer value after divisionUser task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>calculator()</b> which contains operator(any one of given in the constraints), and two operands i.e. two integers.
<b>Constraints:</b>
operators - {+, - , *, /}
1 <= integers <= 10^4You need to return the result. If '/' operator is used, return the integer value after division.Sample Input
*
10 15
Sample Output
150
Explanation:
10*15 = 150
Sample Input:
/
15 4
Sample output:
3
Explanation:
15/4 = 3.75; floor(3.75) = 3, I have written this Solution Code: function calculate(op,num1,num2) {
// write code here
// return the output using return keyword
// do not use console.log here
switch (op) {
case '+':return num1 + num2
case '-' : return num1 - num2
case '*' : return num1 * num2
case '/' : return Math.floor(num1/num2)
default:
break;
}
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculators are widely used devices nowadays. It makes calculations easier and faster. A simple calculator consists of the following operators:
1. '+': adding two numbers
2. '- ': subtraction
3. '*': multiplying numbers
4. '/': division
You will be given operator '+' or '- ' or '*' or '/' followed by two operands(integers), you need to perform a mathematical operation based on a given operator.
If '/' operator is used, print the integer value after divisionUser task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>calculator()</b> which contains operator(any one of given in the constraints), and two operands i.e. two integers.
<b>Constraints:</b>
operators - {+, - , *, /}
1 <= integers <= 10^4You need to return the result. If '/' operator is used, return the integer value after division.Sample Input
*
10 15
Sample Output
150
Explanation:
10*15 = 150
Sample Input:
/
15 4
Sample output:
3
Explanation:
15/4 = 3.75; floor(3.75) = 3, I have written this Solution Code: def calculator(operator,a,b):
if(operator == '/'):
print(a//b)
if(operator == '*'):
print(a*b)
if(operator == '+'):
print(a+b)
if(operator == '-'):
print(a-b), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Calculators are widely used devices nowadays. It makes calculations easier and faster. A simple calculator consists of the following operators:
1. '+': adding two numbers
2. '- ': subtraction
3. '*': multiplying numbers
4. '/': division
You will be given operator '+' or '- ' or '*' or '/' followed by two operands(integers), you need to perform a mathematical operation based on a given operator.
If '/' operator is used, print the integer value after divisionUser task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>calculator()</b> which contains operator(any one of given in the constraints), and two operands i.e. two integers.
<b>Constraints:</b>
operators - {+, - , *, /}
1 <= integers <= 10^4You need to return the result. If '/' operator is used, return the integer value after division.Sample Input
*
10 15
Sample Output
150
Explanation:
10*15 = 150
Sample Input:
/
15 4
Sample output:
3
Explanation:
15/4 = 3.75; floor(3.75) = 3, I have written this Solution Code: static int calculator(char ch, int a, int b)
{
if(ch == '+')
return a+b;
else if(ch == '-')
return a-b;
else if(ch == '*')
return a*b;
else return a/b;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers, your task is to print the sum of elements present at even places and print the product of elements present at the odd places.
Note:- Consider the first element to start from 1.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 50
1 <= Arr[i] <= 5Print the sum of elements present at even places and print the product of elements present at the odd places separated by a space.Sample Input:-
5
1 2 3 4 5
Sample Output:-
6 15
Explanation:-
Sum = 2 + 4
Product = 1*3*5
Sample Input:-
2
4 6
Sample Output:-
6 4, I have written this Solution Code: function altSumProduct(arr, n) {
let sum = 0;
let prod = 1;
arr.forEach((el, idx) => {
if (idx % 2 !== 0) {
sum += el
} else {
prod *= el
}
})
console.log(`${sum} ${prod}`)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers, your task is to print the sum of elements present at even places and print the product of elements present at the odd places.
Note:- Consider the first element to start from 1.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 50
1 <= Arr[i] <= 5Print the sum of elements present at even places and print the product of elements present at the odd places separated by a space.Sample Input:-
5
1 2 3 4 5
Sample Output:-
6 15
Explanation:-
Sum = 2 + 4
Product = 1*3*5
Sample Input:-
2
4 6
Sample Output:-
6 4, 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 Arr[] = new int[n];
for(int i=0;i<n;i++){
Arr[i] = sc.nextInt();
}
int sum=0;
int product=1;
for(int i=0;i<n;i++){
if(i%2==0){
product*=Arr[i];
}
else{
sum+=Arr[i];
}
}
System.out.print(sum+" "+product);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N integers, your task is to print the sum of elements present at even places and print the product of elements present at the odd places.
Note:- Consider the first element to start from 1.The first line of input contains a single integer N, the next line of input contains N space- separated integers depicting the values of the array.
Constraints:-
1 <= N <= 50
1 <= Arr[i] <= 5Print the sum of elements present at even places and print the product of elements present at the odd places separated by a space.Sample Input:-
5
1 2 3 4 5
Sample Output:-
6 15
Explanation:-
Sum = 2 + 4
Product = 1*3*5
Sample Input:-
2
4 6
Sample Output:-
6 4, I have written this Solution Code: def EvenOddSum(a, n):
even = 0
odd = 1
for i in range(0,n):
if i % 2 == 0:
odd *= a[i]
else:
even += a[i]
print (even,odd)
n = input()
n=int(n)
arr = [int(i) for i in input().split()]
EvenOddSum(arr, n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: def two_way_sort(arr, arr_len):
l, r = 0, arr_len - 1
k = 0
while(l < r):
while(arr[l] % 2 != 0):
l += 1
k += 1
while(arr[r] % 2 == 0 and l < r):
r -= 1
if(l < r):
arr[l], arr[r] = arr[r], arr[l]
odd = arr[:k]
even = arr[k:]
odd.sort(reverse = True)
even.sort()
odd.extend(even)
return odd
n = int(input())
lst = input()
arr = lst.split()
for i in range(len(arr)):
arr[i] = int(arr[i])
result = two_way_sort(arr, n)
for i in result:
print(str(i), end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> odd,even;
int a;
for(int i=0;i<n;i++){
cin>>a;
if(a&1){odd.emplace_back(a);}
else{
even.emplace_back(a);
}
}
sort(odd.begin(),odd.end());
sort(even.begin(),even.end());
for(int i=odd.size()-1;i>=0;i--){
cout<<odd[i]<<" ";
}
for(int i=0;i<even.size();i++){
cout<<even[i]<<" ";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: function sortEvenOdd(arr, arrSize)
{
var even = [];
var odd = [];
for(let i = 0; i < arrSize; ++i)
{
if((arr[i]) % 2 === 0)
even.push(arr[i]);
else
odd.push(arr[i]);
}
even.sort((x, y) => (x - y));
odd.sort((x, y) => (x - y));
let res = [];
for(let i = odd.length - 1; i >= 0; i--)
res.push(odd[i]);
for(let i = 0; i < even.length; i++)
res.push(even[i]);
return res;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, 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));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
long[] arr = new long[n];
String[] raw = br.readLine().split("\\s+");
int eCount = 0;
for (int i = 0; i < n; i++) {
long tmp = Long.parseLong(raw[i]);
if (tmp % 2 == 0) arr[i] = tmp;
else arr[i] = -tmp;
}
Arrays.sort(arr);
int k = 0;
while (arr[k] % 2 != 0) {
arr[k] = -arr[k];
k++;
}
for (int i = 0; i < n; i++)
pw.print(arr[i] + " ");
pw.println();
pw.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a positive integer N. Print "Yes" if N is a multiple of 3, otherwise, print "No".The input consists of a single integer N.
<b> Constraints: </b>
1 ≤ N ≤ 1000If N is a multiple of 3, print "Yes". Otherwise, print "No" (without the quotation marks). Note that the <b>output is case-sensitive</b>.Sample Input 1:
6
Sample Output 1:
Yes
Sample Input 2:
10
Sample Output 2:
No
(6 is a multiple of 3 (3*2==6) while 10 is not a multiple of 3), I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
if (n % 3) cout << "No";
else cout << "Yes";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has an MCQ exam containing 100 questions this sunday but she isn't prepared. She came to know that in her exam
- >P number of Questions will have A as the correct option
- >Q number of Questions will have B as the correct option
- >R number of Questions will have C as the correct option
- >S number of Questions will have D as the correct option
Sara doesn't know the order of the questions. If Sara filled the MCQs optimally (same option for each question) using the above information what will be the maximum marks she can get.The input contains 4 integers P, Q, R, and S.
Constraints:-
0 <= P, Q, R, S <= 100
Note:- P + Q + R + S will always be 100Print the maximum marks Sara can get.Sample Input:-
8 10 20 62
Sample Output:-
62, 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().toString();
String str[] = s.split(" ");
int P = Integer.parseInt(str[0]);
int Q = Integer.parseInt(str[1]);
int R = Integer.parseInt(str[2]);
int S = Integer.parseInt(str[3]);
if(P>=Q && P>=R && P>=S)
System.out.println(P);
else if(Q>=P && Q>=R && Q>=S)
System.out.println(Q);
else if(R>=P && R>=Q && R>=S)
System.out.println(R);
else
System.out.println(S);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has an MCQ exam containing 100 questions this sunday but she isn't prepared. She came to know that in her exam
- >P number of Questions will have A as the correct option
- >Q number of Questions will have B as the correct option
- >R number of Questions will have C as the correct option
- >S number of Questions will have D as the correct option
Sara doesn't know the order of the questions. If Sara filled the MCQs optimally (same option for each question) using the above information what will be the maximum marks she can get.The input contains 4 integers P, Q, R, and S.
Constraints:-
0 <= P, Q, R, S <= 100
Note:- P + Q + R + S will always be 100Print the maximum marks Sara can get.Sample Input:-
8 10 20 62
Sample Output:-
62, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<max(a,max(b,max(c,d)));
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara has an MCQ exam containing 100 questions this sunday but she isn't prepared. She came to know that in her exam
- >P number of Questions will have A as the correct option
- >Q number of Questions will have B as the correct option
- >R number of Questions will have C as the correct option
- >S number of Questions will have D as the correct option
Sara doesn't know the order of the questions. If Sara filled the MCQs optimally (same option for each question) using the above information what will be the maximum marks she can get.The input contains 4 integers P, Q, R, and S.
Constraints:-
0 <= P, Q, R, S <= 100
Note:- P + Q + R + S will always be 100Print the maximum marks Sara can get.Sample Input:-
8 10 20 62
Sample Output:-
62, I have written this Solution Code: print(max(list(map(int, input().strip().split(" "))))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a positive integer N. Print "Yes" if N is a multiple of 3, otherwise, print "No".The input consists of a single integer N.
<b> Constraints: </b>
1 ≤ N ≤ 1000If N is a multiple of 3, print "Yes". Otherwise, print "No" (without the quotation marks). Note that the <b>output is case-sensitive</b>.Sample Input 1:
6
Sample Output 1:
Yes
Sample Input 2:
10
Sample Output 2:
No
(6 is a multiple of 3 (3*2==6) while 10 is not a multiple of 3), I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
if (n % 3) cout << "No";
else cout << "Yes";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function increaseArray(arr, n) {
// write code here
// do not console.log
// return "YES" or "NO"
let cnt = 2;
for (let i = 1; i < n; i++) {
while (cnt <= arr[i] && arr[i] % cnt != 0) {
cnt++;
}
if (cnt > arr[i]) { return "NO" }
cnt++;
}
return "YES"
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: #include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];}
int cnt=2;
for(int i=1;i<n;i++){
while(cnt<=a[i] && a[i]%cnt!=0){
cnt++;}
if(cnt>a[i]){cout<<"NO";return 0;}
cnt++;
}
cout<<"YES";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: n = int(input())
lst = list(map(int, input().split()))
count = 1
ls = []
for i in range(n):
if (lst[i]%count == 0):
lst[i] = count
ls.append(lst[i])
count += 1
change = 0
while( count > lst[i-1] and count <= lst[i]):
if (lst[i]%count == 0) :
lst[i] = count
change = 1
ls.append(lst[i])
count += 1
break
else:
count += 1
if len(ls) == n:
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhijit has an Array <b>Arr</b> which may or may not be in strictly increasing order. He wants to make this array increasing but does not wish to change the position of any element so he came with an idea that he will replace an element with any of its divisors i.e <b> he changes an element X to M if X%M=0.</b>
Your task is to tell whether the given array can be transformed to <b>strictly increasing</b> by performing the operation given above.First line of input contains the size of the array N. Next line of input contains N space- separated integers depicting the values of the array Arr.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= 100000Print "YES" if the array can be transformed in the strictly increasing order else print "NO".Sample Input:-
5
1 2 16 16 16
Sample Output:-
YES
Sample Input:-
4
1 3 8 4
Sample Output:-
NO, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
boolean flag=true;
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(str[i]);
arr[0]=1;
int j=2;
for(int i=1;i<n;i++){
if(arr[i]%j==0)
j=j+1;
else if((arr[i]%j)!=0 && (arr[i]/j)>=1){
int fight=1;
while(fight==1){
for(int x=j+1;x<=arr[i];x++){
if(arr[i]%x==0){
j=x+1;
fight=0;
break;
}
else
fight=1;
}
}
}
else
flag=false;
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, your task is to check whether the given integer is even or odd.
If the integer is even print 0 else if it is odd print 1.The input contains a single integer N.
Constraint:
1 ≤ N ≤10000If the integer is even print 0 else if it is odd print 1.Sample Input:-
15
Sample Output:-
1
Sample Input:-
16
Sample Output:-
0, I have written this Solution Code: import java.util.Scanner;
class Main {
public static void main (String[] args)
{
//Capture the user's input
Scanner scanner = new Scanner(System.in);
//Storing the captured value in a variable
int side = scanner.nextInt();
int area = EvenOrOdd(side);
System.out.println(area);
}
static int EvenOrOdd(int N){
return N%2;
}}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer, your task is to check whether the given integer is even or odd.
If the integer is even print 0 else if it is odd print 1.The input contains a single integer N.
Constraint:
1 ≤ N ≤10000If the integer is even print 0 else if it is odd print 1.Sample Input:-
15
Sample Output:-
1
Sample Input:-
16
Sample Output:-
0, I have written this Solution Code: inp = int(input())
if inp % 2 == 0 :
print(0)
else:
print(1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: def check(N):
if(N<0):
return 1
a=0
b=0
while(N):
if(N%10==7):
a=1
if(N%10==9):
b=1
N=N//10
if( a==1 and b==1):
return 0
return 1
def MagicNumber(N):
i=0
while(check(N-i)==1 and check(N+i)==1):
i=i+1
if(check(N-i)==0):
return N-i
return N+i
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: int check(int n){
int s=0;
int p=0;
while(n){
if(n%10==7){s=1;}
if(n%10==9){p=1;}
n/=10;
}
if(s && p){return 0;}
return 1;
}
int MagicNumber(int n){
int i=0;
while(check(n-i) && check(n+i)){
i++;
}
if(check(n-i)==0){return n-i;}
else{
return n+i;}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: int check(int n){
int s=0;
int p=0;
while(n){
if(n%10==7){s=1;}
if(n%10==9){p=1;}
n/=10;
}
if(s && p){return 0;}
return 1;
}
int MagicNumber(int n){
int i=0;
while(check(n-i) && check(n+i)){
i++;
}
if(check(n-i)==0){return n-i;}
else{
return n+i;}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: static boolean check(int n){
boolean s=false;
boolean p=false;
while(n>0){
if(n%10==7){s=true;}
if(n%10==9){p=true;}
n/=10;
}
if(s && p){return false;}
return true;
}
static int MagicNumber(int n){
int i=0;
while(check(n-i)==true && check(n+i)==true){
i++;
}
if(check(n-i)==false){return n-i;}
else{
return n+i;}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Hawkeye has been assigned to eliminate n monsters, the i<sup>th</sup> of which is of type a<sub>i</sub>. In one move, he can do either of the following:
i. Shoot down atmost any k monsters. In other words, he can shoot down any set of monsters such that the size of that set is smaller than or equal to k.
ii. Shoot down all monsters of any one type.
Find the minimum number of moves it will take him to shoot down all the monsters.The first line contains one integer t — the number of test cases. Each test case consists of two lines.
The first line contains two space-separated integers n and k.
The second line contains n space-separated integers a<sub>1</sub>, a<sub>2</sub> . . . a<sub>n</sub>.
<b>Constraints:</b>
1 ≤ t ≤ 10<sup>5</sup>
1 ≤ k ≤ n ≤ 10<sup>5</sup>
1 ≤ a<sub>i</sub> ≤ n
The sum of n over all test cases does not exceed 10<sup>5</sup>. For each test case, print a single value - the minimum number of moves required.Sample input
3
7 2
1 2 3 4 4 4 4
5 3
1 2 3 1 2
5 3
5 5 5 5 5
Sample output
3
2
1
Explanation:
For the first test case, the minimum moves required is 3. One way to do so is that Hawkeye can first shoot monsters 1 and 2 in one move using the second operation.
Then he can shoot down all monsters of type 4 using the first operation, followed by shooting monster 3 by either method., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int t;
cin>>t;
while(t--)
{
int n, k;
cin>>n>>k;
vector<int> a(n);
for(auto &i:a)
cin>>i;
map<int, int> type;
int sm = 0;
int cnt = 0;
for(auto i:a)
type[i]++;
for(auto i:type)
if(i.second >= k)
cnt++;
else
sm+=i.second;
cnt += (sm + k-1)/k;
cout<<cnt<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#endif
solve();
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Hawkeye has been assigned to eliminate n monsters, the i<sup>th</sup> of which is of type a<sub>i</sub>. In one move, he can do either of the following:
i. Shoot down atmost any k monsters. In other words, he can shoot down any set of monsters such that the size of that set is smaller than or equal to k.
ii. Shoot down all monsters of any one type.
Find the minimum number of moves it will take him to shoot down all the monsters.The first line contains one integer t — the number of test cases. Each test case consists of two lines.
The first line contains two space-separated integers n and k.
The second line contains n space-separated integers a<sub>1</sub>, a<sub>2</sub> . . . a<sub>n</sub>.
<b>Constraints:</b>
1 ≤ t ≤ 10<sup>5</sup>
1 ≤ k ≤ n ≤ 10<sup>5</sup>
1 ≤ a<sub>i</sub> ≤ n
The sum of n over all test cases does not exceed 10<sup>5</sup>. For each test case, print a single value - the minimum number of moves required.Sample input
3
7 2
1 2 3 4 4 4 4
5 3
1 2 3 1 2
5 3
5 5 5 5 5
Sample output
3
2
1
Explanation:
For the first test case, the minimum moves required is 3. One way to do so is that Hawkeye can first shoot monsters 1 and 2 in one move using the second operation.
Then he can shoot down all monsters of type 4 using the first operation, followed by shooting monster 3 by either method., I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(t);
int lim = 3e5;
assert(1 <= t && t <= 3e5);
int nsum = 0;
while (t--)
{
readb(n, k);
readarr(a, n);
assert(1 <= n && n <= lim);
assert(1 <= k && k <= n);
FOR (i, 1, n) assert(1 <= a[i] && a[i] <= n);
int freq[n + 1] = {};
FOR (i, 1, n) freq[a[i]]++;
int cnt = 0, sum = 0;
FOR (i, 1, n)
{
if (freq[i] < k) sum += freq[i];
else cnt++;
}
int ans = cnt + (sum + k - 1)/k;
print(ans);
nsum += n;
}
assert(nsum <= lim);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Hawkeye has been assigned to eliminate n monsters, the i<sup>th</sup> of which is of type a<sub>i</sub>. In one move, he can do either of the following:
i. Shoot down atmost any k monsters. In other words, he can shoot down any set of monsters such that the size of that set is smaller than or equal to k.
ii. Shoot down all monsters of any one type.
Find the minimum number of moves it will take him to shoot down all the monsters.The first line contains one integer t — the number of test cases. Each test case consists of two lines.
The first line contains two space-separated integers n and k.
The second line contains n space-separated integers a<sub>1</sub>, a<sub>2</sub> . . . a<sub>n</sub>.
<b>Constraints:</b>
1 ≤ t ≤ 10<sup>5</sup>
1 ≤ k ≤ n ≤ 10<sup>5</sup>
1 ≤ a<sub>i</sub> ≤ n
The sum of n over all test cases does not exceed 10<sup>5</sup>. For each test case, print a single value - the minimum number of moves required.Sample input
3
7 2
1 2 3 4 4 4 4
5 3
1 2 3 1 2
5 3
5 5 5 5 5
Sample output
3
2
1
Explanation:
For the first test case, the minimum moves required is 3. One way to do so is that Hawkeye can first shoot monsters 1 and 2 in one move using the second operation.
Then he can shoot down all monsters of type 4 using the first operation, followed by shooting monster 3 by either method., I have written this Solution Code: import math
from collections import defaultdict
n = int(input())
def utility(n,k,arr):
hm = defaultdict(int)
for i in range(n):
hm[arr[i]]+=1
arr1 = []
for key in hm.keys():
arr1.append([key,hm[key]])
arr1 = sorted(arr1,key = lambda x : x[1],reverse = True)
count = 0
s = 0
for ele in arr1:
if(ele[1] > k ):
count+=1
else:
s+=ele[1]
ans = count + math.ceil(s/k)
return(ans )
for i in range(n):
arr = [int(x) for x in input().split()]
n,k = arr[0],arr[1]
arr = [int(x) for x in input().split()]
print(utility(n,k,arr)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Hawkeye has been assigned to eliminate n monsters, the i<sup>th</sup> of which is of type a<sub>i</sub>. In one move, he can do either of the following:
i. Shoot down atmost any k monsters. In other words, he can shoot down any set of monsters such that the size of that set is smaller than or equal to k.
ii. Shoot down all monsters of any one type.
Find the minimum number of moves it will take him to shoot down all the monsters.The first line contains one integer t — the number of test cases. Each test case consists of two lines.
The first line contains two space-separated integers n and k.
The second line contains n space-separated integers a<sub>1</sub>, a<sub>2</sub> . . . a<sub>n</sub>.
<b>Constraints:</b>
1 ≤ t ≤ 10<sup>5</sup>
1 ≤ k ≤ n ≤ 10<sup>5</sup>
1 ≤ a<sub>i</sub> ≤ n
The sum of n over all test cases does not exceed 10<sup>5</sup>. For each test case, print a single value - the minimum number of moves required.Sample input
3
7 2
1 2 3 4 4 4 4
5 3
1 2 3 1 2
5 3
5 5 5 5 5
Sample output
3
2
1
Explanation:
For the first test case, the minimum moves required is 3. One way to do so is that Hawkeye can first shoot monsters 1 and 2 in one move using the second operation.
Then he can shoot down all monsters of type 4 using the first operation, followed by shooting monster 3 by either method., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
Cf cf = new Cf();
cf.solve();
}
static class Cf {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int mod = (int)1e9+7;
public void solve() {
int t = in.readInt();
while(t--!=0) {
int n = in.readInt();
int k = in.readInt();
int max = 0;
int arr1[] = new int[n];
for(int i=0;i<n;i++) {
int x = in.readInt();
arr1[i]=x;
if(x>max) {
max = x;
}
}
int arr[] = new int[max+2];
for(int i=0;i<n;i++) {
int x = arr1[i];
arr[x]++;
}
int killedwithsame = 0;
int tobekilledwithk = 0;
for(int i=0;i<max+1;i++) {
if(arr[i]>=k) {
killedwithsame++;
}else {
tobekilledwithk+=arr[i];
}
}
int ans = killedwithsame;
if(tobekilledwithk%k==0) {
ans+=(tobekilledwithk/k);
}else {
ans+=(tobekilledwithk/k);
ans+=1;
}
out.printLine(ans);
}
}
public long findPower(long x,long n) {
long ans = 1;
long nn = n;
while(nn>0) {
if(nn%2==1) {
ans = (ans*x) % mod;
nn-=1;
}else {
x = (x*x)%mod;
nn/=2;
}
}
return ans%mod;
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double readDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
private static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: From wiki-
The Tower of Hanoi is a mathematical puzzle where we have 3 rods and N disks. The puzzle starts with all the disks in ascending order of size on the first row. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
-----x--x--x------
Let the rods have names A, B and C. Given N number of disks, numbered 1 to N from top to bottom, display all the moves required to move the disks from rod A to C in minimum number of steps.The only line of input contains an integer N denoting the number of disks
Constraints:
1 ≤ N ≤ 16Print sequence of moving disks, where each move is shown in the following format-
{DiskNumber}:{FromRod}->{ToRod}
Each move in the sequence is separated by a new lineInput
2
Output
1:A->B
2:A->C
1:B->C, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void print(int dn, int from, int to){
cout << dn << ":" << (char)('A'+from-1) << "->" << (char)('A'+to-1) << endl;
}
void rec(int n, int src, int des, int aux){
if(n == 0)
return;
rec(n-1, src, aux, des);
print(n, src, des);
rec(n-1, aux, des, src);
}
signed main() {
IOS;
int n; cin >> n;
rec(n, 1, 3, 2);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: From wiki-
The Tower of Hanoi is a mathematical puzzle where we have 3 rods and N disks. The puzzle starts with all the disks in ascending order of size on the first row. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
-----x--x--x------
Let the rods have names A, B and C. Given N number of disks, numbered 1 to N from top to bottom, display all the moves required to move the disks from rod A to C in minimum number of steps.The only line of input contains an integer N denoting the number of disks
Constraints:
1 ≤ N ≤ 16Print sequence of moving disks, where each move is shown in the following format-
{DiskNumber}:{FromRod}->{ToRod}
Each move in the sequence is separated by a new lineInput
2
Output
1:A->B
2:A->C
1:B->C, I have written this Solution Code: # Recursive Python function to solve the tower of hanoi
def TowerOfHanoi(n , source, destination, auxiliary):
if n==1:
#print ("1:",source,"->",destination)
print("1:%s->%s"%(source,destination))
return
TowerOfHanoi(n-1, source, auxiliary, destination)
#print (n,"%d:",source,"->",destination)
print("%d:%s->%s"%(n,source,destination))
TowerOfHanoi(n-1, auxiliary, destination, source)
# Driver code
n = int(input())
TowerOfHanoi(n,'A','C','B'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: From wiki-
The Tower of Hanoi is a mathematical puzzle where we have 3 rods and N disks. The puzzle starts with all the disks in ascending order of size on the first row. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
-----x--x--x------
Let the rods have names A, B and C. Given N number of disks, numbered 1 to N from top to bottom, display all the moves required to move the disks from rod A to C in minimum number of steps.The only line of input contains an integer N denoting the number of disks
Constraints:
1 ≤ N ≤ 16Print sequence of moving disks, where each move is shown in the following format-
{DiskNumber}:{FromRod}->{ToRod}
Each move in the sequence is separated by a new lineInput
2
Output
1:A->B
2:A->C
1:B->C, I have written this Solution Code: // n is the number of disks
function towerOfHanoiSequence(n) {
// write code here
// console.log the seqence steps
function towerOfHanoi(n, from_rod, to_rod, aux_rod) {
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
console.log(`${n}:${from_rod}->${to_rod}`)
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
towerOfHanoi(n,'A','C','B')
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: From wiki-
The Tower of Hanoi is a mathematical puzzle where we have 3 rods and N disks. The puzzle starts with all the disks in ascending order of size on the first row. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
-----x--x--x------
Let the rods have names A, B and C. Given N number of disks, numbered 1 to N from top to bottom, display all the moves required to move the disks from rod A to C in minimum number of steps.The only line of input contains an integer N denoting the number of disks
Constraints:
1 ≤ N ≤ 16Print sequence of moving disks, where each move is shown in the following format-
{DiskNumber}:{FromRod}->{ToRod}
Each move in the sequence is separated by a new lineInput
2
Output
1:A->B
2:A->C
1:B->C, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
System.out.println("1:" + from_rod + "->" + to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
System.out.println(n + ":" + from_rod + "->" + to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
towerOfHanoi(n, 'A', 'C', 'B');
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<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>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:-</b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code:
def distributingMoney(x,y,z,k) :
# Final result of summation of divisors
result = x+y+z+k;
if result%3!=0:
return 0
result =result/3
if result<x or result<y or result<z:
return 0
return 1;
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<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>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:-</b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: int distributingMoney(long x, long y, long z,long k){
long long sum=x+y+z+k;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<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>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:-</b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: public static int distributingMoney(long x, long y, long z, long K){
long sum=0;
sum+=x+y+z+K;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<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>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:-</b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: int distributingMoney(long x, long y, long z,long k){
long int sum=x+y+z+k;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alexa is a very kind girl. She has 'x' red gems and 'y' blue gems. She wants to distribute them among her two friends (Alice and Julia).
But she will only distribute them if she can give equal number of each colour of gems to Alice and Julia. Further, she must distribute all available gems in this way. So, can you help her make a decision?First line contains two space- separated integers 'x' and 'y' (0 <= x,y <= 100) i. e. number of red and blue gems.In the first line, output "YES" is she can distribute gems else "NO" (without double quotes).Sample Input:
2 3
Sample Output:
NO
Explanation:
Alexa has 3 blue gems, so she can not distribute blue gems., I have written this Solution Code: import java.io.*;import java.util.*;
class Main{
static InputReader fr = new InputReader(System.in);
public static void main(String[] args) throws IOException {
PrintWriter w = new PrintWriter(System.out);
int tt=1;
while(tt-->0) {
int x=fr.ni();
int y=fr.ni();
if(x%2==0 && y%2==0) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
w.close();
}
static void opil(List<Integer> al) {StringBuilder sb= new StringBuilder();for(int x:al)sb.append(x+" ");System.out.println(sb.toString());}
static void opll(List<Long> al) {StringBuilder sb= new StringBuilder();for(long x:al)sb.append(x+" ");System.out.println(sb.toString());}
static int[] iarr(int n) {int a[]=new int[n];for(int i=0;i<n;i++)a[i]=fr.ni();return a;}
static long[] larr(int n) {long a[]=new long[n];for(int i=0;i<n;i++)a[i]=fr.nl();return a;}
static void op(int a[]) {StringBuilder sb= new StringBuilder();for(int x:a)sb.append(x+" ");System.out.println(sb.toString());}
static void op(long a[]) {StringBuilder sb= new StringBuilder();for(long x:a)sb.append(x+" ");System.out.println(sb.toString());}
static void sort(int[] a) {ArrayList<Integer>l=new ArrayList<>();for(int i:a) l.add(i);Collections.sort(l);for (int i=0; i<a.length; i++) a[i]=l.get(i);}
static void sort(long[] a) {ArrayList<Long>l=new ArrayList<>();for(long i:a) l.add(i);Collections.sort(l);for (int i=0; i<a.length; i++) a[i]=l.get(i);}
static int gcd(int a,int b) {if(a<b) return gcd(b,a);if(b==0) return a;return gcd(b,a%b);}
static String rev(String s) {StringBuilder sb= new StringBuilder();sb.append(s);return sb.reverse().toString();}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alexa is a very kind girl. She has 'x' red gems and 'y' blue gems. She wants to distribute them among her two friends (Alice and Julia).
But she will only distribute them if she can give equal number of each colour of gems to Alice and Julia. Further, she must distribute all available gems in this way. So, can you help her make a decision?First line contains two space- separated integers 'x' and 'y' (0 <= x,y <= 100) i. e. number of red and blue gems.In the first line, output "YES" is she can distribute gems else "NO" (without double quotes).Sample Input:
2 3
Sample Output:
NO
Explanation:
Alexa has 3 blue gems, so she can not distribute blue gems., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef unsigned long long int ull;
const long double PI = acos(-1);
const ll mod=1e9+7;
const ll mod1=998244353;
const int inf = 1e9;
const ll INF=1e18;
void precompute(){
}
void TEST_CASE(){
int x1,x2;
cin >> x1 >> x2;
if((x1&1) || (x2&1)){
cout << "NO\n";
}else{
cout << "YES\n";
}
}
signed main(){
fast;
//freopen ("INPUT.txt","r",stdin);
//freopen ("OUTPUT.txt","w",stdout);
int test=1,TEST=1;
precompute();
// cin >> test;
while(test--){
TEST_CASE();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alexa is a very kind girl. She has 'x' red gems and 'y' blue gems. She wants to distribute them among her two friends (Alice and Julia).
But she will only distribute them if she can give equal number of each colour of gems to Alice and Julia. Further, she must distribute all available gems in this way. So, can you help her make a decision?First line contains two space- separated integers 'x' and 'y' (0 <= x,y <= 100) i. e. number of red and blue gems.In the first line, output "YES" is she can distribute gems else "NO" (without double quotes).Sample Input:
2 3
Sample Output:
NO
Explanation:
Alexa has 3 blue gems, so she can not distribute blue gems., I have written this Solution Code: x,y=map(int,input().split(" "))
if x%2==0 and y%2==0:
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: def two_way_sort(arr, arr_len):
l, r = 0, arr_len - 1
k = 0
while(l < r):
while(arr[l] % 2 != 0):
l += 1
k += 1
while(arr[r] % 2 == 0 and l < r):
r -= 1
if(l < r):
arr[l], arr[r] = arr[r], arr[l]
odd = arr[:k]
even = arr[k:]
odd.sort(reverse = True)
even.sort()
odd.extend(even)
return odd
n = int(input())
lst = input()
arr = lst.split()
for i in range(len(arr)):
arr[i] = int(arr[i])
result = two_way_sort(arr, n)
for i in result:
print(str(i), end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> odd,even;
int a;
for(int i=0;i<n;i++){
cin>>a;
if(a&1){odd.emplace_back(a);}
else{
even.emplace_back(a);
}
}
sort(odd.begin(),odd.end());
sort(even.begin(),even.end());
for(int i=odd.size()-1;i>=0;i--){
cout<<odd[i]<<" ";
}
for(int i=0;i<even.size();i++){
cout<<even[i]<<" ";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, I have written this Solution Code: function sortEvenOdd(arr, arrSize)
{
var even = [];
var odd = [];
for(let i = 0; i < arrSize; ++i)
{
if((arr[i]) % 2 === 0)
even.push(arr[i]);
else
odd.push(arr[i]);
}
even.sort((x, y) => (x - y));
odd.sort((x, y) => (x - y));
let res = [];
for(let i = odd.length - 1; i >= 0; i--)
res.push(odd[i]);
for(let i = 0; i < even.length; i++)
res.push(even[i]);
return res;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.First line of each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.
<b>Constraints:</b>
1 <b>≤</b> N <b>≤</b> 10<sup>5</sup>
0 <b>≤</b> A[i] <b>≤</b> 10<sup>6</sup>Print the space separated values of the modified array.Sample Input
7
1 2 3 5 4 7 10
Sample Output
7 5 3 1 2 4 10
Sample Input
7
0 4 5 3 7 2 1
Sample Output
7 5 3 1 0 2 4, 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));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
long[] arr = new long[n];
String[] raw = br.readLine().split("\\s+");
int eCount = 0;
for (int i = 0; i < n; i++) {
long tmp = Long.parseLong(raw[i]);
if (tmp % 2 == 0) arr[i] = tmp;
else arr[i] = -tmp;
}
Arrays.sort(arr);
int k = 0;
while (arr[k] % 2 != 0) {
arr[k] = -arr[k];
k++;
}
for (int i = 0; i < n; i++)
pw.print(arr[i] + " ");
pw.println();
pw.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below equation for the given number.
Equation: -
N
∑ {(X + 1)<sup>2</sup> - (3X + 1) + X}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 10^5You need to return the sum of the equationSample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
5
Explanation:-
For test 2:- (1+1)^2 - (3*1+1) + (1) + (2+1)^2 - (3*2+1) +2
= 4 - 4 +1 + 9 -7 +2
= 5, I have written this Solution Code: def equationSum(N) :
re=N*(N+1)
re=re*(2*N+1)
re=re//6
return re, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below equation for the given number.
Equation: -
N
∑ {(X + 1)<sup>2</sup> - (3X + 1) + X}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 10^5You need to return the sum of the equationSample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
5
Explanation:-
For test 2:- (1+1)^2 - (3*1+1) + (1) + (2+1)^2 - (3*2+1) +2
= 4 - 4 +1 + 9 -7 +2
= 5, I have written this Solution Code: long long equationSum(long long N){
long long ans = (N*(N+1)*(2*N+1))/6;
return ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below equation for the given number.
Equation: -
N
∑ {(X + 1)<sup>2</sup> - (3X + 1) + X}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 10^5You need to return the sum of the equationSample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
5
Explanation:-
For test 2:- (1+1)^2 - (3*1+1) + (1) + (2+1)^2 - (3*2+1) +2
= 4 - 4 +1 + 9 -7 +2
= 5, I have written this Solution Code: long long int equationSum(long long N){
long long ans = (N*(N+1)*(2*N+1))/6;
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below equation for the given number.
Equation: -
N
∑ {(X + 1)<sup>2</sup> - (3X + 1) + X}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 10^5You need to return the sum of the equationSample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
5
Explanation:-
For test 2:- (1+1)^2 - (3*1+1) + (1) + (2+1)^2 - (3*2+1) +2
= 4 - 4 +1 + 9 -7 +2
= 5, I have written this Solution Code: static long equationSum(long N)
{
return (N*(N+1)*(2*N + 1))/6;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: def check(N):
if(N<0):
return 1
a=0
b=0
while(N):
if(N%10==7):
a=1
if(N%10==9):
b=1
N=N//10
if( a==1 and b==1):
return 0
return 1
def MagicNumber(N):
i=0
while(check(N-i)==1 and check(N+i)==1):
i=i+1
if(check(N-i)==0):
return N-i
return N+i
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: int check(int n){
int s=0;
int p=0;
while(n){
if(n%10==7){s=1;}
if(n%10==9){p=1;}
n/=10;
}
if(s && p){return 0;}
return 1;
}
int MagicNumber(int n){
int i=0;
while(check(n-i) && check(n+i)){
i++;
}
if(check(n-i)==0){return n-i;}
else{
return n+i;}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: int check(int n){
int s=0;
int p=0;
while(n){
if(n%10==7){s=1;}
if(n%10==9){p=1;}
n/=10;
}
if(s && p){return 0;}
return 1;
}
int MagicNumber(int n){
int i=0;
while(check(n-i) && check(n+i)){
i++;
}
if(check(n-i)==0){return n-i;}
else{
return n+i;}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A magic number is a natural number that contains both the digits 7 and 9 in it. For eg:- 79, 879, 53729 etc.
Given a number N, your task is to find the closest Magic number from the given number N.
Note:- If more than one answer exists return the minimum one<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>MagicNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return a magic number closest to the given number N.Sample Input:-
8
Sample Output:-
79
Sample Input:-
900
Sample Output:-
897, I have written this Solution Code: static boolean check(int n){
boolean s=false;
boolean p=false;
while(n>0){
if(n%10==7){s=true;}
if(n%10==9){p=true;}
n/=10;
}
if(s && p){return false;}
return true;
}
static int MagicNumber(int n){
int i=0;
while(check(n-i)==true && check(n+i)==true){
i++;
}
if(check(n-i)==false){return n-i;}
else{
return n+i;}
}, 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 Nth number in the fibonacci series. Consider 0 and 1 to be the seed values.
In the fibonacci series, each number (Fibonacci number) is the sum of the two preceding numbers. The series with 0 and 1 as seed values will go like -
0, 1, 1, 2, 3, 5 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 function <b>Fibonacci()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 30Return the Nth Fibonacci number.Sample Input
2
3
5
Sample Output
2
5
Explaination:
Test case 1-> N = 2
Seed values are 0 and 1
So, F<sub>1</sub> = 0 + 1 = 1
F<sub>2</sub> = 1 + F<sub>1</sub> = 1 + 1 = 2, I have written this Solution Code: static long Fibonacci(long n)
{
if (n == 0)
{ return 0;}
else if(n==1){
return 1;
}
return Fibonacci(n-1) + Fibonacci(n-2);
} , 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 Nth number in the fibonacci series. Consider 0 and 1 to be the seed values.
In the fibonacci series, each number (Fibonacci number) is the sum of the two preceding numbers. The series with 0 and 1 as seed values will go like -
0, 1, 1, 2, 3, 5 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 function <b>Fibonacci()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 30Return the Nth Fibonacci number.Sample Input
2
3
5
Sample Output
2
5
Explaination:
Test case 1-> N = 2
Seed values are 0 and 1
So, F<sub>1</sub> = 0 + 1 = 1
F<sub>2</sub> = 1 + F<sub>1</sub> = 1 + 1 = 2, I have written this Solution Code: // n is the input number
function fibonacci(n) {
// write code here
// do not console.log
// return the answer as a number
if (n == 1 || n == 0) return n;
return fibonacci(n-2) + fibonacci(n-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 Nth number in the fibonacci series. Consider 0 and 1 to be the seed values.
In the fibonacci series, each number (Fibonacci number) is the sum of the two preceding numbers. The series with 0 and 1 as seed values will go like -
0, 1, 1, 2, 3, 5 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 function <b>Fibonacci()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 30Return the Nth Fibonacci number.Sample Input
2
3
5
Sample Output
2
5
Explaination:
Test case 1-> N = 2
Seed values are 0 and 1
So, F<sub>1</sub> = 0 + 1 = 1
F<sub>2</sub> = 1 + F<sub>1</sub> = 1 + 1 = 2, I have written this Solution Code: static long Fibonacci(long n)
{
if (n == 0)
{ return 0;}
else if(n==1){
return 1;
}
return Fibonacci(n-1) + Fibonacci(n-2);
} , 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 Nth number in the fibonacci series. Consider 0 and 1 to be the seed values.
In the fibonacci series, each number (Fibonacci number) is the sum of the two preceding numbers. The series with 0 and 1 as seed values will go like -
0, 1, 1, 2, 3, 5 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 function <b>Fibonacci()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 30Return the Nth Fibonacci number.Sample Input
2
3
5
Sample Output
2
5
Explaination:
Test case 1-> N = 2
Seed values are 0 and 1
So, F<sub>1</sub> = 0 + 1 = 1
F<sub>2</sub> = 1 + F<sub>1</sub> = 1 + 1 = 2, I have written this Solution Code: // n is the input number
function fibonacci(n) {
// write code here
// do not console.log
// return the answer as a number
if (n == 1 || n == 0) return n;
return fibonacci(n-2) + fibonacci(n-1)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two numbers n1 and n2. You need to find the sum of the products of their corresponding digits. So, for a number n1= 6 and n2 = 34, you'll do (6*4)+(0*3) = 24.<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>sumOfProductOfDigits()</b> that takes the integers n1 and n2 as a parameter.
To custom test the function, provide input in the following manner:
1st line will contain the number of test cases, let's say t
Then there will be t lines, each line having two numbers n1 and n2, separated by space
Constraints:
1 <= T <= 100
0 <= n1, n2 <= 10^6Return the sum of product of corresponding digits of n1 and n2.Sample Input:
2
9 0
35 6798
Sample Output:
0
67
Explanation:-
For test 2:-
(8*5) + (9*3) + (7*0) + (6*0) = 67, I have written this Solution Code: function sumOfProductOfDigits(n1, n2)
{
if(n1 < 10 && n2 < 10)
return n1*n2;
return (n1%10)*(n2%10) + sumOfProductOfDigits(parseInt(n1/10), parseInt(n2/10));
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two numbers n1 and n2. You need to find the sum of the products of their corresponding digits. So, for a number n1= 6 and n2 = 34, you'll do (6*4)+(0*3) = 24.<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>sumOfProductOfDigits()</b> that takes the integers n1 and n2 as a parameter.
To custom test the function, provide input in the following manner:
1st line will contain the number of test cases, let's say t
Then there will be t lines, each line having two numbers n1 and n2, separated by space
Constraints:
1 <= T <= 100
0 <= n1, n2 <= 10^6Return the sum of product of corresponding digits of n1 and n2.Sample Input:
2
9 0
35 6798
Sample Output:
0
67
Explanation:-
For test 2:-
(8*5) + (9*3) + (7*0) + (6*0) = 67, I have written this Solution Code: public static int sumOfProductOfDigits(int n1, int n2)
{
if(n1 < 10 && n2 < 10)
return n1*n2;
return (n1%10)*(n2%10) + sumOfProductOfDigits(n1/10, n2/10);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two numbers n1 and n2. You need to find the sum of the products of their corresponding digits. So, for a number n1= 6 and n2 = 34, you'll do (6*4)+(0*3) = 24.<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>sumOfProductOfDigits()</b> that takes the integers n1 and n2 as a parameter.
To custom test the function, provide input in the following manner:
1st line will contain the number of test cases, let's say t
Then there will be t lines, each line having two numbers n1 and n2, separated by space
Constraints:
1 <= T <= 100
0 <= n1, n2 <= 10^6Return the sum of product of corresponding digits of n1 and n2.Sample Input:
2
9 0
35 6798
Sample Output:
0
67
Explanation:-
For test 2:-
(8*5) + (9*3) + (7*0) + (6*0) = 67, I have written this Solution Code: def calc(n1,n2):
suma = 0
while n1> 0 or n2>0:
digit1 = n1% 10
digit2 = n2%10
p1=digit1*digit2
suma = suma + p1
n1 = n1 // 10
n2 = n2 // 10
return suma
t=int(input())
while t>0:
t-=1
li = list(map(int,input().strip().split()))
n1 = li[0]
n2 = li[1]
print (calc(n1,n2))
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Check if the given string is a palindrome or not?
Palindromes are a numbers, phrases or words that reads same both the ways, backward as well as forward. For example, 1331 is a palindrome because on reversing the digits the number remains the same. But 123 is not a palindrome, as on reversing the digits it becomes 321. So given a number n you have to output whether the number is a palindrome or not.
Print 1 if True and 0 if false.input contains a single string s.
Constraints:-
1<=|s|<=100000
Note:- String will only contains characters from '0' to '9'.1 or 0. where 1 means the number is palindrome and zero indicates it's not.Sample Input-1
1235321
Sample Output-1
1
Sample Input-2
6547
Sample Output-2
0
Sample Input-3
3443
Sample Output-3
1
Sample Input-4
3434
Sample Output-4
0
Explanation:-
Testcase1:-
On reversing the string it becomes 1235321 which is same as original string hence It is a palindrome, I have written this Solution Code: // str is input
function isPalindrome(str) {
// write code here
// do not console.log
// return the 1 or 0
const ans = str.split('').reverse().join('') === str ? 1 : 0
return ans
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Check if the given string is a palindrome or not?
Palindromes are a numbers, phrases or words that reads same both the ways, backward as well as forward. For example, 1331 is a palindrome because on reversing the digits the number remains the same. But 123 is not a palindrome, as on reversing the digits it becomes 321. So given a number n you have to output whether the number is a palindrome or not.
Print 1 if True and 0 if false.input contains a single string s.
Constraints:-
1<=|s|<=100000
Note:- String will only contains characters from '0' to '9'.1 or 0. where 1 means the number is palindrome and zero indicates it's not.Sample Input-1
1235321
Sample Output-1
1
Sample Input-2
6547
Sample Output-2
0
Sample Input-3
3443
Sample Output-3
1
Sample Input-4
3434
Sample Output-4
0
Explanation:-
Testcase1:-
On reversing the string it becomes 1235321 which is same as original string hence It is a palindrome, 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 A[2000][2000];
int vis[200][200];
int dist[200][200];
int xa[8]={1,1,-1,-1,2,2,-2,-2};
int ya[8]={2,-2,2,-2,1,-1,1,-1};
int n,m;
int val,hh;
int check(int x,int y)
{
if (x>=0 && y>=0 && x<n && y<m )return 1;
else return 0;
}
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
signed main()
{
string s;
cin>>s;
int n=s.size();
int ch=1;
for(int j=0;j<=n-1;j++)
{
if(s[j]!=s[n-1-j]) ch=0;
}
cout<<ch;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Check if the given string is a palindrome or not?
Palindromes are a numbers, phrases or words that reads same both the ways, backward as well as forward. For example, 1331 is a palindrome because on reversing the digits the number remains the same. But 123 is not a palindrome, as on reversing the digits it becomes 321. So given a number n you have to output whether the number is a palindrome or not.
Print 1 if True and 0 if false.input contains a single string s.
Constraints:-
1<=|s|<=100000
Note:- String will only contains characters from '0' to '9'.1 or 0. where 1 means the number is palindrome and zero indicates it's not.Sample Input-1
1235321
Sample Output-1
1
Sample Input-2
6547
Sample Output-2
0
Sample Input-3
3443
Sample Output-3
1
Sample Input-4
3434
Sample Output-4
0
Explanation:-
Testcase1:-
On reversing the string it becomes 1235321 which is same as original string hence It is a palindrome, I have written this Solution Code: num=input()
rev=num[::-1]
if rev==num:
print("1")
else:
print("0"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Check if the given string is a palindrome or not?
Palindromes are a numbers, phrases or words that reads same both the ways, backward as well as forward. For example, 1331 is a palindrome because on reversing the digits the number remains the same. But 123 is not a palindrome, as on reversing the digits it becomes 321. So given a number n you have to output whether the number is a palindrome or not.
Print 1 if True and 0 if false.input contains a single string s.
Constraints:-
1<=|s|<=100000
Note:- String will only contains characters from '0' to '9'.1 or 0. where 1 means the number is palindrome and zero indicates it's not.Sample Input-1
1235321
Sample Output-1
1
Sample Input-2
6547
Sample Output-2
0
Sample Input-3
3443
Sample Output-3
1
Sample Input-4
3434
Sample Output-4
0
Explanation:-
Testcase1:-
On reversing the string it becomes 1235321 which is same as original string hence It is a palindrome, 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();
int len=str.length();
boolean temp=true;
int i=0,j=len-1;
for(i=0;i<len/2;i++){
if(str.charAt(i)!=str.charAt(j--)) temp=false;
}
if(temp) System.out.println(1);
else System.out.println(0);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, add 8 to it and then divide it by 3, take the modulus of the resulting value with 5 and then finally multiply it by 5. Display the final value of NYou don't have to worry about the input, you just have to complete the function <b>stepsExecute</b>
<b>Constraints</b>
1000 <= number <= 9999Print the final resultSample Input :
2345
Sample Output :
20, I have written this Solution Code: static void stepsExecute(int N){
System.out.println(((((N+8)/3)%5)*5));
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, add 8 to it and then divide it by 3, take the modulus of the resulting value with 5 and then finally multiply it by 5. Display the final value of NYou don't have to worry about the input, you just have to complete the function <b>stepsExecute</b>
<b>Constraints</b>
1000 <= number <= 9999Print the final resultSample Input :
2345
Sample Output :
20, I have written this Solution Code: a = int(input())
print((((a+8)//3)%5)*5), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.