Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an integer N, your task is to represent the given integer as a binary number.Input contains a single integer containing the value of N.
Constraints:-
0 <= N <= 1000000000Print a string containing the binary representation of the given integer.Sample Input:-
9
Sample Output:-
1001
Sample input:-
3
Sample Output:-
11, 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();
System.out.println(Integer.toBinaryString(N));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to represent the given integer as a binary number.Input contains a single integer containing the value of N.
Constraints:-
0 <= N <= 1000000000Print a string containing the binary representation of the given integer.Sample Input:-
9
Sample Output:-
1001
Sample input:-
3
Sample Output:-
11, I have written this Solution Code: a=int(input())
print(bin(a)[2:]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to represent the given integer as a binary number.Input contains a single integer containing the value of N.
Constraints:-
0 <= N <= 1000000000Print a string containing the binary representation of the given integer.Sample Input:-
9
Sample Output:-
1001
Sample input:-
3
Sample Output:-
11, 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 101
#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 t;
t=1;
while(t--){
int n;
cin>>n;
string s;
if(n==0){
s+='0';
}
while(n>0){
if(n&1){
s+='1';
}
else{
s+='0';
}
n/=2;
}
reverse(s.begin(),s.end());
out(s);
}
}
, 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: Find the determinant of a 3*3 matrix1st 3 lines: 3 space separated values for matrixDeterminant of the matrixInput
1 2 3
2 3 4
3 4 8
Output:
-3, I have written this Solution Code: import numpy as np
a=np.array([input().strip().split() for _ in range(3)],int)
ans=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2])-a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0])
print(ans)
# ans1=np.linalg.det(a)
# print(ans1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
int max = 1000001;
boolean isNotPrime[] = new boolean[max];
ArrayList<Integer> arr = new ArrayList<Integer>();
isNotPrime[0] = true; isNotPrime[1] = true;
for (int i=2; i*i <max; i++) {
if (!isNotPrime[i]) {
for (int j=i*i; j<max; j+= i) {
isNotPrime[j] = true;
}
}
}
for(int i=2; i<max; i++) {
if(!isNotPrime[i]) {
arr.add(i);
}
}
while(t-- > 0) {
String str[] = br.readLine().trim().split(" ");
int l = Integer.parseInt(str[0]);
int r = Integer.parseInt(str[1]);
System.out.println(primeRangeSum(l,r,arr));
}
}
static long primeRangeSum(int l , int r, ArrayList<Integer> arr) {
long sum = 0;
for(int i=l; i<=r;i++) {
sum += arr.get(i-1);
}
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: def SieveOfEratosthenes(n):
prime = [True for i in range(n+1)]
pri = []
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
pri.append(p)
return pri
N = int(input())
X = []
prim = SieveOfEratosthenes(1000000)
for i in range(1,len(prim)):
prim[i] = prim[i]+prim[i-1]
for i in range(N):
nnn = input()
X.append((int(nnn.split()[0]),int(nnn.split()[1])))
for xx,yy in X:
if xx==1:
print(prim[yy-1])
else:
print(prim[yy-1]-prim[xx-2])
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Let's define P[i] as the ith Prime Number. Therefore, P[1]=2, P[2]=3, P[3]=5, so on.
Given two integers L, R (L<=R), find the value of P[L]+P[L+1]+P[L+2]...+P[R].The first line of the input contains an integer T denoting the number of test cases.
The next T lines contain two integers L and R.
Constraints
1 <= T <= 50000
1 <= L <= R <= 50000For each test case, print one line corresponding to the required valueSample Input
4
1 3
2 4
5 5
1 5
Sample Output
10
15
11
28, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
vector<int> v;
v.push_back(0);
for(int i = 2; i < N; i++){
if(a[i]) continue;
v.push_back(i);
for(int j = i*i; j < N; j += i)
a[j] = 1;
}
int p = 0;
for(auto &i: v){
i += p;
p = i;
}
int t; cin >> t;
while(t--){
int l, r;
cin >> l >> r;
cout << v[r] - v[l-1] << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Natasha and Scott are playing a game with a single pile of stones. Initially the pile contains K stones. The players alternate turns, with Natasha starting first. In one move, the player can either add any odd number of stones to the pile or remove any odd number of stones from the pile. The rules are as follows:
1. The number of stones should not go below 0 or go above N.
2. If at any instance of the game there were x stones, the number of stones cannot be x thereafter.
3. Initially some set of numbers S = {s<sub>1</sub>, s<sub>2</sub>, s<sub>3</sub>, … } are given which are blocked, which means that we are not allowed to play a move after which we will end up with a number of stones which belongs to this set.
4. The player who cannot move loses.
For a given set S, let F(S) denote the number of K’s (0 ≤ K ≤ N) such that Natasha has a winning strategy for that game. They need you to find <b>∑ F(S)</b> where summation is over all the subsets of {0, 1, 2, ... N}.
This number can be very large, so they want you to print it modulo 998244353.The file contains multiple test cases. The first line contains an integer T - the number of test cases.
Then T lines follows, each line containing an integer N.
<b>Constraints:</b>
1 ≤ T ≤ 10<sup>6</sup>
1 ≤ N ≤ 10<sup>6</sup>
It is guaranteed that the sum of N over all tests is less than or equal to 10<sup>6</sup>.Output T lines.
The i<sup>th</sup> line should contain a single integer, the answer to the i<sup>th</sup> test case.Sample Input
3
1
2
3
Sample Output
2
5
16
Explanation:
Test Case 1: N = 1. There are 4 subsets {}, {0}, {1}, {0,1}.
a) {0,1}: The non Blocked are {}: No winning positions, as Natasha cannot even choose the first position.
b) {1}. The non Blocked are {0}: Clearly Natasha must start with 0 as only one position, but she cannot make move so she loses.
c) {0}. The non Blocked are {1}: Similar to (a).
d) {}. The non Blocked are {0,1}: In this case if Natasha start with 0, she can make first move to 1, then Scott does not have any valid move, so Natasha wins. similarly if she starts with 1.
So total winning positions = 0 + 0 + 0 + 2 = 2., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998'244'353;
const long long INF = 2e18 + 10;
#ifdef int
#undef int
#endif
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(signed) { Modular result(*this); *this += 1; return result; }
Modular operator--(signed) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
template <typename U>
friend const Modular<U>& abs(const Modular<U>& v) { return v; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
/*
using ModType = int;
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
constexpr int md = mod;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
Mint pwr(Mint a, int k) {
Mint ans = 1;
while(k) {
if(k & 1) ans = ans * a;
a = a * a;
k >>= 1;
}
return ans;
}
#define int long long
const int MAXN=1e6+10;
vector<Mint> fac(MAXN);
vector<Mint> infac(MAXN);
void build_factorial()
{
fac[0]=1;
infac[0]=1;
for(int i=1;i<MAXN;i++)
{
fac[i]=(fac[i-1]*i);
}
infac[MAXN-1] = pwr(fac[MAXN -1], mod - 2);
for(int i= MAXN-2;i>=0;i--)
{
infac[i] = infac[i+1] *(i + 1);
}
}
Mint ncr(int n, int r)
{
if(n == 0)
return 0 ;
if(n<r)
return 0;
return 1LL*fac[n] * infac[r] * infac[n-r];
}
Mint solv(int n)
{
Mint ans = 0;
int even = (n + 1) /2;
int odd = (n + 1) - even;
vector<Mint> pref_sm_odd( odd + 1);
vector<Mint> pref_sm_even( even + 1);
pref_sm_even[0] = 1;
pref_sm_odd[0] = 1;
for(int i = 1;i<=odd;i++)
{
pref_sm_odd[i] = pref_sm_odd[i-1] + ncr(odd, i);
}
for(int i = 1;i<=even;i++)
{
pref_sm_even[i] = pref_sm_even[i-1] + ncr(even, i);
}
for(int left_side_win = 1;left_side_win <=even;left_side_win ++ )
{
Mint pref = pref_sm_odd[odd] - pref_sm_odd[left_side_win-1];
ans += left_side_win * ( ncr( even, left_side_win ) * pref);
}
for(int right_side_win = 1;right_side_win <=odd;right_side_win ++ )
{
Mint pref = pref_sm_even[even] - pref_sm_even[right_side_win-1];
ans += right_side_win * ( ncr( odd, right_side_win ) * pref);
}
return ans;
}
void stress()
{
for(int n = 1;n<=10;n++)
{
cout<<n<<" "<<solv(n)<<endl;
}
exit(0);
}
void solve()
{
build_factorial( );
// stress();
int t = 1;
cin>>t;
for(int T =1;T<=t;T++)
{
int n;
cin>>n;
assert(n);
cout<<solv(n)<<'\n';
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#else
#ifdef ASC
namespace fs = std::filesystem;
std::string path = "./";
string filename;
for (const auto & entry : fs::directory_iterator(path)){
if( entry.path().extension().string() == ".in"){
filename = entry.path().filename().stem().string();
}
}
if(filename != ""){
string input_file = filename +".in";
string output_file = filename +".out";
if (fopen(input_file.c_str(), "r"))
{
freopen(input_file.c_str(), "r", stdin);
freopen(output_file.c_str(), "w", stdout);
}
}
#endif
#endif
// auto clk = clock();
// -------------------------------------Code starts here---------------------------------------------------------------------
signed t = 1;
// cin >> t;
for (signed test = 1; test <= t; test++)
{
// cout<<"Case #"<<test<<": ";
// cout<<endl;
solve();
}
// -------------------------------------Code ends here------------------------------------------------------------------
// clk = clock() - clk;
#ifndef ONLINE_JUDGE
// cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
#endif
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A</b> of <b>N</b> elements. Find the <b>majority</b> element in the array. A majority element in an array A of size N is an element that appears <b>more than N/2</b> times in the array.The first line of the input contains T denoting the number of testcases. The first line of the test case will contains the size of array N and second line will be the elements of the array.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= A[i] <= 10^6
<b>Sum of "N" over all testcases does not exceed 10^6</b>For each test case the output will be the majority element of the array. Output "<b>-1</b>" if no majority element is there in the array.Sample Input:
2
5
3 1 3 3 2
3
1 2 3
Sample Output:
3
-1
Explanation:
Testcase 1: Since, 3 is present more than N/2 times, so it is the majority element.
Testcase 2: Since, each element in {1, 2, 3} appears only once so there is no majority element., 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
const int N = 3e5+5;
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
const double pi=acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int li;
typedef unsigned long int uli;
typedef long long int ll;
typedef unsigned long long int ull;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 998244353;
using vl = vector<ll>;
bool isPowerOfTwo (int x)
{
/* First x in the below expression is
for the case when x is 0 */
return x && (!(x&(x-1)));
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
long long phi[max1], result[max1],F[max1];
// Precomputation of phi[] numbers. Refer below link
// for details : https://goo.gl/LUqdtY
void computeTotient()
{
// Refer https://goo.gl/LUqdtY
phi[1] = 1;
for (int i=2; i<max1; i++)
{
if (!phi[i])
{
phi[i] = i-1;
for (int j = (i<<1); j<max1; j+=i)
{
if (!phi[j])
phi[j] = j;
phi[j] = (phi[j]/i)*(i-1);
}
}
}
for(int i=1;i<=100000;i++)
{
for(int j=i;j<=100000;j+=i)
{ int p=j/i;
F[j]+=(i*phi[p])%mod;
F[j]%=mod;
}
}
}
int gcd(int a, int b, int& x, int& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g) {
g = gcd(abs(a), abs(b), x0, y0);
if (c % g) {
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
int main() {
fast();
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unordered_map<int,int> m;
int a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;}
int ma=0;int ans=-1;
for(auto it=m.begin();it!=m.end();it++){
if(ma<it->second){
ma=it->second;
if(ma>n/2){ans=it->first;}
}
}
out(ans);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array a that contains N integers. All the integers in the array may not be distinct. The number of repetitions of each integer in the array is represented by ri. Your task is to print the integers in the decreasing order of their occurrence in the array.
Note
1. If ri > rj, then ai must be printed before aj.
2. If ri == rj, then among ai and aj whichever has a larger value, is printed first.
Here ri and rj is the number of repetitions of integers ai and aj in the array.The first line of the input contains an integer N.
The second line contains N space- separated integers representing the elements of array a.
<b>Constraints</b>
1<= N <= 1000
1<= ai <= 1000Print the space separated integers in the decreasing order of their occurrence in the array. The output must be printed in a single line.Sample input:
6
1 2 3 3 2 1
Sample Output
3 2 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));
int n = Integer.parseInt(br.readLine());
Map<Integer,Integer> map = new HashMap<>();
String []str = br.readLine().split(" ");
for(int i = 0; i < n; i++){
int e = Integer.parseInt(str[i]);
map.put(e, map.getOrDefault(e,0)+1);
}
List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer,Integer>>(){
public int compare(Map.Entry<Integer,Integer> o1, Map.Entry<Integer,Integer> o2){
if(o1.getValue() == o2.getValue()){
return o2.getKey() - o1.getKey();
}
return o2.getValue() - o1.getValue();
}
});
for(Map.Entry<Integer,Integer> entry: list){
System.out.print(entry.getKey() + " ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array a that contains N integers. All the integers in the array may not be distinct. The number of repetitions of each integer in the array is represented by ri. Your task is to print the integers in the decreasing order of their occurrence in the array.
Note
1. If ri > rj, then ai must be printed before aj.
2. If ri == rj, then among ai and aj whichever has a larger value, is printed first.
Here ri and rj is the number of repetitions of integers ai and aj in the array.The first line of the input contains an integer N.
The second line contains N space- separated integers representing the elements of array a.
<b>Constraints</b>
1<= N <= 1000
1<= ai <= 1000Print the space separated integers in the decreasing order of their occurrence in the array. The output must be printed in a single line.Sample input:
6
1 2 3 3 2 1
Sample Output
3 2 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
bool static comp(pair<int, int> p, pair<int, int> q) {
if (p.second == q.second) {
return p.first > q.first;
}
return p.second > q.second;
}
vector<int> group_Sol(int N, vector<int> Arr) {
unordered_map<int, int> m;
for (auto &it : Arr) {
m[it] += 1;
}
vector<pair<int, int>> v;
for (auto &it : m) {
v.push_back(it);
}
sort(v.begin(), v.end(), comp);
vector<int> res;
for (auto &it : v) {
res.push_back(it.first);
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int> a(N);
for (int i_a = 0; i_a < N; i_a++) {
cin >> a[i_a];
}
vector<int> out_;
out_ = group_Sol(N, a);
cout << out_[0];
for (int i_out_ = 1; i_out_ < out_.size(); i_out_++) {
cout << " " << out_[i_out_];
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array a that contains N integers. All the integers in the array may not be distinct. The number of repetitions of each integer in the array is represented by ri. Your task is to print the integers in the decreasing order of their occurrence in the array.
Note
1. If ri > rj, then ai must be printed before aj.
2. If ri == rj, then among ai and aj whichever has a larger value, is printed first.
Here ri and rj is the number of repetitions of integers ai and aj in the array.The first line of the input contains an integer N.
The second line contains N space- separated integers representing the elements of array a.
<b>Constraints</b>
1<= N <= 1000
1<= ai <= 1000Print the space separated integers in the decreasing order of their occurrence in the array. The output must be printed in a single line.Sample input:
6
1 2 3 3 2 1
Sample Output
3 2 1, I have written this Solution Code: from collections import defaultdict
import numpy as np
n=int(input())
val=np.array([input().strip().split()],int).flatten()
d=defaultdict(int)
for i in val:
d[i]+=1
a=[]
for i in d:
a.append([d[i],i])
a.sort()
for i in range(len(a)-1,-1,-1):
print(a[i][1],end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given weights and values of N items, put some or all of these items in a knapsack of capacity W weight to get the maximum total value in the knapsack. Note that we have at most one quantity of each item.
In other words, given two integer arrays val[0..(N-1)] and wt[0..(N-1)] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
The first line consists of N the number of items.
The second line consists of W, the maximum capacity of the knapsack.
In the next line are N space separated positive integers denoting the values of the N items,
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ v[i] ≤ 1000For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.Input:
2
3
4
1 2 3
4 5 1
3
3
1 2 3
4 5 6
Output:
3
0, I have written this Solution Code: import java.util.*;
import java.io.*;
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public class Main {
private static int solve(int cpacity, int values[], int weights[], int i,int dp[][]) {
if (i == 0) {
if (weights[0]<=cpacity) {
return values[0];
}else{
return 0;
}
}
if (dp[i][cpacity] !=-1) {
return dp[i][cpacity];
}
int include = 0;
if (weights[i]<=cpacity) {
include = values[i] + solve(cpacity-weights[i], values, weights, i-1,dp);
}
int exclude = solve(cpacity, values, weights, i-1,dp);
return dp[i][cpacity] = Math.max(include, exclude);
}
public static void main(String[] args)throws IOException {
Reader sc = new Reader();
int t = sc.nextInt();
int dp[][] = new int[1000][1001];
while (t-- > 0) {
int n = sc.nextInt();
int maxCarry = sc.nextInt();
int values[] = new int[n];
int weights[] = new int[n];
for (int i = 0; i < n; i++) {
values[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
weights[i] = sc.nextInt();
}
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
System.out.println(solve(maxCarry, values, weights,n-1,dp));
}
sc.close();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given weights and values of N items, put some or all of these items in a knapsack of capacity W weight to get the maximum total value in the knapsack. Note that we have at most one quantity of each item.
In other words, given two integer arrays val[0..(N-1)] and wt[0..(N-1)] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of four lines.
The first line consists of N the number of items.
The second line consists of W, the maximum capacity of the knapsack.
In the next line are N space separated positive integers denoting the values of the N items,
and in the fourth line are N space separated positive integers denoting the weights of the corresponding items.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ W ≤ 1000
1 ≤ wt[i] ≤ 1000
1 ≤ v[i] ≤ 1000For each testcase, in a new line, print the maximum possible value you can get with the given conditions that you can obtain for each test case in a new line.Input:
2
3
4
1 2 3
4 5 1
3
3
1 2 3
4 5 6
Output:
3
0, I have written this Solution Code: // A Dynamic Programming based solution for 0-1 Knapsack problem
#include<bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }
// Returns the maximum value that can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
// Build table K[][] in bottom up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int t; cin >> t;
while(t--){
int n, w; cin >> n >> w;
int wt[n+1], val[n+1];
for(int i = 0; i < n; i++)
cin >> val[i];
for(int i = 0; i < n; i++)
cin >> wt[i];
cout << knapSack(w, wt, val, n) << endl;
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int noofterm=Integer.parseInt(br.readLine());
int arr[] = new int[noofterm];
String s[] = br.readLine().split(" ");
for(int i=0; i<noofterm;i++){
arr[i]= Integer.parseInt(s[i]);
}
System.out.println(unique(arr));
}
public static int unique(int[] inputArray)
{
int result = 0;
for(int i=0;i<inputArray.length;i++)
{
result ^= inputArray[i];
}
return (result>0 ? result : -1);
}
}, 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. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: n = int(input())
a = [int (x) for x in input().split()]
mapp={}
for index,val in enumerate(a):
if val in mapp:
del mapp[val]
else:
mapp[val]=1
for key, value in mapp.items():
print(key), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements. In the array, each element is present twice except for 1 element whose frequency in the array is 1. Hence the length of the array will always be odd.
Find the unique number.An integer, N, representing the size of the array. In the next line, N space-separated integers follow.
<b>Constraints:</b>
1 <= N <=10<sup>5</sup>
1 <= A[i] <=10<sup>8</sup>Output the element with frequency 1.Input :
5
1 1 2 2 3
Output:
3, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
signed main()
{
int n;
cin>>n;
int p=0;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
p^=a;
}
cout<<p<<endl;
}, 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 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 an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long subarraysDivByK(int[] A, int k)
{
long ans =0 ;
int rem;
int[] freq = new int[k];
for(int i=0;i<A.length;i++)
{
rem = A[i]%k;
ans += freq[(k - rem)% k] ;
freq[rem]++;
}
return ans;
}
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int [] a = new int [n];
for(int i=0; i<n; i++)
a[i] = Integer.parseInt(input[i]);
System.out.println(subarraysDivByK(a, k));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code: def countKdivPairs(A, n, K):
freq = [0] * K
for i in range(n):
freq[A[i] % K]+= 1
sum = freq[0] * (freq[0] - 1) / 2;
i = 1
while(i <= K//2 and i != (K - i) ):
sum += freq[i] * freq[K-i]
i+= 1
if( K % 2 == 0 ):
sum += (freq[K//2] * (freq[K//2]-1)/2);
return int(sum)
a,b=input().split()
a=int(a)
b=int(b)
arr=input().split()
for i in range(0,a):
arr[i]=int(arr[i])
print (countKdivPairs(arr,a, b)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers and an integer K, your task is to calculate the count of pairs whose sum is divisible by K.The first line of input contains two integers N and K, the next line contains N space-separated integers depicting values of an array.
Constraints:-
1 < = N < = 100000
1 < = Arr[i] <= 100000
1 <= K <= 100000Print the count of required pairs.Sample Input
5 4
1 2 3 4 5
Sample Output
2
Sample Input
5 3
1 2 3 4 5
Sample Output
4
Explanation:-
In Sample 2,
(1 5), (1 2), (2 4), and (4 5) are the required pairs, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
fast();
int n;
cin>>n;
int a;
int k;
cin>>k;
int fre[k];
FOR(i,k){
fre[i]=0;}
FOR(i,n){
cin>>a;
fre[a%k]++;
}
int ans=(fre[0]*(fre[0]-1))/2;
for(int i=1;i<=(k-1)/2;i++){
ans+=fre[i]*fre[k-i];
}
if(k%2==0){
ans+=(fre[k/2]*(fre[k/2]-1))/2;
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
System.out.println("Yes");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: string = str(input())
print("Yes"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string s; cin>>s;
cout<<"Yes";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Doctor Strange stumbles upon a rather curious artefact while spending holidays in Egypt. An antique board game but it can only be unlocked by a magical die. The only clue on the box is to get a sum of B by throwing the die A times. Help Doctor Strange if it is possible to get the particular sum. Print “YES” if it is possible, otherwise print “NO”.
Note:- Consider the die to be a regular one with face {1, 2, 3, 4, 5, 6}.The input contains two integers A and B separated by spaces.
Constraints:-
• 1≤A≤100
• 1≤B≤1000
A and B are integers.Print "YES" if it is possible to get a Sum of B by throwing die A times else print "NO".Sample Input:-
10 100
Sample Output:-
NO
Sample Input:-
6 30
Sample Output:-
YES, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(a*6>=b && a<=b){
cout<<"YES";
}
else{
cout<<"NO";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Doctor Strange stumbles upon a rather curious artefact while spending holidays in Egypt. An antique board game but it can only be unlocked by a magical die. The only clue on the box is to get a sum of B by throwing the die A times. Help Doctor Strange if it is possible to get the particular sum. Print “YES” if it is possible, otherwise print “NO”.
Note:- Consider the die to be a regular one with face {1, 2, 3, 4, 5, 6}.The input contains two integers A and B separated by spaces.
Constraints:-
• 1≤A≤100
• 1≤B≤1000
A and B are integers.Print "YES" if it is possible to get a Sum of B by throwing die A times else print "NO".Sample Input:-
10 100
Sample Output:-
NO
Sample Input:-
6 30
Sample Output:-
YES, 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 A = sc.nextInt();
int B = sc.nextInt();
if (B<A || B>A*6) {
System.out.print("NO");
}
else {
System.out.print("YES");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Doctor Strange stumbles upon a rather curious artefact while spending holidays in Egypt. An antique board game but it can only be unlocked by a magical die. The only clue on the box is to get a sum of B by throwing the die A times. Help Doctor Strange if it is possible to get the particular sum. Print “YES” if it is possible, otherwise print “NO”.
Note:- Consider the die to be a regular one with face {1, 2, 3, 4, 5, 6}.The input contains two integers A and B separated by spaces.
Constraints:-
• 1≤A≤100
• 1≤B≤1000
A and B are integers.Print "YES" if it is possible to get a Sum of B by throwing die A times else print "NO".Sample Input:-
10 100
Sample Output:-
NO
Sample Input:-
6 30
Sample Output:-
YES, I have written this Solution Code: A,B=map(int, input().split())
if 1*A<=B and B<=A*6:
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<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>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: def OccurenceOfX(N,X):
cnt=0
for i in range(1, N+1):
if(X%i==0 and X/i<=N):
cnt=cnt+1
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<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>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<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>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
int main()
{
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<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>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: public static int OccurenceOfX(int N,int X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>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>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: def equationSum(N) :
re=2*N*(N+1)
re=re-3*N
return re, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>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>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code: static long equationSum(long N)
{
return 2*N*(N+1) - 3*N;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>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>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an equation:
T<sub>n</sub> = 2*n + (n + 1)<sup>2</sup> - (n<sup>2</sup> + 4)
Your task is to find S<sub>n</sub>:
S<sub>n</sub> = T<sub>1</sub> + T<sub>2</sub> + T<sub>3</sub> +. .. . + T<sub>n</sub>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>equationSum()</b>, where you will get n as a parameter.
<b>Constraints:</b>
1 <= n <= 10^4You need to return the sum.Sample Input:-
2
Sample Output:-
6
Explanation:-
T1 = 2*1 + (1+1)^2 - (1^2+4)
= 2 + 4 - 5
= 1
T2 = 2*2 + (2 + 1)^2 - (2^2 + 4)
= 4 + 9 - 8
= 5
S2 = T1 + T2
S2 = 6
Sample Input:-
3
Sample Output:-
15, I have written this Solution Code:
long int equationSum( long N){
long ans = (2*N*(N+1)) - 3*N;
return ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pino is super fond of candies. Today she has A chocolate candies, B orange candies, and C unknown candies. You are also given the happiness she gets after eating a particular candy.
She wants to have exactly X chocolate candies and Y orange candies. She can transform an unknown candy into a candy of any type (chocolate or orange).
Find the maximum happiness she can attain.The first line of input contains 5 integers X, Y, A, B, and C.
The second line contains an A integer corresponding to the happiness of the various chocolate candies.
The third line contains B integers corresponding to the happiness of the various orange candies.
The fourth line contains C integers corresponding to the happiness of the various unknown candies.
<b>Constraints:-</b>
1 <= A, B, C <= 100000
1 <= X <= A
1 <= Y <= B
1 <= happiness of any candy <= 1000000000 (10^9)Output a single integer, the maximum happiness Pino can achieve by eating the candies.Sample Input 1:-
1 2 2 2 1
2 4
5 1
3
Sample Output 1:-
12
Sample Input 2:-
2 2 2 2 2
8 6
9 1
2 1
Sample Output 2:-
25
<b>Explanation:-</b>
Pino eats the 2nd chocolate candy (happiness=4), then eats the 1st orange candy (happiness=4+5=9), then transforms the first unknown candy to orange candy and eats it (happiness=9+3=12), I have written this Solution Code: X, Y, A, B, C = [int(i) for i in input().split()]
t = X + Y
a = sorted([int(i) for i in input().split()])
b = sorted([int(i) for i in input().split()])
c = [int(i) for i in input().split()]
o = []
for i in range(A-1,-1,-1):
if (X) == 0:
break
else:
X -= 1
o.append(a[i])
for i in range(B-1,-1,-1):
if (Y) == 0:
break
else:
Y -= 1
o.append(b[i])
o.extend(c)
s = 0
o.sort()
for i in range(len(o)-1,-1,-1):
if t == 0:
break
else:
t -= 1
s += o[i]
print(s), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pino is super fond of candies. Today she has A chocolate candies, B orange candies, and C unknown candies. You are also given the happiness she gets after eating a particular candy.
She wants to have exactly X chocolate candies and Y orange candies. She can transform an unknown candy into a candy of any type (chocolate or orange).
Find the maximum happiness she can attain.The first line of input contains 5 integers X, Y, A, B, and C.
The second line contains an A integer corresponding to the happiness of the various chocolate candies.
The third line contains B integers corresponding to the happiness of the various orange candies.
The fourth line contains C integers corresponding to the happiness of the various unknown candies.
<b>Constraints:-</b>
1 <= A, B, C <= 100000
1 <= X <= A
1 <= Y <= B
1 <= happiness of any candy <= 1000000000 (10^9)Output a single integer, the maximum happiness Pino can achieve by eating the candies.Sample Input 1:-
1 2 2 2 1
2 4
5 1
3
Sample Output 1:-
12
Sample Input 2:-
2 2 2 2 2
8 6
9 1
2 1
Sample Output 2:-
25
<b>Explanation:-</b>
Pino eats the 2nd chocolate candy (happiness=4), then eats the 1st orange candy (happiness=4+5=9), then transforms the first unknown candy to orange candy and eats it (happiness=9+3=12), I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int red[N], grn[N], col[N];
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int x, y, a, b, c; cin>>x>>y>>a>>b>>c;
For(i, 0, a){
cin>>red[i];
}
For(i, 0, b){
cin>>grn[i];
}
For(i, 0, c){
cin>>col[i];
}
vector<int> vect;
sort(red, red+a); sort(grn, grn+b);
reverse(red, red+a); reverse(grn, grn+b);
For(i, 0, x){
vect.pb(red[i]);
}
For(i, 0, y){
vect.pb(grn[i]);
}
For(i, 0, c){
vect.pb(col[i]);
}
sort(all(vect));
reverse(all(vect));
int ans = 0;
For(i, 0, x+y){
ans+=vect[i];
}
cout<<ans;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pino is super fond of candies. Today she has A chocolate candies, B orange candies, and C unknown candies. You are also given the happiness she gets after eating a particular candy.
She wants to have exactly X chocolate candies and Y orange candies. She can transform an unknown candy into a candy of any type (chocolate or orange).
Find the maximum happiness she can attain.The first line of input contains 5 integers X, Y, A, B, and C.
The second line contains an A integer corresponding to the happiness of the various chocolate candies.
The third line contains B integers corresponding to the happiness of the various orange candies.
The fourth line contains C integers corresponding to the happiness of the various unknown candies.
<b>Constraints:-</b>
1 <= A, B, C <= 100000
1 <= X <= A
1 <= Y <= B
1 <= happiness of any candy <= 1000000000 (10^9)Output a single integer, the maximum happiness Pino can achieve by eating the candies.Sample Input 1:-
1 2 2 2 1
2 4
5 1
3
Sample Output 1:-
12
Sample Input 2:-
2 2 2 2 2
8 6
9 1
2 1
Sample Output 2:-
25
<b>Explanation:-</b>
Pino eats the 2nd chocolate candy (happiness=4), then eats the 1st orange candy (happiness=4+5=9), then transforms the first unknown candy to orange candy and eats it (happiness=9+3=12), 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));
String str[] = read.readLine().split(" ");
int X = Integer.parseInt(str[0]);
int Y = Integer.parseInt(str[1]);
int A = Integer.parseInt(str[2]);
int B = Integer.parseInt(str[3]);
int C = Integer.parseInt(str[4]);
int arr[] = new int[X+Y+C];
int arrA[] = new int[A];
int arrB[] = new int[B];
int arrC[] = new int[C];
String strA[] = read.readLine().split(" ");
for(int k = 0; k < A; k++) {
arrA[k] = Integer.parseInt(strA[k]);
}
Arrays.sort(arrA);
String strB[] = read.readLine().split(" ");
for(int p = 0; p < B; p++) {
arrB[p] = Integer.parseInt(strB[p]);
}
Arrays.sort(arrB);
String strC[] = read.readLine().split(" ");
for(int q = 0; q < C; q++) {
arrC[q] = Integer.parseInt(strC[q]);
}
Arrays.sort(arrC);
System.arraycopy(arrA, arrA.length - X, arr, 0, X);
System.arraycopy(arrB, arrB.length - Y, arr, X, Y);
System.arraycopy(arrC, 0, arr, X+Y, C);
Arrays.sort(arr);
long happiness = 0;
int lastIndex = arr.length - 1;
int candies = 0;
for(int z = lastIndex; z >= 0; z--) {
happiness += arr[z];
candies++;
if(candies == X + Y) {
break;
}
}
System.out.print(happiness);
}
}, 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: 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 array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String [] str=br.readLine().trim().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(str[i]);
}
Arrays.sort(a);
int size=a[n-1]+1;
int c[]=new int[size];
for(int i=0;i<size;i++) c[i]=0;
for(int i=0;i<n;i++) c[a[i]]++;
int max=0,freq=c[1];
for(int i=2;i<size;i++){
if(freq<=c[i]){
freq=c[i];
max=i;
}
}
System.out.println(max);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: n = int(input())
a = [int(x) for x in input().split()]
freq = {}
for x in a:
if x not in freq:
freq[x] = 1
else:
freq[x] += 1
mx = max(freq.values())
rf = sorted(freq)
for i in range(len(rf) - 1, -1, -1):
if freq[rf[i]] == mx:
print(rf[i])
break, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, give the number with maximum frequency. If multiple numbers have maximum frequency print the maximum number among them.The first line of the input contains an integer N, and the Second line contains N space-separated integers of the array.
<b>Constraints:</b>
3 <= N <= 1000
1 <= Arr[i] <= 100The output should contain single integer, the number with maximum frequency.If multiple numbers have maximum frequency print the maximum number among them.Sample Input
5
1 4 2 4 5
Sample Output
4
<b>Explanation:-</b>
4 has max frequency=2, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
int mx = 0, id = -1;
for(int i = 1; i <= 100; i++){
if(a[i] >= mx)
mx = a[i], id = i;
}
cout << id;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
if(t%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
catch (Exception e){
return;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch(num % 2)
{
case 0:
printf("Even");
break;
/* Else if n%2 == 1 */
case 1:
printf("Odd");
break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: How would you add your own method to the Array object so
the following code would work?
const arr = [1, 2, 3]
console. log(arr.average())
// 2input will be an array, run like this
const anyArray = [5,6...]
anyArray.average should return average of the arraysAverage of the given arrayconst myArray = [1,2,3,4,5]
console.log(myArray.average())
// 3, I have written this Solution Code: Array.prototype.average = function() {
// calculate sum
var sum = this.reduce(function(prev, cur) { return prev + cur; });
// return sum divided by number of elements
return sum / this.length;
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N intervals, the i<sup>th</sup> of them being [A<sub>i</sub>, B<sub>i</sub>], where A<sub>i</sub> and B<sub>i</sub> are positive integers. Let the union of all these intervals be S. It is easy to see that S can be uniquely represented as an union of disjoint closed intervals. Your task is to find the sum of the lengths of the disjoint closed intervals that comprises S.
For example, if you are given the intervals: [1, 3], [2, 4], [5, 7] and [7, 8], then S can be uniquely represented as the union of disjoint intervals [1, 4] and [5, 8]. In this case, the answer will be 6, as (4 - 1) + (8 - 5) = 6.The first line of the input consists of a single integer N – the number of intervals.
Then N lines follow, the i<sup>th</sup> line containing two space-separated integers A<sub>i</sub> and B<sub>i</sub>.
<b> Constraints: </b>
1 ≤ N ≤ 10<sup>4</sup>
1 ≤ A<sub>i</sub> < B<sub>i</sub> ≤ 2×10<sup>5</sup>Print a single integer, the sum of the lengths of the disjoint intervals of S.Sample Input 1:
3
1 3
3 4
6 7
Sample Output 1:
4
Sample Explanation 1:
Here, S can be uniquely written as union of disjoint intervals [1, 4] and [6, 7]. Thus, answer is (4 - 1) + (7 - 6) = 4.
Sample Input 2:
4
9 12
8 10
5 6
13 15
Sample Output 2:
7
Sample Explanation 2:
Here, S can be uniquely written as union of disjoint intervals [5, 6], [8, 12] and [13, 15]. Thus, answer is (6 - 5) + (12 - 8) + (15 - 13) = 7.
, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << (a) << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (auto i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353; // 1e9 + 7;
const ll N = 4e5 + 100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int arr[N];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(n);
FOR (i, 1, n)
{
readb(a, b);
a *= 2, b *= 2;
arr[a]++;
arr[b + 1]--;
}
int ans = 0, sum = 0, cur = 0;
FOR (i, 0, N - 2)
{
sum += arr[i];
if (sum) cur++;
else ans += cur/2, cur = 0;
}
print(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For an integer N, your task is to calculate sum of first N natural numbers.<b>User Task:</b>
Since this will be a functional problem, you don't have to worry about input. You just have to complete the function <b>sum()</b> which takes the integer N as a parameter.
Constraints:
1 <= N < = 100000000Print the sum of first N natural numbers.Sample Input:-
5
Sample Output:-
15
Sample Input:-
3
Sample Output:-
6, I have written this Solution Code:
static void sum(int N){
long x=N;
x=x*(x+1);
x=x/2;
System.out.print(x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of strings of length N, such that the character 'a' occurs at least once in the string and the string consists of lowercase letters of the english alphabet only.
As the answer can be large, output it modulo 10<sup>9</sup>+7.The first and the only line of input contains an integer N.
Constraints
1 <= N <= 100000Output a single integer, the number of strings of length N satisfying the conditions mentioned in the problem.Sample Input
1
Sample Output
1
Explanation: Only string "a" satisfies the condition.
Sample Input
2
Sample Output
51
Explanation: Strings "aa", "ab",. , "az", "ba", "ca",. , "za" satisfy the condition., 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);
long ans1=1;
long ans2=1;
long ans=0;
int n = sc.nextInt();
int m=1000000007;
for(int i=0;i<n;i++)
{
ans1=((ans1)%m*26)%m;
ans2=((ans2)%m*25)%m;
}
ans=(ans1-ans2)%m;
ans=ans%m <0 ? ans+m:ans;
System.out.print(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of strings of length N, such that the character 'a' occurs at least once in the string and the string consists of lowercase letters of the english alphabet only.
As the answer can be large, output it modulo 10<sup>9</sup>+7.The first and the only line of input contains an integer N.
Constraints
1 <= N <= 100000Output a single integer, the number of strings of length N satisfying the conditions mentioned in the problem.Sample Input
1
Sample Output
1
Explanation: Only string "a" satisfies the condition.
Sample Input
2
Sample Output
51
Explanation: Strings "aa", "ab",. , "az", "ba", "ca",. , "za" satisfy the condition., I have written this Solution Code: m=int(pow(10,9)+7)
n=int(input())
print((pow(26,n,m)-pow(25,n,m))%m), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the number of strings of length N, such that the character 'a' occurs at least once in the string and the string consists of lowercase letters of the english alphabet only.
As the answer can be large, output it modulo 10<sup>9</sup>+7.The first and the only line of input contains an integer N.
Constraints
1 <= N <= 100000Output a single integer, the number of strings of length N satisfying the conditions mentioned in the problem.Sample Input
1
Sample Output
1
Explanation: Only string "a" satisfies the condition.
Sample Input
2
Sample Output
51
Explanation: Strings "aa", "ab",. , "az", "ba", "ca",. , "za" satisfy the condition., 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 = 1e6+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
int powmod(int a, int b, int c = MOD){
int ans = 1;
while(b){
if(b&1){
ans = (ans*a)%c;
}
a = (a*a)%c;
b >>= 1;
}
return ans;
}
void solve(){
int n; cin>>n;
// can calculate using brute as well
int ans = powmod(26, n)-powmod(25, n)+MOD;
cout<<ans%MOD;
}
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 array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
String[] line = br.readLine().split(" ");
int minIndex = 0;
long minVal = Long.MAX_VALUE;
for (int i=0;i<line.length;++i){
long el = Long.parseLong(line[i]);
if (minVal>el){
minVal = el;
minIndex = i;
}
}
StringBuilder sb = new StringBuilder();
for (int i = minIndex;i< line.length;++i){
sb.append(line[i]+" ");
}
for (int i=0;i<minIndex;++i){
sb.append(line[i]+" ");
}
System.out.print(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: N = int(input())
arr = list(map(int, input().split()))
mi = arr.index(min(arr))
ans = arr[mi:] + arr[:mi]
for e in ans:
print(e, end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
int mi=0;
for(int i=0;i<n;++i)
if(a[i]<a[mi])
mi=i;
for(int i=0;i<n;++i)
cout<<a[(i+mi)%n]<<" ";
#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 two elements A and B, your task is to swap the given two elements.<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>Swap(int A, int B)</b>.Print one line which contains the output of swapped elements.Sample Input:-
5 7
Sample Output:-
7 5
Sample Input:-
3 6
Sample Output:-
6 3, I have written this Solution Code: class Solution {
public static void Swap(int A, int B){
int C = A;
A = B;
B = C;
System.out.println(A + " " + B);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two elements A and B, your task is to swap the given two elements.<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>Swap(int A, int B)</b>.Print one line which contains the output of swapped elements.Sample Input:-
5 7
Sample Output:-
7 5
Sample Input:-
3 6
Sample Output:-
6 3, I have written this Solution Code: # Python program to swap two variables
li= list(map(int,input().strip().split()))
x=li[0]
y=li[1]
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print(x,end=" ")
print(y,end="")
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified 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>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}, In this Programming Language: Java, 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 are given an array and Q queries. Your task is to perform these operations:-
enqueue: this operation will add an element to your current queue.
dequeue: this operation will delete the element from the starting of the queue
displayfront: this operation will print the element presented at the frontUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes the integer to be added and the maximum size of array as parameter.
<b>dequeue()</b>:- that takes the queue as parameter.
<b>displayfront()</b> :- that takes the queue as parameter.
Constraints:
1 <= Q(Number of queries) <= 10<sup>3</sup>
<b> Custom Input:</b>
First line of input should contains two integer number of queries Q and the size of the array N. Next Q lines contains any of the given three operations:-
enqueue x
dequeue
displayfrontDuring a dequeue operation if queue is empty you need to print "Queue is empty", during enqueue operation if the maximum size of array is reached you need to print "Queue is full" and during displayfront operation you need to print the element which is at the front and if the queue is empty you need to print "Queue is empty".
Note:-Each msg or element is to be printed on a new line
Sample Input:-
8 2
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
enqueue 5
Sample Output:-
Queue is empty
2
2
4
Queue is full
Explanation:-here size of given array is 2 so when last enqueue operation perfomed the array was already full so we display the msg "Queue is full".
Sample input:
5 5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: public static void enqueue(int x,int k)
{
if (rear >= k) {
System.out.println("Queue is full");
}
else {
a[rear] = x;
rear++;
}
}
public static void dequeue()
{
if (rear <= front) {
System.out.println("Queue is empty");
}
else {
front++;
}
}
public static void displayfront()
{
if (rear<=front) {
System.out.println("Queue is empty");
}
else {
int x = a[front];
System.out.println(x);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<b>User Task:</b>
Complete the function <b>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:- </b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: import math
def distributingMoney(x,y,z,k) :
# Final result of summation of divisors
result = x+y+z+k;
if result%3!=0:
return 0
result =result/3
if result<x or result<y or result<z:
return 0
return 1;
# Driver program to run the case
x,y,z,k= map(int,input().split());
print (int(distributingMoney(x,y,z,k))) , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<b>User Task:</b>
Complete the function <b>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:- </b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: #include <stdio.h>
#include <math.h>
int distributingMoney(long x, long y, long z,long k){
long long sum=x+y+z+k;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
int main(){
long int x,y,z,k;
scanf("%ld %ld %ld %ld",&x,&y,&z,&k);
printf("%d",distributingMoney(x,y,z,k));
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<b>User Task:</b>
Complete the function <b>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:- </b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int distributingMoney(long x, long y, long z,long k){
long long sum=x+y+z+k;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
int main(){
long long x,y,z,k;
cin>>x>>y>>z>>k;
cout<<distributingMoney(x,y,z,k);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Mahesh has won some money K from a lottery, he wants to distribute the money among his three sisters. His sisters already have some money with them. Let's say they have x, y, z amount of money. Now mahesh wants to distribute his money such that all the sisters have the same amount of total money left with them. Your task is to check if it is possible to distribute the money as Mahesh wants.<b>User Task:</b>
Complete the function <b>distributingMoney()</b> that takes the integers x, y, z, and K as parameters.
<b>Constraints:- </b>
0 <= x, y, z, K <= 10^7Return 1 if possible else return 0.Sample Input:-
1 2 3 3
Sample Output:-
1
Explanation:-
initial :- 1 2 3
Final :- 3 3 3
Sample Input:-
1 2 3 4
Sample Output:-
0, I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
long x = sc.nextInt();
long y = sc.nextInt();
long z = sc.nextInt();
long K = sc.nextInt();
System.out.println(distributingMoney(x,y,z,K));
}
public static int distributingMoney(long x, long y, long z, long K){
long sum=0;
sum+=x+y+z+K;
if(sum%3!=0){return 0;}
sum=sum/3;
if(x>sum || y>sum || z>sum){return 0;}
return 1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.
Shift each character of S by N in alphabetical order (see below), and print the resulting string.
We assume that A follows Z. For example, shifting A by 2 results in C (A → B → C), and shifting Y by 3 results in B (Y → Z → A → B).The input contains a number and a string separated by a new line.
N
SPrint the string resulting from shifting each character of S by N in alphabetical orderSample Input 1
2
ABCXYZ
Sample Output 1
CDEZAB
Sample Input 2
0
ABCXYZ
Sample Output 2
ABCXYZ
Sample Input 3
13
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sample Output 3
NOPQRSTUVWXYZABCDEFGHIJKLM, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
string s;
cin >> s;
for (auto&& e : s) {
int i = e - 'A';
i += n;
i %= 26;
e = 'A' + i;
}
cout << s << '\n';
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
Reader sc=new Reader();
int n=sc.nextInt();
int b=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int j, max;
long mul=1;
Deque<Integer> dq= new LinkedList<Integer>();
for (int i = 0;i<b;i++)
{
while (!dq.isEmpty() && a[i] >=a[dq.peekLast()])
dq.removeLast();
dq.addLast(i);
}
mul=((mul%1000000007)*(a[dq.peek()]%1000000007))%1000000007;
for (int i=b; i < n;i++)
{
while ((!dq.isEmpty()) && dq.peek() <=i-b)
dq.removeFirst();
while ((!dq.isEmpty()) && a[i]>=a[dq.peekLast()])
dq.removeLast();
dq.addLast(i);
mul=((mul%1000000007)*(a[dq.peek()]%1000000007))%1000000007;
}
System.out.println(mul%1000000007);
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: from collections import deque
deq=deque()
n,k=list(map(int,input().split()))
array=list(map(int,input().split()))
for i in range(k):
while(deq and array[deq[-1]]<=array[i]):
deq.pop()
deq.append(i)
ans=1
for j in range(k,n):
ans=ans*array[deq[0]]
ans=(ans)%1000000007
while(deq and deq[0]<=j-k):
deq.popleft()
while(deq and array[deq[-1]]<=array[j]):
deq.pop()
deq.append(j)
ans=ans*array[deq[0]]
ans=(ans)%1000000007
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array, A of N integers. Find the product of maximum values for every subarray of size K. Print the answer modulo 10<sup>9</sup>+7.
A subarray is any contiguous sequence of elements in an array.The first line contains two integers N and K, denoting the size of the array and the size of the subarray respectively.
The next line contains N integers denoting the elements of the array.
<b>Constarints</b>
1 <= K <= N <= 1000000
1 <= A[i] <= 1000000Print a single integer denoting the product of maximums for every subarray of size K modulo 1000000007Sample Input 1:
6 4
1 5 2 3 6 4
Sample Output 1:
180
<b>Explanation:</b>
For subarray [1, 5, 2, 3], maximum = 5
For subarray [5, 2, 3, 6], maximum = 6
For subarray [2, 3, 6, 4], maximum = 6
Therefore, ans = 5*6*6 = 180, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
void solve(){
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
deque<int> q;
for(int i = 1; i <= k; i++){
while(!q.empty() && a[q.back()] <= a[i])
q.pop_back();
q.push_back(i);
}
int ans = a[q.front()];
for(int i = k+1; i <= n; i++){
if(q.front() == i-k)
q.pop_front();
while(!q.empty() && a[q.back()] <= a[i])
q.pop_back();
q.push_back(i);
ans = (ans*a[q.front()]) % mod;
}
cout << ans;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified 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>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a String <b>S</b>, you need to typecast this String to Integer. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".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>checkConvertion()</b>, which contains S as parameter.
Constraints:-
1 <= |S| <= 8
The string will contain only numeric digits(1-9)You need to return the typecasted integer value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
548
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: a=input()
try:
b=int(a)
print("Nice Job")
except:
print("Wrong answer"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a String <b>S</b>, you need to typecast this String to Integer. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".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>checkConvertion()</b>, which contains S as parameter.
Constraints:-
1 <= |S| <= 8
The string will contain only numeric digits(1-9)You need to return the typecasted integer value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
548
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: static int checkConevrtion(String S)
{
return Integer.parseInt(S);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two strings S and T consisting of lowercase English letters. Determine if S is a prefix of T.The input contains two strings separated by a new line.
S
T
<b>Constraints</b>
S and T are strings of lengths between 1 and 100 (inclusive) consisting of lowercase English letters.Print "Yes" if S is a prefix of T, "No" otherwise.
Note: that the judge is case-sensitive.<b>Sample Input 1</b>
new
newton
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
ewt
newton
<b>Sample Output 2</b>
No
<b>Sample Input 3</b>
aaaa
aa
<b>Sample Output 3</b>
No, I have written this Solution Code: #include <iostream>
using namespace std;
int main(void)
{
string s, t;
cin >> s >> t;
if(s.size() > t.size()){
cout << "No" << endl;
return 0;
}
for(int i = 0; i < (int)s.size(); i++){
if(s[i] != t[i]){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long minCost(long arr[], int n)
{
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) pq.add(arr[i]);
Long cost = new Long("0");
while (pq.size() != 1)
{
long x = pq.poll();
long y = pq.poll();
cost += (x + y);
pq.add(x + y);
}
arr = null;
System.gc();
return cost;
}
public static void main (String[] args) {
FastReader sc = new FastReader();
OutputStream outputstream = System.out;
PrintWriter out = new PrintWriter(outputstream);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextLong();
System.out.println(minCost(arr, arr.length));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: import heapq
from heapq import heappush, heappop
def findMinCost(prices):
heapq.heapify(prices)
cost = 0
while len(prices) > 1:
x = heappop(prices)
y = heappop(prices)
total = x + y
heappush(prices, total)
cost += total
return cost
t=int(input())
for _ in range(t):
n=int(input())
prices=list(map(int,input().split()))
print( findMinCost(prices)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N ropes of L[i] lengths, you need to connect these ropes into one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N where N is the number of ropes. The second line of each test case contains N input L[i],length of ropes.
Constraints:
1 ≤ T ≤ 100
1 <= N <= 10^5
1 <= L[i] <= 10^5
Sum of N over all test cases does not exceed 5*10^5.For each testcase, print the minimum cost to connect all the ropes.Sample Input:
2
4
4 3 2 6
5
4 2 7 6 9
Sample Output:
29
62
Explanation:
For example if we are given 4 ropes of lengths 4, 3, 2 and 6. We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Now we have three ropes of lengths 4, 6 and 5.
2) Now connect ropes of lengths 4 and 5. Now we have two ropes of lengths 6 and 9.
3) Finally connect the two ropes and all ropes have connected.
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes. Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three strings of 3, 2 and 10), then connect 10 and 3 (we get two strings of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
priority_queue<long long> pq;
int n;
cin>>n;
long long x;
long long sum=0;
for(int i=0;i<n;i++){
cin>>x;
x=-x;
pq.push(x);
}
long long y;
while(pq.size()!=1){
x=pq.top();
pq.pop();
y=pq.top();
pq.pop();
sum+=x+y;
pq.push(x+y);
}
cout<<-sum<<endl;
}}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A 2n digits number is said to be lucky if sum of n most significant digits is equal to sum of n least significant digits.
Given a number find out if the number is lucky or not?First line contains n.
Next line contains a number of 2n digits.
<b>Constraints</b>
1 ≤ n ≤ 10<sup>5</sup>
Number contains digits only.Print "LUCKY" if the number is lucky otherwise print "UNLUCKY".Input:
3
213411
Output:
LUCKY
Explanation :
sum of 3 most significant digits = 2 + 1 + 3 = 6
sum of 3 least significant digits = 4 + 1 + 1 = 6
Input:
1
69
Output:
UNLUCKY, 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());
String s=in.next();
int sum=0;
for(int i=0;i<n;i++){
int d = s.charAt(i) - '0';
sum += d;
}
for(int i=n;i<2*n;i++){
int d = s.charAt(i) - '0';
sum -= d;
}
if(sum == 0)out.print("LUCKY");
else out.print("UNLUCKY");
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: Given two elements A and B, your task is to swap the given two elements.<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>Swap(int A, int B)</b>.Print one line which contains the output of swapped elements.Sample Input:-
5 7
Sample Output:-
7 5
Sample Input:-
3 6
Sample Output:-
6 3, I have written this Solution Code: class Solution {
public static void Swap(int A, int B){
int C = A;
A = B;
B = C;
System.out.println(A + " " + B);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two elements A and B, your task is to swap the given two elements.<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>Swap(int A, int B)</b>.Print one line which contains the output of swapped elements.Sample Input:-
5 7
Sample Output:-
7 5
Sample Input:-
3 6
Sample Output:-
6 3, I have written this Solution Code: # Python program to swap two variables
li= list(map(int,input().strip().split()))
x=li[0]
y=li[1]
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print(x,end=" ")
print(y,end="")
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Main {
public static void main (String[] args)
throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String str = read.readLine();
System.out.println(maxProduct(str));
}
public static long maxProduct(String str) {
StringBuilder sb = new StringBuilder(str);
int x = sb.length();
int[] dpl = new int[x];
int[] dpr = new int[x];
modifiedOddManacher(sb.toString(), dpl);
modifiedOddManacher(sb.reverse().toString(), dpr);
long max=1;
for(int i=0;i<x-1;i++)
max=Math.max(max, (1+(dpl[i]-1)*2L)*(1+(dpr[x-(i+1)-1]-1)*2L));
return max;
}
private static void modifiedOddManacher(String str, int[] dp){
int x = str.length();
int[] center = new int[x];
for(int l=0,r=-1,i=0;i<x;i++){
int radius = (i > r) ? 1 : Math.min(center[l+(r-i)], r-i+1);
while(i-radius>=0 && i+radius<x && str.charAt(i-radius)==str.charAt(i+radius)) {
dp[i+radius] = radius+1;
radius++;
}
center[i] = radius--;
if(i+radius>r){
l = i-radius;
r = i+radius;
}
}
for(int i=0, max=1;i<x;i++){
max = Math.max(max, dp[i]);
dp[i] = max;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, I have written this Solution Code: s=input()
n = len(s)
hlen = [0]*n
center = right = 0
for i in range(n):
if i < right:
hlen[i] = min(right - i, hlen[2*center - i])
while 0 <= i-1-hlen[i] and i+1+hlen[i] < len(s) and s[i-1-hlen[i]] == s[i+1+hlen[i]]:
hlen[i] += 1
if right < i+hlen[i]:
center, right = i, i+hlen[i]
left = [0]*n
right = [0]*n
for i in range(n):
left[i+hlen[i]] = max(left[i+hlen[i]], 2*hlen[i]+1)
right[i-hlen[i]] = max(right[i-hlen[i]], 2*hlen[i]+1)
for i in range(1, n):
left[~i] = max(left[~i], left[~i+1]-2)
right[i] = max(right[i], right[i-1]-2)
for i in range(1, n):
left[i] = max(left[i-1], left[i])
right[~i] = max(right[~i], right[~i+1])
print(max(left[i-1]*right[i] for i in range(1, n))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N. You have to select two non- overlapping (no common index) non- empty substrings of odd lengths from that string such that both those substrings are palindrome. You want the product of lengths of those substring to be maximum.Input contains of a single String of length N.
Constraints
2 <= N <= 100000
String contains lowercase english letters.Print a single integer which is the maximum possible product of lengths of selected substrings.Sample input 1
aabaaba
Sample output 1
9
Explanation : we can select substring [2-4] = aba and [5-7] = aba the product of their lengths is 9.
Sample Input 2
aabababaaa
Sample Output 2
15
, 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_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#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);
}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
#define S second
#define F first
#define int long long
/////////////
int v1[100001]={};
int v2[100001]={};
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
string s;
cin>>s;
n=s.length();
vector<int> d1(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
int c=0;
for(int i=0;i<n;++i)
{
int x=2*d1[i]-1;
int j=i+d1[i]-1;
while(v1[j]<x&&j>=i)
{
v1[j]=x;
x-=2;
++c;
--j;
}
}
for(int i=1;i<n;++i)
{
v1[i]=max(v1[i],v1[i-1]);
}
for(int i=n-1;i>=0;--i)
{
int x=2*d1[i]-1;
int j=i-d1[i]+1;
while(v2[j]<x&&j<=i)
{
v2[j]=x;
x-=2;
++j;
++c;
}
}
for(int i=n-2;i>=0;--i)
{
v2[i]=max(v2[i],v2[i+1]);
}
int ans=0;
for(int i=1;i<n;++i)
{
ans=max(ans,v1[i-1]*v2[i]);
}
cout<<ans;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(minValue(arr, n, k));
}
static int minValue(int arr[], int N, int k)
{
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(arr, m, N) <= k)
h = m;
else
l = m;
}
return h;
}
static int f(int a[], int x, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += Math.max(a[i]-x, 0);
return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: def minCut(arr,k):
mini = 1
maxi = k
mid = 0
while mini<=maxi:
mid = mini + int((maxi - mini)/2)
wood = 0
for j in range(n):
if(arr[j]-mid>=0):
wood += arr[j] - mid
if wood == k:
break;
elif wood > k:
mini = mid+1
else:
maxi = mid-1
print(mini)
n,k = list(map(int, input().split()))
arr = list(map(int, input().split()))
minCut(arr,k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and an array height[] of size N, where height[i] denotes the height of the ith tree in a forest. The task is to make a cut of height X from the ground such that at max K units wood is collected. Find the minimum value of X
<b>If you make a cut of height X from the ground then every tree with a height greater than X will be reduced to X and the remaining part of the wood can be collected</b>The first line contains two integers N and K.
The next line contains N integers denoting the elements of the array height[]
<b>Constraints</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>7</sup>Print a single integer with the value of X.Sample Input:
4 2
1 2 1 2
Sample Output:
1
<b>Explanation:</b>
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i. e. 0 + 1 + 0 + 1 = 2., I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define ll 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 = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], n, k;
int f(int x){
int sum = 0;
for(int i = 1; i <= n; i++)
sum += max(a[i]-x, 0);
return sum;
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(m) <= k)
h = m;
else
l = m;
}
cout << h;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
testcases();
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
long int n, el;
cin>>n>>el;
long int arr[n+1];
for(long int i=0; i<n; i++) {
cin>>arr[i];
}
arr[n] = el;
for(long int i=0; i<=n; i++) {
cout<<arr[i];
if (i != n) {
cout<<" ";
}
else if(t != 0) {
cout<<endl;
}
}
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: t=int(input())
while t>0:
t-=1
li = list(map(int,input().strip().split()))
n=li[0]
num=li[1]
a= list(map(int,input().strip().split()))
a.insert(len(a),num)
for i in a:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n =Integer.parseInt(br.readLine().trim());
while(n-->0){
String str[]=br.readLine().trim().split(" ");
String newel =br.readLine().trim()+" "+str[1];
System.out.println(newel);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unordered_map<long long,int> m;
long k;
cin>>k;
long long a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;
}
if(m.find(k)!=m.end()){
cout<<1<<endl;
}
else{
cout<<-1<<endl;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return 1
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def position(n,arr,x):
return binary_search(arr,0,n-1,x)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: // arr is they array to search from
// x is target
function binSearch(arr, x) {
// write code here
// do not console.log
// return the 1 or -1
let l = 0;
let r = arr.length - 1;
let mid;
while (r >= l) {
mid = l + Math.floor((r - l) / 2);
// If the element is present at the middle
// itself
if (arr[mid] == x)
return 1;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
r = mid - 1;
// Else the element can only be present
// in right subarray
else
l = mid + 1;
}
// We reach here when element is not
// present in array
return -1;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
String[] s=br.readLine().trim().split(" ");
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i]);
int max=Integer.MIN_VALUE,c=0;
for(int i=0;i<arr.length;i++){
if(max==arr[i])
c++;
if(max<arr[i]){
max=arr[i];
c=1;
}
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: n=int(input())
a=list(map(int,input().split()))
print(a.count(max(a))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.