Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Bruce Banner is working on a code involving 3 numbers with their sum as a mystery number. Just then the emergency alarm is sounded and Bruce hulks his way out to save the city of New York yet again. The responsibility of cracking the code is then handed over to you since it is crucial to ensure Hulk’s safety. You find the mystery number as X. Now give Hulk a number of all the triplets and help Hulk as he fights the evil Chitauri.First line of input contains a single integer N (length of the array)
second line contain the array elements
third line contain a single integer K(sum)
Constraint:-
1<=N<=1000
1<=elements<=100000
1<=K<=100000Output a single line containing the number of required tripletsSample Input:
6
1 2 3 4 5 6
8
Sample Output:-
2
Explanation:-
(1,2,5) , (1,3,4) are the required triplets
, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
int x = sc.nextInt();
int cnt = 0, sum = 0;
for(int i=0;i<n-2;i++)
{
if(arr[i]>x)
{break;}
int tar=x-arr[i];
int j=i+1,k=n-1;
while(j<k){
if((arr[j]+arr[k])<tar){j++;}
else if((arr[j]+arr[k])>tar){k--;}
else{cnt++;j++;}
}
}
System.out.println(cnt);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Bruce Banner is working on a code involving 3 numbers with their sum as a mystery number. Just then the emergency alarm is sounded and Bruce hulks his way out to save the city of New York yet again. The responsibility of cracking the code is then handed over to you since it is crucial to ensure Hulk’s safety. You find the mystery number as X. Now give Hulk a number of all the triplets and help Hulk as he fights the evil Chitauri.First line of input contains a single integer N (length of the array)
second line contain the array elements
third line contain a single integer K(sum)
Constraint:-
1<=N<=1000
1<=elements<=100000
1<=K<=100000Output a single line containing the number of required tripletsSample Input:
6
1 2 3 4 5 6
8
Sample Output:-
2
Explanation:-
(1,2,5) , (1,3,4) are the required triplets
, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 10000001
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int x;cin>>x;
long cnt=0;
long sum=0;
for(int i=0;i<n-2;i++){
if(a[i]>x){break;}
int tar=x-a[i];
int j=i+1,k=n-1;
while(j<k){
if((a[j]+a[k])<tar){j++;}
else if((a[j]+a[k])>tar){k--;}
else{cnt++;j++;}
}
}
cout<<cnt<<endl;
}
, 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: 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: Create a new class for a bank account (BankAccount)
Create fields for the balance(balance), customer name(name).
Create constructor with two parameter as balance and name
Create two additional methods
1. To allow the customer to deposit funds named depositFund() with one parameter as fund to be added (this should increment the balance field).
2. To allow the customer to withdraw funds named withdrawFund() with one parameter as fund to be withdrawn from account and should return boolean as if withdraw went successful else false. This should deduct from the balance field, but not allow
the withdrawal to complete if their are insufficient funds.
Note: Each methods and variable should be publicYou don't have to take any input, You only have to write class <b>BankAccount</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
class BankAccount{
// if your code works fine for the testert
}
Sample Output:
Correct, I have written this Solution Code: class BankAccount{
public int balance;
public String name;
BankAccount(int _balance,String _name){
balance=_balance;
name =_name;
}
public void depositFund(int fund){
balance += fund;
}
public boolean withdrawFund(int fund){
if(fund <= balance){
balance -= fund;
return true;
}
return false;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Create a new class for a bank account (BankAccount)
Create fields for the balance(balance), customer name(name).
Create constructor with two parameter as balance and name
Create two additional methods
1. To allow the customer to deposit funds named depositFund() with one parameter as fund to be added (this should increment the balance field).
2. To allow the customer to withdraw funds named withdrawFund() with one parameter as fund to be withdrawn from account and should return boolean as if withdraw went successful else false. This should deduct from the balance field, but not allow
the withdrawal to complete if their are insufficient funds.
Note: Each methods and variable should be publicYou don't have to take any input, You only have to write class <b>BankAccount</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
class BankAccount{
// if your code works fine for the testert
}
Sample Output:
Correct, I have written this Solution Code: class Bank_Account:
def __init__(self,b,n):
self.balance=b
self.name=n
def depositFund(self,b):
self.balance += b
def withdrawFund(self,f):
if self.balance>=f:
self.balance-=f
return True
else:
return False
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: def OccurenceOfX(N,X):
cnt=0
for i in range(1, N+1):
if(X%i==0 and X/i<=N):
cnt=cnt+1
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
int main()
{
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: public static int OccurenceOfX(int N,int X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara selects two different numbers from the range 1 to 50 and noted their sum as N. However later she Forgets which numbers she selected and now wants to know the total number of possible combinations she can have.<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>forgottenNumbers()</b> that takes integer N argument.
Constraints:-
3 <= N <= 99Return the total number of choices Sara has.Sample Input:-
8
Sample Output:-
3
Explanation:-
(1, 7), (2, 6), (3, 5)
Sample Input:-
3
Sample Output:-
1, I have written this Solution Code: int forgottenNumbers(int N){
int ans=0;
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(i!=j && i+j==N){
ans++;
}
}
}
return ans/2;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara selects two different numbers from the range 1 to 50 and noted their sum as N. However later she Forgets which numbers she selected and now wants to know the total number of possible combinations she can have.<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>forgottenNumbers()</b> that takes integer N argument.
Constraints:-
3 <= N <= 99Return the total number of choices Sara has.Sample Input:-
8
Sample Output:-
3
Explanation:-
(1, 7), (2, 6), (3, 5)
Sample Input:-
3
Sample Output:-
1, I have written this Solution Code: static int forgottenNumbers(int N){
int ans=0;
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(i!=j && i+j==N){
ans++;
}
}
}
return ans/2;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara selects two different numbers from the range 1 to 50 and noted their sum as N. However later she Forgets which numbers she selected and now wants to know the total number of possible combinations she can have.<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>forgottenNumbers()</b> that takes integer N argument.
Constraints:-
3 <= N <= 99Return the total number of choices Sara has.Sample Input:-
8
Sample Output:-
3
Explanation:-
(1, 7), (2, 6), (3, 5)
Sample Input:-
3
Sample Output:-
1, I have written this Solution Code: int forgottenNumbers(int N){
int ans=0;
for(int i=1;i<=50;i++){
for(int j=1;j<=50;j++){
if(i!=j && i+j==N){
ans++;
}
}
}
return ans/2;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara selects two different numbers from the range 1 to 50 and noted their sum as N. However later she Forgets which numbers she selected and now wants to know the total number of possible combinations she can have.<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>forgottenNumbers()</b> that takes integer N argument.
Constraints:-
3 <= N <= 99Return the total number of choices Sara has.Sample Input:-
8
Sample Output:-
3
Explanation:-
(1, 7), (2, 6), (3, 5)
Sample Input:-
3
Sample Output:-
1, I have written this Solution Code: def forgottenNumbers(N):
ans = 0
for i in range (1,51):
for j in range (1,51):
if i != j and i+j==N:
ans=ans+1
return ans//2
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: n = int(input())
all_no = input().split(' ')
i = 0
joined_str = ''
while(i < n-1):
if(i == 0):
joined_str = str(int(all_no[i]) + int(all_no[i+1]))
else:
joined_str = joined_str + ' ' + str(int(all_no[i]) + int(all_no[i+1]))
i = i + 2
print(joined_str), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int t;
for(int i=0;i<n;i+=2){
System.out.print(a[i]+a[i+1]+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A of size N (N is always even), you need to find exactly (N/2) numbers where each number represents the pair-wise sum of consecutive elements of the array A.
In simple terms print (A[1]+A[2]), (A[3]+A[4]), ..., (A[N-1]+A[N]).The first line of the input contains the number of test cases T.
For each test case, the first line of the input contains an integer N(even number) denoting the number of elements in the array. The next line contains N (white-space separated) integers.
Constraints
1 <= N <= 100
1 <= A[I] <= 1000000000For each test case, output N/2 elements representing the pairwise sum of adjacent elements in the array.Input:-1
4
1 2 6 4
output-1
3 10
input-2
10
1 2 3 4 5 6 0 7 8 9
output-2
3 7 11 7 17
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
Step 1: [1 2 6 4]
Step 2: (1 2) and (6 4)
Step 3: 3 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i+=2){
cout<<a[i]+a[i+1]<<" ";
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)) ;
int n = Integer.parseInt(read.readLine()) ;
String[] str = read.readLine().trim().split(" ") ;
int[] arr = new int[n] ;
for(int i=0; i<n; i++) {
arr[i] = Integer.parseInt(str[i]) ;
}
long ans = 0L;
if((n & 1) == 1) {
for(int i=0; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
else {
Arrays.sort(arr);
for(int i=1; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: n=int(input())
arr=map(int,input().split())
result=[]
for item in arr:
if item%2==0:
result.append(item-1)
else:
result.append(item)
if sum(result)%2==0:
result.sort()
result.pop(0)
print(sum(result))
else:
print(sum(result)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
int n; cin >> n ;
long long int ans = 0;
// int arr[n];
int odd = 0,minn=INT_MAX;
for(int i=0;i<n;i++){
ll temp; cin >> temp;
// cin >> arr[i];
ans += (temp&1) ? temp : temp-1;
if(minn>temp)
minn = temp;
}
if(n&1)
cout << ans ;
else{
if(minn&1)
cout <<ans-minn;
else
cout << ans - minn + 1;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, 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 i=str.length()-1;
if(i==0){
int number=Integer.parseInt(str);
System.out.println(number);
}else{
while(str.charAt(i)=='0'){
i--;
}
for(int j=i;j>=0;j--){
System.out.print(str.charAt(j));
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, I have written this Solution Code: n=int(input())
def reverse(n):
return int(str(n)[::-1])
print(reverse(n)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number, you task is to reverse its digits. If the reversed number contains 0s in the beginning, you must remove them as well.First line consists of a single integer denoting n.
Constraint:-
1<=size of n<=100Output is a single line containing reversed number n.Sample Input
123445
Sample Output
544321
Sample Input
16724368
Sample Output
86342761, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
int I;
for( I=0;I<s.length();I++){
if(s[I]!='0'){break;}
}
if(I==s.length()){cout<<0;return 0;}
for(int j=I;j<s.length();j++){
cout<<s[j];}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <em>Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970</em>
Implement the function <code>msSinceEpoch</code>, which returns milliseconds since the Unix epoch. (Use JS built-in functions)The function takes no argumentThe function returns a numberconsole. log(msSinceEpoch()) // prints 1642595040109, I have written this Solution Code:
function msSinceEpoch() {
// write code here
// return the output , do not use console.log here
return Date.now()
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a rule in ludo that a token can only be unlocked when either a 1 or 6 shown in the die. Given the die number N, Your task is to check whether the token can be unlocked or not.<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>ludo()</b> that takes integers N as argument.
Constraints:-
1 <= N <= 6Return 1 if the token can be unlocked else return 0.Sample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
0, I have written this Solution Code: int ludo(int N){
return (N==1||N==6);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a rule in ludo that a token can only be unlocked when either a 1 or 6 shown in the die. Given the die number N, Your task is to check whether the token can be unlocked or not.<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>ludo()</b> that takes integers N as argument.
Constraints:-
1 <= N <= 6Return 1 if the token can be unlocked else return 0.Sample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
0, I have written this Solution Code: def ludo(N):
if N==1 or N==6:
return 1
return 0
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a rule in ludo that a token can only be unlocked when either a 1 or 6 shown in the die. Given the die number N, Your task is to check whether the token can be unlocked or not.<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>ludo()</b> that takes integers N as argument.
Constraints:-
1 <= N <= 6Return 1 if the token can be unlocked else return 0.Sample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
0, I have written this Solution Code:
int ludo(int N){
return (N==1||N==6);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a rule in ludo that a token can only be unlocked when either a 1 or 6 shown in the die. Given the die number N, Your task is to check whether the token can be unlocked or not.<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>ludo()</b> that takes integers N as argument.
Constraints:-
1 <= N <= 6Return 1 if the token can be unlocked else return 0.Sample Input:-
1
Sample Output:-
1
Sample Input:-
2
Sample Output:-
0, I have written this Solution Code: static int ludo(int N){
if(N==1 || N==6){return 1;}
return 0;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sequence of n numbers such that the difference between the consecutive terms is constant, find the missing term in logarithmic time. Assume that the first and last elements are always part of the input sequence and the missing number lies between index 1 to n-1.
Solve the problem using divide and conquer approach.first line contain a single integer N.
second line contain N space separated integer A[i].print missing number in given array.
Constraint :
1<=N<=10^5
1<=A[i]<=10^9Sample Input 1:
5
5 7 9 11 15
Sample Output 1:
13
Sample Input 2:
5
1 4 7 13 16
Sample Output 2:
10, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
int N=Integer.parseInt(br.readLine());
int arr[]=new int[N];
String [] line = br.readLine().split("\\s+");
for(int i=0;i<N;i++) {
arr[i] = Integer.parseInt(line[i]);
}
System.out.println(findMissingValue(arr,N));
}
static int findMissingValue(int arr[], int n)
{
int left=0,right=n-1;
int dif=(arr[right]-arr[left])/n;
while(left<=right)
{
int mid=right-(right-left)/2;
if(mid+1<n && arr[mid+1]-arr[mid]!=dif)
{
return arr[mid+1]-dif;
}
if(mid-1>=0 && arr[mid]-arr[mid-1] !=dif)
{
return arr[mid-1]+dif;
}
if(arr[mid]-arr[0]!=(mid-0)*dif)
{
right=mid-1;
}
else
left=mid+1;
}
return -1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sequence of n numbers such that the difference between the consecutive terms is constant, find the missing term in logarithmic time. Assume that the first and last elements are always part of the input sequence and the missing number lies between index 1 to n-1.
Solve the problem using divide and conquer approach.first line contain a single integer N.
second line contain N space separated integer A[i].print missing number in given array.
Constraint :
1<=N<=10^5
1<=A[i]<=10^9Sample Input 1:
5
5 7 9 11 15
Sample Output 1:
13
Sample Input 2:
5
1 4 7 13 16
Sample Output 2:
10, I have written this Solution Code: def find_missing(arr, n):
first = arr[0]
last = arr[-1]
if (first + last) % 2:
s = (n + 1) / 2
s *= (first + last)
else:
s = (first + last) / 2
s *= (n + 1)
print(int(s - sum(arr)))
N=int(input())
arr = list(map(int,input().split()))
n = len(arr)
missing = find_missing(arr, n)
missing, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sequence of n numbers such that the difference between the consecutive terms is constant, find the missing term in logarithmic time. Assume that the first and last elements are always part of the input sequence and the missing number lies between index 1 to n-1.
Solve the problem using divide and conquer approach.first line contain a single integer N.
second line contain N space separated integer A[i].print missing number in given array.
Constraint :
1<=N<=10^5
1<=A[i]<=10^9Sample Input 1:
5
5 7 9 11 15
Sample Output 1:
13
Sample Input 2:
5
1 4 7 13 16
Sample Output 2:
10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
// Function to find a missing term in a sequence
int findMissingTerm(int nums[], int n)
{
// search space is nums[low…high]
int low = 0, high = n - 1;
// calculate the common difference between successive elements
int d = (nums[n - 1] - nums[0]) / n;
// loop till the search space is exhausted
while (low <= high)
{
// find the middle index
int mid = high - (high - low) / 2;
// check the difference of middle element with its right neighbor
if (mid + 1 < n && nums[mid + 1] - nums[mid] != d) {
return nums[mid + 1] - d;
}
// check the difference of middle element with its left neighbor
if (mid - 1 >= 0 && nums[mid] - nums[mid - 1] != d) {
return nums[mid - 1] + d;
}
// if the missing element lies on the left subarray, reduce
// our search space to the left subarray nums[low…mid-1]
if (nums[mid] - nums[0] != (mid - 0) * d) {
high = mid - 1;
}
// if the missing element lies on the right subarray, reduce
// our search space to the right subarray nums[mid+1…high]
else {
low = mid + 1;
}
}
}
int main(void)
{
int n;
cin>>n;
int nums[n];
for(int i=0;i<n;i++)cin>>nums[i];
printf("%d", findMissingTerm(nums, 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 of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
String s=reader.readLine();
long c=0;
for(int i=1;i<s.length();i=i+2){
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u')
c++;
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: def check(c):
if c=='a' or c=='e' or c=='i' or c=='o' or c=='u':
return True
else:
return False
n=int(input())
s=input()
j=0
cnt=0
ans=0
for i in range(0,n):
j=i+1
if j%2==0:
if check(s[i]):
#=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
cnt+=1
print(cnt)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a string of length N, count the number of vowels present at even places.First line of input contains a single integer N. Second line contains a string of size N.
Constraints:-
1 <= N <= 100000
Note:- String will contain only lowercase english letterPrint the number of vowels present at the even placesSample Input:-
12
newtonschool
Sample Output:-
2
Sample Input:-
5
basid
Sample Output:-
2, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
bool check(char c){
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){return true;}
else{
return false;
}
}
int main() {
int n; cin>>n;
string s;
cin>>s;
int j=0;
int cnt=0;
int ans=0;
for(int i=0;i<n;i++){
if(i&1){
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
cnt++;
}}
}
cout<<cnt;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Actually, a pair of chopsticks need not have two sticks that are the same length. You can eat with a pair of sticks as long as the length difference is no greater than D. Samar is appointed as a head chef of a restaurant. He is provided with N sticks, the i<sup>th</sup>stick of which is L[i] units long. More than one set of chopsticks cannot share the same stick. Assist him in matching the sticks to create as many functional pairs of chopsticks as possible.The first line contains two space-separated integers N and D. The next N lines contain one integer each, the i<sup>th</sup>line giving the value of L[i].
<b>Constraints</b>
1 ≤ N ≤ 10,000 (10<sup>5</sup>)
0 ≤ D ≤ 1,000,000,000 (10<sup>9</sup>)
1 ≤ L[i] ≤ 1,000,000,000 (10<sup>9</sup>) for all integers i from 1 to NOutput a single line containing the maximum number of pairs of chopsticks Samar can form.Sample Input :
5 2
1
3
3
9
4
Sample Output :
2
Explanation :
The 5 sticks have lengths 1, 3, 3, 9, and 4 respectively. The maximum allowed difference in the lengths of two sticks forming a pair is at most 2. It is clear that the 4th stick (length 9) cannot be used with any other stick. The remaining 4 sticks can be paired as (1st and 3rd) and (2nd and 5th) to form 2 pairs of usable chopsticks., I have written this Solution Code: import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int d=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int count=0;
Arrays.sort(arr);
for(int i=0;i<n-1;i++){
if(arr[i+1]-arr[i]<=d){
count++;
i++;
}
}
System.out.println(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Actually, a pair of chopsticks need not have two sticks that are the same length. You can eat with a pair of sticks as long as the length difference is no greater than D. Samar is appointed as a head chef of a restaurant. He is provided with N sticks, the i<sup>th</sup>stick of which is L[i] units long. More than one set of chopsticks cannot share the same stick. Assist him in matching the sticks to create as many functional pairs of chopsticks as possible.The first line contains two space-separated integers N and D. The next N lines contain one integer each, the i<sup>th</sup>line giving the value of L[i].
<b>Constraints</b>
1 ≤ N ≤ 10,000 (10<sup>5</sup>)
0 ≤ D ≤ 1,000,000,000 (10<sup>9</sup>)
1 ≤ L[i] ≤ 1,000,000,000 (10<sup>9</sup>) for all integers i from 1 to NOutput a single line containing the maximum number of pairs of chopsticks Samar can form.Sample Input :
5 2
1
3
3
9
4
Sample Output :
2
Explanation :
The 5 sticks have lengths 1, 3, 3, 9, and 4 respectively. The maximum allowed difference in the lengths of two sticks forming a pair is at most 2. It is clear that the 4th stick (length 9) cannot be used with any other stick. The remaining 4 sticks can be paired as (1st and 3rd) and (2nd and 5th) to form 2 pairs of usable chopsticks., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
long int n, d;
cin >> n >> d;
long int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int cnt = 0;
for (int i = 1; i < n; i++) {
if (arr[i] - arr[i - 1] <= d) {
cnt++;
i++;
}
}
cout << cnt << endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
String[] s=br.readLine().trim().split(" ");
for(int i=0;i<arr.length;i++)
arr[i]=Integer.parseInt(s[i]);
int max=Integer.MIN_VALUE,c=0;
for(int i=0;i<arr.length;i++){
if(max==arr[i])
c++;
if(max<arr[i]){
max=arr[i];
c=1;
}
}
System.out.println(c);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: n=int(input())
a=list(map(int,input().split()))
print(a.count(max(a))), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was enjoying her daddy's party, her little sister Tono gave her the following problem to solve:
Given an array A of N integers, find the number of occurrences of the maximum integer in the array.
As Solo is small and enjoying the party, please help her solve this problem.The first line of the input contains an integer N, the size of the array A.
The next line contains N space separated integers, the elements of the array A.
Constraints
1 <= N <= 100
1 <= A[i] <= 100Output a single integer, the number of occurrences of the maximum integer in the array A.Sample Input
5
1 2 3 2 1
Sample Output
1
Explanation: The maximum integer is 3 and it occurs once in the array.
Sample Input
5
5 5 5 5 5
Sample Output
5, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
vector<int> a(101, 0);
For(i, 0, n){
int x; cin>>x;
a[x]++;
}
for(int i=100; i>=1; i--){
if(a[i]){
cout<<a[i];
return;
}
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: static int LikesBoth(int N, int A, int B){
return (A+B-N);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: def LikesBoth(N,A,B):
return (A+B-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: int LikesBoth(int N,int A, int B){
return (A+B-N);
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N people are standing in a queue in which A of them like apple and B of them like oranges. How many people like both apple and oranges.
<b>Note</b>:- It is guaranteed that each and every person likes at least one of the given two.<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>LikesBoth()</b> that takes integers N, A, and B as arguments.
Constraints:-
1 <= N <= 10000
1 <= A <= N
1 <= B <= NReturn the number of people that like both of the fruit.Sample Input:-
5 3 4
Sample Output:-
2
Sample Input:-
5 5 5
Sample Output:-
5, I have written this Solution Code: int LikesBoth(int N,int A, int B){
return (A+B-N);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N nodes and M undirected edges. These nodes and edges represent some trees (graphs with no loops). Now, your task is to merge the trees into a single tree. In one operation of merging, two disconnected trees are merged using an edge from one tree to the other tree. The cost of this merging operation is <b>total number of nodes in the final merged tree</b>. Once merged the trees lose their original existence.
So, if there are T trees in the original state, you'll have to perform the merge operation T-1 times to merge all the trees in a single tree.
All you need to do is find the minimum cost of merging the trees (sum of costs of all T-1 merge operations). See sample for better understanding.The first line of input contains two integers N and M, the total number of nodes and the total number of edges.
The next M lines contain two integers U and V, representing an undirected edge between the two nodes.
Constraints
1 <= N <= 200000
0 <= M < N
1 <= U, V <= N
The given nodes and edges represent some trees (maybe 1)Output a single integer, the cost of merging these trees.Sample Input
7 3
1 2
3 4
4 5
Sample Output
13
Explanation:
The four trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 ; 7
We will first add an edge from 6 to 7. (Cost of merging = 2)
The three remaining trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 --- 7
We will add an edge from 2 to 6. (Cost of merging = 4)
The two remaining trees are as follows:
1 --- 2 --- 6 --- 7 ; 3 --- 4 --- 5
We will add an edge from 7 to 3. (Cost of merging = 7)
Total cost of merging = 2 + 4 + 7 = 13.
Sample Input
2 1
1 2
Sample Output
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int find(int []parent, int x){
if(parent[x] == x){
return x;
}
parent[x] = find(parent, parent[x]);
return parent[x];
}
static void union(int []parent, int x, int y){
int parentX = find(parent, x);
int parentY = find(parent, y);
parent[parentY] = parentX;
}
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]);
int M = Integer.parseInt(str[1]);
int totalCost = 0;
int[] parent = new int[N+1];
for(int i=1; i<=N; i++){
parent[i] = i;
}
for (int i=0; i<M; i++) {
int x, y;
String[] str1 = br.readLine().trim().split(" ");
x = Integer.parseInt(str1[0]);
y = Integer.parseInt(str1[1]);
union(parent, x, y);
}
for(int i=1; i<=N; i++){
find(parent, i);
}
PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();
HashMap<Integer, Integer> counts = new HashMap<>();
for(int i=1; i<=N; i++){
counts.put(parent[i], counts.getOrDefault(parent[i], 0) + 1);
}
for (Map.Entry<Integer,Integer> entry : counts.entrySet()) {
pQueue.add(entry.getValue());
}
while(pQueue.size() > 1) {
int s1 = pQueue.poll();
int s2 = pQueue.poll();
totalCost += s1 + s2;
pQueue.add(s1 + s2);
}
System.out.println(totalCost);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N nodes and M undirected edges. These nodes and edges represent some trees (graphs with no loops). Now, your task is to merge the trees into a single tree. In one operation of merging, two disconnected trees are merged using an edge from one tree to the other tree. The cost of this merging operation is <b>total number of nodes in the final merged tree</b>. Once merged the trees lose their original existence.
So, if there are T trees in the original state, you'll have to perform the merge operation T-1 times to merge all the trees in a single tree.
All you need to do is find the minimum cost of merging the trees (sum of costs of all T-1 merge operations). See sample for better understanding.The first line of input contains two integers N and M, the total number of nodes and the total number of edges.
The next M lines contain two integers U and V, representing an undirected edge between the two nodes.
Constraints
1 <= N <= 200000
0 <= M < N
1 <= U, V <= N
The given nodes and edges represent some trees (maybe 1)Output a single integer, the cost of merging these trees.Sample Input
7 3
1 2
3 4
4 5
Sample Output
13
Explanation:
The four trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 ; 7
We will first add an edge from 6 to 7. (Cost of merging = 2)
The three remaining trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 --- 7
We will add an edge from 2 to 6. (Cost of merging = 4)
The two remaining trees are as follows:
1 --- 2 --- 6 --- 7 ; 3 --- 4 --- 5
We will add an edge from 7 to 3. (Cost of merging = 7)
Total cost of merging = 2 + 4 + 7 = 13.
Sample Input
2 1
1 2
Sample Output
0, I have written this Solution Code: import heapq
def dfs(adjlist,vis,node):
global temparr
temparr.append(node)
vis[node] = True
for i in adjlist[node]:
if not vis[i]:
dfs(adjlist,vis,i)
ne=list(map(int,input().rstrip().split()))
n=ne[0]
e=ne[1]
adjlist = [[] for i in range(n+1)]
for i in range(e):
edge = list(map(int,input().rstrip().split()))
adjlist[edge[0]].append(edge[1])
adjlist[edge[1]].append(edge[0])
vis = [False for i in range(n+1)]
temparr=[]
mx = 0
arr=[]
for x in range(1,n+1):
if vis[x] == False:
temparr = []
dfs(adjlist,vis,x)
arr.append(len(temparr))
heapq.heapify(arr)
while len(arr)>=2:
a=heapq.heappop(arr)
b=heapq.heappop(arr)
mx+=a+b
heapq.heappush(arr,a+b)
print(mx), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given N nodes and M undirected edges. These nodes and edges represent some trees (graphs with no loops). Now, your task is to merge the trees into a single tree. In one operation of merging, two disconnected trees are merged using an edge from one tree to the other tree. The cost of this merging operation is <b>total number of nodes in the final merged tree</b>. Once merged the trees lose their original existence.
So, if there are T trees in the original state, you'll have to perform the merge operation T-1 times to merge all the trees in a single tree.
All you need to do is find the minimum cost of merging the trees (sum of costs of all T-1 merge operations). See sample for better understanding.The first line of input contains two integers N and M, the total number of nodes and the total number of edges.
The next M lines contain two integers U and V, representing an undirected edge between the two nodes.
Constraints
1 <= N <= 200000
0 <= M < N
1 <= U, V <= N
The given nodes and edges represent some trees (maybe 1)Output a single integer, the cost of merging these trees.Sample Input
7 3
1 2
3 4
4 5
Sample Output
13
Explanation:
The four trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 ; 7
We will first add an edge from 6 to 7. (Cost of merging = 2)
The three remaining trees are as follows:
1 --- 2 ; 3 --- 4 --- 5 ; 6 --- 7
We will add an edge from 2 to 6. (Cost of merging = 4)
The two remaining trees are as follows:
1 --- 2 --- 6 --- 7 ; 3 --- 4 --- 5
We will add an edge from 7 to 3. (Cost of merging = 7)
Total cost of merging = 2 + 4 + 7 = 13.
Sample Input
2 1
1 2
Sample Output
0, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(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
vector<int> adj[N];
priority_queue<int, vector<int>, greater<int> > pq;
int ct = 0;
bool vis[N];
void dfs(int u, int p){
ct++;
vis[u]=true;
for(int v: adj[u]){
if(v != p){
dfs(v, u);
}
}
}
void solve(){
int n, m; cin>>n>>m;
For(i, 0, m){
int u, v; cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
For(i, 1, n+1){
if(!vis[i]){
ct = 0;
dfs(i, 0);
pq.push(ct);
}
}
int ans = 0;
while(sz(pq) > 1){
int c1 = pq.top();
pq.pop();
int c2 = pq.top();
pq.pop();
ans += (c1+c2);
pq.push(c1+c2);
}
assert(pq.top() == n);
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 number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
int n = Integer.parseInt(br.readLine());
String ans = check(n);
StringBuffer sbr = new StringBuffer(ans);
System.out.println(sbr.reverse());
}
}
static String check(int x)
{
String ans="";
while(x>0)
{
switch(x%4)
{
case 1:ans +="2"; break;
case 2:ans +="3"; break;
case 3:ans+="5"; break;
case 0:ans+="7"; break;
}
if(x%4==0)
x--;
x/=4;
}
return ans;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: def nthprimedigitsnumber(number):
num=""
while (number > 0):
rem = number % 4
if (rem == 1):
num += '2'
if (rem == 2):
num += '3'
if (rem == 3):
num += '5'
if (rem == 0):
num += '7'
if (number % 4 == 0):
number = number - 1
number = number // 4
return num[::-1]
T=int(input())
for i in range(T):
number = int(input())
print(nthprimedigitsnumber(number)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number 'N'. The task is to find the Nth number whose each digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so on.The first line contains a single integer T i.e. the number of test cases. The first and only line of each test case consists of a single integer N.
Constraints:
1 <= T <= 100
1 <= N <= 100000For each testcase print the nth number of the given sequence made of prime digits in a new line.Input:
2
10
21
Output:
33
222
Explanation:
Testcase 1: 10th number in the sequence of numbers whose each digit is prime is 33.
Testcase 2: 21th number in the sequence of numbers whose each digit is prime is 222., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void nthprimedigitsnumber(long long n)
{
long long len = 1;
long long prev_count = 0;
while (true) {
long long curr_count = prev_count + pow(4, len);
if (prev_count < n && curr_count >= n)
break;
len++;
prev_count = curr_count;
}
for (int i = 1; i <= len; i++) {
for (long long j = 1; j <= 4; j++) {
if (prev_count + pow(4, len - i) < n)
prev_count += pow(4, len - i);
else {
if (j == 1)
cout << "2";
else if (j == 2)
cout << "3";
else if (j == 3)
cout << "5";
else if (j == 4)
cout << "7";
break;
}
}
}
cout << endl;
}
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
nthprimedigitsnumber(n);
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
String[] line = br.readLine().split(" ");
int happyBalloons = 0;
for(int i=1;i<=line.length;++i){
int num = Integer.parseInt(line[i-1]);
if(num%2 == i%2 ){
happyBalloons++;
}
}
System.out.println(happyBalloons);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 5
Sample Output
5, I have written this Solution Code: x=int(input())
arr=input().split()
for i in range(0,x):
arr[i]=int(arr[i])
count=0
for i in range(0,x):
if(arr[i]%2==0 and (i+1)%2==0):
count+=1
elif (arr[i]%2!=0 and (i+1)%2!=0):
count+=1
print (count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There are N balloons numbered from 1 to N. Every balloon has an another integer value Arr[i] assigned to it where i varies from 1 to N, and i represents the number of balloon.
A balloon at an odd position (i = 1, 3, 5. ) is happy if Arr[i] is odd.
A balloon at an even position (i = 2, 4, 6. ) is happy if Arr[i] is even.
Find the number of happy balloons.The first line of the input contains a single integer N.
The second line of the input contains N singly spaced integers, Arr[1], Arr[2], Arr[3],. , Arr[N].
Constrains
1 <= N <= 200000
1 <= Arr[i] <= 1000000Output a single integer, the number of happy balloons.Sample Input
5
1 3 4 6 7
Sample Output
3
Explanation
Happy balloons are balloons numbered 1, 4, 5.
Sample Input
5
1 2 3 4 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(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 n; cin>>n;
int ans = 0;
For(i, 1, n+1){
int a; cin>>a;
if(i%2 == a%2)
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: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: n=int(input())
li = list(map(int,input().strip().split()))
sum=0
for i in li:
if i>0:
sum+=i
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
long arr[] = new long[n];
long sum=0;
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
if(arr[i]>0){
sum+=arr[i];
}
}
System.out.print(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and an integer K, Your task is to multiply the first leftmost digit of the number to the number itself. You have to repeat this process K times.
For eg:- if N=3 and K=5 then:
3 * 3 = 9
9 * 9 = 81
81 * 8 = 648
648 * 6 = 3888
3888 * 3 = 11664<b>User Task:</b>
Complete the function <b>KOperations()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 100
1 <= K <= 10^9Return the Number after K operations
<b>Note:</b> It is guaranteed that the output will always be less than 10^17.Sample Input:-
3 5
Sample Output:-
11664
Explanation:- See problem statement for explanation.
Sample Input:-
22 2
Sample Output:-
176, 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();
System.out.println(KOperations(x,y));
}
public static long KOperations(long N, long K){
long p=N;
while(K-->0){
p=N;
while(p>=10){
p=p/10;
}
if(p==1){return N;}
N=N*p;
}
return N;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and an integer K, Your task is to multiply the first leftmost digit of the number to the number itself. You have to repeat this process K times.
For eg:- if N=3 and K=5 then:
3 * 3 = 9
9 * 9 = 81
81 * 8 = 648
648 * 6 = 3888
3888 * 3 = 11664<b>User Task:</b>
Complete the function <b>KOperations()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 100
1 <= K <= 10^9Return the Number after K operations
<b>Note:</b> It is guaranteed that the output will always be less than 10^17.Sample Input:-
3 5
Sample Output:-
11664
Explanation:- See problem statement for explanation.
Sample Input:-
22 2
Sample Output:-
176, I have written this Solution Code: import math
def KOperations(N,K) :
# Final result of summation of divisors
while K>0 :
p=N
while(p>=10):
p=p/10
if(int(p)==1):
return N;
N=N*int(p)
K=K-1
return N;
# Driver program to run the case
x,y= map(int,input().split());
print (int(KOperations(x,y))) , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and an integer K, Your task is to multiply the first leftmost digit of the number to the number itself. You have to repeat this process K times.
For eg:- if N=3 and K=5 then:
3 * 3 = 9
9 * 9 = 81
81 * 8 = 648
648 * 6 = 3888
3888 * 3 = 11664<b>User Task:</b>
Complete the function <b>KOperations()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 100
1 <= K <= 10^9Return the Number after K operations
<b>Note:</b> It is guaranteed that the output will always be less than 10^17.Sample Input:-
3 5
Sample Output:-
11664
Explanation:- See problem statement for explanation.
Sample Input:-
22 2
Sample Output:-
176, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
long long KOperations(long long N, long long K){
long long p;
while(K--){
p=N;
while(p>=10){
p=p/10;
}
if(p==1){return N;}
N*=p;
}
return N;
}
int main(){
long long x,y;
cin>>x>>y;
cout<<KOperations(x,y);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N and an integer K, Your task is to multiply the first leftmost digit of the number to the number itself. You have to repeat this process K times.
For eg:- if N=3 and K=5 then:
3 * 3 = 9
9 * 9 = 81
81 * 8 = 648
648 * 6 = 3888
3888 * 3 = 11664<b>User Task:</b>
Complete the function <b>KOperations()</b> that takes the integer N as parameter.
<b>Constraints:</b>
1 <= N <= 100
1 <= K <= 10^9Return the Number after K operations
<b>Note:</b> It is guaranteed that the output will always be less than 10^17.Sample Input:-
3 5
Sample Output:-
11664
Explanation:- See problem statement for explanation.
Sample Input:-
22 2
Sample Output:-
176, I have written this Solution Code: #include <stdio.h>
#include <math.h>
long long int KOperations(long long N, long long K){
long long int p;
while(K--){
p=N;
while(p>=10){
p=p/10;
}
if(p==1){return N;}
N*=p;
}
return N;
}
int main(){
long long int x,y;
scanf("%lld",&x);
scanf("%lld",&y);
printf("%lld",KOperations(x,y));
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)) ;
int n = Integer.parseInt(read.readLine()) ;
String[] str = read.readLine().trim().split(" ") ;
int[] arr = new int[n] ;
for(int i=0; i<n; i++) {
arr[i] = Integer.parseInt(str[i]) ;
}
long ans = 0L;
if((n & 1) == 1) {
for(int i=0; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
else {
Arrays.sort(arr);
for(int i=1; i<n; i++) {
ans += ((arr[i] & 1) == 1) ? arr[i] : arr[i] - 1 ;
}
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: n=int(input())
arr=map(int,input().split())
result=[]
for item in arr:
if item%2==0:
result.append(item-1)
else:
result.append(item)
if sum(result)%2==0:
result.sort()
result.pop(0)
print(sum(result))
else:
print(sum(result)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The flower shop near his house sells flowers of N types. It is found that the store has Ai flowers of the i-th type. We like odd numbers. Therefore, we have decided that there should be an odd number of flowers of each type in the bouquet, and the total number of flowers in the bouquet should also be odd.
Determine the maximum number of flowers the bouquet can consist of.The first line contains an integer N — the number of types of flowers that are sold in the store
The second line contains N integers— the number of flowers of each type
1 <= N <= 100000
1 <= Ai <= 1000Print one number — the maximum number of flowers the bouquet can consist of.Sample input
3
3 5 8
Sample output
15
Sample input
3
1 1 1
Sample output
3, I have written this Solution Code: #include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
int n; cin >> n ;
long long int ans = 0;
// int arr[n];
int odd = 0,minn=INT_MAX;
for(int i=0;i<n;i++){
ll temp; cin >> temp;
// cin >> arr[i];
ans += (temp&1) ? temp : temp-1;
if(minn>temp)
minn = temp;
}
if(n&1)
cout << ans ;
else{
if(minn&1)
cout <<ans-minn;
else
cout << ans - minn + 1;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: In Newtonland, everyone who earns strictly more than Y rupees per year has to pay a tax to Newton. Newton has allowed a special scheme where you can invest any amount of money and claim an exemption for it. You have earned X rupees this year. Find the minimum amount of money you have to invest so that you don't have to pay taxes this year.The first line of input consists of two space- separated integers X and Y denoting the amount you earned and the amount above which you will have to pay taxes.
<b>Constraints :</b>
1 ≤ X, Y ≤ 100Output a single integer, denoting the minimum amount you need to invest.Sample Input:-
4 4
Sample Output:-
0
Sample Input:-
90 80
Sample Output:-
10, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public void Solve(int x,int y) {
if(x<=y)
System.out.println(0);
else
System.out.println(x-y);
}
}
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
//StringBuilder sss = new StringBuilder("");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
assert(n >= 1 && n <= 100);
int x=sc.nextInt();
assert(x >= 1 && x <= 100);
Solution obj=new Solution();
obj.Solve(n,x);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the sum of all the digits of the number
Note: Use a recursive method to solve this problem.<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>Sum()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 100
0 ≤ N ≤ 1000000000Return sum of digits.Sample Input
2
25
28
Sample Output
7
10, I have written this Solution Code: // n is the input number
function recSum(n) {
// write code here
// do not console.log
// return the answer as a number
if (n < 10) return n;
return n % 10 + recSum(Math.floor(n / 10))
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N, find the sum of all the digits of the number
Note: Use a recursive method to solve this problem.<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>Sum()</b> that takes the integer N as a parameter.
Constraints:
1 ≤ T ≤ 100
0 ≤ N ≤ 1000000000Return sum of digits.Sample Input
2
25
28
Sample Output
7
10, I have written this Solution Code: static long Sum(long n)
{ if(n==0){return 0;}
return n%10+Sum(n/10);
} , In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException{
BufferedReader rd=new BufferedReader(new InputStreamReader(System.in));
String y[]=rd.readLine().split(" ");
long n=Long.parseLong(y[0]);
long p=Long.parseLong(y[1]);
long v=1;
while(p>0){
if((p&1L)==1L)
v=(v*n)%1000000007;
p/=2;
n=(n*n)%1000000007;
}
System.out.print(v);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code: n, p =input().split()
n, p =int(n), int(p)
def FastModularExponentiation(b, k, m):
res = 1
b = b % m
while (k > 0):
if ((k & 1) == 1):
res = (res * b) % m
k = k >> 1
b = (b * b) % m
return res
m=pow(10,9)+7
print(FastModularExponentiation(n, p, m)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N and a number P, your task is to calculate N<sup>P</sup>. Since the ans can be very long calculate your ans as N<sup>P</sup>%M where M = 10<sup>9</sup>+7Input contains only two integers N and P separated by spaces.
Constraints:-
1 < = N, P <= 1000000000Print N<sup>P</sup>%M.Sample Input:-
2 5
Sample Output:-
32
Sample Input:-
2 100
Sample Output:-
976371285, I have written this Solution Code:
// author-Shivam gupta
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define max1 100001
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0) return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Driver code
signed main()
{
int x ;
int y;
cin>>x>>y;
int p = 1e9+7;
cout<< power(x, y, p);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given a <code>list of employees</code> with their information in a <code>JSON format as an argument</code>. Your task is to implement a JavaScript function, <code>performEmployeeOperations</code>, that performs the following tasks:
<ol>
<li>Find the <code>employee with the highest salary</code> and store it in the <code>highestSalaryEmployee</code>. Note, the <code>highestSalaryEmployee</code> logic has already been provided in the boilerplate. For the rest of the operations, you might have to look into array loop methods, like reduce, filter, map, etc.</li>
<li>The <code>employeesByDepartment</code> returns an <code>object</code> of employees grouped by department. The <code>department should be the key</code> and an <code>array of employees with their information as value</code>.</li>
<li>The <code>averageAgeByDepartment</code> returns an <code>object</code>, with the <code>department as key</code> and the <code>average age of that department as value</code>.</li>
<li><code>employeesWithLongestName</code> returns an <code>array</code> with the <code>employee(s) information having the longest name(s)</code>.</li>
</ol>
<code>Note:</code>
**Remove the extra spaces from the list while inserting the list of employees in the input section.**The <code>performEmployeeOperations</code> function will take in an <code>array of objects in JSON format</code>. Each Object will have a <code>name</code>, <code>age</code>, <code>department</code>, and <code>salary</code> property.The <code>performEmployeeOperations</code> function should return an <code>object</code> of <code>highestSalaryEmployee</code>, <code>employeesByDepartment</code>, <code>averageAgeByDepartment</code>, <code>employeesWithLongestName</code>.
<code>highestSalaryEmployee</code> should store the <code>object of the employee having the highest salary</code>.
<code>employeesByDepartment</code> should return an <code>object</code>.
<code>averageAgeByDepartment</code> should return an <code>object</code>.
<code>employeesWithLongestName</code> should return an <code>array of object</code>.const employees = [{"name":"John","age":30,"department":"HR","salary":50000},{"name":"Jane","age":28,"department":"IT","salary":60000},{"name":"Mark","age":35,"department":"HR","salary":55000},{"name":"Alice","age":32,"department":"Finance","salary":65000},{"name":"Charlie","age":40,"department":"IT","salary":70000}]
const operations = performEmployeeOperations(employees);
console.log(operations.highestSalaryEmployee); // Output: { name: 'Charlie', age: 40, department: 'IT', salary: 70000 }
console.log(operations.employeesByDepartment);
// Output:
{
HR: [
{ name: 'John', age: 30, department: 'HR', salary: 50000 },
{ name: 'Mark', age: 35, department: 'HR', salary: 55000 }
],
IT: [
{ name: 'Jane', age: 28, department: 'IT', salary: 60000 },
{ name: 'Charlie', age: 40, department: 'IT', salary: 70000 }
],
Finance: [ { name: 'Alice', age: 32, department: 'Finance', salary: 65000 } ]
}
console.log(operations.averageAgeByDepartment); //Output: { HR: 32.5, IT: 34, Finance: 32 }
console.log(operations.employeesWithLongestName); //Output: [ { name: 'Charlie', age: 40, department: 'IT', salary: 70000 } ], I have written this Solution Code: function performEmployeeOperations(employees) {
const highestSalaryEmployee = employees.reduce((acc, emp) => emp.salary > acc.salary ? emp : acc, employees[0]);
const employeesByDepartment = employees.reduce((acc, emp) => {
if (!acc[emp.department]) {
acc[emp.department] = [];
}
acc[emp.department].push(emp);
return acc;
}, {});
const averageAgeByDepartment = Object.keys(employeesByDepartment).reduce((acc, department) => {
const employeesInDepartment = employeesByDepartment[department];
const totalAge = employeesInDepartment.reduce((sum, emp) => sum + emp.age, 0);
const averageAge = totalAge / employeesInDepartment.length;
acc[department] = averageAge;
return acc;
}, {});
const longestNameLength = Math.max(...employees.map(emp => emp.name.length));
const employeesWithLongestName = employees.filter(emp => emp.name.length === longestNameLength);
return {
highestSalaryEmployee,
employeesByDepartment,
averageAgeByDepartment,
employeesWithLongestName
};
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
String ref = str[0];
for(String s: str){
if(s.length()<ref.length()){
ref = s;
}
}
for(int i=0; i<n; i++){
if(str[i].contains(ref) == false){
ref = ref.substring(0, ref.length()-1);
}
}
if(ref.length()>0)
System.out.println(ref);
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 an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: def longestPrefix( strings ):
size = len(strings)
if (size == 0):
return -1
if (size == 1):
return strings[0]
strings.sort()
end = min(len(strings[0]), len(strings[size - 1]))
i = 0
while (i < end and
strings[0][i] == strings[size - 1][i]):
i += 1
pre = strings[0][0: i]
if len(pre) > 1:
return pre
else:
return -1
N=int(input())
strings=input().split()
print( longestPrefix(strings) ), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: function commonPrefixUtil(str1,str2)
{
let result = "";
let n1 = str1.length, n2 = str2.length;
// Compare str1 and str2
for (let i = 0, j = 0; i <= n1 - 1 && j <= n2 - 1; i++, j++) {
if (str1[i] != str2[j]) {
break;
}
result += str1[i];
}
return (result);
}
// n is number of individual space seperated strings inside strings variable,
// strings is the string which contains space seperated words.
function longestCommonPrefix(strings,n){
// write code here
// do not console,log answer
// return the answer as string
if(n===1) return strings;
const arr= strings.split(" ")
let prefix = arr[0];
for (let i = 1; i <= n - 1; i++) {
prefix = commonPrefixUtil(prefix, arr[i]);
}
if(!prefix) return -1;
return (prefix);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N strings, find the longest common prefix among all strings present in the array.The fist line of each test contains a single integer N. Next line has space-separated N strings.
Constraints:-
1 <= N <= 10^3
1 <= |S| <= 10^3
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).Sample Input:
4
geeksforgeeks geeks geek geezer
Sample Output:
gee
Sampel Input:-
3
a b c
Sample Output:-
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
string a[n];
int x=INT_MAX;
for(int i=0;i<n;i++){
cin>>a[i];
x=min(x,(int)a[i].length());
}
string ans="";
for(int i=0;i<x;i++){
for(int j=1;j<n;j++){
if(a[j][i]==a[0][i]){continue;}
goto f;
}
ans+=a[0][i];
}
f:;
if(ans==""){cout<<-1;return 0;}
cout<<(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given the first 2 terms A and B of an Arithmetic Series, tell the Nth term of the series.<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>nthAP()</b> that takes the integer A, B, and N as a parameter.
<b>Constraints:</b>
-10^3 <= A <= 10^3
-10^3 <= B <= 10^3
1 <= N <= 10^4Print the Nth term of AP series.Sample Input:
2 3 4
Sample Output:
5
Sample Input:
1 2 10
Sample output:
10, I have written this Solution Code: def nthAP(x, y, z):
res = x + (y-x) * (z-1)
print(res), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a morse code, Your task is to figure out the exact words from it.
Morse Code Dictionary :
dict = {".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G", "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..", "L": "--", "M": "-.", "N": "---", "O": ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T", "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z"};
<img src="https://maker.pro/storage/sKEFbex/sKEFbexA2iF4ZvxgtvPaWXGHxMiwohU6FvxJgkCC.jpeg" height="100px" width="100px">The first line of the input contains the morse string.
<b>Constraints </b>
1<= Total Words <= 1000Print the English Alphabets from the given codeSample Input 1:
<pre>
.-- --- --.- - . .-. -..-
</pre>
Sample Output 1:
WOQTERX, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
String code[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",
".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
Scanner sc =new Scanner(System.in);
String morseCode =sc.nextLine();
morseToWords(code,morseCode);
}
static void morseToWords(String[] code,String morseCode ){
HashMap<String,Character> hs =new HashMap<>();
for(int i=0; i<26; i++)
hs.put(code[i],(char)('A'+i));
String [] array = morseCode.split(" ");
for(int i=0; i< array.length; i++)
System.out.print(hs.get(array[i]));
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a morse code, Your task is to figure out the exact words from it.
Morse Code Dictionary :
dict = {".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G", "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..", "L": "--", "M": "-.", "N": "---", "O": ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T", "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z"};
<img src="https://maker.pro/storage/sKEFbex/sKEFbexA2iF4ZvxgtvPaWXGHxMiwohU6FvxJgkCC.jpeg" height="100px" width="100px">The first line of the input contains the morse string.
<b>Constraints </b>
1<= Total Words <= 1000Print the English Alphabets from the given codeSample Input 1:
<pre>
.-- --- --.- - . .-. -..-
</pre>
Sample Output 1:
WOQTERX, I have written this Solution Code:
dict = dict1 = {".-":"A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G", "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..": "L", "--": "M", "-.": "N", "---": "O", ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T", "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z"}
def word(s):
print(''.join(dict.get(i) for i in s.split()))
if __name__ == "__main__":
word(input()), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a morse code, Your task is to figure out the exact words from it.
Morse Code Dictionary :
dict = {".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G", "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..", "L": "--", "M": "-.", "N": "---", "O": ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T", "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z"};
<img src="https://maker.pro/storage/sKEFbex/sKEFbexA2iF4ZvxgtvPaWXGHxMiwohU6FvxJgkCC.jpeg" height="100px" width="100px">The first line of the input contains the morse string.
<b>Constraints </b>
1<= Total Words <= 1000Print the English Alphabets from the given codeSample Input 1:
<pre>
.-- --- --.- - . .-. -..-
</pre>
Sample Output 1:
WOQTERX, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-18 01:39:40
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
float calculateSD(vector<float> a, int n) {
float sum = 0.0, mean, SD = 0.0;
for (int i = 0; i < n; ++i) {
sum += a[i];
}
mean = sum / n;
for (int i = 0; i < n; ++i) {
SD += pow(a[i] - mean, 2);
}
return sqrt(SD / n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// Morse Code
{
char str[100000];
cin.getline(str, sizeof(str), '\n');
string temp = "";
vector<string> words;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ' ') {
if (temp.length() > 0) {
words.push_back(temp);
}
temp = "";
} else {
temp += str[i];
}
}
if (temp.length() > 0) {
words.push_back(temp);
}
// debug(words);
map<string, string> dict = {
{".-", "A"},
{"-...", "B"},
{"-.-.", "C"},
{"-..", "D"},
{".", "E"},
{"..-.", "F"},
{"--.", "G"},
{"....", "H"},
{"..", "I"},
{".---", "J"},
{"-.-", "K"},
{".-..", "L"},
{"--", "M"},
{"-.", "N"},
{"---", "O"},
{".--.", "P"},
{"--.-", "Q"},
{".-.", "R"},
{"...", "S"},
{"-", "T"},
{"..-", "U"},
{"...-", "V"},
{".--", "W"},
{"-..-", "X"},
{"-.--", "Y"},
{"--..", "Z"}};
for (auto &it : words) {
cout << dict[it];
}
cout << "\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and M, your task is to print the number of primes present between N and M (both included).
<b>Note</b>:- You have already provided a function that will check if the given number is prime or not. To use the given function you need to call <b>check_prime(x)</b> where x is the number you want to check. If the given number is prime the function will return 1 else it returns 0.
<b>Note</b>:- Do not close your main class.The input contains two space- separated integers depicting the values of N and M.
Constraints:-
1 <= N <= M <= 10000Print the count of prime numbers in the given range.Sample Input:-
1 10
Sample Output:-
4
Sample Input:-
8 10
Sample Output:-
0, I have written this Solution Code: from math import sqrt
def isPrime(n):
if (n <= 1):
return False
for i in range(2, int(sqrt(n))+1):
if (n % i == 0):
return False
return True
x=input().split()
n=int(x[0])
m=int(x[1])
count = 0
for i in range(n,m):
if isPrime(i):
count = count +1
print(count), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers N and M, your task is to print the number of primes present between N and M (both included).
<b>Note</b>:- You have already provided a function that will check if the given number is prime or not. To use the given function you need to call <b>check_prime(x)</b> where x is the number you want to check. If the given number is prime the function will return 1 else it returns 0.
<b>Note</b>:- Do not close your main class.The input contains two space- separated integers depicting the values of N and M.
Constraints:-
1 <= N <= M <= 10000Print the count of prime numbers in the given range.Sample Input:-
1 10
Sample Output:-
4
Sample Input:-
8 10
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) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m = sc.nextInt();
int cnt=0;
for(int i=n;i<=m;i++){
if(check_prime(i)==1){cnt++;}
}
System.out.println(cnt);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a <b>BST</b> and some keys, the task is to insert the keys in the given BST. Duplicates are not inserted. (If a test case contains duplicate keys, you need to consider the first occurrence and ignore duplicates).<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>insertInBST()</b> that takes "root" node and value to be inserted as parameter. The printing is done by the driver code.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^3
1 <= node values <= 10^4
<b>Sum of "N" over all testcases does not exceed 10^5</b>Return the node of BST after insertion.Input:
2
3
2 1 3
4
8
2 1 3 N N N 6 4
1
Output:
1 2 3 4
1 2 3 4 6
Explanation:
Testcase 1: After inserting the node 4 the tree will be
2
/ \
1 3
\
4
Inorder traversal will be 1 2 3 4.
Testcase 2: After inserting the node 1 the tree will be
2
/ \
1 3
/ \ / \
N N N 6
/
4
Inorder traversal of the above tree will be 1 2 3 4 6., I have written this Solution Code:
static Node insertInBST(Node root,int key)
{
if(root == null) return new Node(key);
if(key < root.data)
root.left = insertInBST(root.left,key);
else if(key > root.data)
root.right = insertInBST(root.right,key);
return root;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
long int n, el;
cin>>n>>el;
long int arr[n+1];
for(long int i=0; i<n; i++) {
cin>>arr[i];
}
arr[n] = el;
for(long int i=0; i<=n; i++) {
cout<<arr[i];
if (i != n) {
cout<<" ";
}
else if(t != 0) {
cout<<endl;
}
}
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: t=int(input())
while t>0:
t-=1
li = list(map(int,input().strip().split()))
n=li[0]
num=li[1]
a= list(map(int,input().strip().split()))
a.insert(len(a),num)
for i in a:
print(i,end=" ")
print(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Insertion is a basic but frequently used operation. Arrays in most languages cannnot be dynamically shrinked or expanded. Here, we will work with such arrays and try to insert an element at the end of array.
You are given an array arr. The size of the array is N. You need to insert an element at its end and print this newly modified array.The first line of input contains T denoting the number of testcases.
T testcases follow. Each testcase contains two lines of input.
The first line contains size of the array denoted by N and element to be inserted.
The third line contains N elements separated by spaces.
Constraints:
1 <= T <= 20
2 <= N <= 10000
0 <= element, arri <= 10^6For each testcase, in a new line, print the modified array.Input:
2
5 90
1 2 3 4 5
3 50
1 2 3
Output:
1 2 3 4 5 90
1 2 3 50
Explanation:
Testcase 1: After inserting 90 at end, we have array elements as 1 2 3 4 5 90.
Testcase 2: After inserting 50 at end, we have array elements as 1 2 3 50., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n =Integer.parseInt(br.readLine().trim());
while(n-->0){
String str[]=br.readLine().trim().split(" ");
String newel =br.readLine().trim()+" "+str[1];
System.out.println(newel);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: As they were totally exhausted with the daily chores, Ketani and Nilswap decided to play a game.They get alternate turns in the game and Ketani starts the game. The game consists of a tree containing N nodes numbered 1 to N. On each turn the person chooses a node of the tree that is not yet numbered. If it's Ketani's turn, he writes an odd number to this node whereas if it's Nilswap's turn, he writes an even number to this node.
After the entire tree has been numbered, all the odd numbered nodes that are adjacent to an even numbered node turn even (instantaneous, no propagation).
If the tree contains any odd number in the end, Ketani wins otherwise Nilswap wins. You need to output the name of the winner.
Note: Initially none of the nodes contains a number. Both players play optimally.The first line of the input contains an integer N, the number of nodes in the tree.
The next N-1 lines contain two integers u, v indicating there is an edge between u and v.
Constraints
2 <= N <= 100000
1 <= u, v <= 100000
u != v
The given edges form a tree.Output a single string, the winner of the game.Sample Input 1
3
1 2
2 3
Sample Output 1
Ketani
Sample Input 2
2
1 2
Sample Output 2
Nilswap
Explanation 1:
-> Ketani writes an odd number to node 2.
-> Nilswap writes an even number to node 3.
-> Ketani writes an odd number to node 1.
-> After this, node 2 converts to an even number. But node 1 is still white. So Ketani wins., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
vector<int> adj[N];
bool ans;
bool dfs(int u, int par = 0) {
int cnt = 0;
for (auto v: adj[u])
if (v != par)
cnt += dfs(v, u);
if (cnt >= 2) {
ans = true;
return false;
}
return 1 - cnt;
}
signed main(){
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fast
int n; cin >> n;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
adj[v].pb(u);
adj[u].pb(v);;
}
ans |= dfs(1);
if(ans){
cout<<"Ketani";
}
else{
cout<<"Nilswap";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a double linked list consisting of N nodes, your task is to reverse the linked list and return the head of the modified list.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Reverse()</b> that takes head node of the linked list as a parameter.
Constraints:
1 <= N <= 10^3
1<=value<=100Return the head of the modified linked list.Input:
6
1 2 3 4 5 6
Output:
6 5 4 3 2 1
Explanation:
After reversing the list, elements are as 6 <-> 5 <-> 4 <-> 3 <-> 2 <-> 1., I have written this Solution Code: public static Node Reverse(Node head) {
Node temp = null;
Node current = head;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
head = temp.prev;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N integers, your task is to calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
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 n=Integer.parseInt(br.readLine());
String str[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(str[i]);
}
long res=0;
for (int i=0;i<32;i++){
long cnt=0;
for (int j=0;j<n;j++)
if ((a[j] & (1 << i)) == 0)
cnt++;
res=(res+(cnt*(n-cnt)*2))%1000000007;
}
System.out.println(res%1000000007);
}
}, 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, your task is to calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
Sample Output:-
4, I have written this Solution Code: def suBD(arr, n):
ans = 0 # Initialize result
for i in range(0, 64):
count = 0
for j in range(0, n):
if ( (arr[j] & (1 << i)) ):
count+= 1
ans += (count * (n - count)) * 2;
return (ans)%(10**9+7)
n=int(input())
arr = map(int,input().split())
arr=list(arr)
print(suBD(arr, n)), 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 calculate the sum of bit difference in all pairs which can be formed.The first line of input contains a single integer N, the second line of input contains N space separated integers depicting values of the array.
Constraints:-
1 <= N <= 100000
0 <= Arr[i] <= 1000000000Print the sum of bit difference of all possible pairs.
Note:- Since the answer can be quite large print your answer modulo 10<sup>9</sup> + 7Sample Input:-
2
1 3
Sample Output:-
2
Explanation:-
(1, 1) = 0
(1, 3) = 1
(3, 1) = 1
(3, 3) = 0
Sample Input:-
2
1 2
Sample Output:-
4, I have written this Solution Code:
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 101
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}signed main(){
int N;
cin>>N;
int a[55];
int A[N];
FOR(i,N){
cin>>A[i];}
for(int i=0;i<55;i++){
a[i]=0;
}
int ans=1,p=2;
for(int i=0;i<55;i++){
for(int j=0;j<N;j++){
if(ans&A[j]){a[i]++;}
}
ans*=p;
// out(ans);
}
ans=0;
for(int i=0;i<55;i++){
ans+=(a[i]*(N-a[i])*2);
ans%=MOD;
}
out(ans);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer <b>N</b> and <b>Q</b> queries. For every query, you will be given <b>start</b> and <b>end</b> point and a positive integer <b>K</b> which is going to be added to the numbers in the range(from start to end) as per query. Once you are done with all the queries, you need to print the maximum number obtained after updation.
Note:-Initially the array is-<b> 1,2,3,....N </b>First line of input contains number of testcases T. For each testcase, first line contains N and Q( number of queries). Q lines after this will contain three integers each line has start, end and K, where start is the starting index of the range, end is the ending index of the range and K is the value to add with the elements in the range.
<b>Note:</b> Array is 1-based index
<b>Constraints:</b>
1 <= T <= 100
1 <= N, K <= 10^5
1 <= Q <= 10^5
1 <= start <= end <= N
Sum of N, Q for every test case is less than or equal to 10^5For each testcase, you need to print the maximum.Input:
1
5 3
1 2 5
2 5 10
3 4 5
Output:
19
Explanation:
Testcase 1: After the queries, we have elements added in the given ranges, so updated numbers from 1 to 5 will be as 6, 17, 18, 19, 15. Thus maximum after updation comes as19., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void abc(int[] a,int s,int e,int k)
{
for(int i=s;i<e;i++)
a[i]+=k;
}
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
for(int z=0;z<t;z++)
{
String[] s=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int q=Integer.parseInt(s[1]);
int[] arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=i+1;
for(int i=0;i<q;i++)
{
String[] ss=br.readLine().split(" ");
int st=Integer.parseInt(ss[0]);
int en=Integer.parseInt(ss[1]);
int k=Integer.parseInt(ss[2]);
abc(arr,st-1,en,k);
}
int max=-9999999;
for(int i=0;i<n;i++)
{
if(arr[i]>max)
max=arr[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 a positive integer <b>N</b> and <b>Q</b> queries. For every query, you will be given <b>start</b> and <b>end</b> point and a positive integer <b>K</b> which is going to be added to the numbers in the range(from start to end) as per query. Once you are done with all the queries, you need to print the maximum number obtained after updation.
Note:-Initially the array is-<b> 1,2,3,....N </b>First line of input contains number of testcases T. For each testcase, first line contains N and Q( number of queries). Q lines after this will contain three integers each line has start, end and K, where start is the starting index of the range, end is the ending index of the range and K is the value to add with the elements in the range.
<b>Note:</b> Array is 1-based index
<b>Constraints:</b>
1 <= T <= 100
1 <= N, K <= 10^5
1 <= Q <= 10^5
1 <= start <= end <= N
Sum of N, Q for every test case is less than or equal to 10^5For each testcase, you need to print the maximum.Input:
1
5 3
1 2 5
2 5 10
3 4 5
Output:
19
Explanation:
Testcase 1: After the queries, we have elements added in the given ranges, so updated numbers from 1 to 5 will be as 6, 17, 18, 19, 15. Thus maximum after updation comes as19., I have written this Solution Code: tc=int(input())
while(tc>0):
n,q=[int(i) for i in input().split()]
li=[0]*(n+1);
while(q>0):
a,b,k=[int(i) for i in input().split()]
li[a-1]+=k
li[b]-=k
q-=1
m=0
for i in range(1,n):
li[i]+=li[i-1]
for i in range(0,n):
if(li[i]+i+1>m):
m=li[i]+i+1
print(m)
tc-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer <b>N</b> and <b>Q</b> queries. For every query, you will be given <b>start</b> and <b>end</b> point and a positive integer <b>K</b> which is going to be added to the numbers in the range(from start to end) as per query. Once you are done with all the queries, you need to print the maximum number obtained after updation.
Note:-Initially the array is-<b> 1,2,3,....N </b>First line of input contains number of testcases T. For each testcase, first line contains N and Q( number of queries). Q lines after this will contain three integers each line has start, end and K, where start is the starting index of the range, end is the ending index of the range and K is the value to add with the elements in the range.
<b>Note:</b> Array is 1-based index
<b>Constraints:</b>
1 <= T <= 100
1 <= N, K <= 10^5
1 <= Q <= 10^5
1 <= start <= end <= N
Sum of N, Q for every test case is less than or equal to 10^5For each testcase, you need to print the maximum.Input:
1
5 3
1 2 5
2 5 10
3 4 5
Output:
19
Explanation:
Testcase 1: After the queries, we have elements added in the given ranges, so updated numbers from 1 to 5 will be as 6, 17, 18, 19, 15. Thus maximum after updation comes as19., 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+1];
for(int i=0;i<=n;i++){
a[i]=0;
}
int x,y,p;
while(k--){
cin>>x>>y>>p;
x--;
a[x]+=p;
a[y]-=p;
}
long long sum=0,ans=0;
for(int i=0;i<n;i++){
sum+=a[i];
ans=max(sum+i+1,ans);
}
cout<<ans<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Power Rangers have to trap a evil spirit monster into a cuboid shape box with dimensions A*B*C, where A, B and C are positive integers. The volume of the box should be exactly X cubic units (that is A*B*C should be equal to X). To allow minimum evil aura to leak from the box the surface area of the box should be minimized. Given X, find the minimum surface area of the optimal box.The first and the only line of input contains a single integer X.
Constraints:
1 <= X <= 1000000Print the minimum surface area of the optimal box.Sample Input 1
125
Sample Output 1
150
Explanation: Optimal dimensions are 5*5*5.
Sample Input 2
100
Sample Output 1
130
Explanation: Optimal dimensions are 5*4*5., I have written this Solution Code:
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)throws Exception{ new Main().run();}
long mod=1000000000+7;
long tsa=Long.MAX_VALUE;
void solve() throws Exception
{
long X=nl();
div(X);
out.println(tsa);
}
long cal(long a,long b, long c)
{
return 2l*(a*b + b*c + c*a);
}
void div(long n)
{
for (long i=1; i*i<=n; i++)
{
if (n%i==0)
{
if (n/i == i)
{
all_div(i, i);
}
else
{
all_div(i, n/i);
all_div(n/i, i);
}
}
}
}
void all_div(long n , long alag)
{
ArrayList<Long> al = new ArrayList<>();
for (long i=1; i*i<=n; i++)
{
if (n%i==0)
{
if (n/i == i)
tsa=min(tsa,cal(i,i,alag));
else
{
tsa=min(tsa,cal(i,n/i,alag));
}
}
}
}
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
private SpaceCharFilter filter;
PrintWriter out;
int min(int... ar){int min=Integer.MAX_VALUE;for(int i:ar)min=Math.min(min, i);return min;}
long min(long... ar){long min=Long.MAX_VALUE;for(long i:ar)min=Math.min(min, i);return min;}
int max(int... ar) {int max=Integer.MIN_VALUE;for(int i:ar)max=Math.max(max, i);return max;}
long max(long... ar) {long max=Long.MIN_VALUE;for(long i:ar)max=Math.max(max, i);return max;}
void reverse(int a[]){for(int i=0;i<a.length>>1;i++){int tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}}
void reverse(long a[]){for(int i=0;i<a.length>>1;i++){long tem=a[i];a[i]=a[a.length-1-i];a[a.length-1-i]=tem;}}
String reverse(String s){StringBuilder sb=new StringBuilder(s);sb.reverse();return sb.toString();}
void shuffle(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<a.length;i++)
al.add(a[i]);
Collections.sort(al);
for(int i=0;i<a.length;i++)
a[i]=al.get(i);
}
long lcm(long a,long b)
{
return (a*b)/(gcd(a,b));
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
long expo(long p,long q)
{
long z = 1;
while (q>0) {
if (q%2 == 1) {
z = (z * p)%mod;
}
p = (p*p)%mod;
q >>= 1;
}
return z;
}
void run()throws Exception
{
in=System.in; out = new PrintWriter(System.out);
solve();
out.flush();
}
private int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
private int ni() throws IOException
{
int c = scan();
while (isSpaceChar(c))
c = scan();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = scan();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = scan();
} while (!isSpaceChar(c));
return res * sgn;
}
private long nl() throws IOException
{
long num = 0;
int b;
boolean minus = false;
while ((b = scan()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = scan();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = scan();
}
}
private double nd() throws IOException{
return Double.parseDouble(ns());
}
private String ns() throws IOException {
int c = scan();
while (isSpaceChar(c))
c = scan();
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c))
res.appendCodePoint(c);
c = scan();
} while (!isSpaceChar(c));
return res.toString();
}
private String nss() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
private char nc() throws IOException
{
int c = scan();
while (isSpaceChar(c))
c = scan();
return (char) c;
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
private boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhiteSpace(c);
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.