Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
vector<int> a(101, 0);
For(i, 0, n){
int x; cin>>x;
a[x]++;
}
for(int i=100; i>=1; i--){
if(a[i]){
cout<<a[i];
return;
}
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an ArrayList of N lowercase characters. The task is to insert given elements in the list and count frequency of elements present in the list.
You can use some inbuilt functions as:-
add() to append element in the list
contains() to check an element is present or not in the list
collections.frequency() to find the frequency of the element in the 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>insert()</b> and <b>freq()</b> that takes the array list and the character c as parameters.
Constraints:
1 <= T <= 100
1 <= N <= 1000
c will be a lowercase english character
You need to print the count of the character c if it is present else you need to print "Not Present" all in a separate line in function freq().Sample Input:
2
6
i n i e i w i t i n f n
4
i c i p i p f f
Sample Output:
2
Not Present
Explanation:
Testcase 1: Inserting n, e, w, t, n into the list. Frequency of n is 2 in the list.
Testcase 2: Inserting c, p, p into the list. Frequency of f is 0 in the list., I have written this Solution Code:
// Function to insert element
public static void insert(ArrayList<Character> clist, char c)
{
clist.add(c);
}
// Function to count frequency of element
public static void freq(ArrayList<Character> clist, char c)
{
if(clist.contains(c) == true)
System.out.println(Collections.frequency(clist, c));
else
System.out.println("Not Present");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N places, numbered 1,2,…,N. For each i (1≤i≤N), the height of place i is hi.
There is a man who is initially on place 1. He will repeat the following action some number of times to reach place N:
If the man is currently at place i, he will go to place i+1 or place i+2 i.e. maximum 2 jumps allowed. Here, a cost of |hi−hj| is incurred, where j is the place where the man goes. Find the minimum possible total cost incurred before the man reaches place N.First line contains N size of array, second line contains elements of array.
Constraints
2 ≤ N ≤ 10^5
1 ≤ hi ≤ 10^4Print the minimum possible total cost incurred.Sample Input:
4
10 30 40 20
Sample Output:
30
Explanation:
Testcase 1: If we follow the path 1→ 2→ 4, the total cost incurred would be |10−30|+|30−20|=30., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static long minCostIncurred(int arr[],int n){
long costTillNow[]=new long[n];
costTillNow[0]=0;
costTillNow[1]=Math.abs(arr[1]-arr[0]);
for(int i=2;i<n;i++){
costTillNow[i]=Math.min(costTillNow[i-1]+(long)Math.abs(arr[i]-arr[i-1]),costTillNow[i-2]+(long)Math.abs(arr[i]-arr[i-2]));
}
return costTillNow[n-1];
}
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 arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
System.out.println(minCostIncurred(arr,n));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N places, numbered 1,2,…,N. For each i (1≤i≤N), the height of place i is hi.
There is a man who is initially on place 1. He will repeat the following action some number of times to reach place N:
If the man is currently at place i, he will go to place i+1 or place i+2 i.e. maximum 2 jumps allowed. Here, a cost of |hi−hj| is incurred, where j is the place where the man goes. Find the minimum possible total cost incurred before the man reaches place N.First line contains N size of array, second line contains elements of array.
Constraints
2 ≤ N ≤ 10^5
1 ≤ hi ≤ 10^4Print the minimum possible total cost incurred.Sample Input:
4
10 30 40 20
Sample Output:
30
Explanation:
Testcase 1: If we follow the path 1→ 2→ 4, the total cost incurred would be |10−30|+|30−20|=30., 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;
}
int a[max1];
bool b[max1];
int dp[max1];
int n,k;
ll solve(int i){
if(i+k>=n-1){return abs(a[i]-a[n-1]);}
else{
ll ans=LONG_MAX;
if(dp[i]==-1){
for(int j=i+1;j<=i+k;j++){
ans=min(solve(j)+abs(a[j]-a[i]),ans);
}
dp[i]=ans;
}
return dp[i];
}
}
int main()
{
cin>>n;
k=2;
FOR(i,n){b[i]=false;dp[i]=-1;cin>>a[i];}
cout<<solve(0);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: [o,a,u]=[int(i) for i in input().split()]
s=o+a+u
[x,y]=[int(i) for i in input().split()]
o+=u
a+=u
if o<=x and a<=y:
print(s)
elif o<=x:
print(y)
elif a<=y:
print(x)
else:
print(min(x,y)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main implements Runnable {
private boolean console=false;
private long MOD = 1000_000_007L;
private int MAX = 1000_001;
private void solve1(){
long a=in.nl(),b=in.nl(),c= in.nl();
long x=in.nl(),y=in.nl();
long ans =0;
if(x>y){
long t = x;
x=y;
y=t;
t = a;
a=b;
b=t;
}
if(x-a-c <0){
ans = x;
}else if(y-b-c<0) {
ans = y;
}else {
ans = a+b+c;
}
out.printLn(ans);
}
private void solve() {
int testCases = 1;
while (testCases-->0){
solve1();
}
}
private void add(TreeMap<Integer, Integer> map, int key){
map.put(key,map.getOrDefault(key,0)+1);
}
private void remove(TreeMap<Integer,Integer> map,int key){
if(!map.containsKey(key))
return;
map.put(key,map.getOrDefault(key,0)-1);
if(map.get(key)==0)
map.remove(key);
}
@Override
public void run() {
long time = System.currentTimeMillis();
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
solve();
out.flush();
System.err.println(System.currentTimeMillis()-time);
System.exit(0);
}catch (Exception e){
e.printStackTrace(); System.exit(1);
}
}
private FastInput in;
private FastOutput out;
public static void main(String[] args) throws Exception {
new Main().run();
}
private void init() throws FileNotFoundException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
try {
if (!console && System.getProperty("user.name").equals("puneetkumar")) {
outputStream = new FileOutputStream("/Users/puneetkumar/output.txt");
inputStream = new FileInputStream("/Users/puneetkumar/input.txt");
}
} catch (Exception ignored) {
}
out = new FastOutput(outputStream);
in = new FastInput(inputStream);
}
private void maualAssert(int a,int b,int c){
if(a<b || a>c) throw new RuntimeException();
}
private void maualAssert(long a,long b,long c){
if(a<b || a>c) throw new RuntimeException();
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private long ModPow(long x, long y, long MOD) {
long res = 1L;
x = x % MOD;
while (y >= 1L) {
if ((y & 1L) > 0) res = (res * x) % MOD;
x = (x * x) % MOD;
y >>= 1L;
}
return res;
}
private int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private int[] arrInt(int n){
int[] arr=new int[n];for(int i=0;i<n;++i)arr[i]=in.ni();
return arr;
}
private long[] arrLong(int n){
long[] arr=new long[n];for(int i=0;i<n;++i)arr[i]=in.nl();
return arr;
}
private int arrMax(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMax(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private int arrMin(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMin(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
class FastInput { InputStream obj;
public FastInput(InputStream obj) {
this.obj = obj;
}
private byte inbuffer[] = new byte[1024];
private int lenbuffer = 0, ptrbuffer = 0;
private int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
try { lenbuffer = obj.read(inbuffer);
} catch (IOException e) { throw new InputMismatchException(); } }
if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }
String ns() { int b = skip();StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b)))
{ sb.appendCodePoint(b);b = readByte(); }return sb.toString();}
int ni() {
int num = 0, b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); }}
long nl() { long num = 0;int b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); } }
private boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
float nf() {return Float.parseFloat(ns());}
double nd() {return Double.parseDouble(ns());}
char nc() {return (char) skip();}
}
class FastOutput{
private final PrintWriter writer;
public FastOutput(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public PrintWriter getWriter(){
return writer;
}
public void print(Object obj){
writer.print(obj);
}
public void printLn(){
writer.println();
}
public void printLn(Object obj){
writer.print(obj);
printLn();
}
public void printSp(Object obj){
writer.print(obj+" ");
}
public void printArr(int[] arr){
for(int i:arr)
printSp(i);
printLn();
}
public void printArr(long[] arr){
for(long i:arr)
printSp(i);
printLn();
}
public void flush(){
writer.flush();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int a, b, c;
cin >> a >> b >> c;
int n, m;
cin >> n >> m;
int ans = a+b+c;
if (n < a + c) ans = min(n, ans);
if (m < b + c) ans = min(m, ans);
cout << ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. Two position i and j of the string are friends if they have the same character. The distance between two friends at positions i and j is defined as |i- j|. Find the sum of distances of all the pairs of friends in the given strings.First line of input contains a single string S.
Constraints:
1 <= |S| <= 1000000
String contains lowercase english letters.Output a single integer which is the sum of distance of all the pair of friends in the given strings.Sample Input
ababa
Sample Output
10
Explanation: Friend pairs - (1, 3) (1, 5) (2, 4) (3, 5), I have written this Solution Code: import java.io.*;
import java.io.IOException;
import java.util.*;
class Main {
public static long mod = (long)Math.pow(10,9)+7 ;
public static double epsilon=0.00000000008854;
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) {
String s=sc.nextLine();
int n=s.length();
int hnum[]=new int[26];
int hpast[]=new int[26];
Arrays.fill(hpast,-1);
long hsum[]=new long[26];
long ans=0;
for(int i=0;i<n;i++){
int k=s.charAt(i)-'a';
if(hpast[k]!=-1)
hsum[k]=hsum[k]+(i-hpast[k])*hnum[k];
ans+=hsum[k];
hnum[k]++;
hpast[k]=i;
}
pw.println(ans);
pw.flush();
pw.close();
}
public static Comparator<Long[]> column(int i){
return
new Comparator<Long[]>() {
@Override
public int compare(Long[] o1, Long[] o2) {
return o1[i].compareTo(o2[i]);
}
};
}
public static Comparator<Integer[]> col(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[i].compareTo(o2[i]);
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
public static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. Two position i and j of the string are friends if they have the same character. The distance between two friends at positions i and j is defined as |i- j|. Find the sum of distances of all the pairs of friends in the given strings.First line of input contains a single string S.
Constraints:
1 <= |S| <= 1000000
String contains lowercase english letters.Output a single integer which is the sum of distance of all the pair of friends in the given strings.Sample Input
ababa
Sample Output
10
Explanation: Friend pairs - (1, 3) (1, 5) (2, 4) (3, 5), I have written this Solution Code: def findS(s):
visited= [ 0 for i in range(256)];
distance =[0 for i in range (256)];
for i in range(256):
visited[i]=0;
distance[i]=0;
sum=0;
for i in range(len(s)):
sum+=visited[ord(s[i])] * i - distance[ord(s[i])];
visited[ord(s[i])] +=1;
distance[ord(s[i])] +=i;
return sum;
if __name__ == '__main__':
s=input("");
print(findS(s));, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S. Two position i and j of the string are friends if they have the same character. The distance between two friends at positions i and j is defined as |i- j|. Find the sum of distances of all the pairs of friends in the given strings.First line of input contains a single string S.
Constraints:
1 <= |S| <= 1000000
String contains lowercase english letters.Output a single integer which is the sum of distance of all the pair of friends in the given strings.Sample Input
ababa
Sample Output
10
Explanation: Friend pairs - (1, 3) (1, 5) (2, 4) (3, 5), I have written this Solution Code: #pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
string s;
cin>>s;
int c[26]={};
int f[26]={};
int ans=0;
int n=s.length();
for(int i=0;i<n;++i){
ans+=f[s[i]-'a']*i-c[s[i]-'a'];
f[s[i]-'a']++;
c[s[i]-'a']+=i;
}
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the inorder traversal of the tree.
Algorithm Inorder(tree)
1. Traverse the left subtree
2. Visit the root.
3. Traverse the right subtreeThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 100000Print a single line containing N space separated integers representing the inorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 2 3 1 4
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
Node root=new Node(1);
Node temp=root;
HashMap<Integer,Node> map=new HashMap<>();
map.put(1,temp);
int l,r;
for(int i=0;i<n;i++){
String[] arr=br.readLine().trim().split(" ");
l=Integer.parseInt(arr[0]);
r=Integer.parseInt(arr[1]);
temp=map.get(i+1);
if(l!=-1){
temp.left=new Node(l);
map.put(l,temp.left);
}
if(r!=-1){
temp.right=new Node(r);
map.put(r,temp.right);
}
}
inorder(root);
System.out.println(s);
}
static StringBuilder s = new StringBuilder("");
public static void inorder(Node root){
if(root==null){
return;
}
inorder(root.left);
s.append(root.data);
s.append(" ");
inorder(root.right);
}
}
class Node{
int data;
Node left,right;
Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the inorder traversal of the tree.
Algorithm Inorder(tree)
1. Traverse the left subtree
2. Visit the root.
3. Traverse the right subtreeThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 100000Print a single line containing N space separated integers representing the inorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 2 3 1 4
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, I have written this Solution Code: def inorder(u,c,l,r):
if c<=n:
if l[u]!=-1:
inorder(l[u],c,l,r)
print(u,end=" ")
if r[u]!=-1:
inorder(r[u],c,l,r)
c+=1
n=int(input())
left=[1]*(n+1)
right=[1]*(n+1)
for i in range(1,n+1):
left[i],right[i] = map(int,input().split())
count=0
root=1
inorder(root,count,left,right), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary tree, with root 1, print the inorder traversal of the tree.
Algorithm Inorder(tree)
1. Traverse the left subtree
2. Visit the root.
3. Traverse the right subtreeThe input consists of N+1 lines.
First line contains the integer N, denoting the number of nodes in the binary tree.
Next N lines contains two integers denoting the left and right child of the i'th node respectively.
If the node doesn't have a left or right child, it is denoted by '-1'
1 <= N <= 100000Print a single line containing N space separated integers representing the inorder traversal of the given treeSample Input 1:
5
2 4
5 3
-1 -1
-1 -1
-1 -1
Sample output 1:
5 2 3 1 4
Explanation: Given binary tree
1
/ \
2 4
/ \
5 3
, 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 l[N], r[N], c = 0;
void dfs(int u){
if(l[u] != -1)
dfs(l[u]);
cout << u << " ";
if(r[u] != -1)
dfs(r[u]);
c++;
}
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
cin >> l[i] >> r[i];
}
dfs(1);
assert(c == n);
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
int n=Integer.parseInt(br.readLine().trim());
if(n%3==1)
System.out.println("Dead");
else
System.out.println("Alive");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: n = int(input().strip())
if n%3 == 1:
print("Dead")
else:
print("Alive"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Joffrey has issued orders for Stark's execution. Now, you are trying to predict if Stark will survive or not. You are confident that Stark's survival depends on the N<sup>th</sup> fibonacci number's parity. If it is odd you will predict that Stark lives but if it is even you will predict that Stark dies. Given N, print your prediction.
Fibonacci series is a series where,
Fibonacci(1) = 0
Fibonacci(2) = 1
Fibonacci(i) = Fibonacci(i-1) + Fibonacci(i-2); for i > 2Input Contains a single integer N.
Constraints:
1 <= N <= 1000000Print "Alive" if Nth Fibonacci number is odd and print "Dead" if Nth Fibonacci number is even.Sample Input 1
3
Sample Output 1
Alive
Explanation: Fibonacci(3) = 1 which is odd.
Sample Input 2
4
Sample Output 1
Dead
Explanation: Fibonacci(4) = 2 which is even., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
typedef unsigned long long ull;
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main(){
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
if(n%3==1)
cout<<"Dead";
else
cout<<"Alive";
}, 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: 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: There are n people participating in some contest, they start participating in x minutes intervals. That means the first participant starts at time 0, the second participant starts at time x, the third — at time 2⋅x, and so on. The duration of the contest is t minutes for each participant, so the first participant finishes the contest at time t, the second — at time t+x, and so on. When a participant finishes the contest, their dissatisfaction equals the number of participants that started the game (or starting it now) but haven't yet finished it.
Determine the sum of dissatisfaction of all participants.The input consists of three space separated integers n, t and x — the number of participants, the start interval and the contest duration.
<b>Constraints</b>
1 ≤ n, x, t ≤ 2⋅10<sup>9</sup>Print the total dissatisfaction of participants.<b>Sample Input 1</b>
4 2 5
<b>Sample Output 1</b>
5
<b>Sample Input 2</b>
3 1 2
<b>Sample Output 2</b>
3, I have written this Solution Code: #include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
void fastio() {ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
const int mod = 1000000007;
/*
-----------------------------------------------
Author : Abhas
-----------------------------------------------
*/
#define int long long int
#define cint(n) int n;cin>>n;
#define PI 3.141592653589793238
#define ailoop(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define loop(i,a,b) for(int i=a;i<b;i++)
#define swapf(dt) void swap(dt &a,dt &b) {dt temp=a;a=b;b=temp;}
#define vsort(v) sort(v.begin(),v.end())
#define asort(a,n) sort(a,a+n)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define precise(digits) fixed<<setprecision(digits)
#define debug(func) cerr<<#func<<" : "<<func<<endl;
#define endl "\n"
#define spc " "
// int power(int a, int b) {
// if(b==0) return 1;
// if(b&1) return a*pow(a,b-1);
// else return pow(a*a,b/2);
// }
// bool issquare(int n) {
// int temp = sqrt(n);
// return temp*temp==n;
// }
void solution() {
int n,x,t;
cin>>n>>x>>t;
int count=0;
int m,p,o;
m = min(n-1,t/x);
if(m==0){
cout<<0<<endl;
}
else{
count = m*(m-1)/2 + m*(n-m);
cout<<count<<endl;
}
}
int32_t main() {
auto start=chrono::system_clock::now();
{
fastio();
int t = 1;
// cin>>t;cin.ignore();
for(int caseno=1;caseno<=t;caseno++) {
// cout<<"Case #"<<caseno<<": ";
solution();
}
}
auto end=chrono::system_clock::now();
// chrono::duration<double> elapsed=end-start;cout<<"Time taken: "<<elapsed.count()<<" sec";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer K and a queue of integers, your task is to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>ReverseK()</b>:- that takes the Queue and the integer K as parameters.
Constraints:
1 ≤ K ≤ N ≤ 10000
1 ≤ elements ≤ 10000
You need to return the modified Queue.Input 1:
5 3
1 2 3 4 5
Output 1:
3 2 1 4 5
Input 2:
5 5
1 2 3 4 5
Output 2:
5 4 3 2 1, I have written this Solution Code: static Queue<Integer> ReverseK(Queue<Integer> queue, int k) {
Stack<Integer> stack = new Stack<Integer>();
// Push the first K elements into a Stack
for (int i = 0; i < k; i++) {
stack.push(queue.peek());
queue.remove();
}
// Enqueue the contents of stack at the back
// of the queue
while (!stack.empty()) {
queue.add(stack.peek());
stack.pop();
}
// Remove the remaining elements and enqueue
// them at the end of the Queue
for (int i = 0; i < queue.size() - k; i++) {
queue.add(queue.peek());
queue.remove();
}
return queue;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In number theory, the totient Q(N) of a positive integer N is defined as the number of positive integers less than or equal to N that are co-prime to N.
Given an integer N. Compute the value of the totient Q(N) .First line contain integer t denoting number of test cases.
Next t lines contains an integer x.
Constraints
1<=t<=5
1<=x<=10^12For each test case print Q(n) in separate lineSample Input
4
1
2
3
6
Sample output
1
1
2
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static long CoPrime(long n)
{
double result = n;
for (long p = 2; p * p <= n; ++p) {
if (n % p == 0) {
while (n % p == 0)
n /= p;
result *= (1.0 - (1.0 / (double)p));
}
}
if (n > 1)
result *= (1.0 - (1.0 / (double)n));
return (long)result;
}
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long t=Long.parseLong(br.readLine());
while(t-->0){
long ans=1;
long k=Long.parseLong(br.readLine());
System.out.println(CoPrime(k));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In number theory, the totient Q(N) of a positive integer N is defined as the number of positive integers less than or equal to N that are co-prime to N.
Given an integer N. Compute the value of the totient Q(N) .First line contain integer t denoting number of test cases.
Next t lines contains an integer x.
Constraints
1<=t<=5
1<=x<=10^12For each test case print Q(n) in separate lineSample Input
4
1
2
3
6
Sample output
1
1
2
2, I have written this Solution Code: def totient(n):
result = n;
p = 2;
while(p * p <= n):
if (n % p == 0):
while (n % p == 0):
n = int(n / p);
result -= int(result / p);
p += 1;
if (n > 1):
result -= int(result / n);
return result;
for n in range(int(input())):
print(totient(int(input()))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In number theory, the totient Q(N) of a positive integer N is defined as the number of positive integers less than or equal to N that are co-prime to N.
Given an integer N. Compute the value of the totient Q(N) .First line contain integer t denoting number of test cases.
Next t lines contains an integer x.
Constraints
1<=t<=5
1<=x<=10^12For each test case print Q(n) in separate lineSample Input
4
1
2
3
6
Sample output
1
1
2
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int mod=1000000007;
int fact[sz];
int ifact[sz];
int lpf[2000000];
int power(int a,int b)
{
if(a==0) return 0;
if(a==1) return 1;
if(b==0) return 1;
if(b==1) return a;
int xx=power(a,b/2);
int yy=(xx*xx)%mod;
if(b%2==0) return xx;
else return (xx*a)%mod;
}
int inverse(int a)
{
return power(a,mod-2LL);
}
int nCr(int a,int b)
{
if(b>a) return 0;
int x=fact[a];
x=(x*ifact[b])%mod;
x=(x*ifact[a-b])%mod;
return x;
}
void seiv()
{
lpf[1]=1;
for(int i=2;i<=1000000;i++)
{
if(lpf[i]==0)
{
for(int j=i;j<=1000000;j+=i)
{
if(lpf[j]==0) lpf[j]=i;
}
}
}
}
int totient(int n)
{ if(n==1) return 1;
int vh=1;
for(int i=2;i*i<=n;i++)
{ if(n%i==0)
{
int x=i;
int cnt=0;
while(n%x==0) {
n/=x;
cnt++;
}
vh*=(pow(x,cnt)-pow(x,cnt-1));
}
}
if(n>1) vh*=(n-1LL);
return vh;
}
signed main()
{
int t;
cin>>t;
while(t>0)
{t--;
int n;
cin>>n;
cout<<totient(n)<<endl;
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Perform the loop operation to create a square pattern for the given number of rows.
<b>Example</b>
number of rows = 5
*****
* *
* *
* *
*****The only line contains the integer "size" denoting the number of rows.
<b>Constarints</b>
1 ≤ size ≤ 7Print the square pattern.Sample input:
4
Sample output:
*****
* *
* *
*****, I have written this Solution Code: import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
assert(size >= 1 && size <= 7);
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= size; j++) {
if(i == 1 || i == size || j == 1 || j == size) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array 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: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, 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());
long fact=1;
for(int i=1; i<=n;i++){
fact*=i;
}
System.out.print(fact);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, I have written this Solution Code: def fact(n):
if( n==0 or n==1):
return 1
return n*fact(n-1);
n=int(input())
print(fact(n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find factorial of a given number where n! = n * n-1 * n-2 .....* 1First line consists of a single integer denoting n
Constraints:-
0 <= n <= 20Output is a single line containing factorial(n)Sample Input
5
Sample Output
120
Explanation:-
5!= 5 * 4 * 3 * 2 * 1 = 120
Sample Input
10
Sample Output
3628800, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
int main(){
int t;
t=1;
while(t--){
int n;
cin>>n;
unsigned long long sum=1;
for(int i=1;i<=n;i++){
sum*=i;
}
cout<<sum<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. In one move, you can choose two indices i, j (1 <= i, j <= N) such that A<sub>i</sub>+A<sub>j</sub> is odd and swap A<sub>i</sub> and A<sub>j</sub>. Find the lexicographically smallest array you can obtain after any number of operations.The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print N space seperated integers, the lexicographically smallest array you can obtain after any number of operations.Sample Input:
3
5 2 8
Sample Output:
2 5 8
Explaination:
We can swap 5 and 2 as their sum is odd., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static void lexicographically_smaller(int arr[],int n){
int odd = 0, even = 0;
for (int i=0;i<n;i++)
{
if(arr[i]%2==1)
odd++;
else
even++;
}
if (odd>0 && even>0)
Arrays.sort(arr);
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
lexicographically_smaller(arr, n);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array A of size N. In one move, you can choose two indices i, j (1 <= i, j <= N) such that A<sub>i</sub>+A<sub>j</sub> is odd and swap A<sub>i</sub> and A<sub>j</sub>. Find the lexicographically smallest array you can obtain after any number of operations.The first line of the input contains a single integer N.
The second line of the input contains N space seperated integers.
Constraints:
1 <= N <= 10<sup>5</sup>
1 <= A<sub>i</sub> <= 10<sup>9</sup>Print N space seperated integers, the lexicographically smallest array you can obtain after any number of operations.Sample Input:
3
5 2 8
Sample Output:
2 5 8
Explaination:
We can swap 5 and 2 as their sum is odd., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
vector<int> a(n);
for(auto &i : a) cin >> i;
int odd = 0;
for(auto i : a){
if(i % 2) odd++;
}
if(odd != 0 and odd != n){
sort(a.begin(), a.end());
}
for(auto i : a) cout << i << ' ';
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After saving the planet of Xander in 2014 from Ronan, the guardians of the galaxy decided to play a game on it. Between the vast furious oceans, they discover a row of buildings. The buildings are of different heights arranged at each index along a number line. Gamora starts at building 0 and a height of 0. Groot gets the task to <b>determine the minimum thrust that Gamora needs at the start of the game so that she can jump to the top of each building without her thrust going below zero</b>. Write a code to help Groot with his task.
The units of height relate directly to units of thrust. Gamora’s thrust level is calculated as follows:
• If gamora's thrust is less than the height of the building, her new thrust = gamora's thrust – (height – gamora's thrust)
• If gamora's thrust is greater than equal to the height of the building, her new thrust = gamora's thrust + (gamora's thrust - height)The first line contains an integer n, the number of buildings.
The second line n space-separated integers, arr[1], arr[2]…arr[n], the heights of the building.
<b>Constraints:-</b>
1 ≤ n ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>The output contains a single integer denoting the minimum thrust Gamora needs at the start of the game.Sample Input:-
5
3 4 3 2 4
Sample Output:-
4
Explanation:-
If we take 3 then:-
at index 1:- 3 + 3-3 = 3
at index 2:- 3 - (4-3) = 2
at index 3:- 2 - (3-2) = 1
at index 4:- 1 - (2-1) = 0
Sample Input:-
3
4 4 4
Sample Output:-
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i]= sc.nextLong();
}
long l = 0;
long r= 1000000000;
long x=0;
while (l!=r){
x= (l+r)/2;
if(checkThrust(arr,x)) {
r=x;
}
else {
l=x+1;
}
}
System.out.print(l);
}
static boolean checkThrust(long[] arr,long r){
long thrust = r;
for (int i = 0; i < arr.length; i++) {
thrust = 2*thrust-arr[i];
if(thrust>=1000000000000L) return true;
if(thrust<0) return false;
}
return true;
}
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 {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: After saving the planet of Xander in 2014 from Ronan, the guardians of the galaxy decided to play a game on it. Between the vast furious oceans, they discover a row of buildings. The buildings are of different heights arranged at each index along a number line. Gamora starts at building 0 and a height of 0. Groot gets the task to <b>determine the minimum thrust that Gamora needs at the start of the game so that she can jump to the top of each building without her thrust going below zero</b>. Write a code to help Groot with his task.
The units of height relate directly to units of thrust. Gamora’s thrust level is calculated as follows:
• If gamora's thrust is less than the height of the building, her new thrust = gamora's thrust – (height – gamora's thrust)
• If gamora's thrust is greater than equal to the height of the building, her new thrust = gamora's thrust + (gamora's thrust - height)The first line contains an integer n, the number of buildings.
The second line n space-separated integers, arr[1], arr[2]…arr[n], the heights of the building.
<b>Constraints:-</b>
1 ≤ n ≤ 10<sup>5</sup>
1 ≤ arr[i] ≤ 10<sup>5</sup>The output contains a single integer denoting the minimum thrust Gamora needs at the start of the game.Sample Input:-
5
3 4 3 2 4
Sample Output:-
4
Explanation:-
If we take 3 then:-
at index 1:- 3 + 3-3 = 3
at index 2:- 3 - (4-3) = 2
at index 3:- 2 - (3-2) = 1
at index 4:- 1 - (2-1) = 0
Sample Input:-
3
4 4 4
Sample Output:-
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
const long long linf = 0x3f3f3f3f3f3f3f3fLL;
const int N = 111111;
int n;
int h[N];
int check(long long x) {
long long energy = x;
for(int i = 1; i <= n; i++) {
energy += energy - h[i];
if(energy >= linf) return 1;
if(energy < 0) return 0;
}
return 1;
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
long long L = 0, R = linf;
long long ans=0;
while(L < R) {
long long M = (L + R) / 2;
if(check(M)) {
R = M;
ans=M;
} else {
L = M + 1;
}
}
cout << ans << endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a N x M integer matrix A and Q queries of form X1 Y1 X2 Y2. Print the sum of A[i][j], for X1 <= i <= X2 and Y1 <= j <= Y2.The first line contains two integer N and M, denoting the size of the matrix.
Next N line contains M integers denoting elements of the matrix.
Next line contains a single integer Q, denoting the number of queries.
Next Q lines lines four integers X1 Y1 X2 Y2, denoting the query as mentioned in problem statement
1 <= N, M <= 100
1 <= A[i][j] <= 100
1 <= Q <= 100000
1 <= X1 <= X2 <= N
1 <= Y1 <= Y2 <= MPrint Q lines containing the answer to each query.Sample Input:
2 2
1 5
2 3
3
1 1 1 1
1 1 1 2
1 1 2 2
Sample Output:
1
6
11
Explanation:
Q1: 1
Q2: 1 + 5 = 6
Q3: 1 + 5 + 2 + 3 = 11, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public
static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] a = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
a[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = sc.nextInt();
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
int q = sc.nextInt();
while (q-- > 0) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int sum = 0;
sum = a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
System.out.println(sum);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a N x M integer matrix A and Q queries of form X1 Y1 X2 Y2. Print the sum of A[i][j], for X1 <= i <= X2 and Y1 <= j <= Y2.The first line contains two integer N and M, denoting the size of the matrix.
Next N line contains M integers denoting elements of the matrix.
Next line contains a single integer Q, denoting the number of queries.
Next Q lines lines four integers X1 Y1 X2 Y2, denoting the query as mentioned in problem statement
1 <= N, M <= 100
1 <= A[i][j] <= 100
1 <= Q <= 100000
1 <= X1 <= X2 <= N
1 <= Y1 <= Y2 <= MPrint Q lines containing the answer to each query.Sample Input:
2 2
1 5
2 3
3
1 1 1 1
1 1 1 2
1 1 2 2
Sample Output:
1
6
11
Explanation:
Q1: 1
Q2: 1 + 5 = 6
Q3: 1 + 5 + 2 + 3 = 11, I have written this Solution Code: #include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;
const int N = 1e2 + 5;
const int ten6 = 1e6;
const int inf = 1e9 + 9;
int a[N][N];
void solve(){
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
}
}
int q; cin >> q;
while(q--){
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << a[x2][y2] - a[x1-1][y2] - a[x2][y1-1] + a[x1-1][y1-1] << endl;
}
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms: ";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given the following pseudocode:
code :
res = a
for i = 1 to k
if i is odd
res = res & b
else
res = res | b
You are also given the values of a, b and k. Find the value of res.First line contains of single integer t denoting number of test cases.
Each test cases consists of 3 lines where each line consists of a single integer denoting the values of a, b and k respectively.
<b>Constraints</b>
1<= T <= 1e5
1<= a, b, k <= 1e18Print the final value of res for each test case in a new lineSample Input :
1
4 5 1
Sample Output :
4, I have written this Solution Code: def solve(a,b,k):
res=a
for i in range(1,k):
if(i%2==1):
res= res & b
else:
res= res | b
return res
t=int(input())
for i in range(t):
a,b,k=list(map(int, input().split()))
if k==1:
print(a&b)
else:
print(b), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given the following pseudocode:
code :
res = a
for i = 1 to k
if i is odd
res = res & b
else
res = res | b
You are also given the values of a, b and k. Find the value of res.First line contains of single integer t denoting number of test cases.
Each test cases consists of 3 lines where each line consists of a single integer denoting the values of a, b and k respectively.
<b>Constraints</b>
1<= T <= 1e5
1<= a, b, k <= 1e18Print the final value of res for each test case in a new lineSample Input :
1
4 5 1
Sample Output :
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int a, b, k;
cin >> a >> b >> k;
cout << ((k == 1) ? a & b : b) << "\n";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given the following pseudocode:
code :
res = a
for i = 1 to k
if i is odd
res = res & b
else
res = res | b
You are also given the values of a, b and k. Find the value of res.First line contains of single integer t denoting number of test cases.
Each test cases consists of 3 lines where each line consists of a single integer denoting the values of a, b and k respectively.
<b>Constraints</b>
1<= T <= 1e5
1<= a, b, k <= 1e18Print the final value of res for each test case in a new lineSample Input :
1
4 5 1
Sample Output :
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
for(int j=0;j<t;j++){
String[] arr = br.readLine().split(" ");
long a = Long.parseLong(arr[0]);
long b = Long.parseLong(arr[1]);
long k = Long.parseLong(arr[2]);
System.out.println((k==1)?a&b:b);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., 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 infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int k;
cin>>k;
int a[n+1];
int to=0;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
int j=1;
int s=0;
int ans=n;
for(int i=1;i<=n;++i)
{
while(s<k&&j<=n)
{
s+=a[j];
++j;
}
if(s>=k)
{
ans=min(ans,j-i);
}
s-=a[i];
}
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., 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));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
long k=Long.parseLong(s[1]);
int a[]=new int[n];
s=br.readLine().split(" ");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(s[i]);
}
int length=Integer.MAX_VALUE,i=0,j=0;
long currSum=0;
for(j=0;j<n;j++)
{
currSum+=a[j];
while(currSum>=k)
{
length=Math.min(length,j-i+1);
currSum-=a[i];
i++;
}
}
System.out.println(length);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array Arr of N integers. A subarray is good if the sum of elements of that subarray is greater than or equal to K. Print the length of good subarray of minimum length.First line of input contains N and K.
Second line of input contains N integers representing the elements of the array Arr.
Constraints
1 <= N <= 100000
1 <= Arr[i] <= 100000
1 <= K <= 1000000000000Print the length of good subarray of minimum length.Sample input
5 12
2 3 2 5 5
Sample output
3
Explanation :
Subarray from index 3 to 5 has sum 12 and is therefore good and its length(3) is minimum among all possible good subarray., I have written this Solution Code: def result(arr,n,k):
minnumber = n + 1
start = 0
end = 0
curr_sum = 0
while(end < n):
while(curr_sum < k and end < n):
curr_sum += arr[end]
end += 1
while( curr_sum >= k and start < n):
if (end - start < minnumber):
minnumber = end - start
curr_sum -= arr[start]
start += 1
return minnumber
n = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(result(arr, n[0], n[1])), 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: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] val = new int[n];
for(int i=0; i<n; i++){
val[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] freq = new int[n];
for(int i=0; i<n; i++){
freq[i] = Integer.parseInt(st.nextToken());
}
int k = Integer.parseInt(br.readLine());
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (val[j] < val[i]) {
int temp = val[i];
val[i] = val[j];
val[j] = temp;
int temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;
}
}
}
int element=0;
for(int i=0; i<n; i++){
for(int j=0; j<freq[i]; j++){
element++;
int value = val[i];
if(element==k){
System.out.print(value);
break;
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: def myFun():
n = int(input())
arr1 = list(map(int,input().strip().split()))
arr2 = list(map(int,input().strip().split()))
k = int(input())
arr = []
for i in range(n):
arr.append((arr1[i], arr2[i]))
arr.sort()
c = 0
for i in arr:
k -= i[1]
if k <= 0:
print(i[0])
return
myFun()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define inf 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int N;ll K;
cin>>N;
int c=0;
pair<int, ll> A[N];
for(int i=0;i<N;++i){
cin >> A[i].first ;
}
for(int i=0;i<N;++i){
cin >> A[i].second ;
}
cin>>K;
sort(A, A+N);
for(int i=0;i<N;++i){
K -= A[i].second;
if(K <= 0){
cout << A[i].first << endl;;
break;
}
}
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String st = br.readLine();
int len = 0;
int c=0;
for(int i=0;i<st.length();i++){
if(st.charAt(i)=='A'){
c++;
len = Math.max(len,c);
}else{
c=0;
}
}
System.out.println(len);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: S=input()
max=0
flag=0
for i in range(0,len(S)):
if(S[i]=='A' or S[i]=='B'):
if(S[i]=='A'):
flag+=1
if(flag>max):
max=flag
else:
flag=0
print(max), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string S consisting of characters 'A' or 'B' only, you need to find the maximum length of substring consisting of character 'A' only.The first and the only line of input contains the string S.
Constraints
1 <= |S| <= 100000
S consists of characters 'A' or 'B' only.Output a single integer, the answer to the problem.Sample Input
ABAAABBBAA
Sample Output
3
Explanation: Substring from character 3-5 is the longest consisting of As only.
Sample Input
AAAA
Sample Output
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
string s; cin>>s;
int ct = 0;
int ans = 0;
for(char c: s){
if(c == 'A')
ct++;
else
ct=0;
ans = max(ans, ct);
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string A. The only operation allowed is to insert characters at the beginning of the string.
Find how many minimum characters are needed to be inserted to make the string a palindrome string.
Note: Strings are to be considered case sensitive.Input contains a single line string.
Constraints:
1 ≤ S ≤ 400Output the minimum characters that are needed to be inserted to make the string a palindrome string.Input 1:
ABC
Output 1:
2
Explanation 1:
Insert 'B' at beginning, string becomes: "BABC".
Insert 'C' at beginning, string becomes: "CBABC"., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int count = 0;
int flag = 0;
while (str.length() > 0)
{
if (ispalindrome(str))
{
flag = 1;
break;
} else
{
count++;
str = str.substring(0, str.length() - 1);
}
}
if (flag == 1) {
System.out.println(count);
}
}
static boolean ispalindrome(String s)
{
int l = s.length();
for (int i = 0, j = l - 1; i <= j; i++, j--)
{
if (s.charAt(i) != s.charAt(j))
return false;
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string A. The only operation allowed is to insert characters at the beginning of the string.
Find how many minimum characters are needed to be inserted to make the string a palindrome string.
Note: Strings are to be considered case sensitive.Input contains a single line string.
Constraints:
1 ≤ S ≤ 400Output the minimum characters that are needed to be inserted to make the string a palindrome string.Input 1:
ABC
Output 1:
2
Explanation 1:
Insert 'B' at beginning, string becomes: "BABC".
Insert 'C' at beginning, string becomes: "CBABC"., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int A[2000][2000];
int vis[200][200];
int dist[200][200];
int xa[8]={1,1,-1,-1,2,2,-2,-2};
int ya[8]={2,-2,2,-2,1,-1,1,-1};
int n,m;
int val,hh;
int check(int x,int y)
{
if (x>=0 && y>=0 && x<n && y<m )return 1;
else return 0;
}
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
signed main()
{
string s;
cin>>s;
int n=s.size();
int ans=1;
for(int i=1;i<n;i++)
{ int ch=1;
for(int j=0;j<=i;j++)
{
if(s[j]!=s[i-j]) ch=0;
}
if(ch==1) ans=max(ans,i+1);
}
cout<<n-ans;
}, 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 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 unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String StrInput[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(StrInput[0]);
int s = Integer.parseInt(StrInput[1]);
int arr[] = new int[n];
String StrInput2[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(StrInput2[i]);
}
int sum = arr[0];
int startingindex = 0;
int endingindex = 1;
int j = 0;
int i;
for(i=1;i<=n;i++)
{
if(sum < s && arr[i] != 0)
{
sum += arr[i];
}
while(sum > s && startingindex < i-1)
{
sum -= arr[startingindex];
startingindex++;
}
if(sum == s)
{
endingindex = i+1;
if(arr[0] == 0)
{
System.out.print(startingindex+2 + " " + endingindex);
}
else
{
System.out.print(startingindex+1 + " "+ endingindex);
}
break;
}
if(i == n && sum < s)
{
System.out.print(-1);
break;
}
}
}
catch(Exception e)
{
System.out.print(-1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int sum=0;
unordered_map<int,int> m;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==k){cout<<1<<" "<<i+1;return 0;}
if(m.find(sum-k)!=m.end()){
cout<<m[sum-k]+2<<" "<<i+1;
return 0;
}
m[sum]=i;
}
cout<<-1;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to a given number S.Each test case consists of two lines. The first line of each test case is N and S, where N is the size of the array and S is the sum. The second line of each test case contains N space-separated integers denoting the array elements.
<b>Constraints:-</b>
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ Ai ≤ 10<sup>5</sup>Print the starting and ending positions (1 indexing) of first such occurring subarray from the left if sum equals to subarray, else print -1.Sample Input
5 12
1 2 3 7 5
Sample Output
2 4
Explanation:
subarray starting from index 2 and ending at index 4 => {2 , 3 , 7}
sum = 2 + 3 + 7 = 12
Sample Input
10 15
1 2 3 4 5 6 7 8 9 10
Sample Output
1 5, I have written this Solution Code: def sumFinder(N,S,a):
currentSum = a[0]
start = 0
i = 1
while i <= N:
while currentSum > S and start < i-1:
currentSum = currentSum - a[start]
start += 1
if currentSum == S:
return (start+1,i)
if i < N:
currentSum = currentSum + a[i]
i += 1
return(-1)
N, S = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = sumFinder(N,S,a)
if(ans==-1):
print(ans)
else:
print(ans[0],ans[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton is standing before a typical staircase with N steps. Newton is standing on the 0-th step and wants to go to the N-th step. To reach the top he can either take a single step or he can take a double step (two steps) at the same time.
However, M of the N steps are broken i. e. S<sub>1</sub>, S<sub>2</sub>, ... , S<sub>M</sub> are broken and Newton cannot visit those steps,
Find out the number of different ways in which Newton can climb to the top of the staircase. Since the number can be very large, find it modulo 1,000,000,007The first line contains two integers, N and M.
The next M lines contains a single integer each, S<sub>i</sub>
<b>Constraints:</b>
1) 1 ≤ N ≤ 2 x 10<sup>5</sup>
2) 0 ≤ M ≤ N - 1
3) 1 ≤ S<sub>1</sub> < S<sub>2</sub>, < ... < S<sub>M</sub> ≤ N - 1Print the number of ways<b>Sample Input 1:</b>
6 1
3
<b>Sample Output 1:</b>
4
<b>Sample Explanation 1:</b>
There are four ways to climb up the stairs, as follows:
0→1→2→4→5→6
0→1→2→4→6
0→2→4→5→6
0→2→4→6
<b>Sample Input 2:</b>
100 5
1
23
45
67
89
<b>Sample Output 2:</b>
608200469, I have written this Solution Code: #include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
int x,n,m;
cin>>n>>m;
vector<int>v(n+1,0);
for(int i=1;i<=m;i++){
cin>>x;
v[x]=1;
}
vector<int>dp(n+1,0);
int M = 1e9+7;
dp[0]=1;
for(int i=0;i<n+1;i++){
if(v[i]==0){
if(i+2<=n)dp[i+2]+=dp[i];
if(i+1<=n)dp[i+1]+=dp[i];
dp[i+2]%=M;
dp[i+1]%=M;
}
}
int ans = dp[n]%M;
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <b>arr</b> and a target value <b>K</b>. Print the minimum integer value <b>V</b> such that when we change all values greater than V in the array to V, <b>the absolute difference (|sum - K|) between the sum of the array elements and K is minimized.</b>
Note: the answer is not necessarily a number from arr.The input line containing T, denoting the number of testcases. Each testcase contains 2 lines. First line contains N, size of array and target K separated by space. Second line contains elements of array.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
1 <= arr[i], K <= 10^5For each testcase you need to print the minimum such integer possible in a new line.Input:
2
3 10
4 9 3
3 10
2 3 5
Output:
3
5
Explanation:
Testcase 1: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer., I have written this Solution Code: def findBestValue(arr,target):
org_sum=sum(arr)
if org_sum<target:
return max(arr)
l=1
r=max(arr)
while l<r:
mid=l+(r-l)//2;
tmp=0
for val in arr:
if val>mid:
tmp+=mid
else:
tmp+=val
if tmp<target:
l=mid+1
else:
r=mid
sum1=0
sum2=0
for val in arr:
if val>l:
sum1+=l
else:
sum1+=val
if val>l-1:
sum2+=l-1
else:
sum2+=val
if abs(sum2-target)<= abs(sum1-target):
return l-1
else:
return l
t=int(input())
while t:
n,target=map(int,input().split())
list1=list(map(int,input().split()))
print(findBestValue(list1,target))
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <b>arr</b> and a target value <b>K</b>. Print the minimum integer value <b>V</b> such that when we change all values greater than V in the array to V, <b>the absolute difference (|sum - K|) between the sum of the array elements and K is minimized.</b>
Note: the answer is not necessarily a number from arr.The input line containing T, denoting the number of testcases. Each testcase contains 2 lines. First line contains N, size of array and target K separated by space. Second line contains elements of array.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
1 <= arr[i], K <= 10^5For each testcase you need to print the minimum such integer possible in a new line.Input:
2
3 10
4 9 3
3 10
2 3 5
Output:
3
5
Explanation:
Testcase 1: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
long sum=0;
int j=0;
long ans=0;
long res;
long total=INT_MAX;
for(int i=0;i<=100000;i++){
if(i>a[j] && j!=n){sum+=a[j];j++;}
ans=sum+i*(n-j);
if(abs(ans-k)<total){res=i;total=abs(ans-k);}
}
cout<<res<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <b>arr</b> and a target value <b>K</b>. Print the minimum integer value <b>V</b> such that when we change all values greater than V in the array to V, <b>the absolute difference (|sum - K|) between the sum of the array elements and K is minimized.</b>
Note: the answer is not necessarily a number from arr.The input line containing T, denoting the number of testcases. Each testcase contains 2 lines. First line contains N, size of array and target K separated by space. Second line contains elements of array.
Constraints:
1 <= T <= 50
1 <= N <= 10^4
1 <= arr[i], K <= 10^5For each testcase you need to print the minimum such integer possible in a new line.Input:
2
3 10
4 9 3
3 10
2 3 5
Output:
3
5
Explanation:
Testcase 1: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer., I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main(String args[])throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
int N = Integer.parseInt(str[0]);
int K = Integer.parseInt(str[1]);
//`int D = Integer.parseInt(str[1]);
int arr[] = new int[N];
str = read.readLine().trim().split(" ");
for(int i = 0; i < N; i++)
arr[i] = Integer.parseInt(str[i]);
//int res[] = moveZeroes(arr);
//print(res);
System.out.println(findBestValue(arr, K));
}
}
static void print(int list[])
{
for(int i = 0; i < list.length; i++)
System.out.print(list[i] + " ");
}
static int findBestValue(int arr[], int target) {
int sum = 0;
int mx = Integer.MIN_VALUE;
int sz = arr.length;
int remaining_target = target;
int remaining_items = sz;
int one_part = target / sz;
for(int i = 0; i < sz; ++i) {
mx = Math.max(mx, arr[i]);
sum += arr[i];
if(arr[i] < one_part) {
remaining_items--;
remaining_target -= arr[i];
}
}
if(sum <= target) {
return mx;
}
int val1 = remaining_target / remaining_items;
int min_val = val1 - 1;
int min_diff = Math.abs(remaining_target - (min_val * remaining_items));
int diff1 = Math.abs(remaining_target - (val1 * remaining_items));
if(diff1 < min_diff) {
min_val = val1;
min_diff = diff1;
}
int val2 = val1 + 1;
int diff2 = Math.abs(remaining_target - (val2 * remaining_items));
return (diff2 < min_diff) ? val2 : min_val;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are three sticks with integer lengths l<sub>1</sub>, l<sub>2</sub> and l<sub>3</sub>. You are asked to break exactly one of them into two pieces in such a way that:
- both pieces have positive (strictly greater than 0) integer length;
- the total length of the pieces is equal to the original length of the stick;
- it's possible to construct a rectangle from the resulting four sticks such that each stick is used as exactly one of its sides. A square is also considered a rectangle.
Determine if it's possible to do that.The input consists of 3 space- separated integers l<sub>1</sub>, l<sub>2</sub> and l<sub>3</sub>.
<b>Constraints</b>
1 ≤ l<sub>i</sub> ≤ 10<sup>8</sup>Print "Yes" if it's possible to break one of the sticks into two pieces with positive integer length in such a way that it's possible to construct a rectangle from the resulting four sticks. Otherwise, print "No".<b>Sample Input 1</b>
6 1 5
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
2 5 2
<b>Sample Output 2</b>
No, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int tt=1;
// cin >> tt;
while (tt--) {
int a, b, c;
cin >> a >> b >> c;
if (a == b + c || b == c + a || c == a + b) {
cout << "Yes" << '\n';
} else {
if ((a == b && c % 2 == 0) || (a == c && b % 2 == 0) || (b == c && a % 2 == 0)) {
cout << "Yes" << '\n';
} else {
cout << "No" << '\n';
}
}
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a number N we have N! unique permutations. <b>A permutation is a sequence of integers from 1 to N of length N containing each number exactly once.</b>
For a positive integer X, <b>X! = 1 * 2 * 3 *...* X-1 * X</b>
Your task is to find the Kth smallest permutation when all possible permutations of size N are arranged in sorted order.Input contains only two integers, the value of N and K.
Constraints:-
1 <= N <= 10000
1 <= K <= min(N!,100000000)Print the Kth permutation in form of a string. i. e don't print spaces between two numbers.Sample Input:-
3 5
Sample Output:-
312
Explanation:-
All permutations of length 3 are:-
123
132
213
231
312
321
Sample Input:-
11 2
Sample Output:-
1234567891110, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[1]);
Main m=new Main();
System.out.print(m.getPermutation(n,k));
}
public String getPermutation(int n, int k) {
int idx = 1;
for ( idx = 1; idx <= n;idx++) {
if (fact(idx) >= k) break;
}
StringBuilder ans = new StringBuilder();
for( int i = 1; i <=n-idx;i++) {
ans.append(i);
}
ArrayList<Integer> dat = new ArrayList<>(n);
for( int i = 1; i <= idx;i++) {
dat.add(i);
}
for( int i = 1; i <= idx;i++) {
int t = (int) ((k-1)/fact(idx-i));
ans.append(dat.get(t)+(n-idx));
dat.remove(t);
k = (int)(k-t*(fact(idx-i)));
}
return ans.toString();
}
public String getPermutation0(int n, int k) {
int idx = k;
StringBuilder ans = new StringBuilder();
ArrayList<Integer> dat = new ArrayList<>(n);
for( int i = 1; i <= n;i++) {
dat.add(i);
}
for(int i = 1; i <= n;i++) {
idx = (int)((k-1)/fact(n-i));
ans.append(dat.get(idx));
dat.remove(idx);
k = (int)(k - idx*fact(n-i));
}
return ans.toString();
}
public long fact(int n) {
int f = 1;
for( int i = 1; i <= n;i++) {
f *= i;
}
return f;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a number N we have N! unique permutations. <b>A permutation is a sequence of integers from 1 to N of length N containing each number exactly once.</b>
For a positive integer X, <b>X! = 1 * 2 * 3 *...* X-1 * X</b>
Your task is to find the Kth smallest permutation when all possible permutations of size N are arranged in sorted order.Input contains only two integers, the value of N and K.
Constraints:-
1 <= N <= 10000
1 <= K <= min(N!,100000000)Print the Kth permutation in form of a string. i. e don't print spaces between two numbers.Sample Input:-
3 5
Sample Output:-
312
Explanation:-
All permutations of length 3 are:-
123
132
213
231
312
321
Sample Input:-
11 2
Sample Output:-
1234567891110, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int factorial(int n) {
if (n > 12) {
// this overflows in int. So, its definitely greater than k
// which is all we care about. So, we return INT_MAX which
// is also greater than k.
return INT_MAX;
}
// Can also store these values. But this is just < 12 iteration, so meh!
int fact = 1;
for (int i = 2; i <= n; i++) fact *= i;
return fact;
}
string getPermutationi(int k, vector<int> &candidateSet) {
int n = candidateSet.size();
if (n == 0) {
return "";
}
if (k > factorial(n)) return ""; // invalid. Should never reach here.
int f = factorial(n - 1);
int pos = k / f;
k %= f;
string ch = to_string(candidateSet[pos]);
// now remove the character ch from candidateSet.
candidateSet.erase(candidateSet.begin() + pos);
return ch + getPermutationi(k, candidateSet);
}
string solve(int n, int k) {
vector<int> candidateSet;
for (int i = 1; i <= n; i++) candidateSet.push_back(i);
return getPermutationi(k - 1, candidateSet);
}
int main(){
int n,k;
cin>>n>>k;
cout<<solve(n,k);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String s2 = br.readLine();
boolean flag = true;
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for(int i=0; i<s1.length(); i++){
arr1[s1.charAt(i)-97]++;
}
for(int i=0; i<s2.length(); i++){
arr2[s2.charAt(i)-97]++;
}
for(int i=0; i<25; i++){
if(arr1[i]!=arr2[i]){
flag = false;
break;
}
}
if(flag==true)
System.out.print("YES");
else System.out.print("NO");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: s1 = input().strip()
s2 = input().strip()
dict1 = dict()
dict2 = dict()
for i in s1:
dict1[i] = dict1.get(i, 0) + 1
for j in s2:
dict2[j] = dict2.get(j, 0) + 1
print(("NO", "YES")[dict1 == dict2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int A[26],B[26];
signed main()
{
string s,p;
cin>>s>>p;
for(int i=0;i<s.size();i++)
{
int y=s[i]-'a';
A[y]++;
}
for(int i=0;i<p.size();i++)
{
int y=p[i]-'a';
B[y]++;
}int ch=1;
for(int i=0;i<26;i++)
{
if(B[i]!=A[i])ch=0;
}
if(ch==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: // str1 and str2 are the two input strings
function isAnagram(str1,str2){
// Get lengths of both strings
let n1 = str1.length;
let n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return "NO";
str1 = str1.split('')
str2 = str2.split('')
// Sort both strings
str1.sort();
str2.sort()
// Compare sorted strings
for (let i = 0; i < n1; i++)
if (str1[i] != str2[i])
return "NO";
return "YES";
}
, In this Programming Language: JavaScript, 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: Newton has 3 groups of cards. Group 1 has X total of cards, Group 2 has Y cards, Group 3 has Z cards. All X cards have 1 written on them, all Y cards have 0 written on them, all Z cards have -1 written on them.
Now Newton wants to pick C cards and add the values written on them. Help Newton pick C cards to get the maximum possible value.The only line of the input contains 4 integers, X, Y, Z and C.
Constraints:
1) 0 <= X, Y, Z <= 10^9
2) 1 <= k <= X+Y+Z;Print the maximum sum of any C cards.Sample Input 1:
3 2 1 4
Sample Output 1:
3
Explanation 1 :
Newton will pick 3 cards of 1 value and 1 card of 0 value.
Sample Input 2:
1 2 3 5
Sample Output 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));
String[] str=br.readLine().split(" ");
int x=Integer.parseInt(str[0]);
int y=Integer.parseInt(str[1]);
int z=Integer.parseInt(str[2]);
int c=Integer.parseInt(str[3]);
if(x >= c)
{
System.out.println(c);
}
else if(x+y >= c)
{
System.out.println(x);
}
else if(x+y+z >= c)
{
System.out.println(x - (c - (x+y)));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton has 3 groups of cards. Group 1 has X total of cards, Group 2 has Y cards, Group 3 has Z cards. All X cards have 1 written on them, all Y cards have 0 written on them, all Z cards have -1 written on them.
Now Newton wants to pick C cards and add the values written on them. Help Newton pick C cards to get the maximum possible value.The only line of the input contains 4 integers, X, Y, Z and C.
Constraints:
1) 0 <= X, Y, Z <= 10^9
2) 1 <= k <= X+Y+Z;Print the maximum sum of any C cards.Sample Input 1:
3 2 1 4
Sample Output 1:
3
Explanation 1 :
Newton will pick 3 cards of 1 value and 1 card of 0 value.
Sample Input 2:
1 2 3 5
Sample Output 2:
-1, I have written this Solution Code: x,y,z,c = map(int, input().split())
ans=0
ans+=(x if c-x>=0 else c)
c-=x
if c>0:
c-=y
if c>0:
ans-=min(c,z)
print(ans), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton has 3 groups of cards. Group 1 has X total of cards, Group 2 has Y cards, Group 3 has Z cards. All X cards have 1 written on them, all Y cards have 0 written on them, all Z cards have -1 written on them.
Now Newton wants to pick C cards and add the values written on them. Help Newton pick C cards to get the maximum possible value.The only line of the input contains 4 integers, X, Y, Z and C.
Constraints:
1) 0 <= X, Y, Z <= 10^9
2) 1 <= k <= X+Y+Z;Print the maximum sum of any C cards.Sample Input 1:
3 2 1 4
Sample Output 1:
3
Explanation 1 :
Newton will pick 3 cards of 1 value and 1 card of 0 value.
Sample Input 2:
1 2 3 5
Sample Output 2:
-1, I have written this Solution Code: // #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define endl "\n"
#define EPS 1e-9
#define MOD 1000000007
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (x).size()
// #define forf(t,i,n) for(t i=0;i<n;i++)
// #define forr(t,i,n) for(t i=n-1;i>=0;i--)
#define forf(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i--)
#define F0(i, n) for(ll i=0; i<n; i++)
#define F1(i, n) for(ll i=1; i<=n; i++)
#define FOR(i, s, e) for(ll i=s; i<=e; i++)
#define ceach(a,x) for(const auto &a: x)
#define each(a,x) for(auto &a: x)
#define print(x) for(const auto &e: (x)) { cout<<e<<" "; } cout<<endl
#define daalo(a) each(x, a) { cin>>x; }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef unordered_map<int, int> umi;
typedef unordered_map<long long, long long> umll;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, long long> umcll;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, long long> umsll;
#ifndef ONLINE_JUDGE
#define deb(x ...) cerr << #x << ": "; _print(x); cerr << endl;
#define pt(x) cerr << "\n---------Testcase " << x << "---------\n" << endl;
// #define deb(x ...) ;
// #define pt(x) ;
#else
#define deb(x ...) ;
#define pt(x) ;
#endif
void _print(unsigned short t){ cerr << t; }
void _print(short t){ cerr << t; }
void _print(unsigned int t){ cerr << t; }
void _print(int t){ cerr << t; }
void _print(unsigned long t){ cerr << t; }
void _print(long t){ cerr << t; }
void _print(unsigned long long t){ cerr << t; }
void _print(long long t){ cerr << t; }
void _print(float t){ cerr << t; }
void _print(double t){ cerr << t; }
void _print(long double t){ cerr << t; }
void _print(unsigned char t){ cerr << t; }
void _print(char t){ cerr << t; }
void _print(string t){ cerr << t; }
template<typename A> void _print(vector<A> v);
template<typename A, typename B> void _print(pair<A, B> p);
template<typename A> void _print(set<A> s);
template<typename A, typename B> void _print(map<A, B> mp);
template<typename A> void _print(multiset<A> s);
template<typename A, typename B> void _print(multimap<A, B> mp);
template<typename A> void _print(unordered_set<A> s);
template<typename A, typename B> void _print(unordered_map<A, B> mp);
template<typename A> void _print(unordered_multiset<A> s);
template<typename A, typename B> void _print(unordered_multimap<A, B> mp);
template<typename A> void _print(stack<A> s);
template<typename A> void _print(queue<A> q);
template<typename A> void _print(priority_queue<A> pq);
template<typename A> void _print(priority_queue<A, vector<A>, greater<A>> pq);
template<typename A> void _print(vector<A> v){ if(!v.empty()){ cerr << "["; for(auto it=v.begin(); it!=(v.end()-1); it++){ _print(*it); cerr <<","; } _print(*(v.end()-1)); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A, typename B> void _print(pair<A, B> p){ cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }
template<typename A> void _print(set<A> s){ if(!s.empty()){ cerr << "{"; for(auto it=s.begin(), lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(s.rbegin())); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(map<A, B> mp){ if(!mp.empty()){ cerr << "["; for(auto it=mp.begin(), lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(mp.rbegin())); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(multiset<A> s){ if(!s.empty()){ cerr << "{"; for(auto it=s.begin(), lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(s.rbegin())); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(multimap<A, B> mp){ if(!mp.empty()){ cerr << "["; for(auto it=mp.begin(), lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(mp.rbegin())); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(unordered_set<A> s){ if(!s.empty()){ cerr << "{"; auto it = s.begin(); for(auto lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(unordered_map<A, B> mp){ if(!mp.empty()){ cerr << "["; auto it = mp.begin(); for(auto lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(unordered_multiset<A> s){ if(!s.empty()){ cerr << "{"; auto it=s.begin(); for(auto lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(unordered_multimap<A, B> mp){ if(!mp.empty()){ cerr << "["; auto it=mp.begin(); for(auto lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(stack<A> s){ if(!s.empty()){ stack<A> t; cerr << "T["; while(s.size() != 1){ _print(s.top()); cerr << ","; t.push(s.top()); s.pop(); } _print(s.top()); cerr << "]B"; t.push(s.top()); s.pop(); while(!t.empty()){ s.push(t.top()); t.pop(); } } else{ cerr << "T[]B"; } }
template<typename A> void _print(queue<A> q){ if(!q.empty()){ queue<A> t; cerr << "F["; while(q.size() != 1){ _print(q.front()); cerr << ","; t.push(q.front()); q.pop(); } _print(q.front()); cerr << "]B"; t.push(q.front()); q.pop(); while(!t.empty()){ q.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename A> void _print(priority_queue<A> pq){ if(!pq.empty()){ queue<A> t; cerr << "T["; while(pq.size() != 1){ _print(pq.top()); cerr << ","; t.push(pq.top()); pq.pop(); } _print(pq.top()); cerr << "]B"; t.push(pq.top()); pq.pop(); while(!t.empty()){ pq.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename A> void _print(priority_queue<A, vector<A>, greater<A>> pq){ if(!pq.empty()){ queue<A> t; cerr << "T["; while(pq.size() != 1){ _print(pq.top()); cerr << ","; t.push(pq.top()); pq.pop(); } _print(pq.top()); cerr << "]B"; t.push(pq.top()); pq.pop(); while(!t.empty()){ pq.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename T, typename... V> void _print(T t, V... v) {_print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_set_dec = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_multiset_dec = tree<T, null_type, greater_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = mod_mul(res , a, mod); a = mod_mul(a , a ,mod); b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
bool revsort(ll a, ll b) {return a > b;}
void swap(int &x, int &y) {int temp = x; x = y; y = temp;}
ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
template<typename T>
T gcd(T a, T b){
if(b == 0)
return a;
return(gcd<T>(b, a%b));
}
template<typename T>
T lcm(T a, T b){
return (a / gcd<T>(a, b)) * b;
}
template<typename T>
void swap_(T &a, T &b){
a = a^b;
b = b^a;
a = a^b;
}
template<typename T>
T modpow(T a, T b, T m){
if(b == 0){
return 1;
}
T c = modpow(a, b/2, m);
c = (c * c)%m;
if(b%2 == 1){
c = (c * a)%m;
}
return c;
}
template<typename T>
vector<T> makeUnique(vector<T> &vec){
vector<T> temp;
unordered_map<T, long long> mp;
for(const auto &e: vec){
if(mp[e]++ == 0){
temp.push_back(e);
}
}
return temp;
}
vector<long long> primeFactorization(long long n) {
vector<long long> factorization;
for (int d : {2, 3, 5}) {
while (n % d == 0) {
factorization.push_back(d);
n /= d;
}
}
static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6};
int i = 0;
for (long long d = 7; d * d <= n; d += increments[i++]) {
while (n % d == 0) {
factorization.push_back(d);
n /= d;
}
if (i == 8)
i = 0;
}
if (n > 1)
factorization.push_back(n);
return factorization;
}
vector<long long> divisors(long long n) {
vector<long long> divisors;
for(long long i = 1; i * i <= n; i++){
if(n % i == 0){
divisors.push_back(i);
if(n/i != i){
divisors.push_back(n/i);
}
}
}
return divisors;
}
vector<bool> seive(long long n){
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (long long i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (long long j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
// ------------ Segment Tree --------------
void segBuild(ll ind, ll l, ll r, vll &arr, vll &segtree){
if(l == r){
segtree[ind] = arr[l];
return;
}
ll m = (l+r)/2;
segBuild(2*ind, l, m, arr, segtree);
segBuild(2*ind+1, m+1, r, arr, segtree);
segtree[ind] = segtree[ind*2]+segtree[ind*2+1];
}
ll segSum(ll ind, ll tl, ll tr, ll l, ll r, vll &segtree){
if(l > r || tr < l || tl > r){
return 0;
}
if(tl >= l && tr <= r){
return segtree[ind];
}
ll m = (tl+tr)/2;
ll left = segSum(2*ind, tl, m, l, r, segtree);
ll right = segSum(2*ind+1, m+1, tr, l, r, segtree);
return left+right;
}
void segUpdate(ll ind, ll l, ll r, ll ind_val, ll val, vll &segtree){
if(l == r){
segtree[ind] = val;
return;
}
ll m = (l+r)/2;
if(ind_val <= m){
segUpdate(2*ind, l, m, ind_val, val, segtree);
}
else{
segUpdate(2*ind+1, m+1, r, ind_val, val, segtree);
}
segtree[ind] = segtree[ind*2]+segtree[ind*2+1];
}
// -----------------------------------
/* ----------STRING AND INTEGER CONVERSIONS---------- */
// 1) number to string -> to_string(num)
// 2) string to int -> stoi(str)
// 3) string to long long -> stoll(str)
// 4) string to decimal -> stod(str)
// 5) string to long decimal -> stold(str)
/* ----------Decimal Precision---------- */
// cout<<fixed<<setprecision(n) -> to fix precision to n decimal places.
// cout<<setprecision(n) -> without fixing
/* ----------Policy Bases Data Structures---------- */
// pbds<ll> s; (almost same as set)
// s.find_by_order(i) 0<=i<n returns iterator to ith element (0 if i>=n)
// s.order_of_key(e) returns elements strictly less than the given element e (need not be present)
/* ------------------Binary Search------------------ */
// 1) Lower Bound -> returns iterator to the first element greater than or equal to the given element or returns end() if no such element exists
// 2) Upper Bound -> returns iterator to the first element greater than the given element or returns end() if no such element exists
/* --------------Builtin Bit Functions-------------- */
// 1) __builtin_clz(x) -> returns the number of zeros at the beginning in the bit representaton of x.
// 2) __builtin_ctz(x) -> returns the number of zeros at the end in the bit representaton of x.
// 3) __builtin_popcount(x) -> returns the number of ones in the bit representaton of x.
// 4) __builtin_parity(x) -> returns the parity of the number of ones in the bit representaton of x.
void solve(){
ll a, b, c, k; cin>>a>>b>>c>>k;
ll ans = 0;
ll mn;
mn = min(a, k);
k -= mn;
ans += mn;
mn = min(b, k);
k -= mn;
mn = min(c, k);
k -= mn;
ans -= mn;
cout<<ans;
}
int main(){
// cfh - ctrl+alt+b
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// freopen("error.txt", "w", stderr);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t=1;
// cin >> t;
for(ll i=1; i<=t; i++){
pt(i);
solve();
}
return 0;
}, In this Programming Language: C++, 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: Yogi and Dharam are playing a game in which one has to solve the problem in order to get out of the room. Dharam goes first and Yogi gave him a problem. Since Dharam is having problem solving it, your task is to solve the problem for him.
Problem:-
Given a range from A to B your task is to count the numbers in which the difference between adjacent digits is not more than one.The first line of input contains a single integer T. The next T lines contain two space- separated integers each containing values of A and B.
Constraints:-
1 <= T <= 100
1 <= A, B <= 1000000000000For each test case print the count of numbers in the range [A, B] such that the difference between adjacent digits is not more than one.Sample Input:-
2
10 30
1 10
Sample Output:-
6
10
Explanation:-
10, 11, 12, 21, 22, 23
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static int count =0;
public static void solve(long a ,long b,long number){
if(number>b){
return;
}
if(number>=a && number <=b){
count++;
}
long last_digit =number % 10;
number *=10;
if(last_digit !=0){
solve(a,b,number+(last_digit-1));
}
if(last_digit !=9){
solve(a,b,number+(last_digit+1));
}
solve(a,b,number+(last_digit));
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
long a = sc.nextLong();
long b =sc.nextLong();
count =0;
for(long i=1l ; i<=9l ;i++){
solve(a,b,i);
}
System.out.println(count);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Yogi and Dharam are playing a game in which one has to solve the problem in order to get out of the room. Dharam goes first and Yogi gave him a problem. Since Dharam is having problem solving it, your task is to solve the problem for him.
Problem:-
Given a range from A to B your task is to count the numbers in which the difference between adjacent digits is not more than one.The first line of input contains a single integer T. The next T lines contain two space- separated integers each containing values of A and B.
Constraints:-
1 <= T <= 100
1 <= A, B <= 1000000000000For each test case print the count of numbers in the range [A, B] such that the difference between adjacent digits is not more than one.Sample Input:-
2
10 30
1 10
Sample Output:-
6
10
Explanation:-
10, 11, 12, 21, 22, 23
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int cnt=0;
inline void solve(int a, int b, int p, int c, int i){
if(p>b){return;}
if(p>=a){cnt++;}
int x = p%10;
p*=10;
if(i==c){return;}
solve(a,b,p+x,c,i+1);
if(x!=9){
solve(a,b,p+x+1,c,i+1);
}
if(x!=0){
solve(a,b,p+x-1,c,i+1);
}
}
signed main(){
fast();
int t;
cin>>t;
while(t--){
int a,b;
cnt=0;
cin>>a>>b;
if(b<a){out(-1);continue;}
int ans=b;
int c=0;
while(ans){
c++;
ans/=10;
}
for(int i=1;i<=9;i++){
solve(a,b,i,c,1);
}
out(cnt);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alita has two a sequence of numbers x1, x2,…..xn. She has to go fight a battle with these sequence of numbers. But these numbers are not correct for the battle. She has to convert these numbers to another sequence of numbers y1, y2,…….yn. Alita is busy preparing for the battle so she needs your help.
There is only two type of moves you can perform on any xi (where 1<=i<=n) only.
You have to make initial sequence (x1, x2…..xn) equal to the correct sequence (y1, y2,...yn) in minimum number of moves.
Moreover you can not rearrange anything.
There are two types of moves :-
1. Multiply xi by 2.
2. Decrease xi by 1.
Calculate the minimum number of moves required.
Help Alita!!The first line of input contains the size of array N.
The second line contains the array x1, x2,...xn.
The second line contains the array y1, y2,...yn.
Constraints:-
1 <= N <= 100000
1 <= xi, yi <10^9Print the minimum number of operation required.Sample Input:
4
3 12 2 4
15 9 17 5
Sample Output:
19
Explanation:
For 3 to 15:- 3->2->4->8->16->15 = 5
For 12 to 9:- 12->11->10->9 = 3
For 2 to 17:- 2->4->3->6->5->10->9->18->17 = 8
For 4 to 5:- 4->3->6->5 = 3
total operation = 5+3+8+3 = 19
Sample Input:
1
6077
27091
Sample Output:
2695, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
long a[] = new long[n];
long b[] = new long[n];
String strg[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
a[i] = Long.parseLong(strg[i]);
String strgg[] = br.readLine().trim().split(" ");
for(int i=0;i<n;i++)
b[i] = Long.parseLong(strgg[i]);
long count = 0;
for(int i=0;i<n;i++)
{
count = helpAnita(a[i], b[i]) + count;
}
System.out.print(count);
}
public static long helpAnita(long a, long b)
{
if(a==b)
return 0;
if(a>b)
return a-b;
else
{
if(b%2 == 0)
return 1 + helpAnita(a, b/2);
else
return 1 + helpAnita(a, b+1);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alita has two a sequence of numbers x1, x2,…..xn. She has to go fight a battle with these sequence of numbers. But these numbers are not correct for the battle. She has to convert these numbers to another sequence of numbers y1, y2,…….yn. Alita is busy preparing for the battle so she needs your help.
There is only two type of moves you can perform on any xi (where 1<=i<=n) only.
You have to make initial sequence (x1, x2…..xn) equal to the correct sequence (y1, y2,...yn) in minimum number of moves.
Moreover you can not rearrange anything.
There are two types of moves :-
1. Multiply xi by 2.
2. Decrease xi by 1.
Calculate the minimum number of moves required.
Help Alita!!The first line of input contains the size of array N.
The second line contains the array x1, x2,...xn.
The second line contains the array y1, y2,...yn.
Constraints:-
1 <= N <= 100000
1 <= xi, yi <10^9Print the minimum number of operation required.Sample Input:
4
3 12 2 4
15 9 17 5
Sample Output:
19
Explanation:
For 3 to 15:- 3->2->4->8->16->15 = 5
For 12 to 9:- 12->11->10->9 = 3
For 2 to 17:- 2->4->3->6->5->10->9->18->17 = 8
For 4 to 5:- 4->3->6->5 = 3
total operation = 5+3+8+3 = 19
Sample Input:
1
6077
27091
Sample Output:
2695, I have written this Solution Code: def minoperation1(n1,n2):
count = 0
while(n2 > n1):
if n2%2 == 0 and n2>n1:
count += 1
n2 = n2//2
else:
count +=1
n2 = n2+1
return count + n1 - n2
n = int(input())
xi = list(map(int,input().split()))
yi = list(map(int,input().split()))
f = 0
mini = 0
for i in range(n):
mini += minoperation1(xi[i],yi[i])
print(mini), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alita has two a sequence of numbers x1, x2,…..xn. She has to go fight a battle with these sequence of numbers. But these numbers are not correct for the battle. She has to convert these numbers to another sequence of numbers y1, y2,…….yn. Alita is busy preparing for the battle so she needs your help.
There is only two type of moves you can perform on any xi (where 1<=i<=n) only.
You have to make initial sequence (x1, x2…..xn) equal to the correct sequence (y1, y2,...yn) in minimum number of moves.
Moreover you can not rearrange anything.
There are two types of moves :-
1. Multiply xi by 2.
2. Decrease xi by 1.
Calculate the minimum number of moves required.
Help Alita!!The first line of input contains the size of array N.
The second line contains the array x1, x2,...xn.
The second line contains the array y1, y2,...yn.
Constraints:-
1 <= N <= 100000
1 <= xi, yi <10^9Print the minimum number of operation required.Sample Input:
4
3 12 2 4
15 9 17 5
Sample Output:
19
Explanation:
For 3 to 15:- 3->2->4->8->16->15 = 5
For 12 to 9:- 12->11->10->9 = 3
For 2 to 17:- 2->4->3->6->5->10->9->18->17 = 8
For 4 to 5:- 4->3->6->5 = 3
total operation = 5+3+8+3 = 19
Sample Input:
1
6077
27091
Sample Output:
2695, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long long a[n],b[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long ans=0;
for(int i=0;i<n;i++){
cin>>b[i];}
for(int i=0;i<n;i++){
long long x=a[i],y=b[i];
while(y>x){
if(y&1){y++;ans++;}
else{y=y/2;ans++;}
}
ans+=x-y;
}
cout<<ans<<endl;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
Sample output
-1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(read.readLine());
int count=0;
int cnt[]=new int[n+2];
for(int i=0;i<n;i++){
StringTokenizer st=new StringTokenizer(read.readLine()," ");
int ai=Integer.parseInt(st.nextToken());
int bi=Integer.parseInt(st.nextToken());
for(int j=ai;j<=bi;j++)
{
cnt[j]++;
}
}
int ans=-1;
for(int i=0;i<=n;i++){
if(cnt[i]==i)
{
ans=i;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
Sample output
-1, I have written this Solution Code: n=int(input())
a=[0]*(n+1)
m=0
for i in range(n):
b=[int(k) for k in input().split()]
for j in range(b[0],b[1]+1):
a[j]+=1;
for i in range(n,0,-1):
if a[i]==i:
print(i)
exit()
print(-1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are n people in a circle, numbered from 1 to n, each of whom always tells the truth or always lies.
Each person i makes a claim of the form: “the number of truth-tellers in this circle is between a<sub>i</sub> and b<sub>i</sub>, inclusive.”
Compute the maximum number of people who could be telling the truth.The first line contains a single integer n (1 ≤ n ≤ 10^3).
Each of the next n lines contains two space-separated integers a<sub>i</sub> and b<sub>i</sub>
(0 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ n).Print, on a single line, the maximum number of people who could be telling the truth. If the given
set of statements are inconsistent, print -1 instead.Sample input
3
1 1
2 3
2 2
Sample output
2
Sample input
8
0 1
1 7
4 8
3 7
1 2
4 5
3 7
1 8
Sample output
-1, I have written this Solution Code: #include<stdio.h>
#define maxn 1100
struct node{
int l,r;
}a[maxn];
int main(){
int n,i,cnt,j,ans=-1;
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d%d",&a[i].l,&a[i].r);
for (i=n;i>=0;i--) {
cnt=0;
for (j=1;j<=n;j++) if(a[j].l<=i&&i<=a[j].r) cnt++;
if (cnt==i) {ans=i;break;}
}
printf("%d\n",ans);
return 0;
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N strings and Q queries, for each query you will be given a string, your task is to print all the anagrams of the string from the given N strings. If no anagrams exist then print -1.
Note:- Given string may contain duplicated strings you should print it as many times as it occurs.First line of input contains a single integer N, second line of input contains N space separated strings. Third line of input contains a single integer Q, next Q line contains a single string each.
Constraints:-
1 < = N < = 1000
1 < = |String| < = 10
1 < = Q < = 100000
Note:-String will only contain lower case english lettersFor each query in a new line print the anagrams in lexicographical order separated by spaces.Sample Input:-
6
cat tea pet tac act eat
4
cat
tca
eee
tea
Sample Output:-
act cat tac
act cat tac
-1
eat tea, 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));
HashMap<String, List<String> > hs=new HashMap<>();
int n=Integer.parseInt(br.readLine());
String st[]=br.readLine().split(" ");
for(int i=0;i<n;i++){
String temp=st[i];
char ch[]=temp.toCharArray();
Arrays.sort(ch);
String temp1=new String(ch);
if(hs.containsKey(temp1)){
hs.get(temp1).add(temp);
}
else{
List<String> ValueList = new ArrayList<>();
ValueList.add(temp);
hs.put(temp1,ValueList);
}
}
int q=Integer.parseInt(br.readLine());
for(int i=0;i<q;i++){
String check=br.readLine();
char temp[]=check.toCharArray();
Arrays.sort(temp);
check=new String(temp);
if(hs.containsKey(check)){
List<String> li=hs.get(check);
Collections.sort(li);
int x=li.size();
for(int j=0;j<x;j++){
System.out.print(li.get(j)+" ");
}
System.out.println();
}
else{
System.out.println(-1);
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given N strings and Q queries, for each query you will be given a string, your task is to print all the anagrams of the string from the given N strings. If no anagrams exist then print -1.
Note:- Given string may contain duplicated strings you should print it as many times as it occurs.First line of input contains a single integer N, second line of input contains N space separated strings. Third line of input contains a single integer Q, next Q line contains a single string each.
Constraints:-
1 < = N < = 1000
1 < = |String| < = 10
1 < = Q < = 100000
Note:-String will only contain lower case english lettersFor each query in a new line print the anagrams in lexicographical order separated by spaces.Sample Input:-
6
cat tea pet tac act eat
4
cat
tca
eee
tea
Sample Output:-
act cat tac
act cat tac
-1
eat tea, 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 200001
#define MOD 1000000007
#define read(type) readInt<type>()
#define int long long
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
unordered_map<string,vector<string>> m;
signed main(){
int n;
cin>>n;
string s,p;
while(n--){
cin>>s;
p=s;
sort(s.begin(),s.end());
m[s].EB(p);
}
for(auto it =m.begin();it!=m.end();it++){
sort(it->second.begin(),it->second.end());
}
int q;
cin>>q;
while(q--){
cin>>s;
sort(s.begin(),s.end());
if(m.find(s)==m.end()){out(-1);continue;}
else{
vector<string> v = m[s];
for(int i=0;i<v.size();i++){
out1(v[i]);
}
END;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Create a basic table namely TEST_TABLE with 1 field TEST_FIELD as int.
( USE ONLY UPPER CASE LETTERS )
<schema>[{'name': 'TEST_TABLE', 'columns': [{'name': 'TEST_FIELD', 'type': 'INT'}]}]</schema>nannannan, I have written this Solution Code: CREATE TABLE TEST_TABLE (
TEST_FIELD INT
);, In this Programming Language: SQL, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, your task is to print all subarrays of the given array in the order given as:-
First, print all the subarrays starting from the first element of the array in increasing order of their size. Then go for the second element and print all of its subarrays in increasing order and so on.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>PrintSubarrays</b> that takes the integer N and the array Arr as parameters.
Constraints:-
1 <= N <= 100
1 <= Arr[i] <= 100000Print all the subarray in a new line in the order mentioned above.Sample input:-
3
1 2 3
Sample Output:-
1
1 2
1 2 3
2
2 3
3
Sample Input:-
2
2 4
Sample Output:-
2
2 4
4, I have written this Solution Code: num = int(input())
arr = list(map(int,input().split()))
for i in range(0,num):
for j in range(i,num):
for k in range(i,j+1):
print (arr[k],end=" ")
print ("\n",end=""), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, your task is to print all subarrays of the given array in the order given as:-
First, print all the subarrays starting from the first element of the array in increasing order of their size. Then go for the second element and print all of its subarrays in increasing order and so on.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>PrintSubarrays</b> that takes the integer N and the array Arr as parameters.
Constraints:-
1 <= N <= 100
1 <= Arr[i] <= 100000Print all the subarray in a new line in the order mentioned above.Sample input:-
3
1 2 3
Sample Output:-
1
1 2
1 2 3
2
2 3
3
Sample Input:-
2
2 4
Sample Output:-
2
2 4
4, I have written this Solution Code: static void PrintSubarrays(int Arr[], int N){
solve(Arr,N,0,0);
}
static void solve(int a[],int n, int cnt, int e){
if(e==n){cnt++;e=cnt;}
if(cnt==n){return;}
for(int i=cnt;i<=e;i++){
System.out.print(a[i]+" ");
}
System.out.println();
solve(a,n,cnt,e+1);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int len = Integer.parseInt(br.readLine());
char[] str = new char[len];
for(int i = 0; i < len; i++){
if(i%2 == 0){
str[i] = 'a';
} else{
str[i] = 'b';
}
}
System.out.println(str);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s(n,'a');
for(int i=1;i<n;i+=2)
s[i]='b';
cout<<s;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: a="ab"
inp = int(input())
print(a*(inp//2)+a[0:inp%2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.First line of input contains a single integer N.
Second line of input contains N space separated integers, denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9Print a single integer which is the minimum number of elements that must be deleted.Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
3
Explanation: we will delete 1 of each 1, 2 and 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int N = Integer.parseInt(br.readLine());
String[] arr = br.readLine().split(" ");
int n = arr.length;
int[] array1 = new int[n];
for(int i = 0 ;i<n; i++){
array1[i] = Integer.parseInt(arr[i]);
}
int res = 0;
for (int i = 0; i < n; i++)
{
int j = 0;
for (j = 0; j < i; j++)
if (array1[i] == array1[j])
break;
if (i == j)
res++;
}
System.out.print(n-res);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of N elements. Find the minimum number of elements that you need to delete from this array such that all the elements in the array become distinct.First line of input contains a single integer N.
Second line of input contains N space separated integers, denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 10^9Print a single integer which is the minimum number of elements that must be deleted.Sample Input 1
6
1 1 2 2 3 3
Sample Output 1
3
Explanation: we will delete 1 of each 1, 2 and 3., I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
unordered_map<int,int> m;
for(int i=1;i<=n;++i){
int d;
cin>>d;
m[d]++;
}
int ans=0;
for(auto r:m)
ans+=r.second-1;
cout<<ans;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.