Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given a string S. Two position i and j of the string are friends if they have the same character. The distance between two friends at positions i and j is defined as |i- j|. Find the sum of distances of all the pairs of friends in the given strings.First line of input contains a single string S.
Constraints:
1 <= |S| <= 1000000
String contains lowercase english letters.Output a single integer which is the sum of distance of all the pair of friends in the given strings.Sample Input
ababa
Sample Output
10
Explanation: Friend pairs - (1, 3) (1, 5) (2, 4) (3, 5), I have written this Solution Code: #pragma GCC optimize ("O3")
#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;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
string s;
cin>>s;
int c[26]={};
int f[26]={};
int ans=0;
int n=s.length();
for(int i=0;i<n;++i){
ans+=f[s[i]-'a']*i-c[s[i]-'a'];
f[s[i]-'a']++;
c[s[i]-'a']+=i;
}
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bo=new BufferedWriter(new OutputStreamWriter(System.out));
int t;
try{
t=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
return;
}
while(t-->0)
{
String[] g=br.readLine().split(" ");
int n=Integer.parseInt(g[0]);
int k=Integer.parseInt(g[1]);
if(k>n || (k==1) || (k>26))
{
if(n==1 && k==1)
bo.write("a\n");
else
bo.write(-1+"\n");
}
else
{
int extra=k-2;
boolean check=true;
while(n>extra)
{
if(check==true)
bo.write("a");
else
bo.write("b");
if(check==true)
check=false;
else
check=true;
n--;
}
for(int i=0;i<extra;i++)
bo.write((char)(i+99));
bo.write("\n");
}
}
bo.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: t=int(input())
for tt in range(t):
n,k=map(int,input().split())
if (k==1 and n>1) or (k>n):
print(-1)
continue
s="abcdefghijklmnopqrstuvwxyz"
ss="ab"
if (n-k)%2==0:
a=ss*((n-k)//2)+s[:k]
else:
a=ss*((n-k)//2)+s[:2]+"a"+s[2:k]
print(a), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N and K, find the lexicographically smallest string of length N using only the first K lowercase letters of the alphabet such that each letter is used at least once and no two adjacent characters are equal.
If such a string doesn't exist, print -1.The first line of input contains a single integer, T (1 <= T <= 100).
Then T lines follow, each containing two space-separated integers, N (1 <= N <= 10<sup>5</sup>) and K (1 <= K <= 26).
It is guaranteed that sum of N over all test cases does not exceed 10<sup>6</sup>For each test case, output its answer in a new line.Sample Input:
2
2 3
3 2
Sample Output:
-1
aba, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef unsigned long long int ull;
const long double PI = acos(-1);
const ll mod=1e9+7;
const ll mod1=998244353;
const int inf = 1e9;
const ll INF=1e18;
void precompute(){
}
void TEST_CASE(){
int n,k;
cin >> n >> k;
if(k==1){
if(n>1){
cout << -1 << endl;
}else{
cout << 'a' << endl;
}
}else if(n<k){
cout << -1 << endl;
}else if(n==k){
string s="";
for(int i=0 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}else{
string s="";
for(int i=0 ; i<(n-k+2) ; i++){
if(i%2){
s+="b";
}else{
s+="a";
}
}
for(int i=2 ; i<k ; i++){
s+=('a'+i);
}
cout << s << endl;
}
}
signed main(){
fast;
//freopen ("INPUT.txt","r",stdin);
//freopen ("OUTPUT.txt","w",stdout);
int test=1,TEST=1;
precompute();
cin >> test;
while(test--){
TEST_CASE();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int len = Integer.parseInt(br.readLine());
char[] str = new char[len];
for(int i = 0; i < len; i++){
if(i%2 == 0){
str[i] = 'a';
} else{
str[i] = 'b';
}
}
System.out.println(str);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s(n,'a');
for(int i=1;i<n;i+=2)
s[i]='b';
cout<<s;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: a="ab"
inp = int(input())
print(a*(inp//2)+a[0:inp%2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int len = Integer.parseInt(br.readLine());
char[] str = new char[len];
for(int i = 0; i < len; i++){
if(i%2 == 0){
str[i] = 'a';
} else{
str[i] = 'b';
}
}
System.out.println(str);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s(n,'a');
for(int i=1;i<n;i+=2)
s[i]='b';
cout<<s;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: a="ab"
inp = int(input())
print(a*(inp//2)+a[0:inp%2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)) ;
int n = Integer.parseInt(read.readLine()) ;
String[] str = read.readLine().trim().split(" ") ;
int[] arr = new int[n] ;
for(int i=0; i<n; i++) {
arr[i] = Integer.parseInt(str[i]) ;
}
long ans = 0L;
if((n & 1) == 1) {
for(int i=0; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
else {
Arrays.sort(arr);
for(int i=1; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: n=int(input())
arr=map(int,input().split())
result=[]
for item in arr:
if item%2==0:
result.append(item-1)
else:
result.append(item)
if sum(result)%2==0:
result.sort()
result.pop(0)
print(sum(result))
else:
print(sum(result)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
int n; cin >> n ;
long long int ans = 0;
// int arr[n];
int odd = 0,minn=INT_MAX;
for(int i=0;i<n;i++){
ll temp; cin >> temp;
// cin >> arr[i];
ans += (temp&1) ? temp : temp-1;
if(minn>temp)
minn = temp;
}
if(n&1)
cout << ans ;
else{
if(minn&1)
cout <<ans-minn;
else
cout << ans - minn + 1;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code: def GCD(x, y):
while(y):
x, y = y, x % y
return x
def primefactor(N) :
if N%2==0:
return 2
i = 3
while(i<=math.sqrt(N)):
if(N%i==0):
return i;
i=i+2;
return N;
def printM_SpecialGCD(N,M) :
prime=primefactor(N)
i=prime
count=0
while count!=M :
res=GCD(N,N+i)
if(res == prime):
count=count+1
print(N+i, end =" "),
i=i+prime
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code:
int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
for (int i = 3; i <= sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
cout<<N+i<<" ";
count++;
}
i+=prime;
}
}
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code:
static void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
System.out.print(N+i + " ");
count++;
}
i+=prime;
}
}
public static int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
public static int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code: int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
int c =sqrt(n);
for (int i = 3; i <= c; i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
printf("%d ",N+i);
count++;
}
i+=prime;
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Bob participated in N Quizzes. Each quiz consists of M questions. Given an N X M matrix results where results[i][j] = 1 if Bob answered j<sup>th</sup> question correctly in the i<sup>th</sup> Quiz.
Each correct answer gives 10 marks and he has the option to select any quiz session for the marks evaluation.
What is the maximum marks Bob can get in a quiz?First line contains N and M.
Next N lines contain M integers each.
<b>Constraints</b>
1 ≤ N, M ≤ 1000
results[i][j] = 0 or 1A single integer denotes maximum marks.Input:
3 4
0 0 1 0
1 0 1 0
1 0 1 1
Output:
30
Explanation:
Bob should choose the 4th Quiz session for evaluation. He answered 3 question correctly so his marks will be 30., I have written this Solution Code: import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
int n=Integer.parseInt(in.next());
int m=Integer.parseInt(in.next());
int ans=0;
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<m;j++){
int x=Integer.parseInt(in.next());
count += x;
}
ans=Math.max(ans,count);
}
ans *= 10;
out.print(ans);
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
double arr[] = new double[N];
String str[] = br.readLine().trim().split(" ");
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
double resistance=0;
int equResistance=0;
for(int i=0;i<N;i++)
arr[i]=Integer.parseInt(str[i]);
for(int i=0;i<N;i++)
{
resistance=resistance+(1/arr[i]);
}
equResistance = (int)Math.floor((1/resistance));
System.out.println(equResistance);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: r = input("")
r = int(r)
n = input("").split()
resistance=0.0
for i in range(0,r):
resistor = float(n[i])
resistance = resistance + (1/resistor)
print(int(1/resistance)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given resistance value of N resistors. Find the net resistance of the system when all of these N resistors are connected in parallel.
If there are three resistors A1, A2, A3, when they are connected in parallel, the net resistance will be 1/((1/A1) + (1/A2) + (1/A3))
Since this number can also have a fraction part, you only have to print the floor of the result obtained.
For example, if value of 1/((1/A1) + (1/A2) + (1/A3)) if 7.54567, you only have to print 7.First line contains a single integer N denoting the number of resistors.
Next line contains N space separated integers containing the value of different resistors.
Constraints
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9Print the integral part or floor of the value obtained from the formula 1/((1/A1) + (1/A2) + ..... + (1/AN)).Input
2
10 30
Output
7
1/((1/10) + (1/30)) = 30/4 = 7.5 and floor of 7.5 is 7, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
double s = 0;
for(int i = 1; i <= n; i++){
double p; cin >> p;
s = s + (1/p);
}
s = 1/s;
cout << floor(s);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers N and M. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code: def GCD(x, y):
while(y):
x, y = y, x % y
return x
def primefactor(N) :
if N%2==0:
return 2
i = 3
while(i<=math.sqrt(N)):
if(N%i==0):
return i;
i=i+2;
return N;
def printM_SpecialGCD(N,M) :
prime=primefactor(N)
i=prime
count=0
while count!=M :
res=GCD(N,N+i)
if(res == prime):
count=count+1
print(N+i, end =" "),
i=i+prime
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code:
int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
for (int i = 3; i <= sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
cout<<N+i<<" ";
count++;
}
i+=prime;
}
}
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code:
static void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
System.out.print(N+i + " ");
count++;
}
i+=prime;
}
}
public static int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
public static int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
, 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. Your task is to print first <b>M Special GCD numbers</b> which are greater than N.
<b>M Special GCD numbers</b> : First M numbers which are greater than N and whose GCD with N is equal to the smallest prime factor of N.User Task:
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>printM_SpecialGCD()</b>, where you will get N and M as a parameter.
Constraints:
2 <= N <= 10^6
1 <= M <= 10^5Print the required answers separated by space.Sample Input:-
10 2
Sample Output:-
12 14
Explanation:-least prime divisor of 10 is 2.
Numbers greater than 10 whose gcd with 10 is equal to 2 are :- 12 14 16 18 22 . . . .
First two numbers of this series are:- 12 and 14
Sample Input:-
9 3
Sample Output:-
12 15 21, I have written this Solution Code: int gcd(int a,int b){
int temp = 0;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
return a;
}
int primeFactors(int n)
{
int m=0;
// Print the number of 2s that divide n
if (n%2==0)
{
m= 2;
return m;
}
int c =sqrt(n);
for (int i = 3; i <= c; i+= 2)
{
// While i divides n, print i and divide n
if (n%i == 0)
{
m=i;
return m;
}
}
if(n>2) {
m=n;
}
return m;
}
void printM_SpecialGCD(int N, int M)
{
int prime = primeFactors(N);
int count =0;
int i=prime;
while(count!=M){
int res = gcd(N,N+i);
if(res==prime){
printf("%d ",N+i);
count++;
}
i+=prime;
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Olivia likes triangles. Hence, John decided to give her three integers and ask whether a triangle with edge length as given three integers is possible.
Help Olivia in answering it.The input consists of a single line containing three space-separated integers A, B, and C.
<b>Constraints </b>
1 <= A, B, C <= 100Output "Yes" if the triangle is possible otherwise, "No" (without quotes).Sample Input 1:
5 3 4
Sample Output 1:
Yes
Sample Explanation 1:
The possible triangle is a right-angled triangle with a hypotenuse of 5., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
if(a<(b+c) && b<(c+a) && c<(a+b)){
cout << "Yes\n";
}else{
cout << "No\n";
}
return 0;
}, 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: Remove duplicates of an array and return an array of only unique elements.An array containing numbers.Space separated unique elements from the array.Sample Input:-
1 2 3 5 1 5 9 1 2 8
Sample Output:-
1 2 3 5 9 8
<b>Explanation:-</b>
Extra 1, 2, and 5 were removed since they were occurring multiple times.
Note: You only have to remove the extra occurrences i.e. each element in the final array should have a frequency equal to one., I have written this Solution Code: nan, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Remove duplicates of an array and return an array of only unique elements.An array containing numbers.Space separated unique elements from the array.Sample Input:-
1 2 3 5 1 5 9 1 2 8
Sample Output:-
1 2 3 5 9 8
<b>Explanation:-</b>
Extra 1, 2, and 5 were removed since they were occurring multiple times.
Note: You only have to remove the extra occurrences i.e. each element in the final array should have a frequency equal to one., I have written this Solution Code:
inp = eval(input(""))
new_set = []
for i in inp:
if(str(i) not in new_set):
new_set.append(str(i))
print(" ".join(new_set)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 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));
String str[]=br.readLine().split(" ");
long l=Long.parseLong(str[0]);
long r=Long.parseLong(str[1]);
long k=Long.parseLong(str[2]);
long count=(r/k)-((l-1)/k);
System.out.print(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: l,r,k=[int(x)for x in input().split()]
print(r//k - (l-1)//k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long x,l,r,k;
cin>>l>>r>>k;
x=l/k;
if(l%k==0){x--;}
cout<<r/k-x;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 5*10<sup>4</sup>
0 ≤ Arr[i] ≤ 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2
Sample Output:-
16
Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16
Sample Input:-
4
5 2 0 6
Sample Output:-
28, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a [] = new int[n];
for(int i = 0;i<n;i++){
a[i] = sc.nextInt();
}
System.out.print(subArrayRanges(a));
}
public static long subArrayRanges(int[] nums) {
int n = nums.length;
long sum = 0;
Deque<Integer> q = new ArrayDeque<>();
q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] <= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum += 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
q.clear();
q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] >= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum -= 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.
<b>Constraints:-</b>
1 ≤ N ≤ 5*10<sup>4</sup>
0 ≤ Arr[i] ≤ 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2
Sample Output:-
16
Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16
Sample Input:-
4
5 2 0 6
Sample Output:-
28, 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 100005
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int arr[max1],left1[max1],right1[max1];
int maxsum(int n)
{
FOR(i,n){
left1[i]=right1[i]=0;
}
stack<int> s1,s2;
FOR(i,n){
while(s1.size() != 0 && arr[s1.top()]<=arr[i]) {
left1[i] += left1[s1.top()] + 1;
s1.pop();
}
s1.push(i);
}
for (int i = n - 1; i >= 0; i--) {
while (s2.size() != 0 && arr[s2.top()] < arr[i]) {
right1[i] += right1[s2.top()] + 1;
s2.pop();
}
s2.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];
return ans;
}
int minsum(int n)
{ FOR(i,n){
left1[i]=right1[i]=0;
}
stack<int> s1,s2;
FOR(i,n){
while(s1.size() != 0 && arr[s1.top()]>arr[i]) {
left1[i] += left1[s1.top()] + 1;
s1.pop();
}
s1.push(i);
}
for (int i = n - 1; i >= 0; i--) {
while (s2.size() != 0 && arr[s2.top()] >= arr[i]) {
right1[i] += right1[s2.top()] + 1;
s2.pop();
}
s2.push(i);
}
int ans = 0;
for (int i = 0; i < n; i++)
ans += (left1[i] + 1) * (right1[i] + 1) * arr[i];
return ans;
}
void solve(){
int n;
cin>>n;
FOR(i,n){
cin>>arr[i];}
int maxs=maxsum(n);
int mins=minsum(n);
out1(maxs-mins);
}
signed main(){
fast();
solve();
}
, 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: 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: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: n = int(input())
arr = list(map(int, input().split()))
l = [0] * (max(arr) + 1)
for i in arr:
l[i] += 1
for i in range(len(l)):
if l[i] > 1:
print(i), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int m = 100001;
int main(){
int n,k;
cin>>n;
long long a[n],sum=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<0){
a[i]=-a[i];
}
}
sort(a,a+n);
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){cout<<a[i];return 0;}
}
cout<<sum;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Deque and Q queries. The task is to perform some operation on Deque according to the queries as described in input:
Note:-if deque is empty than pop operation will do nothing, and -1 will be printed as a front and rear element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push_front_pf()</b>:- that takes the deque and the integer to be added as a parameter.
<b>push_bac_pb()</b>:- that takes the deque and the integer to be added as a parameter.
<b>pop_back_ppb()</b>:- that takes the deque as parameter.
<b>front_dq()</b>:- that takes the deque as parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>
<b>Custom Input: </b>
First line of input should contain the number of queries Q. Next, Q lines should contain any of the given operations:-
For <b>push_front</b> use <b> pf x</b> where x is the element to be added
For <b>push_rear</b> use <b> pb x</b> where x is the element to be added
For <b>pop_back</b> use <b> pp_b</b>
For <b>Display Front</b> use <b>f</b>
Moreover driver code will print
Front element of deque in each push_front opertion
Last element of deque in each push_back operation
Size of deque in each pop_back operation The front_dq() function will return the element at front of your deque in a new line, if the deque is empty you just need to return -1 in the function.Sample Input:
6
push_front 2
push_front 3
push_rear 5
display_front
pop_rear
display_front
Sample Output:
3
3, I have written this Solution Code:
static void push_back_pb(Deque<Integer> dq, int x)
{
dq.add(x);
}
static void push_front_pf(Deque<Integer> dq, int x)
{
dq.addFirst(x);
}
static void pop_back_ppb(Deque<Integer> dq)
{
if(!dq.isEmpty())
dq.pollLast();
else return;
}
static int front_dq(Deque<Integer> dq)
{
if(!dq.isEmpty())
return dq.peek();
else return -1;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, 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 y[]=rd.readLine().split(" ");
long n=Long.parseLong(y[0]);
long p=Long.parseLong(y[1]);
long v=1;
while(p>0){
if((p&1L)==1L)
v=(v*n)%1000000007;
p/=2;
n=(n*n)%1000000007;
}
System.out.print(v);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code: n, p =input().split()
n, p =int(n), int(p)
def FastModularExponentiation(b, k, m):
res = 1
b = b % m
while (k > 0):
if ((k & 1) == 1):
res = (res * b) % m
k = k >> 1
b = (b * b) % m
return res
m=pow(10,9)+7
print(FastModularExponentiation(n, p, m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0) return 0; // In case x is divisible by 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;
}
// Driver code
signed main()
{
int x ;
int y;
cin>>x>>y;
int p = 1e9+7;
cout<< power(x, y, p);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] val = new int[n];
for(int i=0; i<n; i++){
val[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] freq = new int[n];
for(int i=0; i<n; i++){
freq[i] = Integer.parseInt(st.nextToken());
}
int k = Integer.parseInt(br.readLine());
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (val[j] < val[i]) {
int temp = val[i];
val[i] = val[j];
val[j] = temp;
int temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;
}
}
}
int element=0;
for(int i=0; i<n; i++){
for(int j=0; j<freq[i]; j++){
element++;
int value = val[i];
if(element==k){
System.out.print(value);
break;
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: def myFun():
n = int(input())
arr1 = list(map(int,input().strip().split()))
arr2 = list(map(int,input().strip().split()))
k = int(input())
arr = []
for i in range(n):
arr.append((arr1[i], arr2[i]))
arr.sort()
c = 0
for i in arr:
k -= i[1]
if k <= 0:
print(i[0])
return
myFun()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define inf 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int N;ll K;
cin>>N;
int c=0;
pair<int, ll> A[N];
for(int i=0;i<N;++i){
cin >> A[i].first ;
}
for(int i=0;i<N;++i){
cin >> A[i].second ;
}
cin>>K;
sort(A, A+N);
for(int i=0;i<N;++i){
K -= A[i].second;
if(K <= 0){
cout << A[i].first << endl;;
break;
}
}
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list, the task is to move all 0’s to the front of the linked list. The order of all another element except 0 should be same after rearrangement.
Note: Avoid use of any type of Java Collection frameworks.
Note: For custom input/output, enter the list in reverse order, and the output will come in right order.<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>moveZeroes()</b> that takes head node as parameter.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0<=Node.data<=100000
Note:- Sum of all test cases doesn't exceed 10^5
Return the head of the modified linked list.Input:
2
10
0 4 0 5 0 2 1 0 1 0
7
1 1 2 3 0 0 0
Output:
0 0 0 0 0 4 5 2 1 1
0 0 0 1 1 2 3
Explanation:
Testcase 1:
Original list was 0->4->0->5->0->2->1->0->1->0->NULL.
After processing list becomes 0->0->0->0->0->4->5->2->1->1->NULL.
Testcase 2:
Original list was 1->1->2->3->0->0->0->NULL.
After processing list becomes 0->0->0->1->1->2->3->NULL., I have written this Solution Code: static public Node moveZeroes(Node head){
ArrayList<Integer> a=new ArrayList<>();
int c=0;
while(head!=null){
if(head.data==0){
c++;
}
else{
a.add(head.data);
}
head=head.next;
}
head=null;
for(int i=a.size()-1;i>=0;i--){
if(head==null){
head=new Node(a.get(i));
}
else{
Node temp=new Node(a.get(i));
temp.next=head;
head=temp;
}
}
while(c-->0){
Node temp=new Node(0);
temp.next=head;
head=temp;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below Equation for the given number.
Equation: -
N
∑ {(X - 1)<sup>3</sup> + 3(X + 1)<sup>2</sup> + 2}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 100Return the sum of equation.Sample Input:-
1
Sample Output:-
14
Sample Input:-
2
Sample Output:-
44, I have written this Solution Code: static int equationSum(int n)
{
int sum=n*(n+1);
sum=sum/2;
sum=sum*sum;
sum+=9*((n*(n+1))/2);
sum+=4*(n);
return sum;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below Equation for the given number.
Equation: -
N
∑ {(X - 1)<sup>3</sup> + 3(X + 1)<sup>2</sup> + 2}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 100Return the sum of equation.Sample Input:-
1
Sample Output:-
14
Sample Input:-
2
Sample Output:-
44, I have written this Solution Code: def equationSum(N) :
re=(N*(N+1))//2
re=re*re
re=re+9*((N*(N+1))//2)
re=re+4*N
return re
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below Equation for the given number.
Equation: -
N
∑ {(X - 1)<sup>3</sup> + 3(X + 1)<sup>2</sup> + 2}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 100Return the sum of equation.Sample Input:-
1
Sample Output:-
14
Sample Input:-
2
Sample Output:-
44, I have written this Solution Code: int equationSum(int n){
int sum=n*(n+1);
sum=sum/2;
sum=sum*sum;
sum+=9*((n*(n+1))/2);
sum+=4*(n);
return sum;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the value of the below Equation for the given number.
Equation: -
N
∑ {(X - 1)<sup>3</sup> + 3(X + 1)<sup>2</sup> + 2}
X = 1<b>User task:</b>
Since this is a functional problem, you don’t have to worry about the input, you just have to complete the function <b>equationSum()</b>, where you will get N as a parameter.
<b>Constraints:</b>
1 <= N <= 100Return the sum of equation.Sample Input:-
1
Sample Output:-
14
Sample Input:-
2
Sample Output:-
44, I have written this Solution Code: int equationSum(int n){
int sum=n*(n+1);
sum=sum/2;
sum=sum*sum;
sum+=9*((n*(n+1))/2);
sum+=4*(n);
return sum;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
boolean r=false,e=false,d=false;
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if(ch=='r')r=true;
if(ch=='e')e=true;
if(ch=='d')d=true;
}
String ans = (r && e && d)?"Yes":"No";
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, I have written this Solution Code: S = input()
if ("r" in S and "e" in S and "d" in S ) :
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You're given a string S of lowercase letters of the english alphabet. Find whether you can choose some characters of the string S in any order to create the string "red".The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100
All the characters in S are lowercase letters of the english alphabet.Output "Yes" (without quotes) if you can create the string "red", else output "No" (without quotes).Sample Input
damngrey
Sample Output
Yes
Explanation: We choose character at position 6, then position 7, then position 1.
Sample Input
newtonschool
Sample Output
No, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
string s; cin>>s;
map<char, int> m;
For(i, 0, sz(s)){
m[s[i]]++;
}
if(m['r'] && m['e'] && m['d']){
cout<<"Yes";
}
else{
cout<<"No";
}
}
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 sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main
{
public static void main(String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int n = Integer.parseInt(read.readLine());
int[] arr = new int[n];
String str[] = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
arr = sortedSquares(arr);
for(int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}
public static int[] sortedSquares(int[] A) {
int[] nums = new int[A.length];
int k=A.length-1;
int i=0, j=A.length-1;
while(i<=j){
if(Math.abs(A[i]) <= Math.abs(A[j])){
nums[k--] = A[j]*A[j];
j--;
}
else{
nums[k--] = A[i]*A[i];
i++;
}
}
return nums;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an sorted array <b>Arr[]</b> of size <b>N</b>, containing both <b>negative</b> and <b>positive</b> integers, you need to print the squared sorted output.
<b>Note</b> Try using two pointer approachThe first line of input contains T, denoting the number of test cases. Each testcase contains 2 lines. The first line contains the N size of the array. The second line contains elements of an array separated by space.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10000
-10000 ≤ A[i] ≤ 10000
The Sum of N over all test cases does not exceed 10^6For each test case you need to print the sorted squared output in new lineInput:
1
5
-7 -2 3 4 6
Output:
4 9 16 36 49, I have written this Solution Code: t = int(input())
for i in range(t):
n = int(input())
for i in sorted(map(lambda j:int(j)**2,input().split())):
print(i,end=' ')
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a Doubly linked list and an integer K . Your task is to insert the integer K at the head of the given linked list<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>insertnew()</b> that takes the head node and the integer K as a parameter.
<b>Constraints:</b>
1 <=N<= 1000
1 <=K, value<= 1000Return the head of the modified linked listSample Input:-
5 2
1 2 3 4 5
Sample Output:
2 1 2 3 4 5
, I have written this Solution Code: public static Node insertnew(Node head, int k) {
Node temp = new Node(k);
temp.next = head;
head.prev=temp;
return temp;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 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));
String str[]=br.readLine().split(" ");
long l=Long.parseLong(str[0]);
long r=Long.parseLong(str[1]);
long k=Long.parseLong(str[2]);
long count=(r/k)-((l-1)/k);
System.out.print(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: l,r,k=[int(x)for x in input().split()]
print(r//k - (l-1)//k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long x,l,r,k;
cin>>l>>r>>k;
x=l/k;
if(l%k==0){x--;}
cout<<r/k-x;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code: n=1
index=0
li=[]
while n!=0:
n=int(input())
li.append(n)
index=index+1
#li = list(map(int,input().strip().split()))
for i in range(0,len(li)-1):
print(li[i],end=" ")
print(li[len(li)-1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 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 n=100001;
int a;
for(int i=0;i<n;i++){
a=sc.nextInt();
System.out.print(a+" ");
if(a==0){break;}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Take input from standard input while you do not get 0 as an input. Print all the inputs separated by space. It is guaranteed that the number of integers are less than 100000.The input will contain a series of integers in one line each. Input should be taken while you have not get a 0 as an input.
0 <= input <= 10Print the input integers seperated by space.Sample Input
6
5
5
0
Sample Output
6 5 5 0
Sample Input
9
3
5
7
6
9
8
3
2
7
7
3
5
0
Sample Output
9 3 5 7 6 9 8 3 2 7 7 3 5 0, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n;
while(cin >> n){
cout << n << " ";
if(n == 0) break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have r red and b blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet:
1. Has at least one red bean (or the number of red beans r<sub>i</sub> ≤ 1);
2. Has at least one blue bean (or the number of blue beans b<sub>i</sub> ≤ 1);
3. The number of red and blue beans should differ in no more than d (or |r<sub>i</sub>−b<sub>i</sub>| ≤ d)
Can you distribute all beans?The input consists of three space- separated integers r, b, and d — the number of red and blue beans and the maximum absolute difference in each packet.
<b>Constraints</b>
1 ≤ r, b ≤10<sup>9</sup>
0 ≤ d ≤10<sup>9</sup>If you can distribute all beans, print Yes. Otherwise, print No.<b>Sample Input 1</b>
1 1 0
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
6 1 4
<b>Sample Output 2</b>
No, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
t = 1;
while (t--)
{
long long r, b, d;
cin >> r >> b >> d;
long long k, m;
if (r > b)
{
k = b;
m = r;
}
else
{
k = r;
m = b;
}
if (((1 + d) * k) >= 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: You must be familiar with various types of operators. One of the most commonly used operators in any language are increment and decrement operators. Given two numbers X and Y. Your task is to print the value of X decremented by 1 and value of Y after incremented by 1.The first line of the input contains two integers X and Y.
<b>Constraints:</b>
1 ≤ X, Y ≤ 10000You need to perform the task as mentioned in the question, and finally, print the result separated by a space.Sample Input 1:
4 5
Sample Output 1:
3 6
Sample Input 2:
5 6
Sample Output 2:
4 7, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
x--;
y++;
System.out.print(x);
System.out.print(" ");
System.out.print(y);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You must be familiar with various types of operators. One of the most commonly used operators in any language are increment and decrement operators. Given two numbers X and Y. Your task is to print the value of X decremented by 1 and value of Y after incremented by 1.The first line of the input contains two integers X and Y.
<b>Constraints:</b>
1 ≤ X, Y ≤ 10000You need to perform the task as mentioned in the question, and finally, print the result separated by a space.Sample Input 1:
4 5
Sample Output 1:
3 6
Sample Input 2:
5 6
Sample Output 2:
4 7, I have written this Solution Code: def incremental_decremental(x, y):
x -= 1
y += 1
print(x, y, end =' ')
def main():
input1 = input().split()
x = int(input1[0])
y = int(input1[1])
#z = int(input1[2])
incremental_decremental(x, y)
if __name__ == '__main__':
main(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String StrInput[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(StrInput[0]);
int s = Integer.parseInt(StrInput[1]);
int arr[] = new int[n];
String StrInput2[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(StrInput2[i]);
}
int sum = arr[0];
int startingindex = 0;
int endingindex = 1;
int j = 0;
int i;
for(i=1;i<=n;i++)
{
if(sum < s && arr[i] != 0)
{
sum += arr[i];
}
while(sum > s && startingindex < i-1)
{
sum -= arr[startingindex];
startingindex++;
}
if(sum == s)
{
endingindex = i+1;
if(arr[0] == 0)
{
System.out.print(startingindex+2 + " " + endingindex);
}
else
{
System.out.print(startingindex+1 + " "+ endingindex);
}
break;
}
if(i == n && sum < s)
{
System.out.print(-1);
break;
}
}
}
catch(Exception e)
{
System.out.print(-1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==k){cout<<1<<" "<<i+1;return 0;}
if(m.find(sum-k)!=m.end()){
cout<<m[sum-k]+2<<" "<<i+1;
return 0;
}
m[sum]=i;
}
cout<<-1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: def sumFinder(N,S,a):
currentSum = a[0]
start = 0
i = 1
while i <= N:
while currentSum > S and start < i-1:
currentSum = currentSum - a[start]
start += 1
if currentSum == S:
return (start+1,i)
if i < N:
currentSum = currentSum + a[i]
i += 1
return(-1)
N, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = sumFinder(N,S,a)
if(ans==-1):
print(ans)
else:
print(ans[0],ans[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: 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: Given an array Arr with N distinct integers from 1 to N. If you are currently at index i then in a single jump you move to the index Arr[i]. You start at standing at index 1. Find the index you will be after K jumps.First line of input contains two integers N and K.
Second line of input contains N integers, denoting Arr.
Constraints:
1 <= N <= 100000
1 <= K <= 1000000000000
1 <= Arr[i] <= NPrint the index you will be on after K jumps.Sample Input 1
5 3
3 4 2 5 1
Sample Output 1
4
Explanation:
You start at index 1
After first jump you reach index 3
After second jump you reach index 2
After third jump you reach index 4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer str = new StringTokenizer(read.readLine());
int N = Integer.parseInt(str.nextToken());
long K = Long.parseLong(str.nextToken());
int[] arr = new int[N];
StringTokenizer newStr = new StringTokenizer(read.readLine());
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(newStr.nextToken());
}
long count = 0;
int ele = 1;
long copyOfK = K;
while(K > 0) {
ele = arr[ele - 1];
count++;
K--;
if(ele == 1) {
K = copyOfK % count;
}
}
System.out.print(ele);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr with N distinct integers from 1 to N. If you are currently at index i then in a single jump you move to the index Arr[i]. You start at standing at index 1. Find the index you will be after K jumps.First line of input contains two integers N and K.
Second line of input contains N integers, denoting Arr.
Constraints:
1 <= N <= 100000
1 <= K <= 1000000000000
1 <= Arr[i] <= NPrint the index you will be on after K jumps.Sample Input 1
5 3
3 4 2 5 1
Sample Output 1
4
Explanation:
You start at index 1
After first jump you reach index 3
After second jump you reach index 2
After third jump you reach index 4, I have written this Solution Code: n,k = map(int,input().split())
arr = list(map(int,input().split()))
visitedElements = {}
i = 0
curr = 0
for i in range(k):
currentIndex = arr[curr]
visitedElements[currentIndex] = visitedElements.get(currentIndex,0)+1
if visitedElements[currentIndex]>1:
break
curr = currentIndex -1
print(list(visitedElements.keys())[(k-1)%len(list(visitedElements.keys()))])
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr with N distinct integers from 1 to N. If you are currently at index i then in a single jump you move to the index Arr[i]. You start at standing at index 1. Find the index you will be after K jumps.First line of input contains two integers N and K.
Second line of input contains N integers, denoting Arr.
Constraints:
1 <= N <= 100000
1 <= K <= 1000000000000
1 <= Arr[i] <= NPrint the index you will be on after K jumps.Sample Input 1
5 3
3 4 2 5 1
Sample Output 1
4
Explanation:
You start at index 1
After first jump you reach index 3
After second jump you reach index 2
After third jump you reach index 4, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int a[n+1];
int k;
cin>>k;
for(int i=1;i<=n;++i)
cin>>a[i];
int vis[n+1]={};
int x=1;
int c=0;
while(vis[x]==0){
vis[x]=1;
++c;
x=a[x];
}
k=k%c;
x=1;
while(k){
--k;
x=a[x];
}
cout<<x;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
int t=Integer.parseInt(str[0]);
while(t-->0){
str=br.readLine().split(" ");
int k=Integer.parseInt(str[0]);
int n=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
String[] st=new String[k];
for(int i=0;i<k;i++){
st[i]="";
}
for(int i=0;i<n;i++){
st[arr[i]%k]+="->"+str[i];
}
for(int i=0;i<k;i++){
if(st[i]!=""){
System.out.println(i+st[i]);
}
}
System.out.println("~");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: t=int(input())
for i in range(t):
rowNcol=input().split()
els=input().split()
ds=[]
for i in range(int(rowNcol[0])):
col=[]
ds.append(col)
for i in range(int(rowNcol[1])):
ds[int(int(els[i])%int(rowNcol[0]))].append(els[i])
for i in range(len(ds)):
if(len(ds[i])==0):
continue
else:
print(i,end="")
for j in range(len(ds[i])):
print("->"+ds[i][j],end="")
print()
print("~"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 1000001
int main(){
int t;
cin>>t;
while(t--){
int n,m,x;
cin>>m>>n;
vector<int> a[m];
for(int i=0;i<n;i++){
cin>>x;
a[x%m].push_back(x);
}
for(int i=0;i<m;i++){
if(a[i].size()==0){continue;}
cout<<i<<"->";
for(int j=0;j<a[i].size()-1;j++){
cout<<a[i][j]<<"->";
}
cout<<a[i][a[i].size()-1]<<endl;
}
cout<<"~"<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Negi is fascinated with the binary representation of the number. Tell him the number of set bits (ones) in the binary representation of an integer N.The first line of the input contains single integer N.
Constraints
1 <= N <= 1000000000000The output should contain a single integer, the number of set bits (ones) in the binary representation of an integer N.Sample Input
7
Sample Output
3
Sample Input
16
Sample Output
1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(br.readLine());
int count = 0;
try{
while (n > 0) {
count += n & 1;
n >>= 1;
}
}catch(Exception e){
return ;
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Negi is fascinated with the binary representation of the number. Tell him the number of set bits (ones) in the binary representation of an integer N.The first line of the input contains single integer N.
Constraints
1 <= N <= 1000000000000The output should contain a single integer, the number of set bits (ones) in the binary representation of an integer N.Sample Input
7
Sample Output
3
Sample Input
16
Sample Output
1, I have written this Solution Code: a=int(input())
l=bin(a)
print(l.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Negi is fascinated with the binary representation of the number. Tell him the number of set bits (ones) in the binary representation of an integer N.The first line of the input contains single integer N.
Constraints
1 <= N <= 1000000000000The output should contain a single integer, the number of set bits (ones) in the binary representation of an integer N.Sample Input
7
Sample Output
3
Sample Input
16
Sample Output
1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
signed main()
{
int n;
cin>>n;
int cnt=0;
while(n>0)
{
int p=n%2LL;
cnt+=p;
n/=2LL;
}
cout<<cnt<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main{
public static void swap(int[] arr,int i, int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static int partition(int[] arr,int l,int r){
int pivot=arr[r];
int i=l-1;
for(int j=l;j<r;j++){
if(arr[j]>pivot){
i++;
swap(arr,i,j);
}
}
swap(arr,i+1,r);
return i+1;
}
public static void quickSort(int[] arr,int l,int r){
if(l<r){
int pivot=partition(arr,l,r);
quickSort(arr,l,pivot-1);
quickSort(arr,pivot+1,r);
}
}
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();
}
int target=sc.nextInt();
quickSort(arr,0,n-1);
int i=0,j=n-1;
while(i<j){
if((arr[i]+arr[j])==target){
System.out.print("Pair found ("+arr[i]+", "+arr[j]+")");
return;
}
else if((arr[i]+arr[j])<target){
j--;
}
else{
i++;
}
}
System.out.print("Pair not found");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code:
N=int(input())
arr=list(map(int,input().split()))
target=int(input())
arr.sort(reverse=True)
'''
ans='Pair not found'
for i in range(N):
for j in range(i+1,N):
if arr[i]+arr[j]==target:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
print(ans)
'''
i=0
j=N-1
ans='Pair not found'
while i<j:
if arr[i]+arr[j]<target:
j-=1
elif arr[i]+arr[j]>target:
i+=1
j+=1
else:
ans='Pair found ({}, {})'.format(arr[i],arr[j])
break
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted integer array having distinct integers, find a pair with the given sum in it.
First element of pair > second element of pairFirst line of input contains a single integer N, next line contains N space separated integers depicting the values of array and third line consist target sum.
Constraints:
2<=N<=5*(10^5)
1<=A[i], target<=2*(10^9)
TargetPrint the pair of integers which sum is target.
Sample Input1:-
6
8 7 4 5 3 1
10
Sample Output:-
Pair found (7, 3)
Sample Input2:
6
5 2 6 8 1 9
12
Sample Output:
Pair not found, I have written this Solution Code: #include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
// Function to find a pair in an array with a given sum using sorting
void findPair(vector<int> &nums, int n, int target)
{
// sort the array in ascending order
sort(nums.begin(), nums.end());
// maintain two indices pointing to endpoints of the array
int low = 0;
int high = n - 1;
// reduce the search space `nums[low…high]` at each iteration of the loop
// loop till the search space is exhausted
while (low < high)
{
// sum found
if (nums[low] + nums[high] == target)
{
if (nums[low] < nums[high])
swap(nums[low], nums[high]);
cout << "Pair found (" << nums[low] << ", " << nums[high] << ")\n";
return;
}
// increment `low` index if the total is less than the desired sum;
// decrement `high` index if the total is more than the desired sum
(nums[low] + nums[high] < target) ? low++ : high--;
}
// we reach here if the pair is not found
cout << "Pair not found";
}
int main()
{
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++)
cin >> nums[i];
int target;
cin >> target;
findPair(nums, n, target);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In this question, you will be adding a new method called <code>squareAndSort</code> to the <code>Array prototype</code>. This means that any array created in the future will have this method available to it. This method will be called on the <code>array with number and string data type elements</code>.
The method should <code>filter</code> the <code>number datatype</code> from the <code>string datatype</code> elements and <code>return</code> a <code>sorted squared array in ascending order</code> of the remaining number element array.The squareAndSort method will be called on an array with elements of number and string datatypes.The squareAndSort method should return an array with elements of number datatype.const numbers = [5,2,1,"four",4];
console. log(numbers. squareAndSort()); // [ 1, 4, 16, 25 ], I have written this Solution Code: Array.prototype.squareAndSort = function() {
const filteredArray = this.filter(element => typeof element === "number");
const squaredArray = filteredArray.map(number => number ** 2);
const sortedArray = squaredArray.sort((a, b) => a - b);
return sortedArray;
};, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given positive integer N, determine if there exist three <b>positive</b> integers a, b and c such that the following two conditions hold:
1. a + b + c = N
2. a ^ b ^ c = 0 where ^ denotes the bitwise XOR operation.
If there exist such a triple (a, b, c), print the lexicographically smallest one.
Else, print -1.The first line of input contains a single integer, T.
T lines follow, each containing a single integer, N.
<b>Constraints:</b>
1 <= T <= 10<sup>3</sup>
3 <= N <= 10<sup>18</sup>For each test, in a new line, print the lexicographically smallest triple (a, b, c) if it exists, else print -1.Sample Input:
3
3
6
12
Sample Output:
-1
1 2 3
2 4 6, I have written this Solution Code: for _ in range(int(input())):
n = int(input())
if n%2 or n<6:
print(-1)
continue
m = n//2
sl = 1
while m % 2 == 0:
m >>= 1
sl <<= 1
if n == sl*2:
print(-1)
continue
print(sl,n//2-sl,n//2), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given positive integer N, determine if there exist three <b>positive</b> integers a, b and c such that the following two conditions hold:
1. a + b + c = N
2. a ^ b ^ c = 0 where ^ denotes the bitwise XOR operation.
If there exist such a triple (a, b, c), print the lexicographically smallest one.
Else, print -1.The first line of input contains a single integer, T.
T lines follow, each containing a single integer, N.
<b>Constraints:</b>
1 <= T <= 10<sup>3</sup>
3 <= N <= 10<sup>18</sup>For each test, in a new line, print the lexicographically smallest triple (a, b, c) if it exists, else print -1.Sample Input:
3
3
6
12
Sample Output:
-1
1 2 3
2 4 6, I have written this Solution Code: //HEADER FILES AND NAMESPACES
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #include <sys/resource.h>
using namespace std;
using namespace __gnu_pbds;
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_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// PRAGMAS (do these even work?)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
// DEFINE STATEMENTS
const long long infty = 1e18;
#define num1 1000000007
#define num2 998244353
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define REPd(i,a,n) for(ll i=a; i>=n; i--)
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (double)(0.000000001)
#define popcount __builtin_popcountll
#define fileio(x) freopen("input.txt", "r", stdin); freopen(x, "w", stdout);
#define out(x) cout << ((x) ? "Yes\n" : "No\n")
#define sz(x) x.size()
#define start_clock() auto start_time = std::chrono::high_resolution_clock::now();
#define measure() auto end_time = std::chrono::high_resolution_clock::now(); cerr << (end_time - start_time)/std::chrono::milliseconds(1) << "ms" << endl;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<int> vii;
// DEBUG FUNCTIONS
#ifdef LOCALY
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}"[it+1==a.end()];
}
template<typename T>
void __p(std::set<T> a) {
cout<<"{";
for(auto it=a.begin(); it!=a.end();){
__p(*it);
cout<<",}"[++it==a.end()];
}
}
template<typename T>
void __p(std::multiset<T> a) {
cout<<"{";
for(auto it=a.begin(); it!=a.end();){
__p(*it);
cout<<",}"[++it==a.end()];
}
}
template<typename T, typename F>
void __p(std::map<T,F> a) {
cout<<"{\n";
for(auto it=a.begin(); it!=a.end();++it)
{
__p(it->first);
cout << ": ";
__p(it->second);
cout<<"\n";
}
cout << "}\n";
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
// DEBUG FUNCTIONS END
// CUSTOM HASH TO SPEED UP UNORDERED MAP AND TO AVOID FORCED CLASHES
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // FOR RANDOM NUMBER GENERATION
ll mod_exp(ll a, ll b, ll c)
{
ll res=1; a=a%c;
while(b>0)
{
if(b%2==1)
res=(res*a)%c;
b/=2;
a=(a*a)%c;
}
return res;
}
ll mymod(ll a,ll b)
{
return (((a = a%b) < 0) ? a + b : a);
}
ll gcdExtended(ll,ll,ll *,ll *);
ll modInverse(ll a, ll m)
{
ll x, y;
ll g = gcdExtended(a, m, &x, &y);
g++; //this line was added just to remove compiler warning
ll res = (x%m + m) % m;
return res;
}
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
ll x1, y1;
ll gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
struct Graph
{
vector<vector<int>> adj;
Graph(int n)
{
adj.resize(n+1);
}
void add_edge(int a, int b, bool directed = false)
{
adj[a].pb(b);
if(!directed) adj[b].pb(a);
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
REP(i, 0, t)
{
ll n;
cin >> n;
if(n&1) cout << "-1\n";
else
{
ll x = n/2;
vll bits;
REP(i, 0, 60)
{
if((x >> i)&1)
{
bits.pb(i);
}
}
if(bits.size() == 1)
{
cout << "-1\n";
}
else
{
ll a = (1ll << bits[0]);
cout << a << " " << x - a << " " << x << "\n";
}
}
}
return 0;
}
/*
1. Check borderline constraints. Can a variable you are dividing by be 0?
2. Use ll while using bitshifts
3. Do not erase from set while iterating it
4. Initialise everything
5. Read the task carefully, is something unique, sorted, adjacent, guaranteed??
6. DO NOT use if(!mp[x]) if you want to iterate the map later
7. Are you using i in all loops? Are the i's conflicting?
*/
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given positive integer N, determine if there exist three <b>positive</b> integers a, b and c such that the following two conditions hold:
1. a + b + c = N
2. a ^ b ^ c = 0 where ^ denotes the bitwise XOR operation.
If there exist such a triple (a, b, c), print the lexicographically smallest one.
Else, print -1.The first line of input contains a single integer, T.
T lines follow, each containing a single integer, N.
<b>Constraints:</b>
1 <= T <= 10<sup>3</sup>
3 <= N <= 10<sup>18</sup>For each test, in a new line, print the lexicographically smallest triple (a, b, c) if it exists, else print -1.Sample Input:
3
3
6
12
Sample Output:
-1
1 2 3
2 4 6, I have written this Solution Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
import static java.lang.System.out;
public class Main {
void solve(){
long n= in.nextLong();
if(n%2==1){
sb.append(-1).append("\n");
}
else{
long cnt=0;
n=n/2;
while(n%2==0){
cnt++;
n=n>>1;
}
long x=(1L<<cnt);
long y=(1L<<cnt);
long z=0;
while(n!=0){
n=n>>1;
cnt++;
if((n&1)==1){
y+=(1L<<cnt);
z+=(1L<<cnt);
}
}
long[] a= new long[3];
a[0]=x;
a[1]=y;
a[2]=z;
sort(a);
if(a[0]!=0){
sb.append(a[0]).append(" ");
sb.append(a[1]).append(" ");
sb.append(a[2]).append("\n");
}
else{
sb.append(-1).append("\n");
}
}
}
FastReader in;
StringBuffer sb;
public static void main(String[] args) {
new Main().run();
}
void run(){
in= new FastReader();
start();
}
void start(){
sb= new StringBuffer();
for(int t=in.nextInt();t>0;t--) {
solve();
}
out.print(sb);
}
void swap( int i , int j) {
int tmp = i;
i = j;
j = tmp;
}
long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int lower_bound(long[] a, long x) {
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
int upper_bound(long[] arr, int key) {
int i=0, j=arr.length-1;
if (arr[j]<=key) return j+1;
if(arr[i]>key) return i;
while (i<j){
int mid= (i+j)/2;
if(arr[mid]<=key){
i= mid+1;
}else{
j=mid;
}
}
return i;
}
void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
int[] intArr(int n){
int[] res= new int[n];
for(int i=0;i<n;i++){
res[i]= in.nextInt();
}
return res;
}
long[] longArr(int n){
long[] res= new long[n];
for(int i=0;i<n;i++){
res[i]= in.nextLong();
}
return res;
}
boolean isDigitSumPalindrome(long N) {
long sum= sumOfDigits(String.valueOf(N));
long rev=0;
long org= sum;
while (sum!=0){
long d= sum%10;
rev = rev*10 +d;
sum /= 10;
}
return org == rev;
}
long sumOfDigits(String n){
long sum= 0;
for (char c: n.toCharArray()){
sum += Integer.parseInt(String.valueOf(c));
}
return sum;
}
long[] revArray(long[] arr) {
int n= arr.length;
int i=0, j=n-1;
while (i<j){
long temp= arr[i];
arr[i]= arr[j];
arr[j]= temp;
i++;
j--;
}
return arr;
}
long gcd(long a, long b){
if (b==0)
return a;
return gcd(b, a%b);
}
long lcm(long a,long b){
return (a*b)/gcd(a,b);
}
static class Pair implements Comparable<Pair>{
long first;
long second;
Pair(long x, long y){
this.first=x;
this.second=y;
}
@Override
public int compareTo(Pair o) {
return 0;
}
}
public 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 (Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
float nextFloat(){
return Float.parseFloat(next());
}
String nextLine(){
String str="";
try{
str=br.readLine();
}catch (Exception e){
e.printStackTrace();
}
return str;
}
}
}, In this Programming Language: Java, 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: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args)throws IOException
{
StringBuilder out=new StringBuilder();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int test=Integer.parseInt(br.readLine());
while(test-->0)
{
int n=Integer.parseInt(br.readLine());
String s=br.readLine();
int c1=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1') c1++;
}
if(c1%4==0) out.append("1\n");
else if(c1==s.length() && (c1/2)%2==0) out.append("1\n");
else
out.append("0\n");
}
System.out.print(out);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: T=int(input())
for i in range(T):
n=int(input())
a=input()
count_1=0
for i in a:
if i=='1':
count_1+=1
if count_1%2==0 and ((count_1)//2)%2==0:
print('1')
else:
print('0'), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: This is a time of conflict in Westeros as Viserys Targaryen, the king of all seven kingdoms, rejected the hand of Lady Laena Velaryon. So, the Velaryon soldiers are not deemed trustworthy anymore. The Targaryen soldiers have to keep an eye on them.
You are given a sequential order of N soldiers standing in a line. The order is provided as a binary string, with 0 representing Velaryon soldiers, and 1 representing the Targaryen soldiers. Viserys wants each contiguous segment of N/2 soldiers to contain an even number of Targaryen soldiers.
Formally you are given a binary string of length N, where N is an even natural number. Each character of the string is either '0' or '1'. You want to rearrange the elements of the string in such a way that the final string contains an even number of 1s in each contiguous subsegment of length N/2.
Your task is to find out whether there exists a rearrangement of the soldiers that satisfies the above conditions.The first line contains an integer T, the number of test cases. Then, T test cases follow.
The first line of each test case contains an even positive integer N, the length of the line.
The second line of each test case contains a binary string of length N, representing the current arrangement of soldiers.
<b> Constraints: </b>
1 ≤ T ≤ 10
2 ≤ N ≤ 10<sup>4</sup>
N is evenPrint a single character in a new line for each test case. Print '1' (without quotes) if a required rearrangement exists, and '0' (without quotes) otherwise.Sample Input:
3
2
10
2
00
4
0011
Sample Output:
0
1
0
(In the last case, no matter how you rearrange the string, there will always be a single one in at least one subsegment of length 2 of the string), I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main() {
int t;
cin >> t;
for(int i=0; i<t; i++) {
int n;
cin >> n;
string s;
cin >> s;
int cnt = 0;
for(int j=0; j<n; j++) {
if(s[j] == '1') cnt++;
}
if(cnt % 4 == 0) cout << 1 << "\n";
else cout << 0 << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an exam of JEE one aspirant got P correct answers, Q wrong answer, and R unattempted question. If the mark for the correct answer is 4, for the wrong answer is -2, and for the unattempted questions is -1. Find the final marks the aspirant got.<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>Marks()</b> that takes integer P, Q, and R as arguments.
Constraints:-
0 <= P, Q, R <= 1000Return the final marks of each student.Sample Input:-
4 2 0
Sample Output:-
12
Sample Input:-
1 1 1
Sample Output:-
1, I have written this Solution Code: int Marks(int P, int Q, int R){
return 4*P - 2*Q - R;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In an exam of JEE one aspirant got P correct answers, Q wrong answer, and R unattempted question. If the mark for the correct answer is 4, for the wrong answer is -2, and for the unattempted questions is -1. Find the final marks the aspirant got.<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>Marks()</b> that takes integer P, Q, and R as arguments.
Constraints:-
0 <= P, Q, R <= 1000Return the final marks of each student.Sample Input:-
4 2 0
Sample Output:-
12
Sample Input:-
1 1 1
Sample Output:-
1, I have written this Solution Code: int Marks(int P, int Q, int R){
return 4*P - 2*Q - R;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.