Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
For this Question: Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.The first line of the input contains n ( length of num )
The second line contains the array num.
<b>Constraints</b>
1 ≤ nums. length ≤ 100
-100 ≤ nums[i] ≤ 100Print the sorted arraySample Input
6
1 1 2 2 2 3
Sample Output
3 1 1 2 2 2
Explanation: '
3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
String[] str1 = str.split(" ");
int[] arr = new int[n];
HashMap<Integer,Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for(int i = 0; i < n; ++i) {
arr[i] = Integer.parseInt(str1[i]);
}
arr = frequencySort(arr);
for(int i : arr) {
System.out.print(i+" ");
}
}
static Map<Integer,Integer>map;
public static int[] frequencySort(int[] nums)
{
map=new HashMap<Integer,Integer>();
for(int i:nums){
if(map.containsKey(i)){
map.put(i,1+map.get(i));
}else{
map.put(i,1);
}
}
Integer[]arr=new Integer[nums.length];
int k=0;
for(int i:nums){
arr[k++]=i;
}
Arrays.sort(arr,new Comp());
k=0;
for(int i:arr){
nums[k++]=i;
}
return nums;
}
}
class Comp implements Comparator<Integer>{
Map<Integer,Integer>map=Main.map;
public int compare(Integer a,Integer b){
if(map.get(a)>map.get(b))return 1;
else if(map.get(b)>map.get(a))return -1;
else{
if(a>b)return -1;
else if(a<b)return 1;
return 0;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.The first line of the input contains n ( length of num )
The second line contains the array num.
<b>Constraints</b>
1 ≤ nums. length ≤ 100
-100 ≤ nums[i] ≤ 100Print the sorted arraySample Input
6
1 1 2 2 2 3
Sample Output
3 1 1 2 2 2
Explanation: '
3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3., I have written this Solution Code: import numpy as np
from collections import defaultdict
d=defaultdict(list)
d_f=defaultdict (int)
n=int(input())
a=np.array([input().strip().split()],int).flatten()
for i in a:
d_f[i]+=1
for i in d_f:
d[d_f[i]].append(i)
d=sorted(d.items())
for i in d:
i[1].sort(reverse=True)
for i in d:
for j in i[1]:
for _ in range(i[0]):
print(j,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.The first line of the input contains n ( length of num )
The second line contains the array num.
<b>Constraints</b>
1 ≤ nums. length ≤ 100
-100 ≤ nums[i] ≤ 100Print the sorted arraySample Input
6
1 1 2 2 2 3
Sample Output
3 1 1 2 2 2
Explanation: '
3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3., I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-01-10 12:51:16
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
bool static comparator(pair<int, int> m, pair<int, int> n) {
if (m.second == n.second)
return m.first > n.first; // m>n can also be written it will return the same
else
return m.second < n.second;
}
vector<int> frequencySort(vector<int>& nums) {
unordered_map<int, int> mp;
for (auto k : nums)
mp[k]++;
vector<pair<int, int>> v1;
for (auto k : mp)
v1.push_back(k);
sort(v1.begin(), v1.end(), comparator);
vector<int> v;
for (auto k : v1) {
while (k.second != 0) {
v.push_back(k.first);
k.second--;
}
}
return v;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> res = frequencySort(a);
for (auto& it : res) {
cout << it << " ";
}
cout << "\n";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: The `POST` table should contain a field `ID` that can be used as the primary key. So make a table post with id as primary key ( ID INT, USERNAME VARCHAR(24), POST_TITLE VARCHAR(72), POST_DESCRIPTION TEXT, DATETIME_CREATED DATETIME, NUMBER_OF_LIKES INT, PHOTO BLOB )
( USE ONLY UPPERCASE LETTERS FOR CODE)
<schema>[{'name': 'POST', 'columns': [{'name': 'ID', 'type': 'INT'}, {'name': 'USERname', 'type': 'VARCHAR(24)'}, {'name': 'POST_TITLE', 'type': 'VARCHAR (72)'}, {'name': 'POST_DESCRIPTION', 'type': 'TEXT'}, {'name': 'DATETIME_CREATED', 'type': 'DATETIME'}, {'name': 'NUMBER_OF_LIKES', 'type': 'INT'}, {'name': 'PHOTO', 'type': 'BLOB'}]}]</schema>nannannan, I have written this Solution Code: CREATE TABLE POST(
ID INT PRIMARY KEY,
USERNAME VARCHAR(24),
POST_TITLE VARCHAR(72),
POST_DESCRIPTION TEXT,
DATETIME_CREATED DATETIME,
NUMBER_OF_LIKES INT,
PHOTO BLOB
);, In this Programming Language: SQL, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list consisting of N nodes and an integer K, your task is to reverse every alternate K nodes.
In other words , you have to reverse first k nodes , then skip the next k nodes , then reverse next k nodes and so on .
NOTE: if there are not K nodes to reverse then reverse all the nodes left (See example for better understanding)<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>ReverseAlternateK()</b> that takes head node and K as parameter.
Constraints:
1 <=k<=N<=10000Return the head of the modified linked list.Sample Input:-
8 3
1 2 3 4 5 6 7 8
Sample Output:-
3 2 1 4 5 6 8 7
Explanation:
[{1 , 2 ,3 } , {4, 5 , 6} , {7 , 8}]
Reverse 1st segment.
Skip the 2nd segment.
Reverse the 3rd segment. , I have written this Solution Code: public static Node ReverseAlternateK(Node head,int k){
Node current = head;
Node next = null, prev = null;
int count = 0;
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}
if (head != null) {
head.next = current;
}
count = 0;
while (count < k - 1 && current != null) {
current = current.next;
count++;
}
if (current != null) {
current.next = ReverseAlternateK(current.next, k);
}
return prev;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: n = int(input())
arr = list(map(int, input().split()))
l = [0] * (max(arr) + 1)
for i in arr:
l[i] += 1
for i in range(len(l)):
if l[i] > 1:
print(i), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: An array of numbers is given. It has all unique elements except one. Find the only duplicate element in that array having all other unique elements.The first line contains an integer N, the number of elements in the array.
The second line contains N integers.
Constraints:
1 <= N <= 10^5
1 <= Elements of Array <= 10^5Print the single duplicate number in the arrayInput
6
1 2 3 4 4 5
Output
4
Explanation: 4 is repeated in this array
Input:
3
2 1 2
Output:
2, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int m = 100001;
int main(){
int n,k;
cin>>n;
long long a[n],sum=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<0){
a[i]=-a[i];
}
}
sort(a,a+n);
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){cout<<a[i];return 0;}
}
cout<<sum;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list, the task is to move all 0’s to the front of the linked list. The order of all another element except 0 should be same after rearrangement.
Note: Avoid use of any type of Java Collection frameworks.
Note: For custom input/output, enter the list in reverse order, and the output will come in right order.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>moveZeroes()</b> that takes head node as parameter.
Constraints:
1 <= T <= 100
1 <= N <= 100000
0<=Node.data<=100000
Note:- Sum of all test cases doesn't exceed 10^5
Return the head of the modified linked list.Input:
2
10
0 4 0 5 0 2 1 0 1 0
7
1 1 2 3 0 0 0
Output:
0 0 0 0 0 4 5 2 1 1
0 0 0 1 1 2 3
Explanation:
Testcase 1:
Original list was 0->4->0->5->0->2->1->0->1->0->NULL.
After processing list becomes 0->0->0->0->0->4->5->2->1->1->NULL.
Testcase 2:
Original list was 1->1->2->3->0->0->0->NULL.
After processing list becomes 0->0->0->1->1->2->3->NULL., I have written this Solution Code: static public Node moveZeroes(Node head){
ArrayList<Integer> a=new ArrayList<>();
int c=0;
while(head!=null){
if(head.data==0){
c++;
}
else{
a.add(head.data);
}
head=head.next;
}
head=null;
for(int i=a.size()-1;i>=0;i--){
if(head==null){
head=new Node(a.get(i));
}
else{
Node temp=new Node(a.get(i));
temp.next=head;
head=temp;
}
}
while(c-->0){
Node temp=new Node(0);
temp.next=head;
head=temp;
}
return head;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono loves to do shopping. Today, she went to the market where there are N different types of products. She wants to buy exactly K of them at the minimum cost. Although she is super smart, she wants to check your smartness as well. Can you tell her the minimum cost required to buy exactly K products if she has already decided to buy product J?
<b>Note:</b> Tono does not buy the same product twice, and Tono will definitely buy product J (J is the <b>index</b> of the item).The first line of the input contains three integers, N, K, and J, denoting the number of products in the market, the number of products Tono needs to buy, and the product that Tono will definitely buy.
The next line contains N singly spaced integers, the cost of the N products C[1], C[2], ..., C[N].
<b>Constraints:</b>
1 <= N <= 200000
1 <= K <= N
1 <= J <= N
1 <= C[i] <= 1000
Output a single integer, the minimum amount Tono needs to pay.Sample Input 1:
5 3 4
1 2 3 4 5
Sample Output 1:
7
Sample Input 2:
5 1 3
2 4 3 1 1
Sample Output 2:
3
<b>Explanation:</b>
Tono needs to buy exactly 3 products, and she will definitely buy the 4th product. Thus, she will buy the 1st, 2nd, and the 4th product. The total cost she pays is 1+2+4=7.
, I have written this Solution Code: import java.util.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
int n = io.nextInt(), k = io.nextInt(), j = io.nextInt() - 1;
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = io.nextInt();
}
int cost = arr[j];
arr[j] = Integer.MAX_VALUE;
Arrays.sort(arr);
for(int i = 0; i < k - 1; i++) {
cost += arr[i];
}
io.println(cost);
io.close();
}
static IO io = new IO();
static class IO {
private byte[] buf;
private InputStream in;
private PrintWriter pw;
private int total, index;
public IO() {
buf = new byte[1024];
in = System.in;
pw = new PrintWriter(System.out);
}
public int next() 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++];
}
public int nextInt() throws IOException {
int n = next(), integer = 0;
while(isWhiteSpace(n))
n = next();
int neg = 1;
if(n == '-') {
neg = -1;
n = next();
}
while(!isWhiteSpace(n)) {
if(n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = next();
}
else
throw new InputMismatchException();
}
return neg * integer;
}
public int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
public long nextLong() throws IOException {
long integer = 0l;
int n = next();
while(isWhiteSpace(n))
n = next();
int neg = 1;
if(n == '-') {
neg = -1;
n = next();
}
while(!isWhiteSpace(n)) {
if(n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = next();
}
else
throw new InputMismatchException();
}
return neg * integer;
}
public double nextDouble() throws IOException {
double doub = 0;
int n = next();
while(isWhiteSpace(n))
n = next();
int neg = 1;
if(n == '-') {
neg = -1;
n = next();
}
while(!isWhiteSpace(n) && n != '.') {
if(n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = next();
}
else
throw new InputMismatchException();
}
if(n == '.') {
n = next();
double temp = 1;
while(!isWhiteSpace(n)) {
if(n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = next();
}
else
throw new InputMismatchException();
}
}
return doub * neg;
}
public String nextString() throws IOException {
StringBuilder sb = new StringBuilder();
int n = next();
while(isWhiteSpace(n))
n = next();
while(!isWhiteSpace(n)) {
sb.append((char)n);
n = next();
}
return sb.toString();
}
public String nextLine() throws IOException {
int n = next();
while(isWhiteSpace(n))
n = next();
StringBuilder sb = new StringBuilder();
while(!isEndOfLine(n)) {
sb.append((char)n);
n = next();
}
return sb.toString();
}
private boolean isWhiteSpace(int n) {
return n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1;
}
private boolean isEndOfLine(int n) {
return n == '\n' || n == '\r' || n == -1;
}
public void print(Object obj) {
pw.print(obj);
}
public void println(Object... obj) {
if(obj.length == 1)
pw.println(obj[0]);
else {
for(Object o: obj)
pw.print(o + " ");
pw.println();
}
}
public void flush() throws IOException {
pw.flush();
}
public void close() throws IOException {
pw.close();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono loves to do shopping. Today, she went to the market where there are N different types of products. She wants to buy exactly K of them at the minimum cost. Although she is super smart, she wants to check your smartness as well. Can you tell her the minimum cost required to buy exactly K products if she has already decided to buy product J?
<b>Note:</b> Tono does not buy the same product twice, and Tono will definitely buy product J (J is the <b>index</b> of the item).The first line of the input contains three integers, N, K, and J, denoting the number of products in the market, the number of products Tono needs to buy, and the product that Tono will definitely buy.
The next line contains N singly spaced integers, the cost of the N products C[1], C[2], ..., C[N].
<b>Constraints:</b>
1 <= N <= 200000
1 <= K <= N
1 <= J <= N
1 <= C[i] <= 1000
Output a single integer, the minimum amount Tono needs to pay.Sample Input 1:
5 3 4
1 2 3 4 5
Sample Output 1:
7
Sample Input 2:
5 1 3
2 4 3 1 1
Sample Output 2:
3
<b>Explanation:</b>
Tono needs to buy exactly 3 products, and she will definitely buy the 4th product. Thus, she will buy the 1st, 2nd, and the 4th product. The total cost she pays is 1+2+4=7.
, I have written this Solution Code:
a=input().split()
b=input().split()
for j in [a,b]:
for i in range(0,len(j)):
j[i]=int(j[i])
n,k,j=a[0],a[1],a[2]
c_j=b[j-1]
b.sort()
if b[k-1]<=c_j:
b[k-1]=c_j
sum=0
for i in range(0,k):
sum+=b[i]
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Tono loves to do shopping. Today, she went to the market where there are N different types of products. She wants to buy exactly K of them at the minimum cost. Although she is super smart, she wants to check your smartness as well. Can you tell her the minimum cost required to buy exactly K products if she has already decided to buy product J?
<b>Note:</b> Tono does not buy the same product twice, and Tono will definitely buy product J (J is the <b>index</b> of the item).The first line of the input contains three integers, N, K, and J, denoting the number of products in the market, the number of products Tono needs to buy, and the product that Tono will definitely buy.
The next line contains N singly spaced integers, the cost of the N products C[1], C[2], ..., C[N].
<b>Constraints:</b>
1 <= N <= 200000
1 <= K <= N
1 <= J <= N
1 <= C[i] <= 1000
Output a single integer, the minimum amount Tono needs to pay.Sample Input 1:
5 3 4
1 2 3 4 5
Sample Output 1:
7
Sample Input 2:
5 1 3
2 4 3 1 1
Sample Output 2:
3
<b>Explanation:</b>
Tono needs to buy exactly 3 products, and she will definitely buy the 4th product. Thus, she will buy the 1st, 2nd, and the 4th product. The total cost she pays is 1+2+4=7.
, 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, k, j; cin>>n>>k>>j;
vector<int> vect;
int ans = 0;
For(i, 1, n+1){
int a; cin>>a;
if(i!=j)
vect.pb(a);
else
ans += a;
}
sort(all(vect));
for(int i=0; i<k-1; i++){
ans += vect[i];
}
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.readLine();
String[] line = br.readLine().split(" ");
int minIndex = 0;
long minVal = Long.MAX_VALUE;
for (int i=0;i<line.length;++i){
long el = Long.parseLong(line[i]);
if (minVal>el){
minVal = el;
minIndex = i;
}
}
StringBuilder sb = new StringBuilder();
for (int i = minIndex;i< line.length;++i){
sb.append(line[i]+" ");
}
for (int i=0;i<minIndex;++i){
sb.append(line[i]+" ");
}
System.out.print(sb);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: N = int(input())
arr = list(map(int, input().split()))
mi = arr.index(min(arr))
ans = arr[mi:] + arr[:mi]
for e in ans:
print(e, end=' '), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array Arr of length N. Print the lexographically minimum rotation of the array Arr.
All the elements of the array are distinct.First line of input contains a single integer N.
Second line of input contains N integers denoting the array Arr.
Constraints:
1 <= N <= 100000
1 <= Arr[i] <= 1000000000Print the lexographically minimum rotation of the array Arr.Sample Input
5
2 3 1 4 10
Sample Output
1 4 10 2 3, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
int a[n];
for(int i=0;i<n;++i){
cin>>a[i];
}
int mi=0;
for(int i=0;i<n;++i)
if(a[i]<a[mi])
mi=i;
for(int i=0;i<n;++i)
cout<<a[(i+mi)%n]<<" ";
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alexa found two positive integers, a and b. Determine whether the concatenation of a and b in this order is a square number.The input consists of two space separated integers a and b.
<b>Constraints</b>
1 ≤ a, b ≤ 100
a and b are integers.If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.<b>Sample Input 1</b>
1 44
<b>Sample Output 1</b>
Yes
<b>Sample Input 2</b>
100 100
<b>Sample Output 2</b>
No
<b>Sample Input 3</b>
12 10
<b>Sample Output 3</b>
No, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
string s,ss;
cin>>s>>ss;
s+=ss;
int n=stoi(s);
for(int i=1;i*i<=n;i++){
if(i*i==n){
cout<<"Yes"<<endl;
return 0;
}
}
cout<<"No"<<endl;
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int toDigit(char ch)
{
return (ch - '0');
}
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = s.length();
long prev = toDigit(s.charAt(0));
long res = prev;
long current = 0L;
for (int i = 1; i < n; i++){
long numi = toDigit(s.charAt(i));
current = ((i + 1) * numi + 10 * prev)%1000000007;
res =(res+current)%1000000007;
prev = current;
}
System.out.print(res);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, I have written this Solution Code: s = input().strip()
ln = len(s)
prev = int(s[0])
sm = prev
for i in range(1,ln):
nxt = (i+1)*int(s[i]) + 10 * prev
sm += nxt%1000000007
prev = nxt%1000000007
print(sm%1000000007), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer S represented as a string, the task is to get the sum of all possible sub-strings of this string.The only line of input contains a single integers S represented as a string.
1 <= len(S) <= 1000000Print the required sum modulo (10^9 + 7)Sample Input 1:
1234
Sample Output 1:
1670
Explanation:
Sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234
= 1670
Sample Input 2:
421
Sample Output 2:
491
Explanation:
Sum = 4 + 2 + 1 + 42 + 21 + 421
= 491, I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
void solve(){
string s;
cin >> s;
int ans = 0, suf = 0;
for(int i = 0; i < (int)s.length(); i++){
suf = (suf*10 + (s[i]-'0')*(i+1)) % mod;
ans = (ans + suf) % mod;
}
cout << ans << endl;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] val = new int[n];
for(int i=0; i<n; i++){
val[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] freq = new int[n];
for(int i=0; i<n; i++){
freq[i] = Integer.parseInt(st.nextToken());
}
int k = Integer.parseInt(br.readLine());
for (int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
if (val[j] < val[i]) {
int temp = val[i];
val[i] = val[j];
val[j] = temp;
int temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;
}
}
}
int element=0;
for(int i=0; i<n; i++){
for(int j=0; j<freq[i]; j++){
element++;
int value = val[i];
if(element==k){
System.out.print(value);
break;
}
}
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: def myFun():
n = int(input())
arr1 = list(map(int,input().strip().split()))
arr2 = list(map(int,input().strip().split()))
k = int(input())
arr = []
for i in range(n):
arr.append((arr1[i], arr2[i]))
arr.sort()
c = 0
for i in arr:
k -= i[1]
if k <= 0:
print(i[0])
return
myFun()
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given two arrays - value and frequency both containing N elements.
There is also a third array C which is currently empty. Then you perform N insertion operation in the array. For ith operation you insert value[i] to the end of the array frequency[i] number of times.
Finally you have to tell the kth smallest element in the array C.First line of input contains N.
Second line contains N integers denoting array - value
Third line contains N integers denoting array - frequency
Fourth line contains single integer K.
Constraints
1 <= N, value[i], frequency[i] <= 100000
1 <= k <= frequency[1] + frequency[2] +frequency[3] +........ + frequency[N]
Output a single integer which is the kth smallest element of the array C.Sample input 1
5
1 2 3 4 5
1 1 1 2 2
3
Sample output 1
3
Explanation 1:
Array C constructed is 1 2 3 4 4 5 5
Third smallest element is 3
Sample input 2
3
2 1 3
3 3 2
2
sample output 2
1
Explanation 2:
Array C constructed is 2 2 2 1 1 1 3 3
Second smallest element is 1, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define inf 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int N;ll K;
cin>>N;
int c=0;
pair<int, ll> A[N];
for(int i=0;i<N;++i){
cin >> A[i].first ;
}
for(int i=0;i<N;++i){
cin >> A[i].second ;
}
cin>>K;
sort(A, A+N);
for(int i=0;i<N;++i){
K -= A[i].second;
if(K <= 0){
cout << A[i].first << endl;;
break;
}
}
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., 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 t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
cout << a[i] << " ";
cout << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: // arr is unsorted array
// n is the number of elements in the array
function bubbleSort(arr, n) {
// write code here
// do not console.log the answer
// return sorted array
return arr.sort((a, b) => a - b)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int temp;
for(int i=1;i<n;i++){
if(a[i]<a[i-1]){
for(int j=i;j>0;j--){
if(a[j]<a[j-1]){
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
else{
break;
}
}
}
}
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[], of size N containing positive integers. You need to print the elements of an array in increasing order.The first line of the input denotes the number of test cases 'T'. The first line of the test case is the size of the array and the second line consists of array elements.
For Python Users just complete the given function.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 1000For each testcase print the sorted array in a new line.Input:
2
5
4 1 3 9 7
10
10 9 8 7 6 5 4 3 2 1
Output:
1 3 4 7 9
1 2 3 4 5 6 7 8 9 10
<b>Explanation:</b>
Testcase 1: 1 3 4 7 9 are in sorted form.
Testcase 2: For the given input, 1 2 3 4 5 6 7 8 9 10 are in sorted form., I have written this Solution Code: def bubbleSort(arr):
arr.sort()
return arr
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc=new Reader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int count=search(a,0,n-1);
System.out.println(count);
}
}
public static int search(int[] a,int l,int h){
while(l<=h){
int mid=l+(h-l)/2;
if ((mid==h||a[mid+1]==0)&&(a[mid]==1))
return mid+1;
if (a[mid]==1)
l=mid+1;
else h=mid-1;
}
return 0;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n; cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = n+1;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] == 1)
l = m;
else
h = m;
}
cout << l << endl;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a binary sorted non-increasing array arr of size <b>N</b>. You need to print the count of <b>1's</b> in the binary array.
Try to solve the problem using binary searchThe input line contains T, denotes the number of testcases.
Each test case contains two lines. The first line contains N (size of binary array). The second line contains N elements of binary array separated by space.
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^6
arr[i] = 0,1
<b>Sum of N over all testcases does not exceed 10^6</b>For each testcase in new line, print the count 1's in binary array.Input:
2
8
1 1 1 1 1 0 0 0
8
1 1 0 0 0 0 0 0
Output:
5
2
Explanation:
Testcase 1: Number of 1's in given binary array : 1 1 1 1 1 0 0 0 is 5.
Testcase 2: Number of 1's in given binary array : 1 1 0 0 0 0 0 0 is 2., I have written this Solution Code: c=int(input())
for x in range(c):
size=int(input())
s=input()
print(s.count('1')), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., 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);
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int k = Integer.parseInt(s[1]);
String str[] = br.readLine().split(" ");
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
System.out.print(minDivisor(arr,n,k));
}
static int minDivisor(int arr[],int N, int limit) {
int low = 0, high = 1000000000;
while (low < high)
{
int mid = (low + high) / 2;
int sum = 0;
for(int i = 0; i < N; i++)
{
sum += Math.ceil((double) arr[i] / (double) mid);
}
if(sum <= limit){
high = mid;
}
else{
low = mid + 1;
}
}
return low;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., I have written this Solution Code: from math import ceil
def kSum(arr, n, k):
low = 0
high = 10 ** 9
while (low < high):
mid = (low + high) // 2
sum = 0
for i in range(int(n)):
sum += ceil( int(arr[i]) / mid)
if (sum <= int(k)):
high = mid
else:
low = mid + 1
return low
n, k =input().split()
arr = input().split()
print( kSum(arr, n, k) ), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., I have written this Solution Code: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N], n, k;
bool f(int x){
int sum = 0;
for(int i = 1; i <= n; i++)
sum += (a[i]-1)/x + 1;
return (sum <= k);
}
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++)
cin >> a[i];
int l = 0, h = N;
while(l+1 < h){
int m = (l + h) >> 1;
if(f(m))
h = m;
else
l = m;
}
cout << h;
}
void testcases(){
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
signed main() {
IOS;
clock_t start = clock();
testcases();
cerr << (double)(clock() - start)*1000/CLOCKS_PER_SEC << " ms" << endl;
return 0;
} , In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int size = Integer.parseInt(line);
String str = br.readLine();
String[] strArray = str.split(" ");
int[] array = new int[size];
for (int i = -0; i < size; i++) {
array[i] = Integer.parseInt(strArray[i]);
}
int count = largestSubarray(array,size);
System.out.println(count);
}
static int largestSubarray(int[] array,int size){
int count = -1;
int sum = 0;
Map<Integer,Integer> mymap = new HashMap<>();
mymap.put(0,-1);
for(int i=0; i<array.length; i++){
sum += array[i];
if(mymap.containsKey(sum)){
count = Math.max(count, i-mymap.get(sum));
}
else{
mymap.put(sum,i);
}
}
if(count > 0){
return count;
}
else{
return -1;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: def maxLen(arr,n,k):
mydict = dict()
sum = 0
maxLen = 0
for i in range(n):
sum += arr[i]
if (sum == k):
maxLen = i + 1
elif (sum - k) in mydict:
maxLen = max(maxLen, i - mydict[sum - k])
if sum not in mydict:
mydict[sum] = i
return maxLen
n=int(input())
arr=input().split()
for i in range(0,n):
arr[i]=int(arr[i])
max_len=maxLen(arr,n,0)
if(max_len==0):
print ("-1")
else:
print (max_len), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array <b>A[]</b>, of length <b>N</b> containing zero , positive and negative integers.
You need to find the length of the largest subarray whose sum of elements is <b>0</b>.The first line contains N denoting the size of the array A. Then in the next line contains N space-separated values of the array A.
<b>Constraints:-</b>
1 <= N <= 1e5
-1e6 <= A[i] <= 1e6Print the length of the largest subarray which has sum 0, If no subarray exist print -1.Sample Input:-
8
15 -2 2 -8 1 7 10 23
Sample Output:-
5
Explanation:-
-2 2 -8 1 7 is the required subarray
Sample Input:-
5
1 2 1 2 3
Sample Output:-
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unordered_map<long long,int> m;
int n,k;
cin>>n;
long a[n];
int ans=-1;
for(int i=0;i<n;i++){cin>>a[i];if(a[i]==0){ans=1;}}
long long sum=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(sum==0){ans=max(i+1,ans);}
if(m.find(sum)==m.end()){m[sum]=i;}
else{
ans=max(i-m[sum],ans);
}
}
cout<<ans;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph G (V, E) and two vertices v1 and v2 (as integers), check if there exists any path between them or not. Print true or false.
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
E is the number of edges present in graph G.Line 1: Two Integers V and E (separated by space)
Next E lines: Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space)
Line (E+2): Two integers v1 and v2 (separated by space)
Constraints :
2 <= V <= 1000
1 <= E <= 1000
0 <= v1, v2 <= V-1For each testcase in new line, you need to print true or false.Sample Input 1 :
4 4
0 1
0 3
1 2
2 3
1 3
Sample Output 1 :
true
Explanation:
There are multiple path exists between 1 and 3. One of them is as such: 1 -> 2 -> 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = br.readLine().trim().split(" ");
int v = Integer.parseInt(str[0]);
int e = Integer.parseInt(str[1]);
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for(int i=0;i<v;i++)
{
list.add(i, new ArrayList<>());
}
for(int i=0;i<e;i++)
{
String st[] = br.readLine().trim().split(" ");
int e1 = Integer.parseInt(st[0]);
int e2 = Integer.parseInt(st[1]);
list.get(e1).add(e2);
list.get(e2).add(e1);
}
String strg[] = br.readLine().trim().split(" ");
int v1 = Integer.parseInt(strg[0]);
int v2 = Integer.parseInt(strg[1]);
System.out.print(bfs(list, v1, v2, v));
}
public static boolean bfs(ArrayList<ArrayList<Integer>> list, int v1,int v2, int v)
{
boolean visit[] = new boolean[v];
LinkedList<Integer> queue = new LinkedList<>();
visit[v1] = true;
queue.add(v1);
while(!queue.isEmpty())
{
int ans = queue.poll();
Iterator<Integer> i = list.get(ans).listIterator();
while(i.hasNext())
{
int n = i.next();
if(n==v2)
{
return true;
}
if(!visit[n])
{
visit[n] = true;
queue.add(n);
}
}
}
return false;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph G (V, E) and two vertices v1 and v2 (as integers), check if there exists any path between them or not. Print true or false.
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
E is the number of edges present in graph G.Line 1: Two Integers V and E (separated by space)
Next E lines: Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space)
Line (E+2): Two integers v1 and v2 (separated by space)
Constraints :
2 <= V <= 1000
1 <= E <= 1000
0 <= v1, v2 <= V-1For each testcase in new line, you need to print true or false.Sample Input 1 :
4 4
0 1
0 3
1 2
2 3
1 3
Sample Output 1 :
true
Explanation:
There are multiple path exists between 1 and 3. One of them is as such: 1 -> 2 -> 3., I have written this Solution Code: from queue import Queue
n,e=[int(x) for x in input().split()]
adj=dict()
for i in range(n):
adj[i]=[]
for i in range(e):
u,v=[int(x) for x in input().split()]
adj[u].append(v)
adj[v].append(u)
src,des=[int(x) for x in input().split()]
stack=[src]
vis=[False]*(n)
vis[src]=True
while len(stack) != 0:
u=stack.pop(-1)
for v in adj[u]:
if(vis[v]==False):
stack.append(v)
vis[v]=True
ans=0
for x in vis:
if x==False:
ans += 1
if(vis[des]==True):
print("true")
else:
print("false"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an undirected graph G (V, E) and two vertices v1 and v2 (as integers), check if there exists any path between them or not. Print true or false.
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
E is the number of edges present in graph G.Line 1: Two Integers V and E (separated by space)
Next E lines: Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space)
Line (E+2): Two integers v1 and v2 (separated by space)
Constraints :
2 <= V <= 1000
1 <= E <= 1000
0 <= v1, v2 <= V-1For each testcase in new line, you need to print true or false.Sample Input 1 :
4 4
0 1
0 3
1 2
2 3
1 3
Sample Output 1 :
true
Explanation:
There are multiple path exists between 1 and 3. One of them is as such: 1 -> 2 -> 3., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int vis[sz];
vector<int> NEB[sz];
void dfs(int x)
{ vis[x]=1;
for(auto it:NEB[x])
{
if(vis[it]==0){
dfs(it);
}
}
}
signed main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
NEB[a].pu(b);
NEB[b].pu(a);
}
int x,y;
cin>>x>>y;
dfs(x);
if(vis[y]==1)cout<<"true";
else cout<<"false";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pairs and vectors can be used together to achieve some amazing results. Here we will learn to use a vector that holds pairs.
You are given a vector V of size N. The vector hold pair of integers. Example V={(1,2),(3,4)...}. Now, you need to sum the second elements.First line contains N denoting the size of the array. The second line contains 2*N elements where the (2i - 1)'th and (2i)'th element represent the i'th pair.
Constraints:
1 <= N <= 10^5
0 <= Vi <= 10^5For each testcase, in a new line, print the required output.Input:
5
1 2 3 4 5 6 7 8 9 10
Output:
30
Explanation:
Sum = 10+8+6+4+2 = 30, I have written this Solution Code: input()
print(sum([int(i) for (j, i) in enumerate(input().split()) if j % 2 == 1])), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pairs and vectors can be used together to achieve some amazing results. Here we will learn to use a vector that holds pairs.
You are given a vector V of size N. The vector hold pair of integers. Example V={(1,2),(3,4)...}. Now, you need to sum the second elements.First line contains N denoting the size of the array. The second line contains 2*N elements where the (2i - 1)'th and (2i)'th element represent the i'th pair.
Constraints:
1 <= N <= 10^5
0 <= Vi <= 10^5For each testcase, in a new line, print the required output.Input:
5
1 2 3 4 5 6 7 8 9 10
Output:
30
Explanation:
Sum = 10+8+6+4+2 = 30, 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));
int a=Integer.parseInt(reader.readLine())*2;
long sum=0;
String arr[]=reader.readLine().split(" ");
reader.close();
for(int i=0;i<a;i++){
if(i%2==1)
{
sum=sum+Integer.parseInt(arr[i]);
}
}
System.out.println(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Pairs and vectors can be used together to achieve some amazing results. Here we will learn to use a vector that holds pairs.
You are given a vector V of size N. The vector hold pair of integers. Example V={(1,2),(3,4)...}. Now, you need to sum the second elements.First line contains N denoting the size of the array. The second line contains 2*N elements where the (2i - 1)'th and (2i)'th element represent the i'th pair.
Constraints:
1 <= N <= 10^5
0 <= Vi <= 10^5For each testcase, in a new line, print the required output.Input:
5
1 2 3 4 5 6 7 8 9 10
Output:
30
Explanation:
Sum = 10+8+6+4+2 = 30, 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 = 1e3 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n, ans = 0; cin >> n;
for(int i = 1; i <= 2*n; i++){
int p; cin >> p;
if(i%2 == 0)
ans += p;
}
cout << ans;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a Binary Tree, your task is to compute the sum of all leaf nodes in the tree.
Note :- All the nodes in the tree are distinct .<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>sumOfLeaf()</b> that takes "root" node as parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
Sum of "N" over all test cases does not exceed 2*10^5
For <b>Custom Input:</b>
First line of input should contains the number of test cases T. For each test case, there will be two lines of input.
First line contains number of nodes N and the required sum X. Second line will be a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
<b>Note:</b> If a node has been declared Null using 'N', no information about its children will be given further in the array.Return the sum of all the leaf nodes of the binary tree.Sample Input:
2
3
10 8 34
2
48 36
Sample Output:
42
36, I have written this Solution Code:
static class Res
{
int sum = 0;
}
public static int sumOfLeaf(Node root)
{
Res r = new Res();
leafSum(root, r);
return r.sum;
}
public static void leafSum(Node root, Res r)
{
if(root == null)
return;
if(root.left == null && root.right == null)
r.sum += root.data;
leafSum(root.left, r);
leafSum(root.right, r);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code: static int numberOfDiagonal(int N){
if(N<=3){return 0;}
return (N*(N-3))/2;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code:
int numberOfDiagonals(int n){
if(n<=3){return 0;}
return (n*(n-3))/2;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code: def numberOfDiagonals(n):
if n <=3:
return 0
return (n*(n-3))//2
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a number N. Write a program to the find number of diagonals possible in N sided convex polygon.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>numberOfDiagonal()</b> that takes the integer N parameter.
Constraints:
1 <= T <= 100
1 <= N <= 10^3Return number of diagonals possible in N sided convex polygon.Sample Input:
3
3
5
6
Sample Output:
0
5
9
Explanation:
For test case 2: The number of diagonals of 5 sided polygon: 5., I have written this Solution Code:
int numberOfDiagonals(int n){
if(n<=3){return 0;}
return (n*(n-3))/2;
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given 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<sup>3</sup> ≤ A ≤ 10<sup>3</sup>
-10<sup>3</sup> ≤ B ≤ 10<sup>3</sup>
1 ≤ N ≤ 10<sup>4</sup>Return the Nth term of AP series.Sample Input 1:
2 3 4
Sample Output 1:
5
Sample Input 2:
1 2 10
Sample output 2:
10, I have written this Solution Code: class Solution {
public static int NthAP(int a, int b, int n){
return a+(n-1)*(b-a);
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Newton is given an array S of N integers S<sub>1</sub>, S<sub>2</sub>,. . S<sub>N</sub>. He needs to perform some function on every subarray of the given array S whose starting element is the first element of the given array S.
The function is as follows:
Given an array A of size N, for each i from 1 to N, add the current maximum of the array A to A<sub>i</sub>. Then calculate the sum of the resultant array.
Perform this function on all the subarrays whose starting element is the starting element of the given array S.The first line of the input contains a single integer N
The next line contains N different integers S<sub>1</sub>, S<sub>2</sub>,. . S<sub>N</sub>
<b>Constraints:</b>
1 <= N <= 4 x 10<sup>5</sup>
1 <= S<sub>i</sub> <= 2 x 10<sup>7</sup>Print the answer for each subarray in a single line<b>Sample Input 1:</b>
3
2 4 6
<b>Sample Output 1:</b>
4
16
38
<b>Explanation:</b>
Lets apply the function to the subarray [2, 4]:
1) For index 1, the current maximum of the array is 4, So the array changes to [6, 4]
2) For index 2, the current maximum of the array is 6, So the array changes to [6, 10]
Now the sum of the resultant array is 6+10 = 16
Similarly the output for the subarray [2] is 4 and for the subarray [2, 4, 6] is 38
<b>Sample Input 2:</b>
5
1 2 3 4 5
<b>Sample Output 2:</b>
2
8
19
36
60, I have written this Solution Code: // #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define endl "\n"
#define EPS 1e-9
#define MOD 1000000007
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (x).size()
// #define forf(t,i,n) for(t i=0;i<n;i++)
// #define forr(t,i,n) for(t i=n-1;i>=0;i--)
#define forf(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i--)
#define F0(i, n) for(ll i=0; i<n; i++)
#define F1(i, n) for(ll i=1; i<=n; i++)
#define FOR(i, s, e) for(ll i=s; i<=e; i++)
#define ceach(a,x) for(const auto &a: x)
#define each(a,x) for(auto &a: x)
#define print(x) for(const auto &e: (x)) { cout<<e<<" "; } cout<<endl
#define daalo(a) each(x, a) { cin>>x; }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef unordered_map<int, int> umi;
typedef unordered_map<long long, long long> umll;
typedef unordered_map<char, int> umci;
typedef unordered_map<char, long long> umcll;
typedef unordered_map<string, int> umsi;
typedef unordered_map<string, long long> umsll;
#ifndef ONLINE_JUDGE
#define deb(x ...) cerr << #x << ": "; _print(x); cerr << endl;
#define pt(x) cerr << "\n---------Testcase " << x << "---------\n" << endl;
// #define deb(x ...) ;
// #define pt(x) ;
#else
#define deb(x ...) ;
#define pt(x) ;
#endif
void _print(unsigned short t){ cerr << t; }
void _print(short t){ cerr << t; }
void _print(unsigned int t){ cerr << t; }
void _print(int t){ cerr << t; }
void _print(unsigned long t){ cerr << t; }
void _print(long t){ cerr << t; }
void _print(unsigned long long t){ cerr << t; }
void _print(long long t){ cerr << t; }
void _print(float t){ cerr << t; }
void _print(double t){ cerr << t; }
void _print(long double t){ cerr << t; }
void _print(unsigned char t){ cerr << t; }
void _print(char t){ cerr << t; }
void _print(string t){ cerr << t; }
template<typename A> void _print(vector<A> v);
template<typename A, typename B> void _print(pair<A, B> p);
template<typename A> void _print(set<A> s);
template<typename A, typename B> void _print(map<A, B> mp);
template<typename A> void _print(multiset<A> s);
template<typename A, typename B> void _print(multimap<A, B> mp);
template<typename A> void _print(unordered_set<A> s);
template<typename A, typename B> void _print(unordered_map<A, B> mp);
template<typename A> void _print(unordered_multiset<A> s);
template<typename A, typename B> void _print(unordered_multimap<A, B> mp);
template<typename A> void _print(stack<A> s);
template<typename A> void _print(queue<A> q);
template<typename A> void _print(priority_queue<A> pq);
template<typename A> void _print(priority_queue<A, vector<A>, greater<A>> pq);
template<typename A> void _print(vector<A> v){ if(!v.empty()){ cerr << "["; for(auto it=v.begin(); it!=(v.end()-1); it++){ _print(*it); cerr <<","; } _print(*(v.end()-1)); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A, typename B> void _print(pair<A, B> p){ cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; }
template<typename A> void _print(set<A> s){ if(!s.empty()){ cerr << "{"; for(auto it=s.begin(), lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(s.rbegin())); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(map<A, B> mp){ if(!mp.empty()){ cerr << "["; for(auto it=mp.begin(), lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(mp.rbegin())); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(multiset<A> s){ if(!s.empty()){ cerr << "{"; for(auto it=s.begin(), lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(s.rbegin())); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(multimap<A, B> mp){ if(!mp.empty()){ cerr << "["; for(auto it=mp.begin(), lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*(mp.rbegin())); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(unordered_set<A> s){ if(!s.empty()){ cerr << "{"; auto it = s.begin(); for(auto lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(unordered_map<A, B> mp){ if(!mp.empty()){ cerr << "["; auto it = mp.begin(); for(auto lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(unordered_multiset<A> s){ if(!s.empty()){ cerr << "{"; auto it=s.begin(); for(auto lit=next(it); lit!=(s.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "}"; } else{ cerr << "{}"; } }
template<typename A, typename B> void _print(unordered_multimap<A, B> mp){ if(!mp.empty()){ cerr << "["; auto it=mp.begin(); for(auto lit=next(it); lit!=(mp.end()); it++, lit++){ _print(*it); cerr <<","; } _print(*it); cerr << "]"; } else{ cerr << "[]"; } }
template<typename A> void _print(stack<A> s){ if(!s.empty()){ stack<A> t; cerr << "T["; while(s.size() != 1){ _print(s.top()); cerr << ","; t.push(s.top()); s.pop(); } _print(s.top()); cerr << "]B"; t.push(s.top()); s.pop(); while(!t.empty()){ s.push(t.top()); t.pop(); } } else{ cerr << "T[]B"; } }
template<typename A> void _print(queue<A> q){ if(!q.empty()){ queue<A> t; cerr << "F["; while(q.size() != 1){ _print(q.front()); cerr << ","; t.push(q.front()); q.pop(); } _print(q.front()); cerr << "]B"; t.push(q.front()); q.pop(); while(!t.empty()){ q.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename A> void _print(priority_queue<A> pq){ if(!pq.empty()){ queue<A> t; cerr << "T["; while(pq.size() != 1){ _print(pq.top()); cerr << ","; t.push(pq.top()); pq.pop(); } _print(pq.top()); cerr << "]B"; t.push(pq.top()); pq.pop(); while(!t.empty()){ pq.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename A> void _print(priority_queue<A, vector<A>, greater<A>> pq){ if(!pq.empty()){ queue<A> t; cerr << "T["; while(pq.size() != 1){ _print(pq.top()); cerr << ","; t.push(pq.top()); pq.pop(); } _print(pq.top()); cerr << "]B"; t.push(pq.top()); pq.pop(); while(!t.empty()){ pq.push(t.front()); t.pop(); } } else{ cerr << "F[]B"; } }
template<typename T, typename... V> void _print(T t, V... v) {_print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_set_dec = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using ordered_multiset_dec = tree<T, null_type, greater_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = mod_mul(res , a, mod); a = mod_mul(a , a ,mod); b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
bool revsort(ll a, ll b) {return a > b;}
void swap(int &x, int &y) {int temp = x; x = y; y = temp;}
ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
template<typename T>
T gcd(T a, T b){
if(b == 0)
return a;
return(gcd<T>(b, a%b));
}
template<typename T>
T lcm(T a, T b){
return (a / gcd<T>(a, b)) * b;
}
template<typename T>
void swap_(T &a, T &b){
a = a^b;
b = b^a;
a = a^b;
}
template<typename T>
T modpow(T a, T b, T m){
if(b == 0){
return 1;
}
T c = modpow(a, b/2, m);
c = (c * c)%m;
if(b%2 == 1){
c = (c * a)%m;
}
return c;
}
template<typename T>
vector<T> makeUnique(vector<T> &vec){
vector<T> temp;
unordered_map<T, long long> mp;
for(const auto &e: vec){
if(mp[e]++ == 0){
temp.push_back(e);
}
}
return temp;
}
vector<long long> primeFactorization(long long n) {
vector<long long> factorization;
for (int d : {2, 3, 5}) {
while (n % d == 0) {
factorization.push_back(d);
n /= d;
}
}
static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6};
int i = 0;
for (long long d = 7; d * d <= n; d += increments[i++]) {
while (n % d == 0) {
factorization.push_back(d);
n /= d;
}
if (i == 8)
i = 0;
}
if (n > 1)
factorization.push_back(n);
return factorization;
}
vector<long long> divisors(long long n) {
vector<long long> divisors;
for(long long i = 1; i * i <= n; i++){
if(n % i == 0){
divisors.push_back(i);
if(n/i != i){
divisors.push_back(n/i);
}
}
}
return divisors;
}
vector<bool> seive(long long n){
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (long long i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (long long j = i * i; j <= n; j += i)
is_prime[j] = false;
}
}
return is_prime;
}
// ------------ Segment Tree --------------
void segBuild(ll ind, ll l, ll r, vll &arr, vll &segtree){
if(l == r){
segtree[ind] = arr[l];
return;
}
ll m = (l+r)/2;
segBuild(2*ind, l, m, arr, segtree);
segBuild(2*ind+1, m+1, r, arr, segtree);
segtree[ind] = segtree[ind*2]+segtree[ind*2+1];
}
ll segSum(ll ind, ll tl, ll tr, ll l, ll r, vll &segtree){
if(l > r || tr < l || tl > r){
return 0;
}
if(tl >= l && tr <= r){
return segtree[ind];
}
ll m = (tl+tr)/2;
ll left = segSum(2*ind, tl, m, l, r, segtree);
ll right = segSum(2*ind+1, m+1, tr, l, r, segtree);
return left+right;
}
void segUpdate(ll ind, ll l, ll r, ll ind_val, ll val, vll &segtree){
if(l == r){
segtree[ind] = val;
return;
}
ll m = (l+r)/2;
if(ind_val <= m){
segUpdate(2*ind, l, m, ind_val, val, segtree);
}
else{
segUpdate(2*ind+1, m+1, r, ind_val, val, segtree);
}
segtree[ind] = segtree[ind*2]+segtree[ind*2+1];
}
// -----------------------------------
template<typename T>
string bitRep(T num, T size){
if(size == 8) return bitset<8>(num).to_string();
if(size == 16) return bitset<16>(num).to_string();
if(size == 32) return bitset<32>(num).to_string();
if(size == 64) return bitset<64>(num).to_string();
}
/* ----------STRING AND INTEGER CONVERSIONS---------- */
// 1) number to string -> to_string(num)
// 2) string to int -> stoi(str)
// 3) string to long long -> stoll(str)
// 4) string to decimal -> stod(str)
// 5) string to long decimal -> stold(str)
/* ----------Decimal Precision---------- */
// cout<<fixed<<setprecision(n) -> to fix precision to n decimal places.
// cout<<setprecision(n) -> without fixing
/* ----------Policy Bases Data Structures---------- */
// pbds<ll> s; (almost same as set)
// s.find_by_order(i) 0<=i<n returns iterator to ith element (0 if i>=n)
// s.order_of_key(e) returns elements strictly less than the given element e (need not be present)
/* ------------------Binary Search------------------ */
// 1) Lower Bound -> returns iterator to the first element greater than or equal to the given element or returns end() if no such element exists
// 2) Upper Bound -> returns iterator to the first element greater than the given element or returns end() if no such element exists
/* --------------Builtin Bit Functions-------------- */
// 1) __builtin_clz(x) -> returns the number of zeros at the beginning in the bit representaton of x.
// 2) __builtin_ctz(x) -> returns the number of zeros at the end in the bit representaton of x.
// 3) __builtin_popcount(x) -> returns the number of ones in the bit representaton of x.
// 4) __builtin_parity(x) -> returns the parity of the number of ones in the bit representaton of x.
/* ----------------------- Bitset ----------------------- */
// 1) Must have a constant size of bitset (not variable)
// 2) bitset<size> set;
// 3) Indexing starts from right
// 4) bitset<size> set; bitset<size> set(20); bitset<size> set(string("110011"));
// 5) set.to_string() -> return string of the binary representation
void solve(){
ll n; cin>>n;
vll a(n), t(n);
F0(i, n) cin>>a[i], t[i] = a[i];
F1(i, n-1) t[i] += t[i-1];
ll mx = -1, sum = 0;
F0(i, n) mx = max(mx, a[i]), sum += t[i], cout<<sum+(i+1)*mx<<endl;
}
int main(){
// cfh - ctrl+alt+b
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// freopen("error.txt", "w", stderr);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t=1;
// cin >> t;
for(ll i=1; i<=t; i++){
pt(i);
solve();
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7An array containing numbers.Print true if such triplet exists summing to 7 else print falseSample input:-
[2, 1, 5, 1, 0]
[1, 6]
Sample output:-
true
false
Explanation:-
1+5+1 = 7
no 3 consecutive numbers so false, I have written this Solution Code: function lucky_sevens(arr) {
// if less than 3 elements then this challenge is not possible
if (arr.length < 3) {
console.log(false)
return;
}
// because we know there are at least 3 elements we can
// start the loop at the 3rd element in the array (i=2)
// and check it along with the two previous elements (i-1) and (i-2)
for (let i = 2; i < arr.length; i++) {
if (arr[i] + arr[i-1] + arr[i-2] === 7) {
console.log(true)
return;
}
}
// if loop is finished and no elements summed to 7
console.log(false)
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a linked list consisting of N nodes, your task is to delete every kth Node from the circular linked list until only one node is left. Also, print the intermediate lists
<b>Note:
Sample Input and Output just show how a linked list will look depending on the questions. Do not copy-paste as it is in custom input</b><b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>deleteK()</b> that takes head node of circular linked list and the integer K as parameter.
Constraints:
1 <= K <= N <= 500
1 <= Node. data<= 1000Print the intermediate nodes until one node is left as shown in example.Sample Input:-
4 2
1 2 3 4
Sample Output:-
1->2->3->4->1
1->2->4->1
2->4->2
2->2
Sample Input:-
9 4
1 2 3 4 5 6 7 8 9
Sample Output:-
1->2->3->4->5->6->7->8->9->1
1->2->3->4->6->7->8->9->1
1->2->3->4->6->7->8->1
1->2->3->6->7->8->1
2->3->6->7->8->2
2->3->6->8->2
2->3->8->2
2->3->2
2->2, I have written this Solution Code: static void printList(Node head)
{
if (head == null)
return;
Node temp = head;
do
{
System.out.print( temp.data + "->");
temp = temp.next;
}
while (temp != head);
System.out.println(head.data );
}
/*Function to delete every kth Node*/
static Node deleteK(Node head_ref, int k)
{
Node head = head_ref;
// If list is empty, simply return.
if (head == null)
return null;
// take two pointers - current and previous
Node curr = head, prev=null;
while (true)
{
// Check if Node is the only Node\
// If yes, we reached the goal, therefore
// return.
if (curr.next == head && curr == head)
break;
// Print intermediate list.
printList(head);
// If more than one Node present in the list,
// Make previous pointer point to current
// Iterate current pointer k times,
// i.e. current Node is to be deleted.
for (int i = 0; i < k; i++)
{
prev = curr;
curr = curr.next;
}
// If Node to be deleted is head
if (curr == head)
{
prev = head;
while (prev.next != head)
prev = prev.next;
head = curr.next;
prev.next = head;
head_ref = head;
}
// If Node to be deleted is last Node.
else if (curr.next == head)
{
prev.next = head;
}
else
{
prev.next = curr.next;
}
}
return head;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Modify the <code>takeMultipleNumbersAndAdd </code> such that it can take any number of arguments
and return its sum.
This is JS only question.Function should be able to take any number of argsSum of the numberstakeMultipleNumbersAndAdd(1, 2, 2) should return 5 because 1 + 2 + 2
takeMultipleNumbersAndAdd(-1, 2, -1, 5) should return 5, I have written this Solution Code:
function takeMultipleNumbersAndAdd (...nums){
// write your code here
return nums.reduce((prev,cur)=>prev+cur,0)
// return the output using return keyword
// do not console.log it
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, 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[] arr1=new int[5];
for(int i=0;i< arr1.length;i++)
arr1[i]=sc.nextInt();
Main m=new Main();
System.out.println(m.sumMaxMin(arr1));
}
public static int sumMaxMin(int []array){
int max =array[0];
int min=array[0];
int sum1=0;
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
sum1= max +(min);
return sum1;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Implement the function <code>sumMaxMin</code>, which should take 5 numbers as input arguments.
The function should return the sum of max and min elements of those 5 numbers (Use JS In built functions)Function will take 5 arguments which will be numbers.Function will return a number which is the sum of min and max element of those 5 arguments.console. log(sumMaxMin(100, 100, -200, 300, 0)) // prints 100 because 300+(-200) = 300-200
console. log(sumMaxMin(1, 3, 2, 4, 5)) // prints 6 because 1+5
console. log(sumMaxMin(-1000, -2000, -10, -120, -60)) // prints -2010 because -2000 min and -10 max sums to -2010, I have written this Solution Code:
function sumMaxMin(a,b,c,d,e){
// write code here
// return the output , do not use console.log here
return Math.max(a,b,c,d,e) + Math.min(a,b,c,d,e)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You have to write 4 classes as BubbleSort, InsertionSort, MergeSort and SelectionSort, and each of should implement following interface:
<code>
interface ISort{
public int[] sort(int[] arr);
}
</code>
Each class should be defined as:
Implement sort in each class with respective method, after sorting return sorted array.You don't have to take any input, you have to write classes mentioned in question which implements ISort.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Test:
BubbleSort bubbleSort = new BubbleSort();
arr = bubbleSort.sort(arr);
if(arr.isSorted()) System.out.println("Correct");
else System.out.println("Wrong");
Sample Output:
Correct, I have written this Solution Code: class BubbleSort implements ISort{
public int[] sort(int[] arr){
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
}
class InsertionSort implements ISort{
public int[] sort(int[] arr){
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return arr;
}
}
class MergeSort implements ISort{
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void sort(int arr[], int l, int r)
{
if (l < r) {
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
public int[] sort(int[] arr){
int n=arr.length;
sort(arr,0,n-1);
return arr;
}
}
class SelectionSort implements ISort{
public int[] sort(int[] arr){
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was playing with an array A of N integers, her little sister Tono asked her the following question:
For each integer i from 1 to N, find the smallest non- negative integer that is missing in subarray A[0]...A[i-1].
As Solo wants to play, please help her solve the following problem.The first line of the input contains a single integer N.
The second line of input contains N space-separated integers, the integers of the array A.
Constraints
1 <= N <= 200000
0 <= A[i] <= 200000Output N integers, the answers for each index <b>i</b> in a new line.Sample Input
4
1 1 0 2
Sample Output
0
0
2
3
Explanation:
For i=1, subarray = [1], smallest non negative missing integer = 0.
For i=2, subarray = [1, 1], smallest non negative missing integer = 0.
For i=3, subarray = [1, 1, 0], smallest non negative missing integer = 2.
For i=4, subarray = [1, 1, 0, 2], smallest non negative missing integer = 3.
Sample Input
10
5 4 3 2 1 0 7 7 6 6
Sample Output
0
0
0
0
0
6
6
6
8
8, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(read.readLine());
StringTokenizer st = new StringTokenizer(read.readLine());
int[] arr = new int[N];
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int[] prefixArr = new int[200010];
int minIndex = 0;
for(int i=0; i<N; i++) {
prefixArr[i] = -1;
}
for(int i=0; i < N; i++) {
prefixArr[arr[i]] = arr[i];
while(minIndex < N && prefixArr[minIndex] != -1) {
minIndex++;
}
System.out.println(minIndex);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was playing with an array A of N integers, her little sister Tono asked her the following question:
For each integer i from 1 to N, find the smallest non- negative integer that is missing in subarray A[0]...A[i-1].
As Solo wants to play, please help her solve the following problem.The first line of the input contains a single integer N.
The second line of input contains N space-separated integers, the integers of the array A.
Constraints
1 <= N <= 200000
0 <= A[i] <= 200000Output N integers, the answers for each index <b>i</b> in a new line.Sample Input
4
1 1 0 2
Sample Output
0
0
2
3
Explanation:
For i=1, subarray = [1], smallest non negative missing integer = 0.
For i=2, subarray = [1, 1], smallest non negative missing integer = 0.
For i=3, subarray = [1, 1, 0], smallest non negative missing integer = 2.
For i=4, subarray = [1, 1, 0, 2], smallest non negative missing integer = 3.
Sample Input
10
5 4 3 2 1 0 7 7 6 6
Sample Output
0
0
0
0
0
6
6
6
8
8, I have written this Solution Code: def mis(arr, N):
m = 0
c = [0] * (N + 1)
for i in range(N):
if (arr[i] >= 0 and arr[i] < N):
c[arr[i]] = True
while (c[m]):
m += 1
print(m)
if __name__ == '__main__':
N = int(input())
arr = list(map(int,input().split()))
mis(arr, N), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: While Solo was playing with an array A of N integers, her little sister Tono asked her the following question:
For each integer i from 1 to N, find the smallest non- negative integer that is missing in subarray A[0]...A[i-1].
As Solo wants to play, please help her solve the following problem.The first line of the input contains a single integer N.
The second line of input contains N space-separated integers, the integers of the array A.
Constraints
1 <= N <= 200000
0 <= A[i] <= 200000Output N integers, the answers for each index <b>i</b> in a new line.Sample Input
4
1 1 0 2
Sample Output
0
0
2
3
Explanation:
For i=1, subarray = [1], smallest non negative missing integer = 0.
For i=2, subarray = [1, 1], smallest non negative missing integer = 0.
For i=3, subarray = [1, 1, 0], smallest non negative missing integer = 2.
For i=4, subarray = [1, 1, 0, 2], smallest non negative missing integer = 3.
Sample Input
10
5 4 3 2 1 0 7 7 6 6
Sample Output
0
0
0
0
0
6
6
6
8
8, 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 cur = 0;
set<int> s;
int n; cin>>n;
For(i, 0, n){
int a; cin>>a;
s.insert(a);
while(s.find(cur)!=s.end())
cur++;
cout<<cur<<"\n";
}
}
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 two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
import java.math.BigInteger;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger sum;
String ip1 = sc.next();
String ip2 = sc.next();
BigInteger a = new BigInteger(ip1);
BigInteger b = new BigInteger(ip2);
sum = a.add(b);
System.out.println(sum);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two integers a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: /**
* Author : tourist1256
* Time : 2022-02-03 02:46:30
**/
#include <bits/stdc++.h>
#define NX 105
#define MX 3350
using namespace std;
const int mod = 998244353;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
typedef long long INT;
const int pb = 10;
const int base_digits = 9;
const int base = 1000000000;
const int DIV = 100000;
struct bigint {
vector<int> a;
int sign;
bigint() : sign(1) {}
bigint(INT v) { *this = v; }
bigint(const string &s) { read(s); }
void operator=(const bigint &v) { sign = v.sign, a = v.a; }
void operator=(INT v) {
sign = 1;
if (v < 0) sign = -1, v = -v;
for (; v > 0; v = v / base) a.push_back(v % base);
}
bigint operator+(const bigint &v) const {
if (sign == v.sign) {
bigint res = v;
for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) ||
carry;
i++) {
if (i == (int)res.a.size()) res.a.push_back(0);
res.a[i] += carry + (i < (int)a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry) res.a[i] -= base;
}
return res;
}
return *this - (-v);
}
bigint operator-(const bigint &v) const {
if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int)v.a.size() || carry; i++) {
res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
carry = res.a[i] < 0;
if (carry) res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int)a.size() || carry; i++) {
if (i == (int)a.size()) a.push_back(0);
INT cur = a[i] * (INT)v + carry;
carry = (int)(cur / base);
a[i] = (int)(cur % base);
}
trim();
}
bigint operator*(int v) const {
bigint res = *this;
res *= v;
return res;
}
friend pair<bigint, bigint> DIVmod(const bigint &a1, const bigint &b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
int d = ((INT)base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0) r += b, --d;
q.a[i] = d;
}
q.sign = a1.sign * b1.sign;
r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}
bigint operator/(const bigint &v) const { return DIVmod(*this, v).first; }
bigint operator%(const bigint &v) const { return DIVmod(*this, v).second; }
void operator/=(int v) {
if (v < 0) sign = -sign, v = -v;
for (int i = (int)a.size() - 1, rem = 0; i >= 0; i--) {
INT cur = a[i] + rem * (INT)base;
a[i] = (int)(cur / v);
rem = (int)(cur % v);
}
trim();
}
bigint operator/(int v) const {
bigint res = *this;
res /= v;
return res;
}
int operator%(int v) const {
if (v < 0) v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (INT)base) % v;
return m * sign;
}
void operator+=(const bigint &v) { *this = *this + v; }
void operator-=(const bigint &v) { *this = *this - v; }
void operator*=(const bigint &v) { *this = *this * v; }
void operator/=(const bigint &v) { *this = *this / v; }
bool operator<(const bigint &v) const {
if (sign != v.sign) return sign < v.sign;
if (a.size() != v.a.size()) return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i]) return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const { return v < *this; }
bool operator<=(const bigint &v) const { return !(v < *this); }
bool operator>=(const bigint &v) const { return !(*this < v); }
bool operator==(const bigint &v) const { return !(*this < v) && !(v < *this); }
bool operator!=(const bigint &v) const { return *this < v || v < *this; }
void trim() {
while (!a.empty() && !a.back()) a.pop_back();
if (a.empty()) sign = 1;
}
bool isZero() const { return a.empty() || (a.size() == 1 && !a[0]); }
bigint operator-() const {
bigint res = *this;
res.sign = -sign;
return res;
}
bigint abs() const {
bigint res = *this;
res.sign *= res.sign;
return res;
}
INT longValue() const {
INT res = 0;
for (int i = a.size() - 1; i >= 0; i--) res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) { return b.isZero() ? a : gcd(b, a % b); }
friend bigint lcm(const bigint &a, const bigint &b) { return a / gcd(a, b) * b; }
void read(const string &s) {
sign = 1;
a.clear();
int pos = 0;
while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) {
if (s[pos] == '-') sign = -sign;
pos++;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++) x = x *
pb +
s[j] - '0';
a.push_back(x);
}
trim();
}
friend istream &operator>>(istream &stream, bigint &v) {
string s;
stream >> s;
v.read(s);
return stream;
}
friend ostream &operator<<(ostream &stream, const bigint &v) {
if (v.sign == -1) stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int)v.a.size() - 2; i >= 0; --i) stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
vector<INT> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int)p.size(); i++) p[i] = p[i - 1] * pb;
vector<int> res;
INT cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int)a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int)cur);
while (!res.empty() && !res.back()) res.pop_back();
return res;
}
typedef vector<INT> vll;
static vll karatsubaMultiply(const vll &a, const vll &b) {
int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}
int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());
vll a1b1 = karatsubaMultiply(a1, b1);
vll a2b2 = karatsubaMultiply(a2, b2);
for (int i = 0; i < k; i++) a2[i] += a1[i];
for (int i = 0; i < k; i++) b2[i] += b1[i];
vll r = karatsubaMultiply(a2, b2);
for (int i = 0; i < (int)a1b1.size(); i++) r[i] -= a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) r[i] -= a2b2[i];
for (int i = 0; i < (int)r.size(); i++) res[i + k] += r[i];
for (int i = 0; i < (int)a1b1.size(); i++) res[i] += a1b1[i];
for (int i = 0; i < (int)a2b2.size(); i++) res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a5 = convert_base(this->a, base_digits, 5);
vector<int> b5 = convert_base(v.a, base_digits, 5);
vll a(a5.begin(), a5.end());
vll b(b5.begin(), b5.end());
while (a.size() < b.size()) a.push_back(0);
while (b.size() < a.size()) b.push_back(0);
while (a.size() & (a.size() - 1)) a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int)c.size(); i++) {
INT cur = c[i] + carry;
res.a.push_back((int)(cur % DIV));
carry = (int)(cur / DIV);
}
res.a = convert_base(res.a, 5, base_digits);
res.trim();
return res;
}
inline bool isOdd() { return a[0] & 1; }
};
int main() {
bigint n, m;
cin >> n >> m;
cout << n + m << "\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 a and b your task is to print the summation of these two numbersInput contains a single line containing two space separated integers a and b.
Constraints:-
1 <= a, b <= 10<sup>20000</sup>Print the sum of a and b.Sample Input:-
3 8
Sample Output:-
11
Sample Input:-
15 1
Sample Output:-
16, I have written this Solution Code: n,m = map(int,input().split())
print(n+m) , In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
System.out.println("Yes");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: string = str(input())
print("Yes"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Everyone has heard of palindromes, right! A palindrome is a string that remains the same if reversed.
Let's define a new term, Dalindrome.
A Dalindrome is a string whose atleast one of the substrings is a palindrome.
Given a string, find whether it's a Dalindrome.The only line of input contains a string to be checked.
Constraints
1 <= length of string <= 100Output "Yes" if string is a Dalindrome, else output "No".Sample Input
cbabcc
Sample Output
Yes
Explanation: "bab" is one of the substrings of the string that is a palindrome. There may be other substrings that are palindrome as well like "cc", or "cbabc". The question requires atleast one., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL
const int N = 100005;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
string s; cin>>s;
cout<<"Yes";
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick wants to give Morty a chapo (a super awesome treat :P).
The only condition for Morty to get a chapo is that he should be able to reach Rick's place. Both Rick and Morty live on the number line at integer points A and B respectively. There are N types of moves M<sub>1</sub>, M<sub>2</sub>,. , M<sub>N</sub>. Morty can only take steps of size M<sub>i</sub> (1 <= i <= N) in either of the two directions on the number line (any number of times), while Rick prefers to stay at his place.
Please let Morty know if he can ever reach Rick's place and get a chapo.The first line of the input contains three integers N, A, and B denoting the total number of step sizes, the position of Rick, and the position of Morty.
The next line contains N integers M<sub>i</sub> denoting the various step sizes that Morty can take.
Constraints
1 <= N <= 200000
1 <= M<sub>i</sub> <= 10<sup>9</sup>
-10<sup>9</sup> <= A, B <= 10<sup>9</sup>Output <b>Yes</b> if Morty can reach Rick's place, else output <b>No</b>Sample Input
3 19 2
3 5 4
Sample Output
Yes
Explanation: Morty lives at position 2, while Rick lives at position 19. Morty can take the following path to reach Rick's place (there are several other ways as well):
Move M<sub>2</sub> = 5 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Sample Input 2
4 10 15
10 20 30 40
Sample Output 2
No, I have written this Solution Code: import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main implements Runnable {
private boolean console=false;
private long MOD = 1000_000_007L;
private int MAX = 1000_001;
private void solve1(){
int n=in.ni();
long a=in.nl(),b=in.nl();
long g = 0;
for(int i=0;i<n;++i){
g = gcd(g,in.nl());
}
if( Math.abs(a-b)%g==0){
out.printLn("Yes");
}else{
out.printLn("No");
}
}
private void solve() {
int testCases = 1;
while (testCases-->0){
solve1();
}
}
private void add(TreeMap<Integer, Integer> map, int key){
map.put(key,map.getOrDefault(key,0)+1);
}
private void remove(TreeMap<Integer,Integer> map,int key){
if(!map.containsKey(key))
return;
map.put(key,map.getOrDefault(key,0)-1);
if(map.get(key)==0)
map.remove(key);
}
@Override
public void run() {
long time = System.currentTimeMillis();
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
solve();
out.flush();
System.err.println(System.currentTimeMillis()-time);
System.exit(0);
}catch (Exception e){
e.printStackTrace(); System.exit(1);
}
}
private FastInput in;
private FastOutput out;
public static void main(String[] args) throws Exception {
new Main().run();
}
private void init() throws FileNotFoundException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
try {
if (!console && System.getProperty("user.name").equals("puneetkumar")) {
outputStream = new FileOutputStream("/Users/puneetkumar/output.txt");
inputStream = new FileInputStream("/Users/puneetkumar/input.txt");
}
} catch (Exception ignored) {
}
out = new FastOutput(outputStream);
in = new FastInput(inputStream);
}
private void maualAssert(int a,int b,int c){
if(a<b || a>c) throw new RuntimeException();
}
private void maualAssert(long a,long b,long c){
if(a<b || a>c) throw new RuntimeException();
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private long ModPow(long x, long y, long MOD) {
long res = 1L;
x = x % MOD;
while (y >= 1L) {
if ((y & 1L) > 0) res = (res * x) % MOD;
x = (x * x) % MOD;
y >>= 1L;
}
return res;
}
private int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private int[] arrInt(int n){
int[] arr=new int[n];for(int i=0;i<n;++i)arr[i]=in.ni();
return arr;
}
private long[] arrLong(int n){
long[] arr=new long[n];for(int i=0;i<n;++i)arr[i]=in.nl();
return arr;
}
private int arrMax(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMax(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private int arrMin(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMin(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
class FastInput { InputStream obj;
public FastInput(InputStream obj) {
this.obj = obj;
}
private byte inbuffer[] = new byte[1024];
private int lenbuffer = 0, ptrbuffer = 0;
private int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
try { lenbuffer = obj.read(inbuffer);
} catch (IOException e) { throw new InputMismatchException(); } }
if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }
String ns() { int b = skip();StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b)))
{ sb.appendCodePoint(b);b = readByte(); }return sb.toString();}
int ni() {
int num = 0, b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); }}
long nl() { long num = 0;int b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); } }
private boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
float nf() {return Float.parseFloat(ns());}
double nd() {return Double.parseDouble(ns());}
char nc() {return (char) skip();}
}
class FastOutput{
private final PrintWriter writer;
public FastOutput(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public PrintWriter getWriter(){
return writer;
}
public void print(Object obj){
writer.print(obj);
}
public void printLn(){
writer.println();
}
public void printLn(Object obj){
writer.print(obj);
printLn();
}
public void printSp(Object obj){
writer.print(obj+" ");
}
public void printArr(int[] arr){
for(int i:arr)
printSp(i);
printLn();
}
public void printArr(long[] arr){
for(long i:arr)
printSp(i);
printLn();
}
public void flush(){
writer.flush();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick wants to give Morty a chapo (a super awesome treat :P).
The only condition for Morty to get a chapo is that he should be able to reach Rick's place. Both Rick and Morty live on the number line at integer points A and B respectively. There are N types of moves M<sub>1</sub>, M<sub>2</sub>,. , M<sub>N</sub>. Morty can only take steps of size M<sub>i</sub> (1 <= i <= N) in either of the two directions on the number line (any number of times), while Rick prefers to stay at his place.
Please let Morty know if he can ever reach Rick's place and get a chapo.The first line of the input contains three integers N, A, and B denoting the total number of step sizes, the position of Rick, and the position of Morty.
The next line contains N integers M<sub>i</sub> denoting the various step sizes that Morty can take.
Constraints
1 <= N <= 200000
1 <= M<sub>i</sub> <= 10<sup>9</sup>
-10<sup>9</sup> <= A, B <= 10<sup>9</sup>Output <b>Yes</b> if Morty can reach Rick's place, else output <b>No</b>Sample Input
3 19 2
3 5 4
Sample Output
Yes
Explanation: Morty lives at position 2, while Rick lives at position 19. Morty can take the following path to reach Rick's place (there are several other ways as well):
Move M<sub>2</sub> = 5 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Sample Input 2
4 10 15
10 20 30 40
Sample Output 2
No, I have written this Solution Code: def gcd(a,b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a,b):
return (a / gcd(a,b))* b
n,x,y=map(int,input().split())
a=list(map(int,input().split()))
ans=a[0]
for i in range(1,len(a)):
ans=gcd(ans,a[i])
if abs(x-y)%ans==0:
print("Yes")
else:
print("No"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick wants to give Morty a chapo (a super awesome treat :P).
The only condition for Morty to get a chapo is that he should be able to reach Rick's place. Both Rick and Morty live on the number line at integer points A and B respectively. There are N types of moves M<sub>1</sub>, M<sub>2</sub>,. , M<sub>N</sub>. Morty can only take steps of size M<sub>i</sub> (1 <= i <= N) in either of the two directions on the number line (any number of times), while Rick prefers to stay at his place.
Please let Morty know if he can ever reach Rick's place and get a chapo.The first line of the input contains three integers N, A, and B denoting the total number of step sizes, the position of Rick, and the position of Morty.
The next line contains N integers M<sub>i</sub> denoting the various step sizes that Morty can take.
Constraints
1 <= N <= 200000
1 <= M<sub>i</sub> <= 10<sup>9</sup>
-10<sup>9</sup> <= A, B <= 10<sup>9</sup>Output <b>Yes</b> if Morty can reach Rick's place, else output <b>No</b>Sample Input
3 19 2
3 5 4
Sample Output
Yes
Explanation: Morty lives at position 2, while Rick lives at position 19. Morty can take the following path to reach Rick's place (there are several other ways as well):
Move M<sub>2</sub> = 5 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Move M<sub>1</sub> = 4 to the right.
Sample Input 2
4 10 15
10 20 30 40
Sample Output 2
No, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(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, a, b; cin>>n>>a>>b;
a = abs(a-b);
int gv = 0;
For(i, 0, n){
int m; cin>>m;
gv = __gcd(gv, m);
}
if(a%gv == 0){
cout<<"Yes";
}
else{
cout<<"No";
}
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: void magicTrick(int a, int b){
cout<<a+b/2;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: static void magicTrick(int a, int b){
System.out.println(a + b/2);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is trying a new magic trick on you, The magic trick goes in 6 steps:-
1. Think of a number X(don't tell Sara)
2. Add A(Given by Sara) to it.
3. Double the sum in your mind.
4. Add an even number B(Given by Sara) to it.
5. Half the amount
6. Subtract the initial number which you had taken from the sum
After this Sara will tell the resulting amount without knowing the initial number, can you print the result for her?
<b>Note: B is always even. </b>You don't have to worry about the input, you just have to complete the function <b>magicTrick()</b>
<b>Constraints</b>:-
1 <= A, B <= 1000Print the resulting amountSample Input:-
3 4
Sample Output:-
5
Sample Input:-
8 4
Sample Output:-
10, I have written this Solution Code: A,B = map(int,input().split(' '))
C = A+B//2
print(C)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a queue of integers and N queries. Your the task is to perform these operations:-
enqueue:-this operation will add an element to your current queue.
dequeue:-this operation will delete the element from the starting of the queue
displayfront:-this operation will print the element presented at front
Note:-if queue is empty than dequeue operation will do nothing, and 0 will be printed as a front element of queue if it is empty.User task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes integer to be added as a parameter.
<b>dequeue()</b>:- that takes no parameter.
<b>displayfront()</b> :- that takes no parameter.
Constraints:
1 <= N(Number of queries) <= 10<sup>3</sup>You don't need to print anything else other than in displayfront() in which you require to print the element at front of your queue in a new line, if the queue is empty you just need to print 0.Sample Input:-
7
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
Sample Output:-
0
2
2
4
Sample input:
5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: class Queue
{
private Node front, rear;
private int currentSize;
class Node {
Node next;
int val;
Node(int val) {
this.val = val;
next = null;
}
}
public Queue()
{
front = null;
rear = null;
currentSize = 0;
}
public boolean isEmpty()
{
return (currentSize <= 0);
}
public void dequeue()
{
if (isEmpty())
{
}
else{
front = front.next;
currentSize--;
}
}
//Add data to the end of the list.
public void enqueue(int data)
{
Node oldRear = rear;
rear = new Node(data);
if (isEmpty())
{
front = rear;
}
else
{
oldRear.next = rear;
}
currentSize++;
}
public void displayfront(){
if(isEmpty()){
System.out.println("0");
}
else{
System.out.println(front.val);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: def checkConevrtion(a):
return str(a)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer <b>N</b>, you need to typecast this integer to String. If the typecasting is done successfully then we will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".User task:
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>checkConvertion()</b>, which contains N as a parameter.You need to return the typecasted string value. The driver code will print "<b>Nice Job</b>" otherwise "<b>Wrong answer</b>".Sample Input:
5
Sample Output:
Nice Job
Sample Input:
6
Sample Output:
Nice Job, I have written this Solution Code: static String checkConevrtion(int a)
{
return String.valueOf(a);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, your task is to print a Pyramid pattern of '*' of height N.
For N = 5, the following pattern is printed.
<center> * </center>
<center> *** </center>
<center> ***** </center>
<center> ******* </center>
<center>*********</center>The input contains N as an input.
</b>Constraint:</b>
1 <b>≤</b> N <b>≤</b> 50Print a Pyramid pattern of '*' of height N.Sample Input:
5
Sample Output:
<center> * </center>
<center> *** </center>
<center> ***** </center>
<center> ******* </center>
<center>*********</center>
Sample Input:
2
Sample Output:
<center> * </center>
<center> *** </center>, I have written this Solution Code: import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<=n;i++){
int cnt=2*i-1;
for(int j=0;j<n-i;j++)System.out.print(" ");
for(int j=0;j<cnt;j++)System.out.print("*");
for(int j=0;j<n-i;j++)System.out.print(" ");
System.out.println("");
}
System.out.println("");
return;
}
}
, 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: 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: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: nan, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the elements of nums in order into an initially empty BST. Find the number of different ways to reorder nums so that the constructed BST is identical to that formed from the original array nums.
Return the number of ways to reorder nums such that the BST formed is identical to the original BST formed from nums.
Since the answer may be very large, return it modulo 1e9+7.The first line contains a single space separated integer N.
The second line contains N space-separated integers a[i].
Constraints
1 <= n <= 1000
1 <= a[i] <= n
Print the number of all possible permutations.
Sample Input 1:
3
2 1 3
Sample Output 1:
1
Explanation:
Only single permutations of nums can produce similar BST.
Sample Input 2:
5
3 4 5 1 2
Sample Output 2:
5
Explanation:
3 1 2 4 5
3 1 4 2 5
3 1 4 5 2
3 4 1 2 5
3 4 1 5 2
There are 5 possible permutations of nums that can generate the same binary search tree., I have written this Solution Code: /**
* author: tourist1256
* created: 2022-06-14 14:26:47
**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 2351
#endif
#define int long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) {
uniform_int_distribution<int64_t> generator(l, r);
return generator(rng);
}
const int mod = 1e9 + 7;
vector<int> fac, sz, dp;
vector<vector<int>> adj;
struct Node {
int data;
Node *left = NULL, *right = NULL;
Node(int val) : data(val) {}
};
int power(int x, int y) {
x %= mod;
if (!x) return x;
int res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
y >>= 1;
x = (x * x) % mod;
}
return res;
}
int inverse(int x) {
return power(x, mod - 2);
}
Node *insert(Node *root, int val) {
if (!root) {
Node *tmp = new Node(val);
return tmp;
}
if (root->data < val)
root->right = insert(root->right, val);
else
root->left = insert(root->left, val);
return root;
}
void inorder(Node *root, Node *par) {
if (!root)
return;
inorder(root->left, root);
inorder(root->right, root);
if (par != NULL) {
sz[par->data] += sz[root->data];
}
int val = fac[sz[root->data] - 1];
if (root->left != NULL) {
val = (val * inverse(fac[sz[root->left->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->left->data]) % mod;
}
if (root->right != NULL) {
val = (val * inverse(fac[sz[root->right->data]])) % mod;
dp[root->data] = (dp[root->data] * dp[root->right->data]) % mod;
}
dp[root->data] = (dp[root->data] * val) % mod;
}
int numOfWays(vector<int> &nums) {
int n = nums.size();
fac.assign(n + 10, 1);
for (int i = 2; i <= n; i++)
fac[i] = (fac[i - 1] * i) % mod;
sz.assign(n + 10, 1);
dp.assign(n + 10, 1);
adj.assign(n + 1, vector<int>());
Node *root = new Node(nums[0]);
for (int i = 1; i < n; i++)
insert(root, nums[i]);
inorder(root, NULL);
dp[nums[0]]--;
dp[nums[0]] += mod;
dp[nums[0]] %= mod;
return dp[nums[0]];
}
int32_t main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
debug(a);
cout << numOfWays(a) << "\n";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a Program to read a number and display the corresponding week day name using if_elif_else.The first line contains the week day number to be printed.Prints the following on given inputs:
1- Monday
2- Tuesday
3- Wednesday
4- Thursday
5- Friday
6- Saturday
7- Sunday
and Invalid on any other input.Sample Input:
3
Sample Output:
Wednesday, I have written this Solution Code: weekday = int(input())
if weekday == 1 :
print("Monday");
elif weekday == 2 :
print("Tuesday")
elif(weekday == 3) :
print("Wednesday")
elif(weekday == 4) :
print("Thursday")
elif(weekday == 5) :
print("Friday")
elif(weekday == 6) :
print("Saturday")
elif (weekday == 7) :
print("Sunday")
else :
print("Invalid"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: static void Icecreams (int N, int D){
int x=N;
while(D-->0){
x-=x/2;
x*=3;
}
System.out.println(x);
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
cout << x;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: void Icecreams (int N, int D){
int x=N;
while(D--){
x-=x/2;
x*=3;
}
printf("%d", x);
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara is fond of ice cream initially she had N ice creams with her. If Sara eats exactly half of the ice cream she has in a day and the remaining icecreams get tripled each night. How many ice creams does Sara have at the end of D-days?
<b>Note:- </b>
Take the floor value while dividing by 2.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>Icecreams()</b> that takes integers N and D as parameters.
<b>Constraints:-</b>
1 <= N <= 100
1 <= D <= 20Print a single integer denoting the number of ice creams at the end of D days.Sample Input 1:-
5 1
Sample Output 1:-
9
</b>Explanation:-</b>
Sara will eat 2 ice creams and the remaining 3 will be tripled i. e 9.
Sample Input 2:-
5 3
Sample Output 2:-
24
<b>Explanation:-</b>
Day 1:- N=5, eaten = 2, rem = 3 => remaining = 3*3 = 9
Day 2:- N=9, eaten = 4, rem = 5 => remaining = 5*3 = 15
Day 3:- N=15, eaten = 7, rem = 8 => remaining = 8*3 = 24, I have written this Solution Code: def Icecreams(N,D):
ans = N
while D > 0:
ans = ans - ans//2
ans = ans*3
D = D-1
return ans
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code:
import java.io.*;
import java.util.*;
class Main{
public static void main(String[] args)throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str[]=br.readLine().split(" ");
long l=Long.parseLong(str[0]);
long r=Long.parseLong(str[1]);
long k=Long.parseLong(str[2]);
long count=(r/k)-((l-1)/k);
System.out.print(count);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: l,r,k=[int(x)for x in input().split()]
print(r//k - (l-1)//k), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You think that integers divisible by K are nice. Given L and R (L<=R), find the number of nice integers from L to R (both inclusive).First Line of input contains three integers L R K
Constraints :
0 <= L <= R <= 1000000000000000000(10^18)
1 <= K <= 1000000000000000000(10^18)Output a single integer which is the number of nice integers from L to R (both inclusive).Sample input 1
1 10 2
Sample output 1
5
Sample intput 2
4 5 3
Sample output 2
0
Explanation:-
Required divisors = 2 4 6 8 10, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long x,l,r,k;
cin>>l>>r>>k;
x=l/k;
if(l%k==0){x--;}
cout<<r/k-x;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: static int isPresent(long arr[], int n, long k)
{
int left = 0;
int right = n-1;
int res = -1;
while(left<=right){
int mid = (left+right)/2;
if(arr[mid] == k){
res = 1;
break;
}else if(arr[mid] < k){
left = mid + 1;
}else{
right = mid - 1;
}
}
return res;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unordered_map<long long,int> m;
long k;
cin>>k;
long long a;
for(int i=0;i<n;i++){
cin>>a;
m[a]++;
}
if(m.find(k)!=m.end()){
cout<<1<<endl;
}
else{
cout<<-1<<endl;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: def binary_search(arr, low, high, x):
if high >= low:
mid = (high + low) // 2
if arr[mid] == x:
return 1
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
else:
return binary_search(arr, mid + 1, high, x)
else:
return -1
def position(n,arr,x):
return binary_search(arr,0,n-1,x)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
<b>Note:</b> Use binary search to solve the problemThe first line of input contains a number of test cases T. For each test case, the first line of input contains a number of elements in the array, and the number K is separated by space. The next line contains N elements.
<b>Constraints:</b>
1 ≤ T ≤ 10
1 ≤ N ≤ 10<sup>5</sup>
1 ≤ K ≤ 10<sup>9</sup>
1 ≤ arr[i] ≤ 10<sup>9</sup>
<b>Sum of N over all test cases doesn't exceed 10<sup>6</sup></b>If the element is present in the array print "1" else print "-1".Sample Input:
2
5 6
1 2 3 4 6
5 2
1 3 4 5 6
Sample Output:
1
-1, I have written this Solution Code: // arr is they array to search from
// x is target
function binSearch(arr, x) {
// write code here
// do not console.log
// return the 1 or -1
let l = 0;
let r = arr.length - 1;
let mid;
while (r >= l) {
mid = l + Math.floor((r - l) / 2);
// If the element is present at the middle
// itself
if (arr[mid] == x)
return 1;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
r = mid - 1;
// Else the element can only be present
// in right subarray
else
l = mid + 1;
}
// We reach here when element is not
// present in array
return -1;
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<b>User task:</b> Since this is a functional problem you don't have to worry about the input. You just have to complete the function kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: void kCircleSum(int arr[],int n,int k){
long long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
printf("%lli ",ans);
ans+=arr[(i+k)%n]-arr[i];
}
}, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<b>User task:</b> Since this is a functional problem you don't have to worry about the input. You just have to complete the function kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: function kCircleSum(arr, arrSize, k)
{
var list = new Array(2*arrSize + 5)
for(var i = 0; i < arrSize; i++)
{
list[i+1] = arr[i]
list[i+arrSize+1] = list[i+1]
}
for(var i = 0; i < 2*arrSize; i++)
dp[i] = 0
for(var i=1;i<=2*arrSize;i++)
{
dp[i] = dp[i-1]+list[i]
}
var ans = ""
for(var i = 1; i <= arrSize; i++)
{
ans += (dp[i+k-1]-dp[i-1]) + " "
}
console.log(ans)
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<b>User task:</b> Since this is a functional problem you don't have to worry about the input. You just have to complete the function kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: static void kCircleSum(int arr[],int n,int k){
long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
System.out.print(ans+" ");
ans+=arr[(i+k)%n]-arr[i];
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<b>User task:</b> Since this is a functional problem you don't have to worry about the input. You just have to complete the function kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: def kCircleSum(arr,n,k):
ans=0
for i in range (0,k):
ans=ans+arr[i]
for i in range (0,n):
print(ans,end=" ")
ans=ans+arr[int((i+k)%n)]-arr[i]
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: N numbers are arranged in Circle. Find the Sum of all K contiguous Sub-arrays.The first line of the input contains an integer N, the length of the array, and K. The next line contains N integers which are elements of the array.
<b>User task:</b> Since this is a functional problem you don't have to worry about the input. You just have to complete the function kCircleSum(arr, N, K) which contains arr(array) and N(size of the array), and K as a parameter
<b>Constraints</b>
1 <= N <= 100000
1 <= arr[I] <= 100000
1 <= K <= NYou need to print N space-separated integers ith integer denoting Sum of sub-array of length K starting at index i.Sample Input
3 1
1 2 3
Sample Output
1 2 3
Explanation : k=1 so ans is 1, 2, and 3.
Sample Input
5 2
6 4 3 4 1
Sample Output
10 7 7 5 7, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
void kCircleSum(int arr[],int n,int k){
long long ans=0;
for(int i=0;i<k;i++){
ans+=arr[i];
}
for(int i=0;i<n;i++){
printf("%lli ",ans);
ans+=arr[(i+k)%n]-arr[i];
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main (String[] args)throws IOException {
BufferedReader sc= new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0;
long z=0;
String[] str;
str = sc.readLine().split(" ");
a= Integer.parseInt(str[0]);
b= Integer.parseInt(str[1]);
c= Integer.parseInt(str[2]);
d= Integer.parseInt(str[3]);
BigInteger m = new BigInteger("1000000007");
BigInteger n = new BigInteger("1000000006");
BigInteger zero = new BigInteger("0");
BigInteger ans, y;
if(d==0){
z =1;
}else{
z = (long)Math.pow(c, d);
}
if(b==0){
y= zero;
}else{
y = (BigInteger.valueOf(b)).modPow((BigInteger.valueOf(z)), n);
}
if(y == zero){
System.out.println("1");
}else if(a==0){
System.out.println("0");
}else{
ans = (BigInteger.valueOf(a)).modPow(y, m);
System.out.println(ans);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
Sample Output
0, I have written this Solution Code: nan, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given four integers a, b, c, and d. Find the value of a<sup>(b<sup>(c<sup>d</sup>)</sup>)</sup> modulo 1000000007.
(Fact: 0<sup>0</sup> = 1)The first and the only line of input contains 4 integers a, b, c, and d.
Constraints
1 <= a, b, c, d <= 12Output a single integer, the answer modulo 1000000007.Sample Input
2 2 2 2
Sample Output
65536
Explanation
2^(2^(2^2)) = 2^(2^4) = 2^16 = 65536.
Sample Input
0 7 11 1
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(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
int powmod(int a, int b, int c = MOD){
int ans = 1;
while(b){
if(b&1){
ans = (ans*a)%c;
}
a = (a*a)%c;
b >>= 1;
}
return ans;
}
void solve(){
int a, b, c, d; cin>>a>>b>>c>>d;
int x = pow(c, d);
int y = powmod(b, x, MOD-1);
int ans = powmod(a, y, MOD);
cout<<ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a stack of integers and N queries. Your task is to perform these operations:-
<b>push:-</b>this operation will add an element to your current stack.
<b>pop:-</b>remove the element that is on top
<b>top:-</b>print the element which is currently on top of stack
<b>Note:-</b>if the stack is already empty then the pop operation will do nothing and 0 will be printed as a top element of the stack if it is empty.<b>User task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>push()</b>:- that takes the stack and the integer to be added as a parameter.
<b>pop()</b>:- that takes the stack as parameter.
<b>top()</b> :- that takes the stack as parameter.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>3</sup>You don't need to print anything else other than in <b>top</b> function in which you require to print the topmost element of your stack in a new line, if the stack is empty you just need to print 0.Input:
7
push 1
push 2
top
pop
top
pop
top
Output:
2
1
0
, I have written this Solution Code:
public static void push (Stack < Integer > st, int x)
{
st.push (x);
}
// Function to pop element from stack
public static void pop (Stack < Integer > st)
{
if (st.isEmpty () == false)
{
int x = st.pop ();
}
}
// Function to return top of stack
public static void top(Stack < Integer > st)
{
int x = 0;
if (st.isEmpty () == false)
{
x = st.peek ();
}
System.out.println (x);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array arr[] of size N and a number K, the task is to find the smallest number M such that the sum of the array becomes lesser than or equal to the number K when every element of that array is divided by the number M.
Note: Each result of the division is rounded to the nearest integer greater than or equal to that element. For example: 10/3 = 4 and 6/2 = 3The first line contains two integers N and K.
Next line contains N integers denoting the elements of arr[]
<b>Constraints:</b>
1 <= N <= 100000
1 <= arr[i] <= 100000
1 <= K <= 100000000Print a single integer the value of smallest number MSample Input:
4 6
2 3 4 9
Sample Output:
4
Explanation:
When every element is divided by 4 - 2/4 + 3/4 + 4/4 + 9/4 = 1 + 1 + 1 + 3 = 6
When every element is divided by 3 - 2/3 + 3/3 + 4/3 + 9/3 = 1 + 1 + 2 + 3 = 7 which is greater than K.
Hence the smallest integer which makes the sum less than or equal to K = 6 is 4., 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);
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int k = Integer.parseInt(s[1]);
String str[] = br.readLine().split(" ");
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = Integer.parseInt(str[i]);
}
System.out.print(minDivisor(arr,n,k));
}
static int minDivisor(int arr[],int N, int limit) {
int low = 0, high = 1000000000;
while (low < high)
{
int mid = (low + high) / 2;
int sum = 0;
for(int i = 0; i < N; i++)
{
sum += Math.ceil((double) arr[i] / (double) mid);
}
if(sum <= limit){
high = mid;
}
else{
low = mid + 1;
}
}
return low;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |