Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long check(long x)
{
int count = 0;
while(x > 0 )
{
long temp = x;
while(temp >= 10)
temp /= 10;
x -= temp;
count++;
}
return count;
}
public static long new_check(long x)
{
int count = 0;
while(x >= 10 )
{
long temp_ans =1;
long temp = x;
while(temp >= 10)
{
temp /= 10;
temp_ans *= 10;
}
temp_ans *= temp;
if (x != temp_ans){
long diff = x - temp_ans;
if (diff >= temp)
{
temp_ans += diff - ((diff/temp)*temp);
count += (diff/temp);
}
else
{
temp_ans = x;
temp_ans -= temp;
count++;
}
}
else{
temp_ans -= temp;
count++;
}
x = temp_ans;
}
if (x > 0 && x < 10)
count++;
return count;
}
public static long calculate (long start,long end,long x)
{
if (start <= end )
{
long mid = (start+end)/2;
long temp = new_check(mid);
if (temp == x)
{
return mid;
}
else if (temp > x)
return calculate(start,mid-1,x);
else
return calculate(mid+1,end,x);
}
return -1;
}
public static void main (String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(reader.readLine());
while(t-- > 0)
{
long n = Integer.parseInt(reader.readLine().trim());
long x = n*10;
long ans = calculate(n,x,n);
while (n == new_check(ans+1))
ans++;
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., I have written this Solution Code:
def countdig(m) :
if (m == 0) :
return 0
else :
return 1 + countdig(m // 10)
def countSteps(x) :
c = 0
last = x
while (last) :
digits = countdig(last)
digits -= 1
divisor = pow(10, digits)
first = last // divisor
lastnumber = first * divisor
skipped = (last - lastnumber) // first
skipped += 1
c += skipped
last = last - (first * skipped)
return c
def fun(l,r,ans,x):
#print(l,r,ans,"gjgufy")
if(l>r):
return ans
m=(l+r)//2
midans=countSteps(m)
#print(m,midans)
if(midans<=x):
ans=max(ans,m)
if(midans<=x):
return fun(m+1,r,ans,x)
else:
return fun(l,m-1,ans,x)
for _ in range(int(input())):
x=int(input())
print(fun(0,9999999999,0,x)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rahul wants to impress a girl named Tanu whom he loves very dearly. Rahul gets to know that Tanu is stuck on a programming problem. Help Rahul to solve the problem so that he can impress Tanu.
Here is the description of the problem:-
Choose an integer n and subtract the first digit of the number n from it i.e if n is 245 then subtract 2 from it making it 245-2 = 243.
Keep on doing this operation until the number becomes 0 (for eg. 25 requires 14 operations to reduce to 0)
You are given a number of operation limit X. You have to find the greatest integer such that you can reduce it to 0 using atmost X operations.The first line of input contains the number of test cases T. The next T line contains a single integer X denotes the number of operations.
Constraints:-
1 <= T <= 100
1 <= X <= 314329798For each test case in a new line print the maximum number that can be obtained by performing exactly X operations.Sample Input:-
3
2
20
29
Sample Output:-
10
45
100
Explanation:-
For test 2:- 45 is the largest number that will become 0 after exactly 20 operations., I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 10000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll cal(ll n){
ll ans=0;
ll cnt=0;
ll p=n;
ll res=n;
while(res/10>0){
cnt=0;
p=res;
n=res;
while(n/10>0){
cnt++;
n=n/10;}
ll x=lround(pow(10,cnt));
ll t=p/x;
ll y=p%x;
ans+=(y/t+1);
res=res-t*(y/t+1);
}
ans++;
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
ll x;
cin>>x;
ll i=1,j=1e9+50;
ll mid;
while(i<j){
mid= (i+j)/2;
ll ans=cal(mid);
if(ans>x){j=mid;}
else if(ans==x){break;}
else{
i=mid+1;
}
}
for(ll k=mid-1;k<1e9+20;k++){
if(cal(k)>x){out(k-1);break;}
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N robots standing in a row. Ultron wants to delegate a task of difficulty X to two of them. Each robot has its own ability, A[i] for the i<sup>th</sup> robot from the left. The task can only be completed if the sum of the abilities of the two robots is exactly equal to X. Ultron wants to find out the number of distinct unordered pairs of robots he can choose to complete this task. He wants to find this number for Q (not necessarily distinct) subarrays of robots.
Formally, for each query, you will be given two integers L and R and you need to calculate the number of pairs (i, j) such that L ≤ i < j ≤ R and A[i] + A[j] = X.
Further, these subarrays have a nice property that for every pair of them at least one of the following three statements hold:
1. Their intersection is one of the two intervals itself (for example, segments [2, 6] and [2, 4]).
2. Their intersection is empty (for example, segments [2, 6] and [7, 8]).
3. Their intersection consists of exactly one element (for example, segments [2, 5] and [5, 8]).
For another example, note that the segments [2, 5] and [4, 7] do not satisfy any of the above conditions, and thus cannot both appear in the input.
You need to find the answer to each one of Ultron's queries and report the XOR of their answers.
<b>Note:</b> Since the input is large, it is recommended that you use fast input/output methods.The first line of the input contains two space-separated integers, N, the number of robots and X, the difficulty of the task.
The second line contains N space-separated integers A[1], A[2], ... A[n] - the abilities of the robots.
The third line of input contains an integer Q - the number of queries Ultron has.
Q lines follow, the i<sup>th</sup> of them contains two integers L and R for the i<sup>th</sup> query.
It is guaranteed that the given subarrays satisfy the properties mentioned in the problem statement.
<b>Constraints: </b>
2 ≤ N ≤ 400000
1 ≤ X ≤ 1000000
0 ≤ A[i] ≤ X
1 ≤ Q ≤ 400000
1 ≤ L < R ≤ N for each queryPrint a single integer, the bitwise XOR of the answers of the Q queries.Sample Input:
7 10
2 9 1 8 4 4 6
5
1 7
2 5
1 6
3 4
4 5
Sample Output:
7
Explanation:
For the first query there are 4 pairs of indices, (2, 3), (1, 4), (5, 7), (6, 7).
For the second query there is 1 pair of indices, (2, 3)
Similarly you can verify that the answers for the five queries are (4, 1, 2, 0, 0) in order. So we print their bitwise XOR = 7.
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
int X;
vector<int> adj[400005];
int ans[400005];
int cnt[1000005];
int A[400005];
pair<int, int> Q[400005];
void dfs(int x, bool keep = false)
{
int bigChild = -1, sz = 0;
for(auto &p: adj[x])
{
if(Q[p-1].second - Q[p-1].first > sz)
{
sz = Q[p-1].second - Q[p-1].first;
bigChild = p;
}
}
if(x > 0 && bigChild == -1)
{
for(int cur = Q[x-1].first; cur <= Q[x-1].second; cur++)
{
ans[x-1] += cnt[X-A[cur]];
cnt[A[cur]]++;
}
if(!keep)
{
for(int cur = Q[x-1].first; cur <= Q[x-1].second; cur++)
{
cnt[A[cur]]--;
}
}
return;
}
for(auto &p: adj[x])
{
if(p != bigChild)
{
dfs(p);
}
}
dfs(bigChild, true);
if(x > 0)
{
ans[x-1] = ans[bigChild-1];
for(int cur = Q[x-1].first; cur <= Q[x-1].second; cur++)
{
if(cur == Q[bigChild-1].first)
{
cur = Q[bigChild-1].second;
continue;
}
ans[x-1] += cnt[X-A[cur]];
cnt[A[cur]]++;
}
if(!keep)
{
for(int cur = Q[x-1].first; cur <= Q[x-1].second; cur++)
{
cnt[A[cur]]--;
}
}
}
}
bool my(int a, int b)
{
if(Q[a].first < Q[b].first) return true;
else if(Q[a].first == Q[b].first) return Q[a].second > Q[b].second;
return false;
}
void sack(int n, int q)
{
vector<int> order;
for(int i=0; i<q; i++) order.push_back(i);
sort(order.begin(), order.end(), my);
stack<int> right_ends;
stack<int> st;
for(int i=0; i<q; i++)
{
while(right_ends.size() && right_ends.top() <= Q[order[i]].first)
{
int which = st.top();
st.pop();
right_ends.pop();
if(st.empty())
{
adj[0].push_back(which+1);
}
else
{
adj[st.top()+1].push_back(which+1);
}
}
st.push(order[i]);
// cout << "inserting " << queries[i][2] << " into stack\n";
right_ends.push(Q[order[i]].second);
}
while(!st.empty())
{
int which = st.top();
st.pop();
right_ends.pop();
if(st.empty())
{
adj[0].push_back(which+1);
}
else
{
adj[st.top()+1].push_back(which+1);
}
}
// for(int i=0; i<=q; i++)
// {
// for(auto &x: adj[i])
// {
// cout << i << " has child " << x << "\n";
// }
// }
dfs(0);
}
signed main()
{
// freopen("input.txt", "r", stdin);
// freopen("output_sack.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n >> X;
for(int i=0; i<n; i++)
{
cin >> A[i];
}
int q;
cin >> q;
for(int i=0; i<q; i++)
{
cin >> Q[i].first >> Q[i].second;
Q[i].first--;
Q[i].second--;
}
sack(n, q);
int ansr = 0;
for(int i=0; i<q; i++)
{
ansr ^= ans[i];
}
cout << ansr << "\n";
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code: n=int(input())
x=n/3
if n%3==2:
x+=1
print(int(x)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int ans = n/3;
if(n%3==2){ans++;}
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this season of love, everyone wants to surprise each other.
You are also super excited and you wish to buy roses of 3 different colors. You always buy roses in order, white, yellow, red.
So if you buy 7 roses, they will be "white, yellow, red, white, yellow, red, white".
You need to find the number of yellow roses that you will buy?The only line of input contains a single integer, N, the number of roses that you will buy.
Constraints
1 <= N <= 1000Output a single integer, the number of yellow roses.Sample Input 1
2
Sample Output 1
1
Sample Input 2
8
Sample Ouput 2
3
Explanation;-
testcase1;- 2 flower will be white,yellow
so number of yellow flower is 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int x=n/3;
if(n%3==2){
x++;}
cout<<x;}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you need to check whether the given number is <b>Palindrome</b> or not. A number is said to be Palindrome when it reads the same from backward as forward.User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>isPalindrome()</b> which contains N as a parameter.
<b>Constraints:</b>
1 <= N <= 9999You need to return "true" is the number is palindrome otherwise "false".Sample Input:
5
Sample Output:
true
Sample Input:
121
Sample Output:
true, I have written this Solution Code: static boolean isPalindrome(int N)
{
int sum = 0;
int rev = N;
while(N > 0)
{
int digit = N%10;
sum = sum*10+digit;
N = N/10;
}
if(rev == sum)
return true;
else return false;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, you need to check whether the given number is <b>Palindrome</b> or not. A number is said to be Palindrome when it reads the same from backward as forward.User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>isPalindrome()</b> which contains N as a parameter.
<b>Constraints:</b>
1 <= N <= 9999You need to return "true" is the number is palindrome otherwise "false".Sample Input:
5
Sample Output:
true
Sample Input:
121
Sample Output:
true, I have written this Solution Code: def isPalindrome(N):
sum1 = 0
rev = N
while(N > 0):
digit = N%10
sum1 = sum1*10+digit
N = N//10
if(rev == sum1):
return True
return False, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and M, your task is to print N * M.Input contains two integers N and M.
Constraints:-
1 < = N, M < = 10<sup>10<sup>4</sup></sup>Print N*M.Sample input:-
2 3
Sample Output:-
6
Sample Input:-
123 2
Sample Output:-
246, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.BigInteger;
class Main {
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
BigInteger a=new BigInteger(str1);
BigInteger b=new BigInteger(str2);
BigInteger c=a.multiply(b);
System.out.print(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and M, your task is to print N * M.Input contains two integers N and M.
Constraints:-
1 < = N, M < = 10<sup>10<sup>4</sup></sup>Print N*M.Sample input:-
2 3
Sample Output:-
6
Sample Input:-
123 2
Sample Output:-
246, I have written this Solution Code:
n,m = map(int,input().split())
print(int(n*m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. The task is to generate and print all binary numbers with decimal values from 1 to N.
Try solving this using a queue.The only line of input contains a single integer N.
1 <= N <= 50000Print all binary numbers with decimal values from 1 to N in a single line.Sample Input:
5
Sample Output:
1 10 11 100 101, I have written this Solution Code: def DecimalToBinary(num):
return "{0:b}".format(int(num))
n = int(input())
for i in range(1,n+1):
print(DecimalToBinary(i),end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. The task is to generate and print all binary numbers with decimal values from 1 to N.
Try solving this using a queue.The only line of input contains a single integer N.
1 <= N <= 50000Print all binary numbers with decimal values from 1 to N in a single line.Sample Input:
5
Sample Output:
1 10 11 100 101, I have written this Solution Code: import java.util.*;
import java.math.*;
import java.io.*;
class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
StringBuilder s=new StringBuilder("");
for(int i=1;i<=n;i++){
s.append(Integer.toBinaryString(i)+" ");
}
System.out.print(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. The task is to generate and print all binary numbers with decimal values from 1 to N.
Try solving this using a queue.The only line of input contains a single integer N.
1 <= N <= 50000Print all binary numbers with decimal values from 1 to N in a single line.Sample Input:
5
Sample Output:
1 10 11 100 101, 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 = 500 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
int n; cin >> n;
queue<string> q;
q.push("1");
while(n--){
string x = q.front();
q.pop();
cout << x << " ";
q.push(x + "0");
q.push(x + "1");
}
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
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: A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run-up to the stairs. You need to return all possible number of ways.
The time complexity of your code should be O(n).
Print answer modulo 10^9 + 7.The input line contains T, denoting the number of testcases. Each testcase contains single line containing N, i.e. stairs having number of steps.
Constraints
1 <= T <= 10
1 <= N <= 10^5Find the total number of ways. Since the answer can be very large, thus print them as modulo 10^9 + 7.Sample Input
2
4
3
Sample output
7
4
Explanation:
Testcase 1:
In this case we have stairs with 4 steps. So we can figure out how many ways are there to reach at the top.
1st: (1, 1, 1, 1)
2nd: (1, 1, 2)
3rd: (1, 2, 1)
4th: (1, 3)
5th: (2, 1, 1)
6th: (2, 2)
7th: (3, 1), I have written this Solution Code:
dp = [1,2,4]
n = int(input())
for i in range(n):
k=int(input())
one = 1
two = 2
three = 4
four = 0
if k<4:
print(dp[k-1])
continue
for i in range(3,k):
four = one + two + three
one = two
two = three
three = four
print(four%(10**9+7)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run-up to the stairs. You need to return all possible number of ways.
The time complexity of your code should be O(n).
Print answer modulo 10^9 + 7.The input line contains T, denoting the number of testcases. Each testcase contains single line containing N, i.e. stairs having number of steps.
Constraints
1 <= T <= 10
1 <= N <= 10^5Find the total number of ways. Since the answer can be very large, thus print them as modulo 10^9 + 7.Sample Input
2
4
3
Sample output
7
4
Explanation:
Testcase 1:
In this case we have stairs with 4 steps. So we can figure out how many ways are there to reach at the top.
1st: (1, 1, 1, 1)
2nd: (1, 1, 2)
3rd: (1, 2, 1)
4th: (1, 3)
5th: (2, 1, 1)
6th: (2, 2)
7th: (3, 1), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long waysBottomUp(int n, long dp[]){
for(int i=4; i<=n; i++){
dp[i]= ((dp[i-1]%1000000007) + (dp[i-2]%1000000007) + (dp[i-3]%1000000007))%1000000007;
}
return dp[n];
}
public static void main (String[] args)throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
long dp[] = new long[100001];
int t = Integer.parseInt(rd.readLine());
while(t-->0){
int n = Integer.parseInt(rd.readLine());
dp[0] =1;
dp[1] = 1;
dp[2] = 2;
dp[3] = 4;
System.out.println(waysBottomUp(n, dp)%1000000007);}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A child is running up a staircase with n steps and can hop either 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run-up to the stairs. You need to return all possible number of ways.
The time complexity of your code should be O(n).
Print answer modulo 10^9 + 7.The input line contains T, denoting the number of testcases. Each testcase contains single line containing N, i.e. stairs having number of steps.
Constraints
1 <= T <= 10
1 <= N <= 10^5Find the total number of ways. Since the answer can be very large, thus print them as modulo 10^9 + 7.Sample Input
2
4
3
Sample output
7
4
Explanation:
Testcase 1:
In this case we have stairs with 4 steps. So we can figure out how many ways are there to reach at the top.
1st: (1, 1, 1, 1)
2nd: (1, 1, 2)
3rd: (1, 2, 1)
4th: (1, 3)
5th: (2, 1, 1)
6th: (2, 2)
7th: (3, 1), I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
a[0] = 1;
for(int i = 1; i < N; i++){
if(i >= 1) a[i] += a[i-1];
if(i >= 2) a[i] += a[i-2];
if(i >= 3) a[i] += a[i-3];
a[i] %= mod;
}
while(t--){
int n; cin >> n;
cout << a[n] << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader sc= new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0;
long z=0;
String[] str;
str = sc.readLine().split(" ");
a= Integer.parseInt(str[0]);
b= Integer.parseInt(str[1]);
c= Integer.parseInt(str[2]);
d= Integer.parseInt(str[3]);
BigInteger m = new BigInteger("1000000007");
BigInteger n = new BigInteger("1000000006");
BigInteger zero = new BigInteger("0");
BigInteger ans, y;
if(d==0){
z =1;
}else{
z = (long)Math.pow(c, d);
}
if(b==0){
y= zero;
}else{
y = (BigInteger.valueOf(b)).modPow((BigInteger.valueOf(z)), n);
}
if(y == zero){
System.out.println("1");
}else if(a==0){
System.out.println("0");
}else{
ans = (BigInteger.valueOf(a)).modPow(y, m);
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: nan, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int powmod(int a, int b, int c = MOD){
int ans = 1;
while(b){
if(b&1){
ans = (ans*a)%c;
}
a = (a*a)%c;
b >>= 1;
}
return ans;
}
void solve(){
int a, b, c, d; cin>>a>>b>>c>>d;
int x = pow(c, d);
int y = powmod(b, x, MOD-1);
int ans = powmod(a, y, MOD);
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a singly linked list and an element K, your task is to insert the element at the tail of the linked list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>addElement()</b> that takes head node and the integer K as a parameter.
Constraints:
1 <=N<= 1000
1 <=K, value<= 1000Return the head of the modified linked listSample Input:-
5 2
1 2 3 4 5
Sample Output:
1 2 3 4 5 2
, I have written this Solution Code: a,b=[int(x) for x in input().split()]
c=[int(x) for x in input().split()]
for i in c:
print(i,end=" ")
print(b), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a singly linked list and an element K, your task is to insert the element at the tail of the linked list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>addElement()</b> that takes head node and the integer K as a parameter.
Constraints:
1 <=N<= 1000
1 <=K, value<= 1000Return the head of the modified linked listSample Input:-
5 2
1 2 3 4 5
Sample Output:
1 2 3 4 5 2
, I have written this Solution Code: public static Node addElement(Node head,int k) {
Node temp=head;
while(temp.next!=null){
temp=temp.next;}
Node x= new Node(k);
temp.next = x;
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You need to make an order counter to keep track of the total number of orders received.
Complete the function <code> generateOrder() </code> which returns a <code>function func()</code>. This function <code>func</code> should maintain a <code> count (initially 0)</code>. Every time <code>func</code> is called, <code> count</code> must be incremented by 1 and the string <code>"Total orders = " + count</code> must be returned.
<b>Note:</b> The function generateOrder() will be called internally. You do not need to call it yourself. The generateOrder() takes no argument. It is called internally.The generateOrder() function returns a function that returns the string <code>"Total orders = " + count</code>, where <code>count</code> is the number of times the function is called.
const initC = generateOrder(starting);
console.log(initC()) //prints "Total orders = 1"
console.log(initC()) //prints "Total orders = 2"
console.log(initC()) //prints "Total orders = 3"
, I have written this Solution Code: let generateOrder = function() {
let prefix = "Total orders = ";
let count = 0;
let totalOrders = function(){
count++
return prefix + count;
}
return totalOrders;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, 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));
BufferedWriter bo=new BufferedWriter(new OutputStreamWriter(System.out));
int t;
try{
t=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
return;
}
while(t-->0)
{
String[] g=br.readLine().split(" ");
int n=Integer.parseInt(g[0]);
int k=Integer.parseInt(g[1]);
if(k>n || (k==1) || (k>26))
{
if(n==1 && k==1)
bo.write("a\n");
else
bo.write(-1+"\n");
}
else
{
int extra=k-2;
boolean check=true;
while(n>extra)
{
if(check==true)
bo.write("a");
else
bo.write("b");
if(check==true)
check=false;
else
check=true;
n--;
}
for(int i=0;i<extra;i++)
bo.write((char)(i+99));
bo.write("\n");
}
}
bo.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: t=int(input())
for tt in range(t):
n,k=map(int,input().split())
if (k==1 and n>1) or (k>n):
print(-1)
continue
s="abcdefghijklmnopqrstuvwxyz"
ss="ab"
if (n-k)%2==0:
a=ss*((n-k)//2)+s[:k]
else:
a=ss*((n-k)//2)+s[:2]+"a"+s[2:k]
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, 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 n,k;
cin >> n >> k;
if(k==1){
if(n>1){
cout << -1 << endl;
}else{
cout << 'a' << endl;
}
}else if(n<k){
cout << -1 << endl;
}else if(n==k){
string s="";
for(int i=0 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}else{
string s="";
for(int i=0 ; i<(n-k+2) ; i++){
if(i%2){
s+="b";
}else{
s+="a";
}
}
for(int i=2 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}
}
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: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
public static int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;break;}
ch*=(long)3;
}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
def Ifpossible(x,y) :
result = y-x
ans = 0
ch = 1
while ans<result :
ans+=ch
if ans==result:
return 1;
ch*=3
return 0;
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print a number is positive/negative using if- else.The first line contains the number to be checked.
Constraints:
-100000<=n<=100000Prints "Positive number" if the number is positive, "Zero" if the number is zero and "Negative number" if the number is negative.Sample input:
33
Sample Output:
Positive number, I have written this Solution Code: num = int(input())
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: static int Probability(int A,int B){
int x=1;
for(int i=2;i<=A;i++){
if(A%i==0 && B%i==0){
x=i;
}
}
A=A/x;
B=B/x;
return A+B;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a certain event, the value of a favorable outcome is A and the total outcome is B. If the probability of this event is represented by p/q where p and q are coprime then what will be the value of p+q.<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>Probability()</b> that takes integers A, and B as arguments.
Constraints:-
1 <= A <= B <= 10000Return the value of p+q.Sample Input:-
4 6
Sample Output:-
5
Sample Input:-
3 5
Sample Output:-
8, I have written this Solution Code: def Probability(A,B):
x=1
for i in range (2,A+1):
if (A%i==0) and (B%i==0):
x=i
A=A//x
B=B//x
return A+B
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three numbers tell whether they form a pythagorus triplet or not.
Given A , B and C.
Three integers form pythagorus triplet if sum of square of two is equal to square of third.The first line of the input contains three space separated integers A, B and C.
Constraints
1 <= A,B,C <= 100The output should contain "YES" if they form a triplet else you should print "NO" without quotes.Sample Input
4 3 5
Sample Output
YES
Sample Input
4 6 5
Sample Output
NO, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner in =new Scanner(System.in);
int A = in.nextInt();
int B = in.nextInt();
int C = in.nextInt();
System.out.println(isPythagorasTriplet(A,B,C));
}
static String isPythagorasTriplet(int A, int B, int C){
if(A>B && A>C){
if(A==Math.sqrt(B*B+C*C)){
return "YES";
}
return "NO";
}else if(B>C){
if(B==Math.sqrt(A*A+C*C)){
return "YES";
}
return "NO";
}else{
if(C==Math.sqrt(A*A+B*B)){
return "YES";
}
return "NO";
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given three numbers tell whether they form a pythagorus triplet or not.
Given A , B and C.
Three integers form pythagorus triplet if sum of square of two is equal to square of third.The first line of the input contains three space separated integers A, B and C.
Constraints
1 <= A,B,C <= 100The output should contain "YES" if they form a triplet else you should print "NO" without quotes.Sample Input
4 3 5
Sample Output
YES
Sample Input
4 6 5
Sample Output
NO, I have written this Solution Code: nums=list(map(int,input().rstrip().split()))
nums.sort()
if((nums[0]**2)+(nums[1]**2)==(nums[2]**2)):
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 three numbers tell whether they form a pythagorus triplet or not.
Given A , B and C.
Three integers form pythagorus triplet if sum of square of two is equal to square of third.The first line of the input contains three space separated integers A, B and C.
Constraints
1 <= A,B,C <= 100The output should contain "YES" if they form a triplet else you should print "NO" without quotes.Sample Input
4 3 5
Sample Output
YES
Sample Input
4 6 5
Sample Output
NO, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main()
{
int a[3];
cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
if((a[0]*a[0]+a[1]*a[1])==a[2]*a[2]){cout<<"YES";}
else{cout<<"NO";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Power Rangers have to trap a evil spirit monster into a cuboid shape box with dimensions A*B*C, where A, B and C are positive integers. The volume of the box should be exactly X cubic units (that is A*B*C should be equal to X). To allow minimum evil aura to leak from the box the surface area of the box should be minimized. Given X, find the minimum surface area of the optimal box.The first and the only line of input contains a single integer X.
Constraints:
1 <= X <= 1000000Print the minimum surface area of the optimal box.Sample Input 1
125
Sample Output 1
150
Explanation: Optimal dimensions are 5*5*5.
Sample Input 2
100
Sample Output 1
130
Explanation: Optimal dimensions are 5*4*5., I have written this Solution Code:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)throws Exception{ new Main().run();}
long mod=1000000000+7;
long tsa=Long.MAX_VALUE;
void solve() throws Exception
{
long X=nl();
div(X);
out.println(tsa);
}
long cal(long a,long b, long c)
{
return 2l*(a*b + b*c + c*a);
}
void div(long n)
{
for (long i=1; i*i<=n; i++)
{
if (n%i==0)
{
if (n/i == i)
{
all_div(i, i);
}
else
{
all_div(i, n/i);
all_div(n/i, i);
}
}
}
}
void all_div(long n , long alag)
{
ArrayList<Long> al = new ArrayList<>();
for (long i=1; i*i<=n; i++)
{
if (n%i==0)
{
if (n/i == i)
tsa=min(tsa,cal(i,i,alag));
else
{
tsa=min(tsa,cal(i,n/i,alag));
}
}
}
}
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
private SpaceCharFilter filter;
PrintWriter out;
int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;}
long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;}
int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;}
long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;}
void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}}
void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}}
String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();}
void shuffle(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<a.length;i++)
al.add(a[i]);
Collections.sort(al);
for(int i=0;i<a.length;i++)
a[i]=al.get(i);
}
long lcm(long a,long b)
{
return (a*b)/(gcd(a,b));
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
long expo(long p,long q)
{
long z = 1;
while (q>0) {
if (q%2 == 1) {
z = (z * p)%mod;
}
p = (p*p)%mod;
q >>= 1;
}
return z;
}
void run()throws Exception
{
in=System.in; out = new PrintWriter(System.out);
solve();
out.flush();
}
private int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
private int ni() throws IOException
{
int c = scan();
while (isSpaceChar(c))
c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = scan();
} while (!isSpaceChar(c));
return res * sgn;
}
private long nl() throws IOException
{
long num = 0;
int b;
boolean minus = false;
while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = scan();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = scan();
}
}
private double nd() throws IOException{
return Double.parseDouble(ns());
}
private String ns() throws IOException {
int c = scan();
while (isSpaceChar(c))
c = scan();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = scan();
} while (!isSpaceChar(c));
return res.toString();
}
private String nss() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
private char nc() throws IOException
{
int c = scan();
while (isSpaceChar(c))
c = scan();
return (char) c;
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhiteSpace(c);
}
private 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: Power Rangers have to trap a evil spirit monster into a cuboid shape box with dimensions A*B*C, where A, B and C are positive integers. The volume of the box should be exactly X cubic units (that is A*B*C should be equal to X). To allow minimum evil aura to leak from the box the surface area of the box should be minimized. Given X, find the minimum surface area of the optimal box.The first and the only line of input contains a single integer X.
Constraints:
1 <= X <= 1000000Print the minimum surface area of the optimal box.Sample Input 1
125
Sample Output 1
150
Explanation: Optimal dimensions are 5*5*5.
Sample Input 2
100
Sample Output 1
130
Explanation: Optimal dimensions are 5*4*5., I have written this Solution Code: m =int(input())
def print_factors(x):
factors=[]
for i in range(1, x + 1):
if x % i == 0:
factors.append(i)
return(factors)
area=[]
factors=print_factors(m)
for a in factors:
for b in factors:
for c in factors:
if(a*b*c==m):
area.append((2*a*b)+(2*b*c)+(2*a*c))
print(min(area)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Power Rangers have to trap a evil spirit monster into a cuboid shape box with dimensions A*B*C, where A, B and C are positive integers. The volume of the box should be exactly X cubic units (that is A*B*C should be equal to X). To allow minimum evil aura to leak from the box the surface area of the box should be minimized. Given X, find the minimum surface area of the optimal box.The first and the only line of input contains a single integer X.
Constraints:
1 <= X <= 1000000Print the minimum surface area of the optimal box.Sample Input 1
125
Sample Output 1
150
Explanation: Optimal dimensions are 5*5*5.
Sample Input 2
100
Sample Output 1
130
Explanation: Optimal dimensions are 5*4*5., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int x;
cin >> x;
int ans = 6*x*x;
for (int i=1;i*i*i<=x;++i)
if (x%i==0)
for (int j=i;j*j<=x/i;++j)
if (x/i % j==0) {
int k=x/i/j;
int cur=0;
cur=i*j+i*k+k*j;
cur*=2;
ans=min(ans,cur);
}
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 a string S. The letters are numbered from 1 to |S| from left to right. You have to perform M operations. In each operation, you will be given a number P<sub>i</sub>. You need to reverse the substring from P<sub>i</sub> to |S| - P<sub>i</sub> + 1. Print the final string after all the operations.
It is guaranteed that 2*P<sub>i</sub> <= |S|
|S| denotes the length of string SThe first line contains a string S. Second line contains a single integer M. Third line contains M space separated integers denoting the value of P<sub>i</sub>.
1 <= |S| <= 100000
1 <= M <= 100000
1 <= P<sub>i</sub> <= ceil(|S|/2)Print a single line containing the final string after all the operationsSample Input:
abcdef
3
1 2 3
Sample Output:
fbdcea
Explanation:
After 1st step, S = fedcba
After 2nd step, S = fbcdea
After 3rd step, S = fbdcea, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static String reverseString(String str)
{
StringBuilder s=new StringBuilder(str);
int n=s.length();
for(int i=0;i<n/2;i++)
{
char a=s.charAt(i);
s.setCharAt(i,s.charAt(n-i-1));
s.setCharAt(n-i-1,a);
}
return s.toString();
}
public static void main (String[] args) {
Scanner scan=new Scanner(System.in);
String s=scan.next();
int q=scan.nextInt();
String t="";
int[] a=new int[(int)Math.ceil(s.length()/2.0)];
while(q-->0)
{
t="";
int p=scan.nextInt();
a[p-1]=(a[p-1]==0)?1:0;
}
char[] arr=s.toCharArray();
for(int i=1;i<a.length;i++)
{
a[i]=a[i]+a[i-1];
a[i]=(a[i]==2)?0:a[i];
}
for(int i=0;i<a.length;i++)
{
if(a[i]==1)
{
char c=arr[i];
arr[i]=arr[arr.length-i-1];
arr[arr.length-i-1]=c;
}
}
s=String.valueOf(arr);
System.out.println(s);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. The letters are numbered from 1 to |S| from left to right. You have to perform M operations. In each operation, you will be given a number P<sub>i</sub>. You need to reverse the substring from P<sub>i</sub> to |S| - P<sub>i</sub> + 1. Print the final string after all the operations.
It is guaranteed that 2*P<sub>i</sub> <= |S|
|S| denotes the length of string SThe first line contains a string S. Second line contains a single integer M. Third line contains M space separated integers denoting the value of P<sub>i</sub>.
1 <= |S| <= 100000
1 <= M <= 100000
1 <= P<sub>i</sub> <= ceil(|S|/2)Print a single line containing the final string after all the operationsSample Input:
abcdef
3
1 2 3
Sample Output:
fbdcea
Explanation:
After 1st step, S = fedcba
After 2nd step, S = fbcdea
After 3rd step, S = fbdcea, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
string s; cin >> s;
int m; cin >> m;
int n = s.length();
for(int i = 1; i <= m; i++){
int x; cin >> x;
a[x]++;
a[n-x+2]--;
}
for(int i = 1; i <= (n-1)/2 + 1; i++){
a[i] += a[i-1];
if(a[i]&1)
swap(s[i-1], s[n-i]);
}
cout << s;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
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));
String st = br.readLine();
int len = 0;
int c=0;
for(int i=0;i<st.length();i++){
if(st.charAt(i)=='A'){
c++;
len = Math.max(len,c);
}else{
c=0;
}
}
System.out.println(len);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: S=input()
max=0
flag=0
for i in range(0,len(S)):
if(S[i]=='A' or S[i]=='B'):
if(S[i]=='A'):
flag+=1
if(flag>max):
max=flag
else:
flag=0
print(max), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
string s; cin>>s;
int ct = 0;
int ans = 0;
for(char c: s){
if(c == 'A')
ct++;
else
ct=0;
ans = max(ans, ct);
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)) ;
int n = Integer.parseInt(read.readLine()) ;
String[] str = read.readLine().trim().split(" ") ;
int[] arr = new int[n] ;
for(int i=0; i<n; i++) {
arr[i] = Integer.parseInt(str[i]) ;
}
long ans = 0L;
if((n & 1) == 1) {
for(int i=0; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
else {
Arrays.sort(arr);
for(int i=1; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: n=int(input())
arr=map(int,input().split())
result=[]
for item in arr:
if item%2==0:
result.append(item-1)
else:
result.append(item)
if sum(result)%2==0:
result.sort()
result.pop(0)
print(sum(result))
else:
print(sum(result)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
int n; cin >> n ;
long long int ans = 0;
// int arr[n];
int odd = 0,minn=INT_MAX;
for(int i=0;i<n;i++){
ll temp; cin >> temp;
// cin >> arr[i];
ans += (temp&1) ? temp : temp-1;
if(minn>temp)
minn = temp;
}
if(n&1)
cout << ans ;
else{
if(minn&1)
cout <<ans-minn;
else
cout << ans - minn + 1;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., I have written this Solution Code: import java.io.*;
import java.util.*;
class Reader{
BufferedReader reader;
Reader(){
this.reader = new BufferedReader(new InputStreamReader(System.in));
}
public String read() throws IOException {
return reader.readLine();
}
public int readInt() throws IOException {
return Integer.parseInt(reader.readLine());
}
public long readLong() throws IOException {
return Long.parseLong(reader.readLine());
}
public String[] readArray() throws IOException {
return reader.readLine().split(" ");
}
public int[] readIntegerArray() throws IOException {
String[] str = reader.readLine().split(" ");
int[] arr = new int[str.length];
for(int i=0;i<str.length;i++) arr[i] = Integer.parseInt(str[i]);
return arr;
}
public long[] readLongArray() throws IOException {
String[] str = reader.readLine().split(" ");
long[] arr = new long[str.length];
for(int i=0;i<str.length;i++) arr[i] = Long.parseLong(str[i]);
return arr;
}
}
public class Main {
public static void main(String[] args) throws IOException {
Reader rdr = new Reader();
int t = rdr.readInt();
while(t-- > 0){
int[] str = rdr.readIntegerArray();
int n = str[0];
int k = str[1];
int[] arr = rdr.readIntegerArray();
Arrays.sort(arr);
int c=0;
for(int i=0;i<n;i++){
if(arr[i]>=k){
System.out.print(arr[i]+" ");
c++;
}
}
if(c==0) System.out.print(-1);
System.out.println();
if(c==n) System.out.print(-1);
else for(int i=0;i<n-c;i++) System.out.print(arr[i]+" ");
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., I have written this Solution Code: t=int(input())
while t>0:
a=input().split()
b=input().split()
for i in range(len(b)):
b[i]=int(b[i])
lesser=[]
greater=[]
for i in b:
if i>=int(a[1]):
greater.append(i)
else:
lesser.append(i)
if len(greater)==0:
print(-1,end="")
else:
greater.sort()
for x in greater:
print(x,end=" ")
print()
if len(lesser)==0:
print(-1,end="")
else:
lesser.sort()
for y in lesser:
print(y,end=" ")
print()
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N and value K. The elements of the array A contains positive integers. You have to print all the elements which are greater than K in the array in sorted order (including K as well if present in the array A), and print all the elements which are smaller than K in sorted order both of them in separate lines. If the elements greater than or equal to K are not present in the array then print "-1". Similarly, in the case of smaller elements print -1 if elements smaller than K doesn’t exist. If a number appears more than once print number more than once.First line of input contains number of testcases T. For each testcase, there are two lines, first of which contains N and K separated by space, next line contains N space separated integers.
Constraints:
1 <= T <= 100
1 <= N <= 100000
1 <= K <= 1000000
1 <= A[i] <= 1000000
Sum of N over all test cases do not exceed 100000For each testcase, print the required elements(if any), else print "-1" (without quotes)Input:
1
5 1
2 1 5 7 6
Output:
1 2 5 6 7
-1
Explanation:
Testcase 1 : Since, 1, 2, 5, 6, 7 are greater or equal to given K. Also, no element less than K is present in the array., 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
signed main()
{
int t;
cin>>t;
while(t>0)
{
t--;
int n,k;
cin>>n>>k;
vector<int> A,B;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
if(a<k) B.pu(a);
else A.pu(a);
}
sort(all(A));
sort(all(B));
for(auto it:A)
{
cout<<it<<" ";
}if(A.size()==0) cout<<-1;
cout<<endl;
for(auto it:B)
{
cout<<it<<" ";
}if(B.size()==0) cout<<-1;
cout<<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 N elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.First line of input contains a single integer N.
Second line of input contains N space separated integers, denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9Print a single integer which is the minimum number of elements that must be deleted.Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
3
Explanation: we will delete 1 of each 1, 2 and 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int N = Integer.parseInt(br.readLine());
String[] arr = br.readLine().split(" ");
int n = arr.length;
int[] array1 = new int[n];
for(int i = 0 ;i<n; i++){
array1[i] = Integer.parseInt(arr[i]);
}
int res = 0;
for (int i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j < i; j++)
if (array1[i] == array1[j])
break;
if (i == j)
res++;
}
System.out.print(n-res);
}
}, 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 elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.First line of input contains a single integer N.
Second line of input contains N space separated integers, denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9Print a single integer which is the minimum number of elements that must be deleted.Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
3
Explanation: we will delete 1 of each 1, 2 and 3., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
unordered_map<int,int> m;
for(int i=1;i<=n;++i){
int d;
cin>>d;
m[d]++;
}
int ans=0;
for(auto r:m)
ans+=r.second-1;
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 Arr of N elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.First line of input contains a single integer N.
Second line of input contains N space separated integers, denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9Print a single integer which is the minimum number of elements that must be deleted.Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
3
Explanation: we will delete 1 of each 1, 2 and 3., I have written this Solution Code: import numpy as np
from collections import defaultdict
d=defaultdict(int)
n=int(input())
a=np.array([input().strip().split()],int).flatten()
for i in a:
d[i]+=1
print(n-len(d)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Binary Search Tree and 2 node value <b>n1</b> and <b>n2</b>, your task is to find the <b>lowest common ancestor(LCA)</b> of the two nodes given.
It may happen that values n1 and n2 may or may be not present.
<b>Note:</b> Duplicates are not inserted in the BST.<b>User Task:</b>
This is a function problem. You don't have to take any input. You are required to complete the function LCA() that takes root node, n1, n2 as parameters and returns the node that is LCA of n1 and n2.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^4
1 <= node values <= 10^4
<b>Sum of "N" over all testcases does not exceed 10^5</b>
For <b>Custom Input:</b>
First line of input should contains the number of test cases T. For each test case, there will be two lines of input.
First line will be a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
Second line will contain the values of two nodes
<b>Note:</b> If a node has been declared Null using 'N', no information about its children will be given further in the array.
Second line will contain the values of two nodesFor each testcase, you need to return the node containing LCA of n1 and n2. The driver code will print the data. If any of the node values (n1, n2) is not present then return null driver code will print -1 for that.Sample Input
2
5 4 6 3 N N 7 N N N 8
7 8
2 1 3
1 3
Sample Output
7
2
Explanation:
Testcase1:
The BST in above test case will look like
5
/ \
4 6
/ \
3 7
\
8
Here the LCA of 7 and 8 is 7., I have written this Solution Code:
static Node LCA(Node node, int n1, int n2) {
if (node == null) {
return null;
}
// If both n1 and n2 are smaller than root, then LCA lies in left
if (node.data > n1 && node.data > n2) {
return LCA(node.left, n1, n2);
}
// If both n1 and n2 are greater than root, then LCA lies in right
if (node.data < n1 && node.data < n2) {
return LCA(node.right, n1, n2);
}
return node;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: n=int(input())
li = list(map(int,input().strip().split()))
sum=0
for i in li:
if i>0:
sum+=i
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A permutation is simply a name for a reordering. So the permutations of the string
‘abc’ are ‘abc’, ‘acb’, ‘bac’, ‘bca’, ‘cab’, and ‘cba’. Note that a sequence is a
permutation of itself (the trivial permutation). For this problem, you’ll need
to write a recursive function that takes a string and returns a
list of all its permutations.
A couple of notes on the requirements:
1. The order of the returned permutations must be lexicographically.
2. Avoid returning duplicates in your final list.Input contains a single string S.
Constraints:-
1<=|S|<=8Print all the permutations of string S in lexicographical order.Sample Input:
ABC
Sample Output :
ABC ACB BAC BCA CAB CBA
Explanation:
all permutation are arranged in lexicographical order .
Sample Input:
(T(
Sample Output:-
((T (T( T((, I have written this Solution Code: def sol(arr):
dict = {}
for i in arr:
if i in dict.keys():
dict[i] = dict[i] + 1
else:
dict[i] = 1
keys = sorted(dict)
str = []
c = []
s=0
for key in keys:
str.append(key)
c.append(dict[key])
total = [0]*len(arr)
sol2(str, c, total, s)
def sol2(str, c, total, s):
if s == len(total):
str1=''
k=str1.join(total)
print(k,end=' ')
return
for i in range(len(str)):
if c[i] == 0:
continue
total[s] = str[i]
c[i] -= 1
sol2(str, c, total, s + 1)
c[i] += 1
str2=input()
n = list(str2)
sol(n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A permutation is simply a name for a reordering. So the permutations of the string
‘abc’ are ‘abc’, ‘acb’, ‘bac’, ‘bca’, ‘cab’, and ‘cba’. Note that a sequence is a
permutation of itself (the trivial permutation). For this problem, you’ll need
to write a recursive function that takes a string and returns a
list of all its permutations.
A couple of notes on the requirements:
1. The order of the returned permutations must be lexicographically.
2. Avoid returning duplicates in your final list.Input contains a single string S.
Constraints:-
1<=|S|<=8Print all the permutations of string S in lexicographical order.Sample Input:
ABC
Sample Output :
ABC ACB BAC BCA CAB CBA
Explanation:
all permutation are arranged in lexicographical order .
Sample Input:
(T(
Sample Output:-
((T (T( T((, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.util.Arrays;
class Main
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
String str = sc.next();
if(str.length()==1)
System.out.print(str);
else
permutations(str);
}
public static void permutations(String str)
{
char[] charstr = str.toCharArray();
Arrays.sort(charstr);
while (true)
{
System.out.print(new String(charstr) + " ");
if (!next_String(charstr)) {
break;
}
}
}
static void swap(char[] charstr, int i, int j)
{
char ch = charstr[i];
charstr[i] = charstr[j];
charstr[j] = ch;
}
static void reverse(char[] charstr, int start)
{
for (int i = start, j = charstr.length - 1; i < j; i++, j--) {
swap(charstr, i, j);
}
}
public static boolean next_String(char[] charstr)
{
int i = charstr.length - 1;
while (charstr[i - 1] >= charstr[i])
{
if (--i == 0) {
return false;
}
}
int j = charstr.length - 1;
while (j > i && charstr[j] <= charstr[i - 1]) {
j--;
}
swap(charstr, i - 1, j);
reverse(charstr, i);
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A permutation is simply a name for a reordering. So the permutations of the string
‘abc’ are ‘abc’, ‘acb’, ‘bac’, ‘bca’, ‘cab’, and ‘cba’. Note that a sequence is a
permutation of itself (the trivial permutation). For this problem, you’ll need
to write a recursive function that takes a string and returns a
list of all its permutations.
A couple of notes on the requirements:
1. The order of the returned permutations must be lexicographically.
2. Avoid returning duplicates in your final list.Input contains a single string S.
Constraints:-
1<=|S|<=8Print all the permutations of string S in lexicographical order.Sample Input:
ABC
Sample Output :
ABC ACB BAC BCA CAB CBA
Explanation:
all permutation are arranged in lexicographical order .
Sample Input:
(T(
Sample Output:-
((T (T( T((, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
set<string> se;
// Function to find all Permutations of a given string
// containing all distinct characters
void permutations(string str, int n, string res)
{
// base condition (only one character is left in the string)
if (n == 1)
{
string s= res + str;
se.insert(s);
return;
}
// process each character of the remaining string
for (int i = 0; i < n; i++)
{
// push current character to the output string and recur
// for the remaining characters
permutations(str.substr(1), n - 1, res + str[0]);
// left rotate the string by 1 unit for next iteration
// to right rotate the string use reverse iterator
rotate(str.begin(), str.begin() + 1, str.end());
}
}
// Find all Permutations of a string
int main()
{
string s;
cin>>s;
string res="";
permutations(s, s.size(), res);
for(auto it=se.begin();it!=se.end();it++){
cout<<*it<<" ";
}
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 A and a number K, find maximum sum of a subarray of size K.The first line of input contains two integers N and K, denoting the number of elements in the array and the subarray size respectively.
The next line contains N integers denoting the elements of the array respectively.
1 <= K <= N <= 200000
-200000 <= A[i] <= 200000Print a single integer denoting the maximum sum of subarray of size K.Sample Input:
4 2
-1 5 2 -3
Sample Output:
7
Explanation:
Three subarrays of size 2, their sum are 4, 7, -1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
String s[]=sc.nextLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[1]);
s=sc.nextLine().split(" ");
int ar[]=new int[n];
for(int i=0;i<n;i++){
ar[i]=Integer.parseInt(s[i]);
}
int max=0;
for(int i=0;i<=n-k;i++){
int t=0;
for(int j=0;j<k;j++){
t+=ar[i+j];
}
if(t>max)
max=t;
}
System.out.println(max);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers A and a number K, find maximum sum of a subarray of size K.The first line of input contains two integers N and K, denoting the number of elements in the array and the subarray size respectively.
The next line contains N integers denoting the elements of the array respectively.
1 <= K <= N <= 200000
-200000 <= A[i] <= 200000Print a single integer denoting the maximum sum of subarray of size K.Sample Input:
4 2
-1 5 2 -3
Sample Output:
7
Explanation:
Three subarrays of size 2, their sum are 4, 7, -1, I have written this Solution Code: n1,k1=input().split()
n,k=int(n1),int(k1)
l=list(map(int,input().strip().split()))
curr_sum=0
for x in range(k):
curr_sum=curr_sum+l[x]
max_sum=curr_sum
i=0
j=k
while i<len(l)-k:
curr_sum=curr_sum-l[i]+l[j]
max_sum=max(max_sum,curr_sum)
i+=1
j+=1
print(max_sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers A and a number K, find maximum sum of a subarray of size K.The first line of input contains two integers N and K, denoting the number of elements in the array and the subarray size respectively.
The next line contains N integers denoting the elements of the array respectively.
1 <= K <= N <= 200000
-200000 <= A[i] <= 200000Print a single integer denoting the maximum sum of subarray of size K.Sample Input:
4 2
-1 5 2 -3
Sample Output:
7
Explanation:
Three subarrays of size 2, their sum are 4, 7, -1, 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 a[N];
void solve(){
int n, k, ans = 0;
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int sum = 0;
for(int i = 1; i <= k; i++)
sum += a[i];
ans = sum;
for(int i = k+1; i <= n; i++){
sum += a[i] - a[i-k];
ans = max(ans, sum);
}
cout << ans;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
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: You are given a chessboard of size N x N, where the top left square is black. Each square contains a value. Find the sum of the values of all black squares and all white squares.
Remember that in a chessboard black and white squares are alternate.The first line of input will be the N size of the matrix. Then next N lines will consist of elements of the matrix. Each row will contain N elements since it is a square matrix.
<b>Constraints:-</b>
1 ≤ N ≤ 800
1 ≤ Matrix[i][j] ≤ 100000
Print two lines, the first line containing the sum of black squares and the second line containing the sum of white squares.Input 1:
3
1 2 3
4 5 6
7 8 9
Output 1:
25
20
Sample Input 2:
4
1 2 3 4
6 8 9 10
11 12 13 14
15 16 17 18
Sample Output 2:
80
79
<b>Explanation 1</b>
The black square contains 1, 3, 5, 7, 9; sum = 25
The white square contains 2, 4, 6, 8; sum = 20, I have written this Solution Code: n=int(input())
bSum=0
wSum=0
for i in range(n):
c=(input().split())
for j in range(len(c)):
if(i%2==0):
if(j%2==0):
bSum+=int(c[j])
else:
wSum+=int(c[j])
else:
if(j%2==0):
wSum+=int(c[j])
else:
bSum+=int(c[j])
print(bSum)
print(wSum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a chessboard of size N x N, where the top left square is black. Each square contains a value. Find the sum of the values of all black squares and all white squares.
Remember that in a chessboard black and white squares are alternate.The first line of input will be the N size of the matrix. Then next N lines will consist of elements of the matrix. Each row will contain N elements since it is a square matrix.
<b>Constraints:-</b>
1 ≤ N ≤ 800
1 ≤ Matrix[i][j] ≤ 100000
Print two lines, the first line containing the sum of black squares and the second line containing the sum of white squares.Input 1:
3
1 2 3
4 5 6
7 8 9
Output 1:
25
20
Sample Input 2:
4
1 2 3 4
6 8 9 10
11 12 13 14
15 16 17 18
Sample Output 2:
80
79
<b>Explanation 1</b>
The black square contains 1, 3, 5, 7, 9; sum = 25
The white square contains 2, 4, 6, 8; sum = 20, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws IOException {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int mat[][] = new int[N][N];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
mat[i][j] = sc.nextInt();
}
alternate_Matrix_Sum(mat,N);
}
static void alternate_Matrix_Sum(int mat[][], int N)
{
long sum =0, sum1 = 0;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if((i+j)%2 == 0)
sum += mat[i][j];
else sum1 += mat[i][j];
}
}
System.out.println(sum);
System.out.print(sum1);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long minCost(long arr[], int n)
{
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) pq.add(arr[i]);
Long cost = new Long("0");
while (pq.size() != 1)
{
long x = pq.poll();
long y = pq.poll();
cost += (x + y);
pq.add(x + y);
}
arr = null;
System.gc();
return cost;
}
public static void main (String[] args) {
FastReader sc = new FastReader();
OutputStream outputstream = System.out;
PrintWriter out = new PrintWriter(outputstream);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextLong();
System.out.println(minCost(arr, arr.length));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import heapq
from heapq import heappush, heappop
def findMinCost(prices):
heapq.heapify(prices)
cost = 0
while len(prices) > 1:
x = heappop(prices)
y = heappop(prices)
total = x + y
heappush(prices, total)
cost += total
return cost
t=int(input())
for _ in range(t):
n=int(input())
prices=list(map(int,input().split()))
print( findMinCost(prices)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
priority_queue<long long> pq;
int n;
cin>>n;
long long x;
long long sum=0;
for(int i=0;i<n;i++){
cin>>x;
x=-x;
pq.push(x);
}
long long y;
while(pq.size()!=1){
x=pq.top();
pq.pop();
y=pq.top();
pq.pop();
sum+=x+y;
pq.push(x+y);
}
cout<<-sum<<endl;
}}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: static void verticalFive(){
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
}
static void horizontalFive(){
System.out.print("* * * * *");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: def vertical5():
for i in range(0,5):
print("*",end="\n")
#print()
def horizontal5():
for i in range(0,5):
print("*",end=" ")
vertical5()
print(end="\n")
horizontal5(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
public static int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;break;}
ch*=(long)3;
}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
int Ifpossible(long x, long y){
long sum=y-x;
long ans=0;
long ch = 1;
while(ans<sum){
ans+=ch;
if(ans==sum){return 1;}
ch*=3;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers x and y, check if x can be converted to y by adding F(n) to x where F(n){n>=1} is defined as:- 1+3+9+27+81+. .3^n.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Ifpossible()</b> that takes the integer x and y as parameter.
Constraints:-
1<= x < y <= 10^9Return 1 if it is possible to convert x into y else return 0.Sample Input:-
5 7
Sample Output:-
0
Sample Input:-
3 16
Sample Output:-
1
Explanation:-
F(3) = 1 + 3 + 9 = 13
3 + 13 = 16, I have written this Solution Code:
def Ifpossible(x,y) :
result = y-x
ans = 0
ch = 1
while ans<result :
ans+=ch
if ans==result:
return 1;
ch*=3
return 0;
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sequence of three integers, check if the product of all these numbers is divisible by 20.The first line contains 3 space- separated integers a, b, and c.
<b>Constraints</b>
1 ≤ a, b, c ≤ 100Print true, if the product is divisible by 20, otherwise, print false.Sample Input 1 :
8 1 3
Sample Output 1 :
false
Explanation 1:
product = 8*1*3 = 24, which is not divisible by 20.
Sample Input 2 :
5 2 2
Sample Output 2 :
true
Explanation 2 :
product = 8*2*2 = 20, which is divisible by 20., I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int pd=1;
for(int i=0;i<3;i++){
int n=sc.nextInt();
pd*=n;
}
if(pd%20==0)
System.out.println(true);
else
System.out.println(false);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: def factorial(n):
if(n == 1):
return 1
return n * factorial(n-1)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: static int Factorial(int N)
{
if(N==0){
return 1;}
return N*Factorial(N-1);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number N.
<b>Note: </b>
The Factorial of a number is the product of an integer and all the integers below it;
e.g. factorial four ( 4! ) is equal to 24 (4*3*2*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 <i>Factorial()</i> which contains the given number N.
<b>Constraints:</b>
1 <= N <= 15
Return the factorial of the given number.Sample Input:-
5
Sample Output:-
120
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code: // n is the input number
function factorial(n) {
// write code here
// do not console.log
// return the answer as a number
if (n == 1 ) return 1;
return n * factorial(n-1)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int count=search(a,0,n-1);
System.out.println(count);
}
}
public static int search(int[] a,int l,int h){
while(l<=h){
int mid=l+(h-l)/2;
if ((mid==h||a[mid+1]==0)&&(a[mid]==1))
return mid+1;
if (a[mid]==1)
l=mid+1;
else h=mid-1;
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] == 1)
l = m;
else
h = m;
}
cout << l << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: c=int(input())
for x in range(c):
size=int(input())
s=input()
print(s.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: def minDistanceCoveredBySara(N):
if N%4==1 or N%4==2:
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new type of game in which she can jump in either the left direction or in the right direction. Also after each jump the range of her jump increases by 1 unit. i.e if starts from 1 in the next jump she has to jump 2 units then 3 units and so on.
Given the number of jumps as N, the range of the first jump to be 1. What will be the minimum distance Sara can be at from the starting point.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>minDistanceCoveredBySara()</b> that takes integer N as an argument.
Constraints:-
1 <= N <= 1000Return the minimum distance Sara can be at from the starting point.Sample Input:-
3
Sample Output:-
0
Explanation:-
First jump:- right
Second jump:- right
Third jump:- left
Total distance covered = 1+2-3 = 0
Sample Input:-
5
Sample Output:-
1, I have written this Solution Code: static int minDistanceCoveredBySara(int N){
if(N%4==1 || N%4==2){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Deque and Q queries. The task is to perform some operation on Deque according to the queries as described in input:
Note:-if deque is empty than pop operation will do nothing, and -1 will be printed as a front and rear element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push_front_pf()</b>:- that takes the deque and the integer to be added as a parameter.
<b>push_bac_pb()</b>:- that takes the deque and the integer to be added as a parameter.
<b>pop_back_ppb()</b>:- that takes the deque as parameter.
<b>front_dq()</b>:- that takes the deque as parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>
<b>Custom Input: </b>
First line of input should contain the number of queries Q. Next, Q lines should contain any of the given operations:-
For <b>push_front</b> use <b> pf x</b> where x is the element to be added
For <b>push_rear</b> use <b> pb x</b> where x is the element to be added
For <b>pop_back</b> use <b> pp_b</b>
For <b>Display Front</b> use <b>f</b>
Moreover driver code will print
Front element of deque in each push_front opertion
Last element of deque in each push_back operation
Size of deque in each pop_back operation The front_dq() function will return the element at front of your deque in a new line, if the deque is empty you just need to return -1 in the function.Sample Input:
6
push_front 2
push_front 3
push_rear 5
display_front
pop_rear
display_front
Sample Output:
3
3, I have written this Solution Code:
static void push_back_pb(Deque<Integer> dq, int x)
{
dq.add(x);
}
static void push_front_pf(Deque<Integer> dq, int x)
{
dq.addFirst(x);
}
static void pop_back_ppb(Deque<Integer> dq)
{
if(!dq.isEmpty())
dq.pollLast();
else return;
}
static int front_dq(Deque<Integer> dq)
{
if(!dq.isEmpty())
return dq.peek();
else return -1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. The task is to find the Nth Catalan number.
The first few Catalan numbers for N = 1, 2, 3, … are 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …
You can read more about Catalan numbers <a href="https://en.wikipedia.org/wiki/Catalan_number">here</a>.The first line of input contains a single integer T which denotes the number of test cases. The first line of each test case contains a single integer N.
Constraints:
1 <= T <= 100000
1 <= N <= 1000000For each test case, in a new line print the Catalan number at position N.
Since the answer can be large, print answer modulo (10^9 + 7)Sample Input:
3
5
4
10
Sample Output:
42
14
16796, I have written this Solution Code: import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
dp[0]=0;
dp[1]=1;
for (int i = 1; i <=1000000 ; i++) {
dp[i+1]=(((4*i+2)%mod)*dp[i]%mod*modPow(i+2,mod-2))%mod;
}
while(t-->0){
int n = Integer.parseInt(br.readLine());
System.out.println(dp[n]);
}
}
static long [] dp= new long [1000002];
static long mod = 1000000007;
static long modPow(long x, long n){
if(n==0) return 1;
if(n==1) return x;
if(n%2==0) return modPow((x*x)%mod,n/2)%mod;
return (x*modPow((x*x)%mod,n/2)%mod);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. The task is to find the Nth Catalan number.
The first few Catalan numbers for N = 1, 2, 3, … are 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …
You can read more about Catalan numbers <a href="https://en.wikipedia.org/wiki/Catalan_number">here</a>.The first line of input contains a single integer T which denotes the number of test cases. The first line of each test case contains a single integer N.
Constraints:
1 <= T <= 100000
1 <= N <= 1000000For each test case, in a new line print the Catalan number at position N.
Since the answer can be large, print answer modulo (10^9 + 7)Sample Input:
3
5
4
10
Sample Output:
42
14
16796, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ld long double
#define ll long long
#define pb push_back
#define endl '\n'
#define pi pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int f[N], inv[N], res[N];
int power(int a, int b){
int ans = 1;
while(b){
if(b&1)
ans = (ans*a) % mod;
b >>= 1;
a = (a*a) % mod;
}
return ans;
}
void solve(){
int n; cin >> n;
cout << res[n] << endl;
}
void testcases(){
int tt = 1;
f[0] = 1;
for(int i = 1; i < N; i++)
f[i] = (i*f[i-1]) % mod;
inv[N-1] = power(f[N-1], mod-2);
for(int i = N-2; i >= 1; i--)
inv[i] = ((i+1)*inv[i+1]) % mod;
for(int i = 1; i < N/2; i++){
res[i] = f[2*i];
res[i] = (res[i]*inv[i]) % mod;
res[i] = (res[i]*inv[i]) % mod;
res[i] = (res[i]*power(i+1, mod-2)) % mod;
}
cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Yogi and Dharam are playing a game in which one has to solve the problem in order to get out of the room. Dharam goes first and Yogi gave him a problem. Since Dharam is having problem solving it, your task is to solve the problem for him.
Problem:-
Given a range from A to B your task is to count the numbers in which the difference between adjacent digits is not more than one.The first line of input contains a single integer T. The next T lines contain two space- separated integers each containing values of A and B.
Constraints:-
1 <= T <= 100
1 <= A, B <= 1000000000000For each test case print the count of numbers in the range [A, B] such that the difference between adjacent digits is not more than one.Sample Input:-
2
10 30
1 10
Sample Output:-
6
10
Explanation:-
10, 11, 12, 21, 22, 23
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static int count =0;
public static void solve(long a ,long b,long number){
if(number>b){
return;
}
if(number>=a && number <=b){
count++;
}
long last_digit =number % 10;
number *=10;
if(last_digit !=0){
solve(a,b,number+(last_digit-1));
}
if(last_digit !=9){
solve(a,b,number+(last_digit+1));
}
solve(a,b,number+(last_digit));
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
long a = sc.nextLong();
long b =sc.nextLong();
count =0;
for(long i=1l ; i<=9l ;i++){
solve(a,b,i);
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Yogi and Dharam are playing a game in which one has to solve the problem in order to get out of the room. Dharam goes first and Yogi gave him a problem. Since Dharam is having problem solving it, your task is to solve the problem for him.
Problem:-
Given a range from A to B your task is to count the numbers in which the difference between adjacent digits is not more than one.The first line of input contains a single integer T. The next T lines contain two space- separated integers each containing values of A and B.
Constraints:-
1 <= T <= 100
1 <= A, B <= 1000000000000For each test case print the count of numbers in the range [A, B] such that the difference between adjacent digits is not more than one.Sample Input:-
2
10 30
1 10
Sample Output:-
6
10
Explanation:-
10, 11, 12, 21, 22, 23
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int cnt=0;
inline void solve(int a, int b, int p, int c, int i){
if(p>b){return;}
if(p>=a){cnt++;}
int x = p%10;
p*=10;
if(i==c){return;}
solve(a,b,p+x,c,i+1);
if(x!=9){
solve(a,b,p+x+1,c,i+1);
}
if(x!=0){
solve(a,b,p+x-1,c,i+1);
}
}
signed main(){
fast();
int t;
cin>>t;
while(t--){
int a,b;
cnt=0;
cin>>a>>b;
if(b<a){out(-1);continue;}
int ans=b;
int c=0;
while(ans){
c++;
ans/=10;
}
for(int i=1;i<=9;i++){
solve(a,b,i,c,1);
}
out(cnt);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and a queue of integers, your task is to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>ReverseK()</b>:- that takes the Queue and the integer K as parameters.
Constraints:
1 ≤ K ≤ N ≤ 10000
1 ≤ elements ≤ 10000
You need to return the modified Queue.Input 1:
5 3
1 2 3 4 5
Output 1:
3 2 1 4 5
Input 2:
5 5
1 2 3 4 5
Output 2:
5 4 3 2 1, I have written this Solution Code: static Queue<Integer> ReverseK(Queue<Integer> queue, int k) {
Stack<Integer> stack = new Stack<Integer>();
// Push the first K elements into a Stack
for (int i = 0; i < k; i++) {
stack.push(queue.peek());
queue.remove();
}
// Enqueue the contents of stack at the back
// of the queue
while (!stack.empty()) {
queue.add(stack.peek());
stack.pop();
}
// Remove the remaining elements and enqueue
// them at the end of the Queue
for (int i = 0; i < queue.size() - k; i++) {
queue.add(queue.peek());
queue.remove();
}
return queue;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a N x M integer matrix A and Q queries of form X1 Y1 X2 Y2. Print the sum of A[i][j], for X1 <= i <= X2 and Y1 <= j <= Y2.The first line contains two integer N and M, denoting the size of the matrix.
Next N line contains M integers denoting elements of the matrix.
Next line contains a single integer Q, denoting the number of queries.
Next Q lines lines four integers X1 Y1 X2 Y2, denoting the query as mentioned in problem statement
1 <= N, M <= 100
1 <= A[i][j] <= 100
1 <= Q <= 100000
1 <= X1 <= X2 <= N
1 <= Y1 <= Y2 <= MPrint Q lines containing the answer to each query.Sample Input:
2 2
1 5
2 3
3
1 1 1 1
1 1 1 2
1 1 2 2
Sample Output:
1
6
11
Explanation:
Q1: 1
Q2: 1 + 5 = 6
Q3: 1 + 5 + 2 + 3 = 11, 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) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] a = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
a[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = sc.nextInt();
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
int q = sc.nextInt();
while (q-- > 0) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int sum = 0;
sum = a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
System.out.println(sum);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a N x M integer matrix A and Q queries of form X1 Y1 X2 Y2. Print the sum of A[i][j], for X1 <= i <= X2 and Y1 <= j <= Y2.The first line contains two integer N and M, denoting the size of the matrix.
Next N line contains M integers denoting elements of the matrix.
Next line contains a single integer Q, denoting the number of queries.
Next Q lines lines four integers X1 Y1 X2 Y2, denoting the query as mentioned in problem statement
1 <= N, M <= 100
1 <= A[i][j] <= 100
1 <= Q <= 100000
1 <= X1 <= X2 <= N
1 <= Y1 <= Y2 <= MPrint Q lines containing the answer to each query.Sample Input:
2 2
1 5
2 3
3
1 1 1 1
1 1 1 2
1 1 2 2
Sample Output:
1
6
11
Explanation:
Q1: 1
Q2: 1 + 5 = 6
Q3: 1 + 5 + 2 + 3 = 11, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e2 + 5;
const int ten6 = 1e6;
const int inf = 1e9 + 9;
int a[N][N];
void solve(){
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
}
}
int q; cin >> q;
while(q--){
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << a[x2][y2] - a[x1-1][y2] - a[x2][y1-1] + a[x1-1][y1-1] << endl;
}
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader sc= new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0;
long z=0;
String[] str;
str = sc.readLine().split(" ");
a= Integer.parseInt(str[0]);
b= Integer.parseInt(str[1]);
c= Integer.parseInt(str[2]);
d= Integer.parseInt(str[3]);
BigInteger m = new BigInteger("1000000007");
BigInteger n = new BigInteger("1000000006");
BigInteger zero = new BigInteger("0");
BigInteger ans, y;
if(d==0){
z =1;
}else{
z = (long)Math.pow(c, d);
}
if(b==0){
y= zero;
}else{
y = (BigInteger.valueOf(b)).modPow((BigInteger.valueOf(z)), n);
}
if(y == zero){
System.out.println("1");
}else if(a==0){
System.out.println("0");
}else{
ans = (BigInteger.valueOf(a)).modPow(y, m);
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.