Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
int n = Integer.parseInt(br.readLine());
String ans = check(n);
StringBuffer sbr = new StringBuffer(ans);
System.out.println(sbr.reverse());
}
}
static String check(int x)
{
String ans="";
while(x>0)
{
switch(x%4)
{
case 1:ans +="2"; break;
case 2:ans +="3"; break;
case 3:ans+="5"; break;
case 0:ans+="7"; break;
}
if(x%4==0)
x--;
x/=4;
}
return ans;
}
}, 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 number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: def nthprimedigitsnumber(number):
num=""
while (number > 0):
rem = number % 4
if (rem == 1):
num += '2'
if (rem == 2):
num += '3'
if (rem == 3):
num += '5'
if (rem == 0):
num += '7'
if (number % 4 == 0):
number = number - 1
number = number // 4
return num[::-1]
T=int(input())
for i in range(T):
number = int(input())
print(nthprimedigitsnumber(number)), 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 find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void nthprimedigitsnumber(long long n)
{
long long len = 1;
long long prev_count = 0;
while (true) {
long long curr_count = prev_count + pow(4, len);
if (prev_count < n && curr_count >= n)
break;
len++;
prev_count = curr_count;
}
for (int i = 1; i <= len; i++) {
for (long long j = 1; j <= 4; j++) {
if (prev_count + pow(4, len - i) < n)
prev_count += pow(4, len - i);
else {
if (j == 1)
cout << "2";
else if (j == 2)
cout << "3";
else if (j == 3)
cout << "5";
else if (j == 4)
cout << "7";
break;
}
}
}
cout << endl;
}
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
nthprimedigitsnumber(n);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] of size N containing non-negative integers. You have to move all 0's to the end of array while maintaining the relative order of the non-zero elements.
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.The first line of input line contains T, denoting the number of testcases. Each test cases contains 2 lines. First-line contains N, size of array. Second-line contains elements of array separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to print the updated array.Sample Input:
2
5
0 1 0 3 12
8
0 0 0 0 1 2 3 4
Sample Output:
1 3 12 0 0
1 2 3 4 0 0 0 0, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t;
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 j=-1;
for(int i=0;i<n;i++){
if(a[i]==0 && j==-1){
j=i;
}
if(j!=-1 && a[i]!=0){
a[j]=a[i];
a[i]=0;
j++;
}
}
for(int i=0;i<n;i++){
System.out.print(a[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 array A[] of size N containing non-negative integers. You have to move all 0's to the end of array while maintaining the relative order of the non-zero elements.
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.The first line of input line contains T, denoting the number of testcases. Each test cases contains 2 lines. First-line contains N, size of array. Second-line contains elements of array separated by space.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to print the updated array.Sample Input:
2
5
0 1 0 3 12
8
0 0 0 0 1 2 3 4
Sample Output:
1 3 12 0 0
1 2 3 4 0 0 0 0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n;
int a[n];
int cnt=0;
for(int i=0;i<n;i++){
cin>>a[i];\
if(a[i]==0){cnt++;}
}
for(int i=0;i<n;i++){
if(a[i]!=0){
cout<<a[i]<<" ";
}
}
while(cnt--){
cout<<0<<" ";
}
cout<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. For all pairs (i, j) (1 <= i < j <= N), find the maximum value of abs(A<sub>i</sub> - A<sub>j</sub>) in the array.The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
2 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print the maximum value of abs(A<sub>i</sub> - A<sub>j</sub>) in the array.Sample Input:
5
7 9 4 1 8
Sample Output:
8, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
sort(a.begin(), a.end());
cout << a[n - 1] - a[0];
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. The task is to check whether N is a <b>Special-M-visor</b> or not.
<b>Special-M-visor</b>: A number is called Special-m-visor if it has exactly M even divisors for a given N.<b>User Task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>checkSpecial_M_Visor()</b>. Where you will get N and M as a parameter.
Constraints:
1 <= N <= 10^6
0 <= M <= 10^6Return "Yes" without quotes if the number N is a <b>Special- M- visor</b> else return "No"Sample Input:-
4 2
Sample Output:-
Yes
Explanation:- 2 and 4 are the only even divisors of 4
Sample Input:-
8 5
Sample Output:-
No, I have written this Solution Code:
def checkSpecial_M_Visor(N,M) :
# Final result of summation of divisors
i=2;
count=0
while i<=N :
if(N%i==0):
count=count+1
i=i+2
if(count == M):
return "Yes"
return "No"
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. The task is to check whether N is a <b>Special-M-visor</b> or not.
<b>Special-M-visor</b>: A number is called Special-m-visor if it has exactly M even divisors for a given N.<b>User Task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>checkSpecial_M_Visor()</b>. Where you will get N and M as a parameter.
Constraints:
1 <= N <= 10^6
0 <= M <= 10^6Return "Yes" without quotes if the number N is a <b>Special- M- visor</b> else return "No"Sample Input:-
4 2
Sample Output:-
Yes
Explanation:- 2 and 4 are the only even divisors of 4
Sample Input:-
8 5
Sample Output:-
No, I have written this Solution Code:
static String checkSpecial_M_Visor(int N, int M)
{
int temp=0;
for(int i = 2; i <=N; i+=2)
{
if(N%i==0)
temp++;
}
if(temp==M)
return "Yes";
else return "No";
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. The task is to check whether N is a <b>Special-M-visor</b> or not.
<b>Special-M-visor</b>: A number is called Special-m-visor if it has exactly M even divisors for a given N.<b>User Task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>checkSpecial_M_Visor()</b>. Where you will get N and M as a parameter.
Constraints:
1 <= N <= 10^6
0 <= M <= 10^6Return "Yes" without quotes if the number N is a <b>Special- M- visor</b> else return "No"Sample Input:-
4 2
Sample Output:-
Yes
Explanation:- 2 and 4 are the only even divisors of 4
Sample Input:-
8 5
Sample Output:-
No, I have written this Solution Code:
string checkSpecial_M_Visor(int N,int M){
int count=0;
for(int i=2;i<=N;i+=2){
if(N%i==0){
count++;
}
}
if(count==M){return "Yes";}
return "No";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. The task is to check whether N is a <b>Special-M-visor</b> or not.
<b>Special-M-visor</b>: A number is called Special-m-visor if it has exactly M even divisors for a given N.<b>User Task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>checkSpecial_M_Visor()</b>. Where you will get N and M as a parameter.
Constraints:
1 <= N <= 10^6
0 <= M <= 10^6Return "Yes" without quotes if the number N is a <b>Special- M- visor</b> else return "No"Sample Input:-
4 2
Sample Output:-
Yes
Explanation:- 2 and 4 are the only even divisors of 4
Sample Input:-
8 5
Sample Output:-
No, I have written this Solution Code: const char* checkSpecial_M_Visor(int N,int M){
int count=0;
for(int i=2;i<=N;i+=2){
if(N%i==0){
count++;
}
}
char* ans;
if(count==M){return "Yes";}
return "No";
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>We all remember phone numbers, account numbers, etc. by creating a pattern in our brains, but forget them entirely if we fail to recall the initial numbers or characters. </i>
Saloni is highly engrossed with studying the number patterns in the Vedic math. She knows there exists a golden sequence of numbers which can entirely change the understanding of the technology. Since she has memorised the golden sequence once when she was 5, <b>she will identify the golden sequence as soon as it appears in front of her as the prefix of any sequence</b>.
So, to recall the golden sequence, she first creates an empty sequence S and chooses a number X. Now, in each move she appends an integer randomly selected from the integers between 1 to X, at the front of the sequence S. As soon as the golden sequence appears as the prefix of the sequence S, she recalls the golden sequence.
Now given the golden sequence G, find the expected length of the sequence S when Saloni recalls the golden sequence.The first line of the input contains two integer N and X, the length of the golden sequence and the integer chosen by Saloni.
The second line of the input contains N space separated integers denoting the golden sequence G.
Constraints
1 <= N <= 200000
1 <= G[i] <= 1000000
1 <= X <= 1000000
If she can never recall the golden sequence under the given constraints, output -1. Else, if the answer is of the form p/q, output it as (p*r)%1000000007 where r is modulo inverse of q with respect to 1000000007.Sample Input
1 11
7
Sample Output
11
Explanation: Saloni keeps on generating a random integer between 1 to 11 until she gets the number 7. Using simple concepts of geometric distribution, we know the expected length of sequence will be 11.
Sample Input
4 12
7 9 7 9
Sample Output
20880
Sample Input
3 14
12 15 17
Sample Output
-1
, 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
int Z[N];
int powmod(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 getZarr(vector<int>& vect){
int n = vect.size();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && vect[R-L] == vect[R])
R++;
Z[i] = R-L;
R--;
}
else{
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else{
L = i;
while (R<n && vect[R-L] == vect[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
void solve(){
int n, x; cin>>n>>x;
vector<int> vect(n);
bool fl = true;
For(i, 0, n){
cin>>vect[i];
if(vect[i] > x){
fl = false;
}
}
if(!fl){
cout<<-1;
return;
}
reverse(all(vect));
getZarr(vect);
int ans = powmod(x, n);
for(int i = 1; i < n; i++){
if(Z[i]+i == n)
ans = (ans + powmod(x, Z[i])) % MOD;
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int toDigit(char ch)
{
return (ch - '0');
}
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = s.length();
long prev = toDigit(s.charAt(0));
long res = prev;
long current = 0L;
for (int i = 1; i < n; i++){
long numi = toDigit(s.charAt(i));
current = ((i + 1) * numi + 10 * prev)%1000000007;
res =(res+current)%1000000007;
prev = current;
}
System.out.print(res);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, I have written this Solution Code: s = input().strip()
ln = len(s)
prev = int(s[0])
sm = prev
for i in range(1,ln):
nxt = (i+1)*int(s[i]) + 10 * prev
sm += nxt%1000000007
prev = nxt%1000000007
print(sm%1000000007), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, 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 = 2e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
string s;
cin >> s;
int ans = 0, suf = 0;
for(int i = 0; i < (int)s.length(); i++){
suf = (suf*10 + (s[i]-'0')*(i+1)) % mod;
ans = (ans + suf) % mod;
}
cout << ans << endl;
}
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: Given an array of N elements and an integer D. Your task is to rotate the array D times in a circular manner from the right to left direction. Consider the examples for better understanding:-
Try to do without creating another arrayUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>rotate()</b> that takes the array, size of the array, and the integer d as a parameter.
Constraints:
1 <= T <= 25
2 <= N <= 10^4
1<=D<=10^5
1 <= A[i] <= 10^5For each test case, you just need to rotate the array by D times. The driver code will prin the rotated array in a new line.Sample Input:
2
8
4
1 2 3 4 5 6 7 8
10
3
1 2 3 4 5 6 7 8 9 10
Sample Output:
5 6 7 8 1 2 3 4
4 5 6 7 8 9 10 1 2 3
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
After the first rotation, the array becomes 2 3 4 5 6 7 8 1
After the second rotation, the array becomes 3 4 5 6 7 8 1 2
After the third rotation, the array becomes 4 5 6 7 8 1 2 3
After the fourth rotation, the array becomes 5 6 7 8 1 2 3 4
Hence the final result: 5 6 7 8 1 2 3 4, I have written this Solution Code: public static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static void rotate(int arr[], int n, int d){
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;
boolean win=true;
while (win) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
long int n, el;
cin>>n>>el;
long int arr[n+1];
for(long int i=0; i<n; i++) {
cin>>arr[i];
}
arr[n] = el;
for(long int i=0; i<=n; i++) {
cout<<arr[i];
if (i != n) {
cout<<" ";
}
else if(t != 0) {
cout<<endl;
}
}
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: t=int(input())
while t>0:
t-=1
li = list(map(int,input().strip().split()))
n=li[0]
num=li[1]
a= list(map(int,input().strip().split()))
a.insert(len(a),num)
for i in a:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n =Integer.parseInt(br.readLine().trim());
while(n-->0){
String str[]=br.readLine().trim().split(" ");
String newel =br.readLine().trim()+" "+str[1];
System.out.println(newel);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a List of tuples as input, where the First Element is the Name and the second one, is the roll number in the tuple, your job is to sort the function on the Roll number using Python sorted function and lambda function.
eg: [('shalini', 35), ('Suri', 54), ('Aman', 56), ('Anu', 78)]In the first line, enter the number of entries you want to add
In the second line , enter the name and roll number with space separated
eg :
2
Aman 32
Suri 41Print the sorted list of tuples
eg: [('Suri', 41), ('Aman', 32)]Sample example:
Input:
3
Aman 21
Suri 3
darani 45
Output:
[('Suri', 3), ('Aman', 21),('darani', 45)]
, I have written this Solution Code: def sort_second(pairs):
sorted_pairs = sorted(pairs, key=lambda x: x[1])
return sorted_pairs
n = int(input())
pairs = []
for i in range(n):
ip = list(input().split())
ip[1] = int(ip[1])
ip = tuple(ip)
pairs.append(ip)
print(sort_second(pairs)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <i>We all remember phone numbers, account numbers, etc. by creating a pattern in our brains, but forget them entirely if we fail to recall the initial numbers or characters. </i>
Saloni is highly engrossed with studying the number patterns in the Vedic math. She knows there exists a golden sequence of numbers which can entirely change the understanding of the technology. Since she has memorised the golden sequence once when she was 5, <b>she will identify the golden sequence as soon as it appears in front of her as the prefix of any sequence</b>.
So, to recall the golden sequence, she first creates an empty sequence S and chooses a number X. Now, in each move she appends an integer randomly selected from the integers between 1 to X, at the front of the sequence S. As soon as the golden sequence appears as the prefix of the sequence S, she recalls the golden sequence.
Now given the golden sequence G, find the expected length of the sequence S when Saloni recalls the golden sequence.The first line of the input contains two integer N and X, the length of the golden sequence and the integer chosen by Saloni.
The second line of the input contains N space separated integers denoting the golden sequence G.
Constraints
1 <= N <= 200000
1 <= G[i] <= 1000000
1 <= X <= 1000000
If she can never recall the golden sequence under the given constraints, output -1. Else, if the answer is of the form p/q, output it as (p*r)%1000000007 where r is modulo inverse of q with respect to 1000000007.Sample Input
1 11
7
Sample Output
11
Explanation: Saloni keeps on generating a random integer between 1 to 11 until she gets the number 7. Using simple concepts of geometric distribution, we know the expected length of sequence will be 11.
Sample Input
4 12
7 9 7 9
Sample Output
20880
Sample Input
3 14
12 15 17
Sample Output
-1
, 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
int Z[N];
int powmod(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 getZarr(vector<int>& vect){
int n = vect.size();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && vect[R-L] == vect[R])
R++;
Z[i] = R-L;
R--;
}
else{
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else{
L = i;
while (R<n && vect[R-L] == vect[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
void solve(){
int n, x; cin>>n>>x;
vector<int> vect(n);
bool fl = true;
For(i, 0, n){
cin>>vect[i];
if(vect[i] > x){
fl = false;
}
}
if(!fl){
cout<<-1;
return;
}
reverse(all(vect));
getZarr(vect);
int ans = powmod(x, n);
for(int i = 1; i < n; i++){
if(Z[i]+i == n)
ans = (ans + powmod(x, Z[i])) % 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: You are given N intervals, the i<sup>th</sup> of them being [A<sub>i</sub>, B<sub>i</sub>], where A<sub>i</sub> and B<sub>i</sub> are positive integers. Let the union of all these intervals be S. It is easy to see that S can be uniquely represented as an union of disjoint closed intervals. Your task is to find the sum of the lengths of the disjoint closed intervals that comprises S.
For example, if you are given the intervals: [1, 3], [2, 4], [5, 7] and [7, 8], then S can be uniquely represented as the union of disjoint intervals [1, 4] and [5, 8]. In this case, the answer will be 6, as (4 - 1) + (8 - 5) = 6.The first line of the input consists of a single integer N – the number of intervals.
Then N lines follow, the i<sup>th</sup> line containing two space-separated integers A<sub>i</sub> and B<sub>i</sub>.
<b> Constraints: </b>
1 ≤ N ≤ 10<sup>4</sup>
1 ≤ A<sub>i</sub> < B<sub>i</sub> ≤ 2×10<sup>5</sup>Print a single integer, the sum of the lengths of the disjoint intervals of S.Sample Input 1:
3
1 3
3 4
6 7
Sample Output 1:
4
Sample Explanation 1:
Here, S can be uniquely written as union of disjoint intervals [1, 4] and [6, 7]. Thus, answer is (4 - 1) + (7 - 6) = 4.
Sample Input 2:
4
9 12
8 10
5 6
13 15
Sample Output 2:
7
Sample Explanation 2:
Here, S can be uniquely written as union of disjoint intervals [5, 6], [8, 12] and [13, 15]. Thus, answer is (6 - 5) + (12 - 8) + (15 - 13) = 7.
, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << (a) << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (auto i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353; // 1e9 + 7;
const ll N = 4e5 + 100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int arr[N];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(n);
FOR (i, 1, n)
{
readb(a, b);
a *= 2, b *= 2;
arr[a]++;
arr[b + 1]--;
}
int ans = 0, sum = 0, cur = 0;
FOR (i, 0, N - 2)
{
sum += arr[i];
if (sum) cur++;
else ans += cur/2, cur = 0;
}
print(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is called Silly if it can be represented as the sum of the square of consecutive natural numbers starting from 1.
For a given number N, find the closest silly number.<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>SillyNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the closest Silly number.
Note:- If more than one answer exists return the minimum one.Sample Input:-
18
Sample Output:-
14
Explanation:-
1*1 + 2*2 + 3*3 = 14
Sample Input:-
2
Sample Output:-
1, I have written this Solution Code: static int SillyNumber(int N){
int sum=0;
int x=1;
while(sum<N){
sum+=x*x;
x++;
}
x--;
if(sum-N < N-(sum-x*x)){
return sum;
}
else{
return sum-x*x;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is called Silly if it can be represented as the sum of the square of consecutive natural numbers starting from 1.
For a given number N, find the closest silly number.<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>SillyNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the closest Silly number.
Note:- If more than one answer exists return the minimum one.Sample Input:-
18
Sample Output:-
14
Explanation:-
1*1 + 2*2 + 3*3 = 14
Sample Input:-
2
Sample Output:-
1, I have written this Solution Code: int SillyNumber(int N){
int sum=0;
int x=1;
while(sum<N){
sum+=x*x;
x++;
}
x--;
if(sum-N < N-(sum-x*x)){
return sum;
}
else{
return sum-x*x;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is called Silly if it can be represented as the sum of the square of consecutive natural numbers starting from 1.
For a given number N, find the closest silly number.<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>SillyNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the closest Silly number.
Note:- If more than one answer exists return the minimum one.Sample Input:-
18
Sample Output:-
14
Explanation:-
1*1 + 2*2 + 3*3 = 14
Sample Input:-
2
Sample Output:-
1, I have written this Solution Code: int SillyNumber(int N){
int sum=0;
int x=1;
while(sum<N){
sum+=x*x;
x++;
}
x--;
if(sum-N < N-(sum-x*x)){
return sum;
}
else{
return sum-x*x;
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number is called Silly if it can be represented as the sum of the square of consecutive natural numbers starting from 1.
For a given number N, find the closest silly number.<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>SillyNumber()</b> that takes integer N as argument.
Constraints:-
1 <= N <= 100000Return the closest Silly number.
Note:- If more than one answer exists return the minimum one.Sample Input:-
18
Sample Output:-
14
Explanation:-
1*1 + 2*2 + 3*3 = 14
Sample Input:-
2
Sample Output:-
1, I have written this Solution Code: def SillyNumber(N):
sum=0
x=1
while sum<N:
sum=sum+x*x
x=x+1
x=x-1
if (sum-N) < (N-(sum-x*x)):
return sum;
else:
return sum - x*x
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
if(t%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
catch (Exception e){
return;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch(num % 2)
{
case 0:
printf("Even");
break;
/* Else if n%2 == 1 */
case 1:
printf("Odd");
break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array A of size N is called magical if:
1. Each element of the array is a positive integer not exceeding M, that is, 1 ≤ A<sub>i</sub> ≤ M for each i.
2. For each i such that 1 ≤ i ≤ N, define f(i) = A<sub>1</sub> | A<sub>2</sub> | ... A<sub>i</sub>, where x | y denotes the bitwise OR of x and y. Then, f(1) = f(2) = ... f(N) must hold.
Your task is to calculate the number of magical arrays of size N, modulo 998244353.The input consists of two space-separated integers N and M.
<b> Constraints: </b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ M ≤ 10<sup>5</sup>Print a single integer – the number of magical arrays modulo 998244353.Sample Input 1:
2 3
Sample Output 1:
5
Sample Explanation:
The magical arrays are: [1, 1], [2, 2], [3, 3], [3, 1], [3, 2].
Sample Input 2:
1 50
Sample Output 2:
50
Sample Input 3:
707 707
Sample Output 3:
687062898, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
readb(n, m);
int ans = 0;
FOR (i, 1, m)
{
int x = (1 << (__builtin_popcountll(i))) - 1;
ans = (ans + power(x, n - 1)) % mod;
}
cout << ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
String[] s=br.readLine().trim().split(" ");
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i]);
int max=Integer.MIN_VALUE,c=0;
for(int i=0;i<arr.length;i++){
if(max==arr[i])
c++;
if(max<arr[i]){
max=arr[i];
c=1;
}
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: n=int(input())
a=list(map(int,input().split()))
print(a.count(max(a))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
vector<int> a(101, 0);
For(i, 0, n){
int x; cin>>x;
a[x]++;
}
for(int i=100; i>=1; i--){
if(a[i]){
cout<<a[i];
return;
}
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings A and B. The task is to find if the string A can be obtained by rotating the string B by 2 places.The first line of the input contains the string A.
The second line of the input contains the string B.
Constaint:-
1 <= |A|, |B| <= 100Print 1 if the string A can be obtained by rotating string B by two places, else print 0.Sample Input:
amazon
azonam
Sample Output:
1
Input: string1 = “amazon”, string2 = “azonam”
Output: Yes
// rotated anti-clockwise
Input: string1 = “amazon”, string2 = “onamaz”
Output: Yes
// rotated clockwise, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String string1 = rd.readLine();
String string2 = rd.readLine();
int size1 = string1.length();
int size2 = string2.length();
int result = check(string1, string2, size1, size2);
System.out.println(result);
}
public static int check(String string1, String string2, int size1, int size2){
boolean flag = true;
if(size1 != size2){
return 0;
}
if(size1 < 2){
if(string1.equals(string2)){
return 1;
}else{
return 0;
}
}
for(int x = 0; x < size2; x++){
char c = string2.charAt((x + size2 - 2) % size2);
char c3 = string1.charAt(x);
if(c != c3){
flag = false;
break;
}
}
if(flag == false){
for(int x = 0; x < size2; x++){
char c2 = string2.charAt((x + 2) % size2);
char c3 = string1.charAt(x);
if(c2 != c3){
flag = false;
break;
}
}
}
return (flag ? 1 : 0);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings A and B. The task is to find if the string A can be obtained by rotating the string B by 2 places.The first line of the input contains the string A.
The second line of the input contains the string B.
Constaint:-
1 <= |A|, |B| <= 100Print 1 if the string A can be obtained by rotating string B by two places, else print 0.Sample Input:
amazon
azonam
Sample Output:
1
Input: string1 = “amazon”, string2 = “azonam”
Output: Yes
// rotated anti-clockwise
Input: string1 = “amazon”, string2 = “onamaz”
Output: Yes
// rotated clockwise, 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
string rotate(int x,string s)
{
if(x>0)
{
string p="";
int ns=s.size();
for(int i=2;i<=ns+1;i++)
{
int j=(i+ns*5)%ns;
p+=s[j];
}
return p;
}else
{
x*=-1;
string p="";
int ns=s.size();
for(int i=-4;i<ns-4;i++)
{
int j=(i+ns*5)%ns;
p+=s[j];
}
return p;
}
}
signed main()
{
string a,b;
cin>>a>>b;
int na=a.size();
int nb=b.size();
if(na!=nb) {
cout<<0<<endl;
}else
{
a=rotate(2,a);
// cout<<a<<endl;
if(a==b) cout<<1<<endl;
else
{
a=rotate(-4,a);
if(a==b) cout<<1<<endl;
else cout<<0<<endl;
}
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings A and B. The task is to find if the string A can be obtained by rotating the string B by 2 places.The first line of the input contains the string A.
The second line of the input contains the string B.
Constaint:-
1 <= |A|, |B| <= 100Print 1 if the string A can be obtained by rotating string B by two places, else print 0.Sample Input:
amazon
azonam
Sample Output:
1
Input: string1 = “amazon”, string2 = “azonam”
Output: Yes
// rotated anti-clockwise
Input: string1 = “amazon”, string2 = “onamaz”
Output: Yes
// rotated clockwise, I have written this Solution Code: a=input()
b=input()
if a[0]==b[len(b)-2] and a[1]==b[len(b)-1]:
print(1)
else:print(0), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<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>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code: static int numberOfDiagonal(int N){
if(N<=3){return 0;}
return (N*(N-3))/2;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<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>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code:
int numberOfDiagonals(int n){
if(n<=3){return 0;}
return (n*(n-3))/2;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<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>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code: def numberOfDiagonals(n):
if n <=3:
return 0
return (n*(n-3))//2
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<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>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code:
int numberOfDiagonals(int n){
if(n<=3){return 0;}
return (n*(n-3))/2;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine().trim();
char[] express = s.toCharArray();
StringBuilder ans = new StringBuilder("");
Stack<Character> stack = new Stack<>();
for(int i=0; i<express.length; i++){
if(express[i]>=65 && express[i]<=90){
ans.append(express[i]);
}else if(express[i]==')'){
while(stack.peek()!='('){
ans.append(stack.peek());
stack.pop();
}
stack.pop();
}else if(stack.empty() || express[i]=='(' || precedence(express[i])>=precedence(stack.peek())){
stack.push(express[i]);
}else{
while(!stack.empty() && stack.peek()!='(' && precedence(stack.peek())<=express[i]){
ans.append(stack.peek());
stack.pop();
}
stack.push(express[i]);
}
}
while(!stack.empty()){
ans.append(stack.peek());
stack.pop();
}
System.out.println(ans);
}
static int precedence(char operator){
int precedenceValue;
if(operator=='+' || operator=='-')
precedenceValue = 1;
else if(operator=='*' || operator=='/')
precedenceValue = 2;
else if(operator == '^')
precedenceValue = 3;
else
precedenceValue = -1;
return precedenceValue;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: string=input()
stack=list()
preced={'+':1,'-':1,'*':2,'/':2,'^':3}
for i in string:
if i!=')':
if i=='(':
stack.append(i)
elif i in ['*','+','/','-','^']:
while(stack and stack[-1]!='(' and preced[i]<=preced[stack[-1]]):
print(stack.pop(),end="")
stack.append(i)
else:
print(i,end="")
else:
while(stack and stack[-1]!='('):
print(stack.pop(),end="")
stack.pop()
while(stack):
print(stack.pop(),end=""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an infix expression, your task is to convert it into postfix.
<b>Infix expression</b>: The expression of the form a operator b. When an operator is in- between every pair of operands.
<b>Postfix expression</b>: The expression of the form a b operator. When an operator is followed for every pair of operands.
Valid operators are +, - , *, /, ^. Each operand is an uppercase english alphabet. Infix expression may contains parenthesis as '(' and ')'.
See example for better understanding.The input contains a single string of infix expression.
<b>Constraints:-</b>
1 <= |String| <= 40Output the Postfix expression.Sample Input:-
((A-(B/C))*((A/K)-L))
Sample Output:-
ABC/-AK/L-*
Sample Input:-
A+B
Sample Output:-
AB+, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
//Function to return precedence of operators
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
// The main function to convert infix expression
//to postfix expression
void infixToPostfix(string s)
{
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
for(int i = 0; i < l; i++)
{
// If the scanned character is an operand, add it to output string.
if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))
ns+=s[i];
// If the scanned character is an ‘(‘, push it to the stack.
else if(s[i] == '(')
st.push('(');
// If the scanned character is an ‘)’, pop and to output string from the stack
// until an ‘(‘ is encountered.
else if(s[i] == ')')
{
while(st.top() != 'N' && st.top() != '(')
{
char c = st.top();
st.pop();
ns += c;
}
if(st.top() == '(')
{
char c = st.top();
st.pop();
}
}
//If an operator is scanned
else{
while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
//Pop all the remaining elements from the stack
while(st.top() != 'N')
{
char c = st.top();
st.pop();
ns += c;
}
cout << ns << endl;
}
//Driver program to test above functions
int main()
{
string exp;
cin>>exp;
infixToPostfix(exp);
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: To enter Todo's amusement park, there are M counters. Total N people want to enter the amusement park, each of whom will line up in front of one of the counters. Any counter worker gets angry if the number of people lining up in front of him is at least 2 more than the number of people lining up in front of any of its neighbouring counters. The worker at the K-th counter works the fastest.
Find the maximum number of people that can line up in front of the K-th counter such that each counter gets at least one person and no counter worker is angry.Input contains three integers M, N and K.
Constraints:
1 <= N <= 1000000000
1 <= M <= N
1 <= K <= MPrint the maximum number of people that can line up in front of the Kth counter such that each counter gets at least one person and no counter worker is angry.Sample Input 1
3 3 1
Sample Output 1
1
Explanation: Optimal Arrangement is 1 1 1.
Sample Input 2
3 6 1
Sample Output
3
Explanation: Optimal Arrangement is 3 2 1., I have written this Solution Code: import java.io.*;
class Main {
public static void main (String[] args) throws NumberFormatException, IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] s = in.readLine().split(" ");
in.close();
int M = Integer.parseInt(s[0]);
int N = Integer.parseInt(s[1]);
int K = Integer.parseInt(s[2]);
if(M>=N)
System.out.println(1);
else if(M == 1)
System.out.println(N);
else
System.out.println(countersAndLines(M, N, K));
}
static int countersAndLines (int m, int n, int k){
int start = n/m, end = (n/2)+1;
while(start<=end){
int mid = start+(end-start)/2;
long minPeople = option(mid, k) + option(mid, m-k+1) - mid;
if(minPeople == n)
return mid;
else if(minPeople < n)
start = mid+1;
else
end = mid-1;
}
return end;
}
static long sum (int m, int k, int i){
long sum = i;
int temp = --i;
for(int j = k-1; j>=1; j--){
if(i <= 1)
sum += 1;
else{
sum += i;
i--;
}
}
for(int j = k+1; j<=m; j++){
if(temp <= 1)
sum += 1;
else{
sum += temp;
temp--;
}
}
return sum;
}
static long option(int i, int k){
long x = k;
if(x > i)
x = i;
k -= x;
long y = k + x * (2 * i - x + 1) / 2;
return y;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: To enter Todo's amusement park, there are M counters. Total N people want to enter the amusement park, each of whom will line up in front of one of the counters. Any counter worker gets angry if the number of people lining up in front of him is at least 2 more than the number of people lining up in front of any of its neighbouring counters. The worker at the K-th counter works the fastest.
Find the maximum number of people that can line up in front of the K-th counter such that each counter gets at least one person and no counter worker is angry.Input contains three integers M, N and K.
Constraints:
1 <= N <= 1000000000
1 <= M <= N
1 <= K <= MPrint the maximum number of people that can line up in front of the Kth counter such that each counter gets at least one person and no counter worker is angry.Sample Input 1
3 3 1
Sample Output 1
1
Explanation: Optimal Arrangement is 1 1 1.
Sample Input 2
3 6 1
Sample Output
3
Explanation: Optimal Arrangement is 3 2 1., I have written this Solution Code: def get(ed, cnt):
d=cnt
if d>ed:
d=ed
cnt -=d
return cnt+d*(2*ed-d+1)/2
n,p,k = map(int,input().split())
l=1
r=10**9+10
while l+1<r:
m = (l+r)//2
if get(m,k) + get(m,n-k+1)-m>p:
r=m
else:
l=m
print(l), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: To enter Todo's amusement park, there are M counters. Total N people want to enter the amusement park, each of whom will line up in front of one of the counters. Any counter worker gets angry if the number of people lining up in front of him is at least 2 more than the number of people lining up in front of any of its neighbouring counters. The worker at the K-th counter works the fastest.
Find the maximum number of people that can line up in front of the K-th counter such that each counter gets at least one person and no counter worker is angry.Input contains three integers M, N and K.
Constraints:
1 <= N <= 1000000000
1 <= M <= N
1 <= K <= MPrint the maximum number of people that can line up in front of the Kth counter such that each counter gets at least one person and no counter worker is angry.Sample Input 1
3 3 1
Sample Output 1
1
Explanation: Optimal Arrangement is 1 1 1.
Sample Input 2
3 6 1
Sample Output
3
Explanation: Optimal Arrangement is 3 2 1., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
ll get(ll ed, ll cnt){
ll d = cnt;
if (d > ed) d = ed;
cnt -= d;
return cnt + d * (2 * ed - d + 1) / 2;
}
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
ll n, p, k; cin >> n >> p >> k;
ll l = 1, r = 1e9 + 10;
while(l + 1 < r){
ll m = (l + r) / 2;
if ( ull(get(m, k)) + get(m, n - k + 1) - m > p)
r = m;
else l = m;
}
cout << l << endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: static void Icecreams (int N, int D){
int x=N;
while(D-->0){
x-=x/2;
x*=3;
}
System.out.println(x);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
cout << x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
printf("%d", x);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<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>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: def Icecreams(N,D):
ans = N
while D > 0:
ans = ans - ans//2
ans = ans*3
D = D-1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.print(nonRepeatChar(s));
}
static int nonRepeatChar(String s){
char count[] = new char[256];
for(int i=0; i< s.length(); i++){
count[s.charAt(i)]++;
}
for (int i=0; i<s.length(); i++) {
if (count[s.charAt(i)]==1){
return i;
}
}
return -1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: from collections import defaultdict
s=input()
d=defaultdict(int)
for i in s:
d[i]+=1
ans=-1
for i in range(len(s)):
if(d[s[i]]==1):
ans=i
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string s, find the first non-repeating character in the string and return its index. If it does not exist, return -1.First line of the input contains the string s.
<b>Constraints</b>
1<= s. length <= 100000Print the index of the first non- repeating character in a stringInput
s = "newtonschool"
Output
1
Explanation
"e" is the first non- repeating character in a string, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-10 12:51:16
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
int firstUniqChar(string s) {
map<char, int> charCount;
int len = s.length();
for (int i = 0; i < len; i++) {
charCount[s[i]]++;
}
for (int i = 0; i < len; i++) {
if (charCount[s[i]] == 1)
return i;
}
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
cin>>str;
cout<<firstUniqChar(str)<<"\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: 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: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int size = Integer.parseInt(line);
String str = br.readLine();
String[] strArray = str.split(" ");
int[] array = new int[size];
for (int i = -0; i < size; i++) {
array[i] = Integer.parseInt(strArray[i]);
}
int count = largestSubarray(array,size);
System.out.println(count);
}
static int largestSubarray(int[] array,int size){
int count = -1;
int sum = 0;
Map<Integer,Integer> mymap = new HashMap<>();
mymap.put(0,-1);
for(int i=0; i<array.length; i++){
sum += array[i];
if(mymap.containsKey(sum)){
count = Math.max(count, i-mymap.get(sum));
}
else{
mymap.put(sum,i);
}
}
if(count > 0){
return count;
}
else{
return -1;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: def maxLen(arr,n,k):
mydict = dict()
sum = 0
maxLen = 0
for i in range(n):
sum += arr[i]
if (sum == k):
maxLen = i + 1
elif (sum - k) in mydict:
maxLen = max(maxLen, i - mydict[sum - k])
if sum not in mydict:
mydict[sum] = i
return maxLen
n=int(input())
arr=input().split()
for i in range(0,n):
arr[i]=int(arr[i])
max_len=maxLen(arr,n,0)
if(max_len==0):
print ("-1")
else:
print (max_len), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unordered_map<long long,int> m;
int n,k;
cin>>n;
long a[n];
int ans=-1;
for(int i=0;i<n;i++){cin>>a[i];if(a[i]==0){ans=1;}}
long long sum=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==0){ans=max(i+1,ans);}
if(m.find(sum)==m.end()){m[sum]=i;}
else{
ans=max(i-m[sum],ans);
}
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer n, For each i from 1 to N if i is even print "<b>even</b>" else print "<b>odd</b>".<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>For_Loop()</b> that takes the integer n as parameter.
</b>Constraints:</b>
1 <= n <= 100
<b>Note:</b>
<i>But there is a catch here given user function has already code in it which may or may not be correct, now you need to figure out these and correct if it is required</i>Print even or odd for each i separated by white spaces.Sample Input:
5
Sample Output:
odd even odd even odd
Sample Input:
2
Sample Output:
odd even, I have written this Solution Code: public static void For_Loop(int n){
for(int i=0;i<n;i++){
if(i%2==1){System.out.print("even ");}
else{
System.out.print("odd ");
}
}
}, 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: 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: Newton being a student of science gifted an <b>nXn</b> matrix to his girlfriend on her birthday and challenge her to solve the matrix. The matrix consists of natural numbers up to 500(1 ≤ n ≤ 500). Newton instructed a few things about matrix to his girlfriend. He said there are two types of symbols on the matrix( L and R ), you need to rotate the matrix 90 degrees to the left for every L symbol and rotate the matrix 90 degrees right for every R symbol. The symbols were only 3 characters in length.
Help Newton's girlfriend to display the final matrix at the end of the complete turns.The first line consists of one integer n - the size of the matrix.
In the next n lines, you are given n integers, numbers can range from 1 to 500.
<b>Constraints<b>
1 ≤ n ≤ 500Output the final matrix n X n.
<b>Note</b> You should not print any whitespace or newline if it is not necessary.Sample input:
2
1 2
3 4
RLR
Sample output:
3 1
4 2, I have written this Solution Code: #include <bits/stdc++.h>
#define ll long long int
using namespace std;
void solve()
{
ll n;
cin >> n;
vector<vector<ll>> a(n, vector<ll>(n, 0));
for (ll i = 0; i < n; i++)
{
for (ll j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
string s;
cin >> s;
ll count = 0;
for (auto i : s)
{
if (i == 'R')
count++;
else
count--;
}
if (count > 0)
{
while (count--)
{
for (ll i = 0; i < n; i++)
{
for (ll j = 0; j < i; j++)
{
swap(a[i][j], a[j][i]);
}
}
for (ll i = 0; i < n; i++)
{
for (ll j = 0; j < n / 2; j++)
{
swap(a[i][j], a[i][n - 1 - j]);
}
}
}
}
else
{
while (count++)
{
for (ll i = 0; i < n; i++)
{
for (ll j = 0; j < i; j++)
{
swap(a[i][j], a[j][i]);
}
}
for (ll j = 0; j < n; j++)
{
for (ll i = 0; i < n / 2; i++)
{
swap(a[i][j], a[n - 1 - i][j]);
}
}
}
}
for (ll i = 0; i < n; i++)
{
for (ll j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton being a student of science gifted an <b>nXn</b> matrix to his girlfriend on her birthday and challenge her to solve the matrix. The matrix consists of natural numbers up to 500(1 ≤ n ≤ 500). Newton instructed a few things about matrix to his girlfriend. He said there are two types of symbols on the matrix( L and R ), you need to rotate the matrix 90 degrees to the left for every L symbol and rotate the matrix 90 degrees right for every R symbol. The symbols were only 3 characters in length.
Help Newton's girlfriend to display the final matrix at the end of the complete turns.The first line consists of one integer n - the size of the matrix.
In the next n lines, you are given n integers, numbers can range from 1 to 500.
<b>Constraints<b>
1 ≤ n ≤ 500Output the final matrix n X n.
<b>Note</b> You should not print any whitespace or newline if it is not necessary.Sample input:
2
1 2
3 4
RLR
Sample output:
3 1
4 2, I have written this Solution Code: n = int(input())
matrix = []
for _ in range(n):
matrix.append(input().strip().split())
symbols = list(input().strip())
instructions = sum(1 if i == 'R' else -1 for i in symbols)
if instructions in (1, -3):
rotate_matrix = [['0'] * n for _ in range(n)]
for i in range(n):
for j in range(n):
rotate_matrix[i][j] = matrix[n - j - 1][i]
matrix = rotate_matrix
elif instructions in (-1, 3):
rotate_matrix = [['0'] * n for _ in range(n)]
for i in range(n):
for j in range(n):
rotate_matrix[i][j] = matrix[j][n - i - 1]
matrix = rotate_matrix
for i in range(n):
print(' '.join(matrix[i])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton has an array containing <em>12</em> integers from 1 to 12. But later he divides it into <em>3</em> arrays in the following manner.
First array - 1 3 5 7 8 10 12
Second array - 4 6 9 11
Third array - 2
Two integers x and y are given. Find whether these two integers belong to the same array or not.The input contains two integers x and y.
<b>Constraints</b>
1 ≤ x < y ≤ 12If x and y belong to the same group, print Yes; otherwise, print No.<b>Sample Input 1</b>
1 3
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
2 4
<b>Sample Output 2</b>
No, I have written this Solution Code: #include<iostream>
using namespace std;
int main()
{
int a,b,l,m;
cin>>a>>b;
if(a==2) l=1;
else if(a==4||a==6||a==9||a==11) l=2;
else l=3;
if(b==2) m=1;
else if(b==4||b==6||b==9||b==11) m=2;
else m=3;
if(l==m) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
if(t%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
catch (Exception e){
return;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch(num % 2)
{
case 0:
printf("Even");
break;
/* Else if n%2 == 1 */
case 1:
printf("Odd");
break;
}
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 positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String input[]=br.readLine().split("\\s");
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(input[i]);
}
System.out.print(implementMergeSort(a,0,n-1));
}
public static long implementMergeSort(int arr[], int start, int end)
{
long count=0;
if(start<end)
{
int mid=start+(end-start)/2;
count +=implementMergeSort(arr,start,mid);
count +=implementMergeSort(arr,mid+1,end);
count +=merge(arr,start,end,mid);
}
return count;
}
public static long merge(int []a,int start,int end,int mid)
{
int i=start;
int j=mid+1;
int k=0;
int len=end-start+1;
int c[]=new int[len];
long inv_count=0;
while(i<=mid && j<=end)
{
if(a[i]<=a[j])
{
c[k++]=a[i];
i++;
}
else
{
c[k++]=a[j];
j++;
inv_count +=(mid-i)+1;
}
}
while(i<=mid)
{
c[k++]=a[i++];
}
while(j<=end)
{
c[k++]=a[j++];
}
for(int l=0;l<len;l++)
a[start+l]=c[l];
return inv_count;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
long long _mergeSort(int arr[], int temp[], int left, int right);
long long merge(int arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
long long mergeSort(int arr[], int array_size)
{
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
long long _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
long long merge(int arr[], int temp[], int left,
int mid, int right)
{
int i, j, k;
long long inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
signed main()
{
int n;
cin>>n;
int a[n];
unordered_map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
if(m.find(a[i])==m.end()){
m[a[i]]=i;
}
}
cout<<mergeSort(a,n);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Bob is at the origin of a number line. He wants to reach a goal at coordinate X.
There is a wall at coordinate Y, which Bob cannot go beyond at first. However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.
Determine whether Bob can reach the goal. If he can, find the minimum total distance he needs to travel to do so.The input is given from Standard Input in the following format:
X Y Z
<b>Constraints</b>
−1000 ≤ X, Y, Z ≤ 1000
X, Y, and Z are distinct, and none of them is 0.
All values in the input are integers.If Bob can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.<b>Sample Input 1</b>
10 -10 1
<b>Sample Output 1</b>
10
<b>Sample Input 2</b>
20 10 -10
<b>Sample Output 2</b>
40, I have written this Solution Code: #include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define sz(a) ((int) (a).size())
#define vi vector < int >
#define me(a, x) memset(a, x, sizeof(a))
#define ull unsigned long long
#define ld __float128
using namespace std;
const int N = 1e6 + 7;
int n, x, y, z;
int main() {
ios :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> x >> y >> z;
if(abs(y) + abs(x - y) == abs(x)) {
if(abs(y - z) + abs(y) == abs(z)) {
cout << -1 << '\n';
} else {
cout << abs(z) + abs(z - x) << '\n';
}
} else {
cout << abs(x) << '\n';
}
if(y > 0 && x < y) {
}
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 size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, 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 a[]=br.readLine().split(" ");
int n=Integer.parseInt(a[0]);
int k=Integer.parseInt(a[1]);
String s[]=br.readLine().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(s[i]);
int min=100000,max=-100000;
long sum=0;
for(int i=0;i<n-k+1;i++){
min=100000;
max=-100000;
for(int j=i;j<k+i;j++){
min=Math.min(arr[j],min);
max=Math.max(arr[j],max);
}
sum=sum+max+min;
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, I have written this Solution Code: from collections import deque
def solve(arr,n,k):
Sum = 0
S = deque()
G = deque()
for i in range(k):
while ( len(S) > 0 and arr[S[-1]] >= arr[i]):
S.pop()
while ( len(G) > 0 and arr[G[-1]] <= arr[i]):
G.pop()
G.append(i)
S.append(i)
for i in range(k, n):
Sum += arr[S[0]] + arr[G[0]]
while ( len(S) > 0 and S[0] <= i - k):
S.popleft()
while ( len(G) > 0 and G[0] <= i - k):
G.popleft()
while ( len(S) > 0 and arr[S[-1]] >= arr[i]):
S.pop()
while ( len(G) > 0 and arr[G[-1]] <= arr[i]):
G.pop()
G.append(i)
S.append(i)
Sum += arr[S[0]] + arr[G[0]]
return Sum
n,k=map(int,input().split())
l=list(map(int,input().split()))
print(solve(l,n,k)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of size N, your task is to calculate the total sum of maximum and minimum elements in each subarray of size K.
See example for better understanding.First line of input contains an two space separated integers depicting values of N and K, next line contains N space separated integers depicting values of Arr[i].
Constraints:-
1 < = k < = N < = 100000
-100000 < = Arr[i] < = 100000Print the required sumSample Input:-
5 3
1 2 3 4 5
Sample Output:-
18
Explanation:-
For subarray 1 2 3 :- 1 + 3 = 4
For subarray 2 3 4 :- 2 + 4 = 6
For subarray 3 4 5 :- 3 + 5 = 8
total sum = 4+6+8 = 18
Sample Input:-
7 4
2 5 -1 7 -3 -1 -2
Sample Output:-
18, I have written this Solution Code: // C++ program to find sum of all minimum and maximum
// elements Of Sub-array Size k.
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int SumOfKsubArray(int arr[] , int n , int k)
{
int sum = 0; // Initialize result
// The queue will store indexes of useful elements
// in every window
// In deque 'G' we maintain decreasing order of
// values from front to rear
// In deque 'S' we maintain increasing order of
// values from front to rear
deque< int > S(k), G(k);
// Process first window of size K
int i = 0;
for (i = 0; i < k; i++)
{
// Remove all previous greater elements
// that are useless.
while ( (!S.empty()) && arr[S.back()] >= arr[i])
S.pop_back(); // Remove from rear
// Remove all previous smaller that are elements
// are useless.
while ( (!G.empty()) && arr[G.back()] <= arr[i])
G.pop_back(); // Remove from rear
// Add current element at rear of both deque
G.push_back(i);
S.push_back(i);
}
// Process rest of the Array elements
for ( ; i < n; i++ )
{
// Element at the front of the deque 'G' & 'S'
// is the largest and smallest
// element of previous window respectively
sum += arr[S.front()] + arr[G.front()];
// Remove all elements which are out of this
// window
while ( !S.empty() && S.front() <= i - k)
S.pop_front();
while ( !G.empty() && G.front() <= i - k)
G.pop_front();
// remove all previous greater element that are
// useless
while ( (!S.empty()) && arr[S.back()] >= arr[i])
S.pop_back(); // Remove from rear
// remove all previous smaller that are elements
// are useless
while ( (!G.empty()) && arr[G.back()] <= arr[i])
G.pop_back(); // Remove from rear
// Add current element at rear of both deque
G.push_back(i);
S.push_back(i);
}
// Sum of minimum and maximum element of last window
sum += arr[S.front()] + arr[G.front()];
return sum;
}
// Driver program to test above functions
signed main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout << SumOfKsubArray(a, n, k) ;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function replaceArray(arr, n) {
// write code here
// do not console.log
// return the new array
const newArr = []
newArr[0] = arr[0] * arr[1]
newArr[n-1] = arr[n-1] * arr[n-2]
for(let i= 1;i<n-1;i++){
newArr[i] = arr[i-1] * arr[i+1]
}
return newArr
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: n = int(input())
X = [int(x) for x in input().split()]
lst = []
for i in range(len(X)):
if i == 0:
lst.append(X[i]*X[i+1])
elif i == (len(X) - 1):
lst.append(X[i-1]*X[i])
else:
lst.append(X[i-1]*X[i+1])
for i in lst:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long b[n],a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=1;i<n-1;i++){
b[i]=a[i-1]*a[i+1];
}
b[0]=a[0]*a[1];
b[n-1]=a[n-1]*a[n-2];
for(int i=0;i<n;i++){
cout<<b[i]<<" ";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(a[0]*a[1]+" ");
for(int i=1;i<n-1;i++){
System.out.print(a[i-1]*a[i+1]+" ");
}
System.out.print(a[n-1]*a[n-2]);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given arrays A, B each containing N integers. You have to create an array C of N integers where C<sub>i</sub> = A<sub>i</sub> & B<sub>i</sub>. (Here, & denotes the bitwise operation). You can shuffle elements of A and B however you want. Find the maximum value of C<sub>1</sub> & C<sub>2</sub> & C<sub>3</sub>. .. &C<sub>n</sub>.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You have to complete the function <b>MaxAnd()</b> that takes the array A and B of size N as parameters.
<b>Constraints:</b>
1 ≤ T ≤ 10<sup>5</sup>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ A<sub>i</sub>, B<sub>i</sub> ≤ 10<sup>9</sup>For each test case, print maximum value in a separate line.Sample Input:-
1
2
3 2
2 3
Sample Output:-
2
Explanation:
The best configuration here will be have A<sub>1</sub> = 3, and B<sub>1</sub> = 3, and A<sub>2</sub> = 2 and B<sub>2</sub> = 2. So the array C will be [3, 2]. So the answer will be 2 (3&2=2)., I have written this Solution Code: class Solution {
public void MaxAnd(int []a,int []b,int n) {
// int n = a[].length;
int[] bit = new int[32];
for (int i = 0; i < n; ++i) {
int x = a[i];
for (int j = 0; j < 32; ++j) {
if ((x & 1L << j) != 0) {
++bit[j];
}
}
}
for (int i = 0; i < n; ++i) {
int x = b[i];
for (int j = 0; j < 32; ++j) {
if ((x & 1L << j) != 0) {
++bit[j];
}
}
}
int ans = 0;
for (int i = 0; i < bit.length; ++i) {
if (bit[i] == 2 * n) {
ans += 1 << i;
}
}
System.out.println(ans);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code: public static long SumOfDivisors(long N){
long sum=0;
long c=(long)Math.sqrt(N);
for(long i=1;i<=c;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){sum+=N/i;}
}
}
return sum;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
long long SumOfDivisors(long long N){
long long sum=0;
long sq=sqrt(N);
for(long i=1;i<=sq;i++){
if(N%i==0){
sum+=i;
if(i*i!=N){
sum+=N/i;
}
}
}
return sum;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to return the sum of all of its divisors.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>SumOfDivisors()</b> that takes the integer N as parameter.
Constraints:-
1<=N<=10^9Return the sum of all of the divisors.Sample Input:-
4
Sample Output:-
7
Sample Input:-
13
Sample Output:-
14, I have written this Solution Code:
def SumOfDivisors(num) :
# Final result of summation of divisors
result = 0
# find all divisors which divides 'num'
i = 1
while i<= (math.sqrt(num)) :
# if 'i' is divisor of 'num'
if (num % i == 0) :
# if both divisors are same then
# add it only once else add both
if (i == (num / i)) :
result = result + i;
else :
result = result + (i + num/i);
i = i + 1
# Add 1 to the result as 1 is also
# a divisor
return (result); , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. In one move, you can choose two indices i, j (1 <= i, j <= N) such that A<sub>i</sub>+A<sub>j</sub> is odd and swap A<sub>i</sub> and A<sub>j</sub>. Find the lexicographically smallest array you can obtain after any number of operations.The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print N space seperated integers, the lexicographically smallest array you can obtain after any number of operations.Sample Input:
3
5 2 8
Sample Output:
2 5 8
Explaination:
We can swap 5 and 2 as their sum is odd., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static void lexicographically_smaller(int arr[],int n){
int odd = 0, even = 0;
for (int i=0;i<n;i++)
{
if(arr[i]%2==1)
odd++;
else
even++;
}
if (odd>0 && even>0)
Arrays.sort(arr);
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
lexicographically_smaller(arr, n);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. In one move, you can choose two indices i, j (1 <= i, j <= N) such that A<sub>i</sub>+A<sub>j</sub> is odd and swap A<sub>i</sub> and A<sub>j</sub>. Find the lexicographically smallest array you can obtain after any number of operations.The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print N space seperated integers, the lexicographically smallest array you can obtain after any number of operations.Sample Input:
3
5 2 8
Sample Output:
2 5 8
Explaination:
We can swap 5 and 2 as their sum is odd., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
int odd = 0;
for(auto i : a){
if(i % 2) odd++;
}
if(odd != 0 and odd != n){
sort(a.begin(), a.end());
}
for(auto i : a) cout << i << ' ';
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String num = br.readLine();
int N = Integer.parseInt(num);
String result = "";
if(N < 1000 && N > 0 && N == 1){
result = "AC";
} else {
result = "WA";
}
System.out.println(result);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: N=int(input())
if N==1:
print("AC")
else:
print("WA"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Santa has been trying to solve the following task. Would you please help him so that he can go back to distributing gifts?
Given an integer N, print "AC" if N is 1. Otherwise, print "WA".
Note: You are supposed to print "AC" and "WA" without the quotes.The input consists of a single line containing the integer N.
<b> Constraints: </b>
0 ≤ N ≤ 1000Print the required answer.Sample Input 1
1
Sample Output 1
AC
Sample Input 2
0
Sample Output 2
WA
, I have written this Solution Code: //Author: Xzirium
//Time and Date: 03:04:29 27 December 2021
//Optional FAST
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")
//Required Libraries
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//Required namespaces
using namespace std;
using namespace __gnu_pbds;
//Required defines
#define endl '\n'
#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(long long i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define FORI(a,b,c) for(long long a=b;a<c;a++)
#define FORD(a,b,c) for(long long a=b;a>c;a--)
//Required typedefs
template <typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <typename T> using ordered_set1 = tree<T,null_type,greater<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
//Required Constants
const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);
const long double PI=3.14159265358979;
// Required random number generators
// mt19937 gen_rand_int(chrono::steady_clock::now().time_since_epoch().count());
// mt19937_64 gen_rand_ll(chrono::steady_clock::now().time_since_epoch().count());
//Required Functions
ll power(ll b, ll e)
{
ll r = 1ll;
for(; e > 0; e /= 2, (b *= b) %= MOD)
if(e % 2) (r *= b) %= MOD;
return r;
}
ll modInverse(ll a)
{
return power(a,MOD-2);
}
//Work
int main()
{
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen ("INPUT.txt" , "r" , stdin);
//freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t clk;
clk = clock();
//-----------------------------------------------------------------------------------------------------------//
READV(N);
if(N==1)
{
cout<<"AC"<<endl;
}
else
{
cout<<"WA"<<endl;
}
//-----------------------------------------------------------------------------------------------------------//
clk = clock() - clk;
cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array arr[] of N integers and a sum. The task is to count the number of subarrays which adds to a given number.The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
Each test case contains an integer N denoting the size of the array and integer denoting the value of the sum in the first line.
The next line contains N space separated integers forming the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
-105 <= arr[i] <= 10^5
-105 <= sum <= 10^5
Note:- Sum of N over all test cases does not exceed 10^5For each testcase, in a new line, print the count of the subarrays which add to the given sum.Sample Input
2
5 -10
10 2 -2 -20 10
6 33
1 4 20 3 10 5
Sample Output
3
1
Explanation:
Testcase 1: Subarrays with sum -10 are: [10, 2, -2, -20], [2, -2, -20, 10] and [-20, 10].
Testcase 2: Subarray with sum 33 is: [20, 3, 10]., 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 1000001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Returns n^(-1) mod p
ll modInverse(ll n, ll p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
li sum;
cin>>sum;
ll cur=0;
unordered_map<ll,int> m;
ll cnt=0;
for(int i=0;i<n;i++){
cin>>a[i];
cur+=a[i];
if(cur==sum){
cnt++;}
if(m.find(cur-sum)!=m.end()){
cnt+=m[cur-sum];}
m[cur]++;
}
out(cnt);
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array arr[] of N integers and a sum. The task is to count the number of subarrays which adds to a given number.The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
Each test case contains an integer N denoting the size of the array and integer denoting the value of the sum in the first line.
The next line contains N space separated integers forming the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
-105 <= arr[i] <= 10^5
-105 <= sum <= 10^5
Note:- Sum of N over all test cases does not exceed 10^5For each testcase, in a new line, print the count of the subarrays which add to the given sum.Sample Input
2
5 -10
10 2 -2 -20 10
6 33
1 4 20 3 10 5
Sample Output
3
1
Explanation:
Testcase 1: Subarrays with sum -10 are: [10, 2, -2, -20], [2, -2, -20, 10] and [-20, 10].
Testcase 2: Subarray with sum 33 is: [20, 3, 10]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
for(int k=0;k<T;k++)
{
String s[]=br.readLine().split("\\s");
int N=Integer.parseInt(s[0]);
int sum=Integer.parseInt(s[1]);
s=br.readLine().split("\\s");
int arr[]=new int[N];
for(int i=0;i<N;i++)
{
arr[i]=Integer.parseInt(s[i]);
}
long count=0;
long sum1=0;
for(int i=0;i<N;i++)
{
for(int j=i;j<N;j++)
{
sum1+=arr[j];
if(sum1==sum)
{
count++;
}
}
sum1=0;
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array arr[] of N integers and a sum. The task is to count the number of subarrays which adds to a given number.The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
Each test case contains an integer N denoting the size of the array and integer denoting the value of the sum in the first line.
The next line contains N space separated integers forming the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
-105 <= arr[i] <= 10^5
-105 <= sum <= 10^5
Note:- Sum of N over all test cases does not exceed 10^5For each testcase, in a new line, print the count of the subarrays which add to the given sum.Sample Input
2
5 -10
10 2 -2 -20 10
6 33
1 4 20 3 10 5
Sample Output
3
1
Explanation:
Testcase 1: Subarrays with sum -10 are: [10, 2, -2, -20], [2, -2, -20, 10] and [-20, 10].
Testcase 2: Subarray with sum 33 is: [20, 3, 10]., I have written this Solution Code: t = int(input())
while(t > 0):
s = input().split()
n = int(s[0])
k = int(s[1])
li = input().split()
li = list(map(int, li))
sum = 0
d = {}
count = 0
for i in range(n):
sum += li[i]
if sum == k:
count += 1
if sum-k in d:
count += d[sum-k]
if sum not in d:
d[sum] = 1
else:
d[sum] += 1
print(count)
t -= 1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: m,n=map(int ,input().split())
matrix=[]
for i in range(m):
l1=[eval(x) for x in input().split()]
matrix.append(l1)
l2=[]
for coloumn in range(n):
sum1=0
for row in range(m):
sum1+= matrix[row][coloumn]
l2.append(sum1)
print(max(l2))
'''for row in range(n):
sum2=0
for col in range(m):
sum2 += matrix[row][col]
print(sum2)''', In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, I have written this Solution Code: // mat is the matrix/ 2d array
// the dimensions of array are:- a rows, b columns
function colMaxSum(mat,a,b) {
// write code here
// do not console.log
// return the answer as a number
let idx = -1;
// Variable to store max sum
let maxSum = Number.MIN_VALUE;
// Traverse matrix column wise
for (let i = 0; i < b; i++) {
let sum = 0;
// calculate sum of column
for (let j = 0; j < a; j++) {
sum += mat[j][i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum) {
maxSum = sum;
// store index
idx = i;
}
}
let res;
res = [idx, maxSum];
// return result
return maxSum;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, 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);
}
signed main(){
int n,m;
cin>>n>>m;
int a[m];
for(int i=0;i<m;i++){
a[i]=0;
}
int x;
int sum=0;
FOR(i,n){
FOR(j,m){
cin>>x;
a[j]+=x;
sum=max(sum,a[j]);
}
}
out(sum);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a matrix of size M*N, your task is to find the column having the maximum sum and print the sum.The first line of input contains two space-separated integers M and N. The following M lines of input contain N space-separated integers each depicting the values of the matrix.
Constraints:-
1 <= M, N <= 100
1 <= Matrix[][] <= 100000Print the maximum sum.Sample Input:-
3 3
1 2 3
4 5 6
7 8 9
Sample Output:-
18
Explanation:-
1 + 4 + 7 = 12
2 + 5 + 8 = 15
3 + 6 + 9 = 18
maximum = 18
Sample Input:-
3 2
1 4
9 6
9 1
Sample Output:-
19, 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 m = sc.nextInt();
int n = sc.nextInt();
int a[][] = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
a[i][j]=sc.nextInt();
}
}
int sum=0;
int ans=0;
for(int i=0;i<n;i++){
sum=0;
for(int j=0;j<m;j++){
sum+=a[j][i];
}
if(sum>ans){ans=sum;}
}
System.out.print(ans);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of N numbers. Your task is to first reverse the array (the first number becomes last, 2nd number becomes 2nd from the last, and so on) and then print the sum of the numbers at even indices and print the product of the numbers at odd indices.
<b>Note :</b> 1 based indexing is followed .The first line contains a single integer N: the number of elements
The second line contains N different integers separated by spaces
<b>constraints:-</b>
1 <= N <= 35
1 <= a[i] <= 10Two space separated integers representing sum of the numbers at even places and the product of the numbers at odd places.
Sample Input 1:
6
1 2 3 4 5 6
Sample Output 1:
9 48
Sample Input 2:
3
1 2 3
Sample Output 2:
2 3
<b>Explanation 1:-</b>
After reversing 1 2 3 4 5 6 becomes 6 5 4 3 2 1
Hence the sum of the numbers at even indices: 5+3+1=9
product of the numbers at odd indices: 6*4*2=48
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long x=1,y=0;
if(n&1){
for(int i=0;i<n;i++){
if(i%2==0){
x*=a[i];
}
else{
y+=a[i];
}
}
}
else{
for(int i=0;i<n;i++){
if(i%2==0){
y+=a[i];
}
else{
x*=a[i];
}
}
}
cout<<y<<" "<<x;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of N numbers. Your task is to first reverse the array (the first number becomes last, 2nd number becomes 2nd from the last, and so on) and then print the sum of the numbers at even indices and print the product of the numbers at odd indices.
<b>Note :</b> 1 based indexing is followed .The first line contains a single integer N: the number of elements
The second line contains N different integers separated by spaces
<b>constraints:-</b>
1 <= N <= 35
1 <= a[i] <= 10Two space separated integers representing sum of the numbers at even places and the product of the numbers at odd places.
Sample Input 1:
6
1 2 3 4 5 6
Sample Output 1:
9 48
Sample Input 2:
3
1 2 3
Sample Output 2:
2 3
<b>Explanation 1:-</b>
After reversing 1 2 3 4 5 6 becomes 6 5 4 3 2 1
Hence the sum of the numbers at even indices: 5+3+1=9
product of the numbers at odd indices: 6*4*2=48
, I have written this Solution Code: n = int(input())
lst = list(map(int, input().strip().split()))[:n]
lst.reverse()
lst_1 = 0
for i in range(1, n, 2):
lst_1 += lst[i]
lst_2 = 1
for i in range(0, n, 2):
lst_2 *= lst[i]
print(lst_1,lst_2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of N numbers. Your task is to first reverse the array (the first number becomes last, 2nd number becomes 2nd from the last, and so on) and then print the sum of the numbers at even indices and print the product of the numbers at odd indices.
<b>Note :</b> 1 based indexing is followed .The first line contains a single integer N: the number of elements
The second line contains N different integers separated by spaces
<b>constraints:-</b>
1 <= N <= 35
1 <= a[i] <= 10Two space separated integers representing sum of the numbers at even places and the product of the numbers at odd places.
Sample Input 1:
6
1 2 3 4 5 6
Sample Output 1:
9 48
Sample Input 2:
3
1 2 3
Sample Output 2:
2 3
<b>Explanation 1:-</b>
After reversing 1 2 3 4 5 6 becomes 6 5 4 3 2 1
Hence the sum of the numbers at even indices: 5+3+1=9
product of the numbers at odd indices: 6*4*2=48
, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int Arr[] = new int[n];
for(int i=0;i<n;i++){
Arr[i] = sc.nextInt();
}
if(n%2==1){
long sum=0;
long product=1;
for(int i=0;i<n;i++){
if(i%2==0){
product*=Arr[i];
}
else{
sum+=Arr[i];
}
}
System.out.print(sum+" "+product);
}
else{
long sum=0;
long product=1;
for(int i=0;i<n;i++){
if(i%2==1){
product*=Arr[i];
}
else{
sum+=Arr[i];
}
}
System.out.print(sum+" "+product);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.