Instruction
stringlengths 261
35k
| Response
stringclasses 1
value |
---|---|
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 BST containing N nodes. The task is to find the <b>minimum absolute difference</b> possible between any <b>two different nodes</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>absMinDist()</b> that takes "root" node as parameter
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to return the minimum absolute difference between any two different nodes.The driver code will take care of printing new line.Input:
2
5 3 6
5 N 9 8 N
Output:
1
1
Explanation:
Testcase 1:
5
/ \
3 6
the minimum abs difference between two nodes i.e 5 & 6 is 1.
Testcase 2:
5
/ \
N 9
/ \
8 N
the minimum absolute difference between two nodes i.e. 9 & 8 is 1., I have written this Solution Code: t=int(input())
for k in range(0,t):
arr=input().split()
new_arr=[]
for v in arr:
if(v!='N'):
new_arr.append(int(v))
new_arr=sorted(new_arr)
m=1000000000000
for i in range(0,len(new_arr)-1):
m=min(m,abs(new_arr[i+1]-new_arr[i]))
print (m), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a BST containing N nodes. The task is to find the <b>minimum absolute difference</b> possible between any <b>two different nodes</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>absMinDist()</b> that takes "root" node as parameter
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10^5
1 <= node values <= 10^5
<b>Sum of N over all testcases does not exceed 10^6</b>
For each testcase you need to return the minimum absolute difference between any two different nodes.The driver code will take care of printing new line.Input:
2
5 3 6
5 N 9 8 N
Output:
1
1
Explanation:
Testcase 1:
5
/ \
3 6
the minimum abs difference between two nodes i.e 5 & 6 is 1.
Testcase 2:
5
/ \
N 9
/ \
8 N
the minimum absolute difference between two nodes i.e. 9 & 8 is 1., I have written this Solution Code: static Integer prev = null, minDiff = Integer.MAX_VALUE;
public static int absMinDist(Node root) {
prev = null;
minDiff = Integer.MAX_VALUE;
bst(root);
return minDiff;
}
public static void bst(Node root) {
if(root==null) return;
bst(root.left);
if(prev!=null) {
minDiff = Math.min(root.data-prev, minDiff);
}
prev= root.data;
bst(root.right);
}, In this Programming Language: Java, 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: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: static void verticalFive(){
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
System.out.println("*");
}
static void horizontalFive(){
System.out.print("* * * * *");
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to print Five stars ('*') <b><i>vertically</i></b> and 5 <b><i>horizontally</i></b>
There will be two functions:
<ul>
<li>verticalFive(): Print stars in vertical order</li>
<li>horizontalFive(): Print stars in horizontal order</l>
</ul><b>User Task:</b>
Your task is to complete the functions <b>verticalFive()</b> and <b>horizontalFive()</b>.
Print 5 vertical stars in <b> verticalFive</b> and 5 horizontal stars(separated by whitespace) in <b>horizontalFive</b> function.
<b>Note</b>: You don't need to print the extra blank line it will be printed by the driver codeNo Sample Input:
Sample Output:
*
*
*
*
*
* * * * *, I have written this Solution Code: def vertical5():
for i in range(0,5):
print("*",end="\n")
#print()
def horizontal5():
for i in range(0,5):
print("*",end=" ")
vertical5()
print(end="\n")
horizontal5(), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a class with the name SumCalculator. The class needs two fields (public variables) with names num1 and num2 both of type int.
Write the following methods (instance methods):
<b>*Method named sum without any parameters, it needs to return the value of num1 + num2.</b>
<b>*Method named sum2 with two parameters a, b, it needs to return the value of a + b.</b>
<b>*Method named fromObject with two parameters of type sumCalculator object named obj1 and obj2, and you have to call sum function for respective object and return sum of both</b>
NOTE: All methods should be defined as public, NOT public static.
NOTE: In total, you have to write 3 methods.
NOTE: Do not add the main method to the solution code.You don't have to take any input, You only have to write class <b>SumCalculator</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
1
Sample Output:
Correct, I have written this Solution Code: class SumCalculator():
def __init__(self,a,b):
self.a=a
self.b=b
def sum(self):
return self.a+self.b
def sum2(self,a,b):
return a+b
def fromObject(self,ob1,ob2):
return ob1.sum+ob2.sum, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a class with the name SumCalculator. The class needs two fields (public variables) with names num1 and num2 both of type int.
Write the following methods (instance methods):
<b>*Method named sum without any parameters, it needs to return the value of num1 + num2.</b>
<b>*Method named sum2 with two parameters a, b, it needs to return the value of a + b.</b>
<b>*Method named fromObject with two parameters of type sumCalculator object named obj1 and obj2, and you have to call sum function for respective object and return sum of both</b>
NOTE: All methods should be defined as public, NOT public static.
NOTE: In total, you have to write 3 methods.
NOTE: Do not add the main method to the solution code.You don't have to take any input, You only have to write class <b>SumCalculator</b>.Output will be printed by tester, "Correct" if your code is perfectly fine otherwise "Wrong".Sample Input:
1
Sample Output:
Correct, I have written this Solution Code: class SumCalculator{
public int num1,num2;
SumCalculator(int _num1,int _num2){
num1=_num1;
num2=_num2;
}
public int sum() {
return num1+num2;
}
public int sum2(int a,int b){
return a+b;
}
public int fromObject(SumCalculator obj1,SumCalculator obj2){
return obj1.sum() + obj2.sum();
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 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 input[]=br.readLine().split("\\s");
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(input[i]);
}
System.out.print(implementMergeSort(a,0,n-1));
}
public static long implementMergeSort(int arr[], int start, int end)
{
long count=0;
if(start<end)
{
int mid=start+(end-start)/2;
count +=implementMergeSort(arr,start,mid);
count +=implementMergeSort(arr,mid+1,end);
count +=merge(arr,start,end,mid);
}
return count;
}
public static long merge(int []a,int start,int end,int mid)
{
int i=start;
int j=mid+1;
int k=0;
int len=end-start+1;
int c[]=new int[len];
long inv_count=0;
while(i<=mid && j<=end)
{
if(a[i]<=a[j])
{
c[k++]=a[i];
i++;
}
else
{
c[k++]=a[j];
j++;
inv_count +=(mid-i)+1;
}
}
while(i<=mid)
{
c[k++]=a[i++];
}
while(j<=end)
{
c[k++]=a[j++];
}
for(int l=0;l<len;l++)
a[start+l]=c[l];
return inv_count;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of positive integers. The task is to find inversion count of array.
Inversion Count : For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.
Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.
Asked in Adobe, Amazon, Microsoft.The first line of each test case is N, the size of the array. The second line of each test case contains N elements.
Constraints:-
1 ≤ N ≤ 10^5
1 ≤ a[i] ≤ 10^5Print the inversion count of array.Sample Input:
5
2 4 1 3 5
Sample Output:
3
Explanation:
Testcase 1: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
long long _mergeSort(int arr[], int temp[], int left, int right);
long long merge(int arr[], int temp[], int left, int mid, int right);
/* This function sorts the input array and returns the
number of inversions in the array */
long long mergeSort(int arr[], int array_size)
{
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function that sorts the input array and
returns the number of inversions in the array. */
long long _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left, mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
long long merge(int arr[], int temp[], int left,
int mid, int right)
{
int i, j, k;
long long inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
signed main()
{
int n;
cin>>n;
int a[n];
unordered_map<int,int> m;
for(int i=0;i<n;i++){
cin>>a[i];
if(m.find(a[i])==m.end()){
m[a[i]]=i;
}
}
cout<<mergeSort(a,n);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find if the given sentence end with the given word1st line: Sentence
2nd line: Word which has to be in endPrint True or False if the sentence ends with the wordInput:
The world is small
small
Output:
True, I have written this Solution Code: import re
s=input()
w=input()
st=".*"+w+"$"
x = re.search(st, s)
if(x):
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 two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String s2 = br.readLine();
boolean flag = true;
int[] arr1 = new int[26];
int[] arr2 = new int[26];
for(int i=0; i<s1.length(); i++){
arr1[s1.charAt(i)-97]++;
}
for(int i=0; i<s2.length(); i++){
arr2[s2.charAt(i)-97]++;
}
for(int i=0; i<25; i++){
if(arr1[i]!=arr2[i]){
flag = false;
break;
}
}
if(flag==true)
System.out.print("YES");
else System.out.print("NO");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: s1 = input().strip()
s2 = input().strip()
dict1 = dict()
dict2 = dict()
for i in s1:
dict1[i] = dict1.get(i, 0) + 1
for j in s2:
dict2[j] = dict2.get(j, 0) + 1
print(("NO", "YES")[dict1 == dict2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
#define pu push_back
#define fi first
#define se second
#define mp make_pair
#define int long long
#define pii pair<int,int>
#define mm (s+e)/2
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define sz 200000
int A[26],B[26];
signed main()
{
string s,p;
cin>>s>>p;
for(int i=0;i<s.size();i++)
{
int y=s[i]-'a';
A[y]++;
}
for(int i=0;i<p.size();i++)
{
int y=p[i]-'a';
B[y]++;
}int ch=1;
for(int i=0;i<26;i++)
{
if(B[i]!=A[i])ch=0;
}
if(ch==1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings a and b consisting of lowercase characters. The task is to check whether two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “act” and “tac” are an anagram of each other.Input consists of two strings in lowercase english characters.
Constraints:
1 ≤ |s1|, |s2| ≤ 10^5Print "YES" without quotes if the two strings are anagram else print "NO".Sample Input
naman
manan
Sample Output
YES
Explanation: Both String contain 2 'a's, 2 'n's and 1 'm'., I have written this Solution Code: // str1 and str2 are the two input strings
function isAnagram(str1,str2){
// Get lengths of both strings
let n1 = str1.length;
let n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return "NO";
str1 = str1.split('')
str2 = str2.split('')
// Sort both strings
str1.sort();
str2.sort()
// Compare sorted strings
for (let i = 0; i < n1; i++)
if (str1[i] != str2[i])
return "NO";
return "YES";
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main (String[] args) {
FastReader sc=new FastReader();
int n=sc.nextInt();
int m=sc.nextInt();
int arr[][]=new int[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
arr[i][j]= sc.nextInt();
}
}
long maxsum=0;
int index=0;
for(int i=0; i<n; i++)
{
long sum=0;
for(int j=0; j<m; j++)
{
sum += arr[i][j];
}
if(sum > maxsum)
{
maxsum=sum;
index=i+1;
}
}
System.out.print(index);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a grid having N*M elements. Find the index of the row (1-base indexing) which has the maximum sum of elements.
Note: if more than two rows have the same sum of elements, then return the smallest index.The first line of the input contains two integers N and M.
The next N lines each contain M space separated integers.
<b>Constraints:</b>
1 <= N, M <= 10<sup>3</sup>
1 <= A<sub>i, j</sub> <= 10<sup>9Print the index of the row (1-base indexing) which has the maximum sum of elements.Sample Input:
4 3
3 4 2
5 1 7
2 8 1
2 3 3
Sample Output:
2
<b>Explaination:</b>
Row number 2 has sum = 5 + 1 + 7 = 13 which is the maximum among all rows., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int> (m));
vector<int> sum(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> a[i][j];
sum[i] += a[i][j];
}
}
int mx = 0, ans = 0;
for(int i = 0; i < n; i++){
if(mx < sum[i]){
mx = sum[i];
ans = i;
}
}
cout << ans + 1;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str1 = br.readLine();
String[] str2 = str1.split(" ");
int[] arr = new int[n];
for(int i = 0; i < n; ++i) {
arr[i] = Integer.parseInt(str2[i]);
}
PermuteTheArray(arr, n);
}
static void PermuteTheArray(int A[], int n)
{
int []arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[A[i] - 1] = i;
}
int mini = n, maxi = 0;
for(int i = 0; i < n; i++)
{
mini = Math.min(mini, arr[i]);
maxi = Math.max(maxi, arr[i]);
if (maxi - mini == i)
System.out.print(1+" ");
else
System.out.print(0+" ");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, I have written this Solution Code: def find_permutations(arr):
max_ind = -1
min_ind = 10000000;
n = len(arr)
index_of = {}
for i in range(n):
index_of[arr[i]] = i + 1
for i in range(1, n + 1):
max_ind = max(max_ind, index_of[i])
min_ind = min(min_ind, index_of[i])
if (max_ind - min_ind + 1 == i):
print(1,end = " ")
else:
#print( "i = ",i)
print(0,end = " ")
n = int(input())
arr = list(map(int, input().split()))
find_permutations(arr), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sara loves problems on permutation but this time she stuck on a problem and asks for your help.
Given a permutation of N integers as Arr[], your task is to check for each K(1 <= K <= N) there exists a subarray of size K such that it is also a permutation of K integers.
Note:- A permutation of N integers is a sequence of size N where every element from 1- N are present.The first line of input contains a single integer N denoting the size of permutation, the next line of input contains N space separated integers depicting the permutaiton.
Constraints:-
1 <= N <= 100000
1 <= Arr[i] <= NPrint N space separated integers either 1 or 0. Print 1 if there exist a permutation K for the ith number else print 0.Sample Input:-
6
4 5 1 3 2 6
Sample Output:-
1 0 1 0 1 1
Explanation:-
for k=1 permutaion exist from [3, 3]
for k=2 no permutaion exists
for k=3 permutaion exist from [3, 5]
for k=4 no permutaion exists
for k=5 permutaion exist from [1, 5]
for k=6 permutaion exist from [1, 6]
Sample Input:-
6
6 5 4 3 2 1
Sample Output:-
1 1 1 1 1 1, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 1001
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
pair<int,int> ans[100005];
signed main(){
// freopen("ou.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t;t=1;
while(t--){
int n;
cin>>n;
int a[n+10];
int b[n+10];
FOR(i,n){
cin>>a[i];
b[a[i]]=i;
}
ans[1].first=b[1];
ans[1].second=b[1];
for(int i=2;i<=n;i++){
ans[i].first=min(ans[i-1].first,b[i]);
ans[i].second=max(ans[i-1].second,b[i]);
}
for(int i=1;i<=n;i++){
if(ans[i].second-ans[i].first+1==i){cout<<1<<" ";}
else{
cout<<0<<" ";
}
}
END;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code: def DiceProblem(N):
return (7-N)
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
static int diceProblem(int N){
return (7-N);
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given a cubic dice with 6 faces. All the individual faces have numbers printed on them. The numbers are in the range of 1 to 6, like any <b>ordinary dice</b>. You will be provided with a face of this cube, your task is to find the number on the opposite face of the cube.
<b>Note</b>:- The sum of numbers on all opposite faces of the die is constant<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>DiceProblem()</b> that takes the integer N(face) as parameter.
<b>Constraints:</b>
1 <= N <= 6Return the number on the opposite side.Sample Input:-
2
Sample Output:-
5
Sample Input:-
1
Sample Output:-
6, I have written this Solution Code:
int diceProblem(int N){
return (7-N);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements and an integer D. Your task is to rotate the array D times in a circular manner from the right to left direction. Consider the examples for better understanding:-
Try to do without creating another arrayUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions <b>rotate()</b> that takes the array, size of the array, and the integer d as a parameter.
Constraints:
1 <= T <= 25
2 <= N <= 10^4
1<=D<=10^5
1 <= A[i] <= 10^5For each test case, you just need to rotate the array by D times. The driver code will prin the rotated array in a new line.Sample Input:
2
8
4
1 2 3 4 5 6 7 8
10
3
1 2 3 4 5 6 7 8 9 10
Sample Output:
5 6 7 8 1 2 3 4
4 5 6 7 8 9 10 1 2 3
Explanation(might now be the optimal solution):
Testcase 1:
Follow the below steps:-
After the first rotation, the array becomes 2 3 4 5 6 7 8 1
After the second rotation, the array becomes 3 4 5 6 7 8 1 2
After the third rotation, the array becomes 4 5 6 7 8 1 2 3
After the fourth rotation, the array becomes 5 6 7 8 1 2 3 4
Hence the final result: 5 6 7 8 1 2 3 4, I have written this Solution Code: public static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static void rotate(int arr[], int n, int d){
d = d % n;
int g_c_d = gcd(d, n);
for (int i = 0; i < g_c_d; i++) {
/* move i-th values of blocks */
int temp = arr[i];
int j = i;
boolean win=true;
while (win) {
int k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Find the Highest Common divisor (H.C.F) between N and M.<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 <i>HCF()</i> which contains the given number N and P.
<b>Constraints:</b>
1 <= N, P <= 10<sup>5</sup>
<b>Note:</b>
<i>But there is a catch here given user function has already code in it which may or may not be correct, now you need to figure out these and correct if it is required</i>Return the H.C.F of the given numbers.Sample Input:-
2 4
Sample Output:-
2
Sample Input:-
3 8
Sample Output:-
1, I have written this Solution Code: static int HCF(int N, int P){
int ans=1;
for(int i=2;i<=N;i++){
if(N%i==0 && P%i==0){
ans=i;
}
}
return ans;
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array <b>arr[]</b> of size <b>N</b> without duplicates, and given a value <b>x</b>. Find the floor of x in given array. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.
Try to use binary search to solve this problem.First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and element whose floor is to be searched. Last line of input contains array elements.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 10^5
1 ≤ arr[i] ≤ 10^18
0 ≤ X ≤ arr[n-1]Output the index of floor of x if exists, else print -1. Use 0-indexingInput:
3
7 0
1 2 8 10 11 12 19
7 5
1 2 8 10 11 12 19
7 10
1 2 8 10 11 12 19
Output:
-1
1
3
Explanation:
Testcase 1: No element less than or equal to 0 is found. So output is "-1".
Testcase 2: Number less than or equal to 5 is 2, whose index is 1(0-based indexing).
Testcase 3: Number less than or equal to 10 is 10 and its index is 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static int BS(int arr[],int x,int start,int end){
if( start > end) return start-1;
int mid = start + (end - start)/2;
if(arr[mid]==x) return mid;
if(arr[mid]>x) return BS(arr,x,start,mid-1);
return BS(arr,x,mid+1,end);
}
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
String[] s;
for(;t>0;t--){
s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
s = br.readLine().split(" ");
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = Integer.parseInt(s[i]);
int res = BS(arr,x,0,arr.length-1);
System.out.println(res);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array <b>arr[]</b> of size <b>N</b> without duplicates, and given a value <b>x</b>. Find the floor of x in given array. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.
Try to use binary search to solve this problem.First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and element whose floor is to be searched. Last line of input contains array elements.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 10^5
1 ≤ arr[i] ≤ 10^18
0 ≤ X ≤ arr[n-1]Output the index of floor of x if exists, else print -1. Use 0-indexingInput:
3
7 0
1 2 8 10 11 12 19
7 5
1 2 8 10 11 12 19
7 10
1 2 8 10 11 12 19
Output:
-1
1
3
Explanation:
Testcase 1: No element less than or equal to 0 is found. So output is "-1".
Testcase 2: Number less than or equal to 5 is 2, whose index is 1(0-based indexing).
Testcase 3: Number less than or equal to 10 is 10 and its index is 3., I have written this Solution Code: def bsearch(arr,e,l,r):
if(r>=l):
mid = l + (r - l)//2
if (arr[mid] == e):
return mid
elif arr[mid] > e:
return bsearch(arr, e,l, mid-1)
else:
return bsearch(arr,e, mid + 1, r)
else:
return r
t=int(input())
for i in range(t):
ip=list(map(int,input().split()))
l=list(map(int,input().split()))
le=ip[0]
e=ip[1]
print(bsearch(sorted(l),e,0,le-1)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a sorted array <b>arr[]</b> of size <b>N</b> without duplicates, and given a value <b>x</b>. Find the floor of x in given array. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.
Try to use binary search to solve this problem.First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and element whose floor is to be searched. Last line of input contains array elements.
<b>Constraints:</b>
1 ≤ T ≤ 100
1 ≤ N ≤ 10^5
1 ≤ arr[i] ≤ 10^18
0 ≤ X ≤ arr[n-1]Output the index of floor of x if exists, else print -1. Use 0-indexingInput:
3
7 0
1 2 8 10 11 12 19
7 5
1 2 8 10 11 12 19
7 10
1 2 8 10 11 12 19
Output:
-1
1
3
Explanation:
Testcase 1: No element less than or equal to 0 is found. So output is "-1".
Testcase 2: Number less than or equal to 5 is 2, whose index is 1(0-based indexing).
Testcase 3: Number less than or equal to 10 is 10 and its index is 3., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int t; cin >> t;
while(t--){
int n, x; cin >> n >> x;
for(int i = 0; i < n; i++)
cin >> a[i];
int l = -1, h = n;
while(l+1 < h){
int m = (l + h) >> 1;
if(a[m] <= x)
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: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: def OccurenceOfX(N,X):
cnt=0
for i in range(1, N+1):
if(X%i==0 and X/i<=N):
cnt=cnt+1
return cnt, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code:
int OccurenceOfX(int N,long X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
int main()
{
, In this Programming Language: C, Now tell me if this Code is compilable or not? | Compilable |
For this Question: For a given number N, Consider a table of N X N in which elements at the intersection of ith row and jth column are the product of i and j, i. e i x j.
Also given a positive integer X. Your task is to count the number of elements in the table that contain the integer X.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>OccurenceOfX()</b> that takes the integer N and the integer X as parameter.
Constraints:-
1 <= N <= 10^5
1 <= X <= 10^9Return the count of X.Sample Input:-
5 5
Sample Output:-
2
Explanation:-
table :-
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Count of occurrence of X is :- 2
Sample Input:-
10 13
Sample Output:-
0, I have written this Solution Code: public static int OccurenceOfX(int N,int X){
int cnt=0,i;
for( i=1;i<=N;i++){
if(X%i==0 && X/i<=N){cnt++;}}
return cnt;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t= Integer.parseInt(br.readLine());
if(t%2==0)
System.out.println("Even");
else
System.out.println("Odd");
}
catch (Exception e){
return;
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Write a program to check whether a number is even or odd using switch case.First Line of the input contains the number n.
<b>Constraints</b>
1 <= n <= 1e9If the number is even print "Even" otherwise "Odd"Sample Input :
23
Sample Output :
Odd
Sample Input :
24
Sample Output :
Even, I have written this Solution Code: #include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
switch(num % 2)
{
case 0:
printf("Even");
break;
/* Else if n%2 == 1 */
case 1:
printf("Odd");
break;
}
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sky (the blue ranger) wants to replace Jack (the red ranger) from his position. For this, he needs to conquer the entire Base.
The Base can be represented as an M*N grid, and Sky needs to conquer it cell by cell. Sky starts conquering the Base from the cell (1, 1). In each move, he conquers the cell, and moves to an adjacent non- conquered cell (he cannot move if there is no adjacent non- conquered cell). Now, there is a catch, the last cell he needs to conquer is (M, N) so as to complete the quest for the red ranger tag!
Please let us know if Sky can replace Jack by conquering all the cells in the Base.
Note: The diagonal cells are not considered as adjacent cells.The first and the only line of input contains two integers M and N.
Constraints
1 <= M, N <= 1000Output "YES" (without quotes) if Sky can conquer the entire Base to replace Jack, else output "NO" (without quotes).Sample Input
2 2
Sample Output
NO
Explanation
The possible journeys of Sky ending at (2, 2) can be:
(1, 1) - > (1, 2) - > (2, 2)
(1, 1) - > (2, 1) - > (2, 2)
Since, in each of the path that Sky takes, the total cells covered are not 4, hence Sky cannot conquer the entire base.
Sample Input
3 3
Sample Output
YES, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[]=br.readLine().split(" ");
int m=Integer.parseInt(s[0]);
int n=Integer.parseInt(s[1]);
if(m%2==0 && n%2==0)
System.out.println("NO");
else
System.out.println("YES");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sky (the blue ranger) wants to replace Jack (the red ranger) from his position. For this, he needs to conquer the entire Base.
The Base can be represented as an M*N grid, and Sky needs to conquer it cell by cell. Sky starts conquering the Base from the cell (1, 1). In each move, he conquers the cell, and moves to an adjacent non- conquered cell (he cannot move if there is no adjacent non- conquered cell). Now, there is a catch, the last cell he needs to conquer is (M, N) so as to complete the quest for the red ranger tag!
Please let us know if Sky can replace Jack by conquering all the cells in the Base.
Note: The diagonal cells are not considered as adjacent cells.The first and the only line of input contains two integers M and N.
Constraints
1 <= M, N <= 1000Output "YES" (without quotes) if Sky can conquer the entire Base to replace Jack, else output "NO" (without quotes).Sample Input
2 2
Sample Output
NO
Explanation
The possible journeys of Sky ending at (2, 2) can be:
(1, 1) - > (1, 2) - > (2, 2)
(1, 1) - > (2, 1) - > (2, 2)
Since, in each of the path that Sky takes, the total cells covered are not 4, hence Sky cannot conquer the entire base.
Sample Input
3 3
Sample Output
YES, I have written this Solution Code: m,n=map(int, input().split())
if(m%2 or n%2):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Sky (the blue ranger) wants to replace Jack (the red ranger) from his position. For this, he needs to conquer the entire Base.
The Base can be represented as an M*N grid, and Sky needs to conquer it cell by cell. Sky starts conquering the Base from the cell (1, 1). In each move, he conquers the cell, and moves to an adjacent non- conquered cell (he cannot move if there is no adjacent non- conquered cell). Now, there is a catch, the last cell he needs to conquer is (M, N) so as to complete the quest for the red ranger tag!
Please let us know if Sky can replace Jack by conquering all the cells in the Base.
Note: The diagonal cells are not considered as adjacent cells.The first and the only line of input contains two integers M and N.
Constraints
1 <= M, N <= 1000Output "YES" (without quotes) if Sky can conquer the entire Base to replace Jack, else output "NO" (without quotes).Sample Input
2 2
Sample Output
NO
Explanation
The possible journeys of Sky ending at (2, 2) can be:
(1, 1) - > (1, 2) - > (2, 2)
(1, 1) - > (2, 1) - > (2, 2)
Since, in each of the path that Sky takes, the total cells covered are not 4, hence Sky cannot conquer the entire base.
Sample Input
3 3
Sample Output
YES, 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, m; cin>>n>>m;
if(n%2 || m%2){
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: There is a function F defined as,
F(x) = GCD(1, x) + GCD(2, x) +. . + GCD(x, x)
Your task is to find F(x), for the given x.First line contain integer T denoting number of test cases.
Next t lines contain an integer x.
Constraints
1 <= T <= 20000
1 <= x <= 100000For each test case print F(n) in separate lineSample Input
5
1
2
3
4
5
Sample output
1
3
5
8
9, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static int getCount(int no)
{
int result = no;
for(int p = 2; p * p <= no; ++p)
{
if (no % p == 0)
{
while (no % p == 0)
no /= p;
result -= result / p;
}
}
if (no > 1)
result -= result / no;
return result;
}
static int sumOfGCDofPairs(int n)
{
int res = 0;
for(int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
int d1 = i;
int d2 = n / i;
res += d1 * getCount(n/d1);
if (d1 != d2)
res += d2 * getCount(n/d2);
}
}
return res;
}
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine().trim());
for(int T=0;T<t;T++)
{
int x=Integer.parseInt(br.readLine().trim());
System.out.println(sumOfGCDofPairs(x));
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a function F defined as,
F(x) = GCD(1, x) + GCD(2, x) +. . + GCD(x, x)
Your task is to find F(x), for the given x.First line contain integer T denoting number of test cases.
Next t lines contain an integer x.
Constraints
1 <= T <= 20000
1 <= x <= 100000For each test case print F(n) in separate lineSample Input
5
1
2
3
4
5
Sample output
1
3
5
8
9, I have written this Solution Code: def getCount(d,n):
no = n // d
result = no
p=2
while(p * p <= no):
if(no % p == 0):
while(no % p == 0):
no = no // p
result = result - (result // p)
p += 1
if(no > 1):
result = result - (result // no)
return result
def getSumOfGCDOfPairs(n):
result = 0
i =1
while(i * i <= n):
if(n % i == 0):
d1 = i
d2 = n // i
result = result + (d1 * getCount(d1, n))
if(d1 != d2):
result = result + (d2 * getCount(d2, n))
i += 1
return result
k = int(input())
for i in range(k):
n = int(input())
sum1 = 0
sum1 = getSumOfGCDOfPairs(n)
print(sum1), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: There is a function F defined as,
F(x) = GCD(1, x) + GCD(2, x) +. . + GCD(x, x)
Your task is to find F(x), for the given x.First line contain integer T denoting number of test cases.
Next t lines contain an integer x.
Constraints
1 <= T <= 20000
1 <= x <= 100000For each test case print F(n) in separate lineSample Input
5
1
2
3
4
5
Sample output
1
3
5
8
9, I have written this Solution Code: #include<iostream>
#include<string>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cmath>
#include<set>
#include<climits>
using namespace std;
#define mod 1000000007
#define MAX 100000
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
vector < int > phi(MAX+1);
vector <long long > result(MAX+1);
void euler_totient(){
for(int i = 1 ; i <= MAX ; i++) phi[i] = i;
for(long long i = 2 ; i <= MAX ; i++){
if(phi[i] == i){
for(long long j = i ; j <= MAX ; j += i){
phi[j]/=i;
phi[j]*=(i-1);
}
}
}
}
void precalc(){
for(int i = 1 ; i <= MAX ; i++){
result[i] = i ;
}
for(int i = 1 ; i <= MAX ; i++){
for(int j = 2 ; j*i <= MAX ; j++){
result[i*j] += i*phi[j];
}
}
}
int main(){
fast();
euler_totient();
precalc();
int t;cin>>t;
while(t--){
int n;cin>>n;
cout<<result[n]<<"\n";
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given Q queries. Each query consists of a single number N. You can perform any one of the 2 operations in a single move:
1: If we take 2 integers a and b where, N=a*b, (a and b are not equal to 1), then we can change N=max(a,b)
2: Decrease the value of N by 1 .
For each query determine the minimum number of moves required to reduce the value of N to 0.The first line contains the integer Q.
The next Q lines each contain an integer, N.
1 <= Q <= 1000
1 <= N <= 1000000Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves.
Follow 3-> 2 -> 1 ->0 . Hence, 3 moves.
For the case 2, we can either go 4 -> 3 -> 2 -> 1 -> 0 or 4-> 2 -> 1 ->0 . The 2nd option is more optimal. Hence, 3 moves., 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 q = sc.nextInt();
int n = 1000000;
int dp[] = new int[n+1];
for(int i=0; i<n; i++){
dp[i] = Integer.MAX_VALUE;
}
dp[0] = 0;
dp[1] = 1;
for(int i=2; i<n; i++){
dp[i] = Math.min(dp[i], 1+dp[i-1]);
for(int j=2; j<=i && i*j<n; j++){
dp[i*j] = Math.min(dp[i*j], 1+dp[i]);
}
}
while(q-- > 0){
int x = sc.nextInt();
System.out.println(dp[x]);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given Q queries. Each query consists of a single number N. You can perform any one of the 2 operations in a single move:
1: If we take 2 integers a and b where, N=a*b, (a and b are not equal to 1), then we can change N=max(a,b)
2: Decrease the value of N by 1 .
For each query determine the minimum number of moves required to reduce the value of N to 0.The first line contains the integer Q.
The next Q lines each contain an integer, N.
1 <= Q <= 1000
1 <= N <= 1000000Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves.
Follow 3-> 2 -> 1 ->0 . Hence, 3 moves.
For the case 2, we can either go 4 -> 3 -> 2 -> 1 -> 0 or 4-> 2 -> 1 ->0 . The 2nd option is more optimal. Hence, 3 moves., 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 = 1e6 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
vector<int> v[N];
int dp[N];
void solve(){
for(int i = 1; i < N; i++)
dp[i] = inf;
dp[1] = 1;
for(int i = 2; i < N; i++){
dp[i] = min(dp[i], 1 + dp[i-1]);
for(int j = 1; j <= i && j*i < N; j++)
dp[i*j] = min(dp[i*j], 1 + dp[i]);
}
int q; cin >> q;
while(q--){
int n; cin >> n;
cout << dp[n] << 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: Given two positive integers B and A, find the smallest integer height such that there exists a Triangle with base B and area atleast A.The first line of input contains a single integer T, denoting the number of testcases. Each test case contains only one line i.e. B(base) and A(area) separated by space.
Constraints:
1 <= T <= 10
1 <= B <= 100000
1 <= A <= 100000For each testcase in new line you need to print the minimum height of triangle with base B and area atleast A.Input:
2
2 2
17 100
Output:
2
12, I have written this Solution Code: import math
def minheight(B,A):
h=(2*A)/B
return math.ceil(h)
T=int(input())
for i in range(T):
B,A=map(int,input().split())
print(minheight(B,A)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two positive integers B and A, find the smallest integer height such that there exists a Triangle with base B and area atleast A.The first line of input contains a single integer T, denoting the number of testcases. Each test case contains only one line i.e. B(base) and A(area) separated by space.
Constraints:
1 <= T <= 10
1 <= B <= 100000
1 <= A <= 100000For each testcase in new line you need to print the minimum height of triangle with base B and area atleast A.Input:
2
2 2
17 100
Output:
2
12, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int t;
cin>>t;
while(t--){
long long x,y;
cin>>x>>y;
y=y*2;
long long p=y/x;
if(y%x!=0){p++;}
cout<<p<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You will be given an array of several arrays that each contain integers and your goal is to write a function that
will sum up all the numbers in all the arrays. For example, if the input is [[3, 2], [1], [4, 12]] then your
program should output 22 because 3 + 2 + 1 + 4 + 12 = 22An array containing arrays which can contain any number of elements.Sum of all the elements in all of the arrays.Sample input:-
[[3, 2], [1], [4, 12]]
Sample output:-
22
Explanation:-
3 + 2 + 1 + 4 + 12 = 22, I have written this Solution Code: function sum_array(arr) {
// store our final answer
var sum = 0;
// loop through entire array
for (var i = 0; i < arr.length; i++) {
// loop through each inner array
for (var j = 0; j < arr[i].length; j++) {
// add this number to the current final sum
sum += arr[i][j];
}
}
console.log(sum);
}, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alex always skips math class. As a punishment, his teacher has given him an array of size n where every number except one is the power of x.
Being poor in mathematics, Alex has asked for your help to solve the problem. Can you help him to find the bad number which is not a power of x?
<b>NOTE:</b>
It is guaranteed that there always exists an answer.The first line of the input contains the integers n and x
The following line contains n integers describing the array a
<b>Constraints</b>
2 ≤ n ≤ 1000
1 ≤ x ≤ 20
1 &le a<sub>i</sub> ≤ 10<sup>8</sup>For each test case, output a single line containing the bad number.input
6 7
16807 343 50 823543 2401 5764801
output
50, I have written this Solution Code: y=input().split()
n=int(y[0])
x=int(y[1])
a=input().split()
a=list(map(int,a))
k=0
for i in a:
r=i
while r!=1:
r/=x
if int(r)!=r:
print(i)
k=1
break
if k==1:
break, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alex always skips math class. As a punishment, his teacher has given him an array of size n where every number except one is the power of x.
Being poor in mathematics, Alex has asked for your help to solve the problem. Can you help him to find the bad number which is not a power of x?
<b>NOTE:</b>
It is guaranteed that there always exists an answer.The first line of the input contains the integers n and x
The following line contains n integers describing the array a
<b>Constraints</b>
2 ≤ n ≤ 1000
1 ≤ x ≤ 20
1 &le a<sub>i</sub> ≤ 10<sup>8</sup>For each test case, output a single line containing the bad number.input
6 7
16807 343 50 823543 2401 5764801
output
50, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x;
cin >> n >> x;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<long long> powers;
long long MX = 1e8;
powers.push_back(1); // to store all the powers of x which are <= 10^8
while (true) {
long long curr = powers.back() * x;
if (curr <= MX) powers.push_back(curr);
else break;
}
for (int i = 0; i < n; i++) {
// checking if a[i] is in the powers array, i.e whether a[i] is a power of x or not
bool found = binary_search(powers.begin(), powers.end(), a[i]);
if (!found) {
cout << a[i];
return 0;
}
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Alex always skips math class. As a punishment, his teacher has given him an array of size n where every number except one is the power of x.
Being poor in mathematics, Alex has asked for your help to solve the problem. Can you help him to find the bad number which is not a power of x?
<b>NOTE:</b>
It is guaranteed that there always exists an answer.The first line of the input contains the integers n and x
The following line contains n integers describing the array a
<b>Constraints</b>
2 ≤ n ≤ 1000
1 ≤ x ≤ 20
1 &le a<sub>i</sub> ≤ 10<sup>8</sup>For each test case, output a single line containing the bad number.input
6 7
16807 343 50 823543 2401 5764801
output
50, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = sc.nextInt();
int ans = 0;
for(int i=0; i<n; i++){
int a = sc.nextInt();
if(!solve(a,x)){
System.out.print(a);
break;
}
}
}
static boolean solve(int a, int b){
while(a>1){
if(a%b!=0)
return false;
a = a/b;
}
return true;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings X and Y containing both uppercase and lowercase alphabets. The task is to find the length of the longest common substring.Input consist of two lines containing the strings X and Y respectively.
1 <= length(X), length(Y) <= 100Print in a single line the length of the longest common substring of the two strings.Input
abcdgh
acdghr
Output
4
Example:
Testcase 1: cdgh is the longest substring present in both of the strings., 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();
String str2=br.readLine();
int max_len=0;
for(int i=0;i<str.length();i++)
{
for(int j=0;j<str2.length();j++)
{
int len=0;
int u=i;
while(i<str.length() && j<str2.length() && str.charAt(i)==str2.charAt(j))
{
len++;
i++;
j++;
}
i=u;
if(max_len<len)
max_len=len;
}
}
System.out.println(max_len);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given two strings X and Y containing both uppercase and lowercase alphabets. The task is to find the length of the longest common substring.Input consist of two lines containing the strings X and Y respectively.
1 <= length(X), length(Y) <= 100Print in a single line the length of the longest common substring of the two strings.Input
abcdgh
acdghr
Output
4
Example:
Testcase 1: cdgh is the longest substring present in both of the strings., I have written this Solution Code: /* Dynamic Programming solution to find length of the
longest common substring */
#include<iostream>
#include<string.h>
using namespace std;
int LCSubStr(string X, string Y, int m, int n)
{
// common suffix of X[0..i-1] and Y[0..j-1].
int LCSuff[m+1][n+1];
int result = 0; // To store length of the
// longest common substring
/* Following steps build LCSuff[m+1][n+1] in
bottom up fashion. */
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
// The first row and first column
// entries have no logical meaning,
// they are used only for simplicity
// of program
if (i == 0 || j == 0)
LCSuff[i][j] = 0;
else if (X[i-1] == Y[j-1])
{
LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
result = max(result, LCSuff[i][j]);
}
else LCSuff[i][j] = 0;
}
}
return result;
}
/* Driver program to test above function */
int main()
{
string X, Y;
cin >> X >> Y;
int m = X.length();
int n = Y.length();
cout << LCSubStr(X, Y, m, n);
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a circular linked list consisting of N nodes and an integer K, your task is to add the integer K at the end of the list.
<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>Insertion()</b> that takes head node of circular linked list and the integer K as parameter.
Constraints:
1 <=N <= 1000
1 <= Node.data, K<= 1000Return the head node of the modified circular linked list.Sample Input 1:-
3
1- >2- >3
4
Sample Output 1:-
1- >2- >3- >4
Sample Input 2:-
3
1- >3- >2
1
Sample Output 2:-
1- >3- >2- >1, I have written this Solution Code: public static Node Insertion(Node head, int K){
Node node=head;
while ( node.next != head)
{node = node.next; }
Node temp = new Node(K);
node.next=temp;
temp.next=head;
return head;}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
static boolean isPrime(long n)
{
boolean ans=true;
if(n<=3)
{
ans=true;
}
else if(n%2==0 || n%3==0)
{
ans=false;
}
else
{
for(int i=5;i*i<=n;i+=6)
{
if(n%i==0 || n%(i+2)==0)
{
ans=false;
break;
}
}
}
return ans;
}
public static void main (String[] args) throws IOException{
BufferedReader scan=new BufferedReader(new InputStreamReader(System.in));
long n=Long.parseLong(scan.readLine());
long ans=0;
while(true)
{
if(isPrime(n+ans))
{
break;
}
ans++;
}
System.out.println(ans);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: def prime(n):
for i in range(2,int(n**.5)+1):
if(n%i==0):
return False
return True
def check(n):
if(n%2==0):
n +=1
while(True):
if(prime(n)==True):
return n
n +=2;
return n
n=int(input())
x=check(n)
print(x-n), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Piyush has N chocolates in his extremely huge bag. He wants to buy some chocolates (maybe 0) so that the total number of chocolates he has in his bag can never be fairly divided into piles.
A division is considered fair if there are at least 2 piles and each pile has more than 1 chocolate. Moreover, each pile should contain an equal number of chocolates.
You need to help Piyush find the minimum number of chocolates he needs to buy.The first and the only line of input contains N, the total number of chocolates Piyush has in his bag currently.
Constraints
2 ≤ N ≤ 1000000000The output should contain only one integer, the minimum number of chocolates Piyush needs to buy so that the total number of chocolates can never be fairly divided.Sample Input 1
8
Sample Output 1
3
Sample Input 2
17
Sample Output 2
0, I have written this Solution Code: #include <bits/stdc++.h>
// #define ll long long
using namespace std;
int main(){
long long x,n;
cin>>n;
for(int i=n;i<n+500;i++){
x=i;
long long p=sqrt(x);
for(int j=2;j<=p;j++){
if(x%j==0){goto f;}
}
cout<<i-n;
return 0;
f:;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andy loves Xor. Dwight gives Andy an integer N and asks him to tell whether XOR of all the integers from 1 to N is even or odd. As Andy is busy flirting with Erin, help him solve this problem.Input contains a single integer N.
Constraints:
1 <= N <= 10^9Print "even" if XOR of all the integers from 1 to N is even and if it is odd print "odd".Sample Input 1
3
Sample Output 1
even
Explanation: Xor of 1, 2 and 3 is 0 which is even.
Sample Input 2
5
Sample Output 2
odd
Explanation: Xor of 1, 2, 3, 4 and 5 is 1 which is odd., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int input= Integer.parseInt(reader.readLine());
int result=1;
for(int i=2;i<=input;i++)
{
result = result^i;
}
if(result%2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
catch(Exception e){
System.out.println("Something went wrong"+e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andy loves Xor. Dwight gives Andy an integer N and asks him to tell whether XOR of all the integers from 1 to N is even or odd. As Andy is busy flirting with Erin, help him solve this problem.Input contains a single integer N.
Constraints:
1 <= N <= 10^9Print "even" if XOR of all the integers from 1 to N is even and if it is odd print "odd".Sample Input 1
3
Sample Output 1
even
Explanation: Xor of 1, 2 and 3 is 0 which is even.
Sample Input 2
5
Sample Output 2
odd
Explanation: Xor of 1, 2, 3, 4 and 5 is 1 which is odd., I have written this Solution Code: n=int(input())
odds = n//2 if n%2==0 else (n+1)//2
if(odds % 2 == 0 ):
print("even")
else:
print("odd"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Andy loves Xor. Dwight gives Andy an integer N and asks him to tell whether XOR of all the integers from 1 to N is even or odd. As Andy is busy flirting with Erin, help him solve this problem.Input contains a single integer N.
Constraints:
1 <= N <= 10^9Print "even" if XOR of all the integers from 1 to N is even and if it is odd print "odd".Sample Input 1
3
Sample Output 1
even
Explanation: Xor of 1, 2 and 3 is 0 which is even.
Sample Input 2
5
Sample Output 2
odd
Explanation: Xor of 1, 2, 3, 4 and 5 is 1 which is odd., 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;
if(n%4==1||n%4==2)
cout<<"odd";
else
cout<<"even";
#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: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: [o,a,u]=[int(i) for i in input().split()]
s=o+a+u
[x,y]=[int(i) for i in input().split()]
o+=u
a+=u
if o<=x and a<=y:
print(s)
elif o<=x:
print(y)
elif a<=y:
print(x)
else:
print(min(x,y)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main implements Runnable {
private boolean console=false;
private long MOD = 1000_000_007L;
private int MAX = 1000_001;
private void solve1(){
long a=in.nl(),b=in.nl(),c= in.nl();
long x=in.nl(),y=in.nl();
long ans =0;
if(x>y){
long t = x;
x=y;
y=t;
t = a;
a=b;
b=t;
}
if(x-a-c <0){
ans = x;
}else if(y-b-c<0) {
ans = y;
}else {
ans = a+b+c;
}
out.printLn(ans);
}
private void solve() {
int testCases = 1;
while (testCases-->0){
solve1();
}
}
private void add(TreeMap<Integer, Integer> map, int key){
map.put(key,map.getOrDefault(key,0)+1);
}
private void remove(TreeMap<Integer,Integer> map,int key){
if(!map.containsKey(key))
return;
map.put(key,map.getOrDefault(key,0)-1);
if(map.get(key)==0)
map.remove(key);
}
@Override
public void run() {
long time = System.currentTimeMillis();
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
solve();
out.flush();
System.err.println(System.currentTimeMillis()-time);
System.exit(0);
}catch (Exception e){
e.printStackTrace(); System.exit(1);
}
}
private FastInput in;
private FastOutput out;
public static void main(String[] args) throws Exception {
new Main().run();
}
private void init() throws FileNotFoundException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
try {
if (!console && System.getProperty("user.name").equals("puneetkumar")) {
outputStream = new FileOutputStream("/Users/puneetkumar/output.txt");
inputStream = new FileInputStream("/Users/puneetkumar/input.txt");
}
} catch (Exception ignored) {
}
out = new FastOutput(outputStream);
in = new FastInput(inputStream);
}
private void maualAssert(int a,int b,int c){
if(a<b || a>c) throw new RuntimeException();
}
private void maualAssert(long a,long b,long c){
if(a<b || a>c) throw new RuntimeException();
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private long ModPow(long x, long y, long MOD) {
long res = 1L;
x = x % MOD;
while (y >= 1L) {
if ((y & 1L) > 0) res = (res * x) % MOD;
x = (x * x) % MOD;
y >>= 1L;
}
return res;
}
private int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private int[] arrInt(int n){
int[] arr=new int[n];for(int i=0;i<n;++i)arr[i]=in.ni();
return arr;
}
private long[] arrLong(int n){
long[] arr=new long[n];for(int i=0;i<n;++i)arr[i]=in.nl();
return arr;
}
private int arrMax(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMax(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private int arrMin(int[] arr){
int ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
private long arrMin(long[] arr){
long ans = arr[0];
for(int i=1;i<arr.length;++i){
ans = max(ans,arr[i]);
}
return ans;
}
class FastInput { InputStream obj;
public FastInput(InputStream obj) {
this.obj = obj;
}
private byte inbuffer[] = new byte[1024];
private int lenbuffer = 0, ptrbuffer = 0;
private int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
try { lenbuffer = obj.read(inbuffer);
} catch (IOException e) { throw new InputMismatchException(); } }
if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }
String ns() { int b = skip();StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b)))
{ sb.appendCodePoint(b);b = readByte(); }return sb.toString();}
int ni() {
int num = 0, b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); }}
long nl() { long num = 0;int b;boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') { minus = true;b = readByte(); }
while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
return minus ? -num : num; }b = readByte(); } }
private boolean isSpaceChar(int c) {
return (!(c >= 33 && c <= 126));
}
int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
float nf() {return Float.parseFloat(ns());}
double nd() {return Double.parseDouble(ns());}
char nc() {return (char) skip();}
}
class FastOutput{
private final PrintWriter writer;
public FastOutput(OutputStream outputStream) {
writer = new PrintWriter(outputStream);
}
public PrintWriter getWriter(){
return writer;
}
public void print(Object obj){
writer.print(obj);
}
public void printLn(){
writer.println();
}
public void printLn(Object obj){
writer.print(obj);
printLn();
}
public void printSp(Object obj){
writer.print(obj+" ");
}
public void printArr(int[] arr){
for(int i:arr)
printSp(i);
printLn();
}
public void printArr(long[] arr){
for(long i:arr)
printSp(i);
printLn();
}
public void flush(){
writer.flush();
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Rick has got a huge candy bag. The bag contains a total of O orange candies, A apple candies, and U candies with an unknown flavor (can be orange or apple as well).
Now Morty wants to know the maximum number of candies that can be drawn from the bag blindfolded such that no more than X orange and Y apple candies are drawn out.
Can you solve the problem for Morty?The first line of the input contains three integers O, A, and U, representing the number of orange, apple, and unknown flavored candies respectively.
The second line of the input contains two integers X and Y representing the maximum number of orange and apple candies that can be drawn of the bag.
Constraints
0 <= O, A, U <= 10<sup>9</sup>
0 <= X, Y <= 10<sup>9</sup>Output a single integer, the maximum number of candies that can be drawn out of Rick's bag.Sample Input 1
4 2 7
2 7
Sample Output 1
2
Explanation: If we draw out more than 2 candies, there is a possible chance for 3 candies to be orange.
Sample Input 2
1 2 3
8 4
Sample Output 2
4, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(ll i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int a, b, c;
cin >> a >> b >> c;
int n, m;
cin >> n >> m;
int ans = a+b+c;
if (n < a + c) ans = min(n, ans);
if (m < b + c) ans = min(m, ans);
cout << ans;
}
signed main()
{
fast
#ifdef SWAPNIL07
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
// cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a valid parenthesis string S. A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings. Decompose the strings S such that S = P1 + P2 +. . + Pk and there does not exist a way to split any of P1, P2,. . Pk into P = A + B, where A and B are nonempty valid parentheses strings.
Print the concatenation of P1, P2,. , Pk after removing the outermost parentheses from each of them.First line contains the string S.
Constraints:
1 <= |S| <= 10^5Print the concatenation of P1, P2,....Pk after removing the outermost parentheses from each of them.Sample Input 1:
(()())(())
Sample Output 1:
()()()
Explanation:
(()())(()) = (()()) + (())
Concatenating after removing outer parentheses of each part
()() + () = ()()()
Sample Input 2:
(())
Sample Output 2:
()
Explanation:
After removing outer parentheses of (()), we get (), I have written this Solution Code: def removeOuterParentheses(S):
res = ""
count = 0
for c in S:
if (c == '(' and count > 0):
res += c
if (c == '('):
count += 1
if (c == ')' and count > 1):
res += c
if (c == ')'):
count -= 1
return res
if __name__ == '__main__':
S = input()
print(removeOuterParentheses(S)), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a valid parenthesis string S. A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings. Decompose the strings S such that S = P1 + P2 +. . + Pk and there does not exist a way to split any of P1, P2,. . Pk into P = A + B, where A and B are nonempty valid parentheses strings.
Print the concatenation of P1, P2,. , Pk after removing the outermost parentheses from each of them.First line contains the string S.
Constraints:
1 <= |S| <= 10^5Print the concatenation of P1, P2,....Pk after removing the outermost parentheses from each of them.Sample Input 1:
(()())(())
Sample Output 1:
()()()
Explanation:
(()())(()) = (()()) + (())
Concatenating after removing outer parentheses of each part
()() + () = ()()()
Sample Input 2:
(())
Sample Output 2:
()
Explanation:
After removing outer parentheses of (()), we get (), I have written this Solution Code: import java.io.*;
import java.util.*;
class Main
{
public static void main (String args[]) throws IOException{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String s = br.readLine();
Stack<Character> st = new Stack<Character>();
int l=0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ')')st.pop();
else st.push('(');
if (st.size() ==0){
for(int j=l+1;j<i;j++){
System.out.print(s.charAt(j));
}
l=i+1;
}
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to check whether the given number is prime or notThe input contains a single integer N.
Constraints:-
1 <= N <= 100000000000Print "YES" If the given number is prime else print "NO".Sample Input:-
2
Sample Output:-
YES
Sample Input:-
4
Sample Output:-
NO, 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);
long n = sc.nextLong();
int p=(int)Math.sqrt(n);
for(int i=2;i<=p;i++){
if(n%i==0){System.out.print("NO");return;}
}
System.out.print("YES");
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to check whether the given number is prime or notThe input contains a single integer N.
Constraints:-
1 <= N <= 100000000000Print "YES" If the given number is prime else print "NO".Sample Input:-
2
Sample Output:-
YES
Sample Input:-
4
Sample Output:-
NO, I have written this Solution Code: import math
def isprime(A):
if A == 1:
return False
sqrt = int(math.sqrt(A))
for i in range(2,sqrt+1):
if A%i == 0:
return False
return True
inp = int(input())
if isprime(inp):
print("YES")
else:
print("NO"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer N, your task is to check whether the given number is prime or notThe input contains a single integer N.
Constraints:-
1 <= N <= 100000000000Print "YES" If the given number is prime else print "NO".Sample Input:-
2
Sample Output:-
YES
Sample Input:-
4
Sample Output:-
NO, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main(){
long n;
cin>>n;
long x = sqrt(n);
for(int i=2;i<=x;i++){
if(n%i==0){cout<<"NO";return 0;}
}
cout<<"YES";
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given an array and Q queries. Your task is to perform these operations:-
enqueue: this operation will add an element to your current queue.
dequeue: this operation will delete the element from the starting of the queue
displayfront: this operation will print the element presented at the frontUser task:
Since this will be a functional problem, you don't have to take input. You just have to complete the functions:
<b>enqueue()</b>:- that takes the integer to be added and the maximum size of array as parameter.
<b>dequeue()</b>:- that takes the queue as parameter.
<b>displayfront()</b> :- that takes the queue as parameter.
Constraints:
1 <= Q(Number of queries) <= 10<sup>3</sup>
<b> Custom Input:</b>
First line of input should contains two integer number of queries Q and the size of the array N. Next Q lines contains any of the given three operations:-
enqueue x
dequeue
displayfrontDuring a dequeue operation if queue is empty you need to print "Queue is empty", during enqueue operation if the maximum size of array is reached you need to print "Queue is full" and during displayfront operation you need to print the element which is at the front and if the queue is empty you need to print "Queue is empty".
Note:-Each msg or element is to be printed on a new line
Sample Input:-
8 2
displayfront
enqueue 2
displayfront
enqueue 4
displayfront
dequeue
displayfront
enqueue 5
Sample Output:-
Queue is empty
2
2
4
Queue is full
Explanation:-here size of given array is 2 so when last enqueue operation perfomed the array was already full so we display the msg "Queue is full".
Sample input:
5 5
enqueue 4
enqueue 5
displayfront
dequeue
displayfront
Sample output:-
4
5, I have written this Solution Code: public static void enqueue(int x,int k)
{
if (rear >= k) {
System.out.println("Queue is full");
}
else {
a[rear] = x;
rear++;
}
}
public static void dequeue()
{
if (rear <= front) {
System.out.println("Queue is empty");
}
else {
front++;
}
}
public static void displayfront()
{
if (rear<=front) {
System.out.println("Queue is empty");
}
else {
int x = a[front];
System.out.println(x);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <b> The only difference between the easy and the tricky version is the constraint on the number of queries Q. Solving the tricky version automatically solves the easy version.</b>
You are given an array A of N integers. You will be given Q queries with two integers L and R. For each query, you need to find the sum of all elements of the array excluding the elements in the range [L, R] (both inclusive) modulo 10<sup>9</sup>+7.The first line of the input contains an integer N, the size of the array.
The second line of the input contains N integers, the elements of the array A.
The third line of the input contains an integer Q.
The next Q lines contain two integers L and R.
Constraints
1 <= N <= 200000
1 <= A[i] <= 1000000000
1 <= Q <= 100000
1 <= L <= R <= NOutput Q lines, each having a single integer the answer to Q-th query modulo 10<sup>9</sup>+7.Sample Input
4
5 3 3 1
2
2 3
4 4
Sample Output
6
11
Explanation: The array A = [5, 3, 3, 1].
First Query: Sum = 5 + 1 = 6.
Second Query: Sum = 5 + 3 + 3 = 11, 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') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main (String[] args) throws IOException {
Reader sc = new Reader();
int arrSize = sc.nextInt();
long[] arr = new long[arrSize];
for(int i = 0; i < arrSize; i++)
{
arr[i] = sc.nextLong();
}
long[] prefix = new long[arrSize];
prefix[0] = arr[0];
for(int i = 1; i < arrSize; i++)
{
long val = 1000000007;
prefix[i - 1] %= val;
long prefixSum = prefix[i - 1] + arr[i];
prefixSum %= val;
prefix[i] = prefixSum;
}
long[] suffix = new long[arrSize];
suffix[arrSize - 1] = arr[arrSize - 1];
for(int i = arrSize - 2; i >= 0; i--)
{
long val = 1000000007;
suffix[i + 1] %= val;
long suffixSum = suffix[i + 1] + arr[i];
suffixSum %= val;
suffix[i] = suffixSum;
}
int query = sc.nextInt();
for(int x = 1; x <= query; x++)
{
int l = sc.nextInt();
int r = sc.nextInt();
long val = 1000000007;
long ans = 0;
ans += (l != 1 ? prefix[l-2] : 0);
ans %= val;
ans += (r != arrSize ? suffix[r] : 0);
ans %= val;
System.out.println(ans);
}
}
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <b> The only difference between the easy and the tricky version is the constraint on the number of queries Q. Solving the tricky version automatically solves the easy version.</b>
You are given an array A of N integers. You will be given Q queries with two integers L and R. For each query, you need to find the sum of all elements of the array excluding the elements in the range [L, R] (both inclusive) modulo 10<sup>9</sup>+7.The first line of the input contains an integer N, the size of the array.
The second line of the input contains N integers, the elements of the array A.
The third line of the input contains an integer Q.
The next Q lines contain two integers L and R.
Constraints
1 <= N <= 200000
1 <= A[i] <= 1000000000
1 <= Q <= 100000
1 <= L <= R <= NOutput Q lines, each having a single integer the answer to Q-th query modulo 10<sup>9</sup>+7.Sample Input
4
5 3 3 1
2
2 3
4 4
Sample Output
6
11
Explanation: The array A = [5, 3, 3, 1].
First Query: Sum = 5 + 1 = 6.
Second Query: Sum = 5 + 3 + 3 = 11, I have written this Solution Code: n = int(input())
arr = list(map(int ,input().rstrip().split(" ")))
temp = 0
modified = []
for i in range(n):
temp = temp + arr[i]
modified.append(temp)
q = int(input())
for i in range(q):
s = list(map(int ,input().rstrip().split(" ")))
l = s[0]
r = s[1]
sum = 0
sum = ((modified[l-1]-arr[l-1])+(modified[n-1]-modified[r-1]))%1000000007
print(sum), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: <b> The only difference between the easy and the tricky version is the constraint on the number of queries Q. Solving the tricky version automatically solves the easy version.</b>
You are given an array A of N integers. You will be given Q queries with two integers L and R. For each query, you need to find the sum of all elements of the array excluding the elements in the range [L, R] (both inclusive) modulo 10<sup>9</sup>+7.The first line of the input contains an integer N, the size of the array.
The second line of the input contains N integers, the elements of the array A.
The third line of the input contains an integer Q.
The next Q lines contain two integers L and R.
Constraints
1 <= N <= 200000
1 <= A[i] <= 1000000000
1 <= Q <= 100000
1 <= L <= R <= NOutput Q lines, each having a single integer the answer to Q-th query modulo 10<sup>9</sup>+7.Sample Input
4
5 3 3 1
2
2 3
4 4
Sample Output
6
11
Explanation: The array A = [5, 3, 3, 1].
First Query: Sum = 5 + 1 = 6.
Second Query: Sum = 5 + 3 + 3 = 11, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int) v.size()
#define pr(v) For(i, 0, sz(v)) {cout<<v[i]<<" ";} cout<<endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for(int i=st; i<en; i++)
#define tr(x) for(auto it=x.begin(); it!=x.end(); it++)
#define fast std::ios::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long
#define ld long double
#define int long long
#define double long double
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define pi 3.141592653589793238
const int MOD = 1e9+7;
const int INF = 1LL<<60;
const int N = 2e5+5;
// it's swapnil07 ;)
#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif
void solve(){
int n; cin>>n;
vector<int> a(n+1);
vector<int> pre(n+1, 0);
int tot = 0;
For(i, 1, n+1){
cin>>a[i];
assert(a[i]>0LL && a[i]<=1000000000LL);
tot += a[i];
pre[i]=pre[i-1]+a[i];
}
int q; cin>>q;
while(q--){
int l, r; cin>>l>>r;
int s = tot - (pre[r]-pre[l-1]);
s %= MOD;
cout<<s<<"\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();
}
return 0;
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A message containing letters from A - Z is been encoded to numbers using the alphabetical order of the number i. e A - > 1, B - > 2,. , Z - >26.
Given the encoded message your task is to print the number of ways the message can be decoded.Input contains a single line containing the string S.
Constraints:-
1 < = N < = 5000
Note:- String will only contains integers from 0-9Print the number of ways to decode the message.
Note: Since answer can be quite large print your answer modulo 10^9 + 7Sample Input:-
226
Sample Output:-
2
Explanation:-
BZ VF BBF are the only possible decoded messages
Sample Input:-
102
Sample Output:-
1
Explanation:-
JB is the only possibility, 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();
System.out.print(numDecodings(str));
}
static int mod = 1000000007;
static int numDecodings(String str) {
if (str.charAt(0) == '0') {
return 0;
}
int n = str.length();
int[] currWays = new int[n + 1];
currWays[0] = 1;
currWays[1] = 1;
for (int i = 2; i <= n; i++) {
if (str.charAt(i - 1) > '0') {
currWays[i] = currWays[i - 1];
}
if (str.charAt(i - 2) == '1' || (str.charAt(i - 2) == '2' && str.charAt(i - 1) <= '6')) {
currWays[i] = (currWays[i] + currWays[i - 2]) % mod;
}
}
return currWays[n];
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A message containing letters from A - Z is been encoded to numbers using the alphabetical order of the number i. e A - > 1, B - > 2,. , Z - >26.
Given the encoded message your task is to print the number of ways the message can be decoded.Input contains a single line containing the string S.
Constraints:-
1 < = N < = 5000
Note:- String will only contains integers from 0-9Print the number of ways to decode the message.
Note: Since answer can be quite large print your answer modulo 10^9 + 7Sample Input:-
226
Sample Output:-
2
Explanation:-
BZ VF BBF are the only possible decoded messages
Sample Input:-
102
Sample Output:-
1
Explanation:-
JB is the only possibility, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 200001
#define MOD 1000000007
#define read(type) readInt<type>()
#define int long long
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int m[100001];
int solve(int i,string s){
if (i == s.length()) {
return 1;
}
if (s[i] == '0') {
return 0;
}
if (i== s.length()-1) {
return 1;
}
if (m[i]!=-1) {
return m[i];
}
int ans = solve(i+1, s)%MOD;
int t=stoi(s.substr(i, 2));
if ( t<= 26) {
ans += solve(i+2,s)%MOD;
}
ans%=MOD;
m[i]=ans;
return ans;
}
signed main(){
for(int i=0;i<100001;i++){
m[i]=-1;
}
string s;
cin>>s;
cout<<solve(0,s);
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int len = Integer.parseInt(br.readLine());
char[] str = new char[len];
for(int i = 0; i < len; i++){
if(i%2 == 0){
str[i] = 'a';
} else{
str[i] = 'b';
}
}
System.out.println(str);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc __builtin_popcountll
#define m_p make_pair
#define infi 1e18+1
#define eps 0.000000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>//usage rand<long long>()
T rand() {
return uniform_int_distribution<T>()(rng);
}
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
//////////////
auto clk=clock();
#define all(x) x.begin(),x.end()
#define S second
#define F first
#define sz(x) ((long long)x.size())
#define int long long
#define f80 __float128
#define pii pair<int,int>
#define rep(i,n) for (int i=0; i<(n); i++)
/////////////
signed main()
{
fastio;
#ifdef ANIKET_GOYAL
freopen("inputf.in","r",stdin);
freopen("outputf.in","w",stdout);
#endif
int n;
cin>>n;
string s(n,'a');
for(int i=1;i<n;i+=2)
s[i]='b';
cout<<s;
#ifdef ANIKET_GOYAL
// cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given a positive integer N, construct a string of length N such that no two adjacent characters are the same. Among all possible strings, find the lexicographically minimum string.
Note: You can use only lowercase characters from 'a' to 'z'.The first and only line of input contains an integer N.
<b>Constraints:</b>
1 <= N <= 10<sup>5</sup>Print the required string.Sample Input 1:
1
Sample Output 1:
a
Sample Input 2:
2
Sample Output 2:
ab, I have written this Solution Code: a="ab"
inp = int(input())
print(a*(inp//2)+a[0:inp%2]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code:
public static ArrayList<Integer> threeWayPartition(ArrayList<Integer> A, int lowVal, int highVal)
{
int n = A.size();
ArrayList<Integer> arr = A;
int start = 0, end = n-1;
for (int i=0; i<=end;)
{
// swapping the element with those at start
// if array element is less than lowVal
if (arr.get(i) < lowVal){
int temp=arr.get(i);
arr.add(i,arr.get(start));
arr.remove(i+1);
arr.add(start,temp);
arr.remove(start+1);
i++;
start++;
}
// swapping the element with those at end
// if array element is greater than highVal
else if (arr.get(i) > highVal){
int temp=arr.get(i);
arr.add(i,arr.get(end));
arr.remove(i+1);
arr.add(end,temp);
arr.remove(end+1);
end--;
}
// else just move ahead
else
i++;
}
return arr;
}
, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array A[] and a range [a, b]. The task is to partition the array around the range such that array is divided into three parts.
1) All elements smaller than a come first.
2) All elements in range a to b come next.
3) All elements greater than b appear in the end.
The individual elements of three sets can appear in any order. You are required to return the modified arranged array.
<b>Note:-</b>
In the case of custom input, you will get 1 if your code is correct else get a 0.<b>User Task:</b>
Since this is a functional problem you don't have to worry about the input. You just have to complete the function <b>threeWayPartition()</b> which contains following arguments.
A: input array list
low: starting integer of range
high: ending integer of range
<b>Constraints:</b>
1 <= T <= 100
1 <= N <= 10<sup>4</sup>
1 <= A[i] <= 10<sup>5</sup>
1 <= low <= high <= 10<sup>5</sup>
The Sum of N over all test case doesn't exceed 10^5For each test case return the modified array.Sample Input:
2
5
1 8 3 3 4
3 5
3
1 2 3
1 3
Sample Output:
1 3 3 4 8
1 2 3
<b>Explanation:</b>
Testcase 1: First, the array has elements less than or equal to 3. Then, elements between 3 and 5. And, finally elements greater than 5. So, one of the possible outputs is 1 3 3 4 8.
Testcase 2: First, the array has elements less than or equal to 1. Then, elements between 1 and 3. And, finally elements greater than 3. So, the output is 1 2 3., I have written this Solution Code: def threewayPartition(arr,low,high):
i=low-1
pivot = arr[high]
for j in range(low,high):
if arr[j]<=pivot:
i+=1
arr[i],arr[j]=arr[j],arr[i]
i+=1
arr[i],arr[high]=arr[high],arr[i]
return i
def quickSort(arr,low,high):
if low<high:
key=threewayPartition(arr,low,high)
quickSort(arr,low,key-1)
quickSort(arr,key+1,high)
return arr
t= int(input())
while t>0:
s = int(input())
arr = list(map(int,input().split()))
a,b = list(map(int,input().split()))
print(1)
t-=1, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) {
try{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(r);
String a = in.readLine();
String[] nums = a.split(" ");
long[] l = new long[3];
for(int i=0; i<3; i++){
l[i] = Long.parseLong(nums[i]);
}
Arrays.sort(l);
System.out.print(l[1]);
}
catch(Exception e){
System.out.println(e);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code:
//#define ASC
//#define DBG_LOCAL
#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 <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#define int long long
// #define int __int128
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define endl '\n'
#define fi first
#define se second
// const int mod = 1e9 + 7;
const int mod=998'244'353;
const long long INF = 2e18 + 10;
// const int INF=1e9+10;
#define readv(x, n) \
vector<int> x(n); \
for (auto &i : x) \
cin >> i;
template <typename T>
using v = vector<T>;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vector<vector<T>>>;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<vector<int>>>> vvvvi;
typedef vector<vector<double>> vvd;
typedef pair<int, int> pii;
int multiply(int a, int b, int in_mod) { return (int)(1LL * a * b % in_mod); }
int mult_identity(int a) { return 1; }
const double PI = acosl(-1);
auto power(auto a, auto b, const int in_mod)
{
auto prod = mult_identity(a);
auto mult = a % 2;
while (b != 0)
{
if (b % 2)
{
prod = multiply(prod, mult, in_mod);
}
if(b/2)
mult = multiply(mult, mult, in_mod);
b /= 2;
}
return prod;
}
auto mod_inv(auto q, const int in_mod)
{
return power(q, in_mod - 2, in_mod);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define stp cout << fixed << setprecision(20);
void solv()
{
int A ,B, C;
cin>>A>>B>>C;
vector<int> values;
values.push_back(A);
values.push_back(B);
values.push_back(C);
sort(all(values));
cout<<values[1]<<endl;
}
void solve()
{
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++)
{
// cout<<"Case #"<<i<<": ";
solv();
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cerr.tie(NULL);
#ifndef ONLINE_JUDGE
if (fopen("INPUT.txt", "r"))
{
freopen("INPUT.txt", "r", stdin);
freopen("OUTPUT.txt", "w", stdout);
}
#else
#ifdef ASC
namespace fs = std::filesystem;
std::string path = "./";
string filename;
for (const auto & entry : fs::directory_iterator(path)){
if( entry.path().extension().string() == ".in"){
filename = entry.path().filename().stem().string();
}
}
if(filename != ""){
string input_file = filename +".in";
string output_file = filename +".out";
if (fopen(input_file.c_str(), "r"))
{
freopen(input_file.c_str(), "r", stdin);
freopen(output_file.c_str(), "w", stdout);
}
}
#endif
#endif
// auto clk = clock();
// -------------------------------------Code starts here---------------------------------------------------------------------
signed t = 1;
// cin >> t;
for (signed test = 1; test <= t; test++)
{
// cout<<"Case #"<<test<<": ";
// cout<<endl;
solve();
}
// -------------------------------------Code ends here------------------------------------------------------------------
// clk = clock() - clk;
#ifndef ONLINE_JUDGE
// cerr << fixed << setprecision(6) << "\nTime: " << ((float)clk) / CLOCKS_PER_SEC << "\n";
#endif
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: You are given three distinct positive integers, A, B and C, in the input. Your task is to find their median.
The median of a set of integers is the number at the middle position if they are arranged in ascending order.The input consists of a single line containing three integers A, B and C.
<b> Constraints: </b>
1 ≤ A, B, C ≤ 10<sup>9</sup>
A, B, C are pairwise distinct.
Print a single integer, the median of the given numbers.Sample Input 1:
5 1 3
Sample Output 1:
3
Sample Input 2:
1 2 3
Sample Output 2:
2, I have written this Solution Code: lst = list(map(int, input().split()))
lst.sort()
print(lst[1]), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print “<strong>Not found</strong>” without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int t; cin >> t;
while(t--){
vector<int> v;
int n, x; cin >> n >> x;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p == x)
v.push_back(i-1);
}
if(v.size() == 0)
cout << "Not found\n";
else{
for(auto i: v)
cout << 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 integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print “<strong>Not found</strong>” without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, I have written this Solution Code: def position(n,arr,x):
res = []
cnt = 0
for i in arr:
if(i == x):
res.append(cnt)
cnt += 1
return res
, In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an integer array <strong>arr[]</strong> of size <strong>N</strong> and an element <strong>X</strong>. The task is to find and print the indices of the given element if it is present in array if not then print “<strong>Not found</strong>” without quotes.
<strong>Note</strong>: The elements may be present more than once.The first line of input contains T, denoting the number of test cases.
The first line of each test case contains N and X, N is the size of array and X is an element. Second line contains elements of array space separated. If not present then print "Not found" without quotes
<strong>Constraints</strong>:
1 <= T <= 100
1 <= N, X <= 10000
1 <= arr[i] <= 100000For each test case in new line you need to print all the positions where you find the X separated by space.
Assume 0-indexingInput:
2
5 6
2 3 6 5 6
4 3
2 4 6 5
Output:
2 4
Not found, I have written this Solution Code: import java.util.*;
import java.io.*;
import java.lang.*;
class Main
{
public static void main (String[] args)throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t =Integer.parseInt(read.readLine());
while(t-- > 0)
{
String str[] = read.readLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
int arr[] = new int[n];
str = read.readLine().trim().split(" ");
for(int i = 0; i < n; i++)
arr[i] = Integer.parseInt(str[i]);
findPositions(arr, n, x);
}
}
static void findPositions(int arr[], int n, int x)
{
boolean flag = false;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < n; i++)
{
if(arr[i] == x)
{
sb.append(i + " ");
flag = true;
}
}
if(flag ==true)
System.out.println(sb.toString());
else System.out.println("Not found");
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of citations (each citation is a non-negative integer) of a researcher, write a program to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Note: If there are several possible values for h, the maximum one is taken as the h-index.Input contains a single integer denoting the size of array.
The next line contains an array of integers, i.e., an array of citations
Constraints:
1 <= n <= 10^3
1 <= A[i] <= 10^3Output a single integer denoting the h-index.Input:
5
3 0 6 1 5
Output:
3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws Exception{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);
int sizeOfArray = Integer.parseInt(br.readLine());
String[] str = br.readLine().trim().split(" ");
int[] arr = new int[str.length];
for(int i=0; i<arr.length; i++){
arr[i] = Integer.parseInt(str[i]);
}
boolean bl = false;
for(int i=arr.length-1; i>=0; i--){
int count = 0;
for(int j=0; j<arr.length; j++){
if(arr[j]>=i+1){
count++;
}
}
if(count>=i+1){
System.out.println(i+1);
bl = true;
break;
}
}
if(bl==false){
System.out.println(0);
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of citations (each citation is a non-negative integer) of a researcher, write a program to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Note: If there are several possible values for h, the maximum one is taken as the h-index.Input contains a single integer denoting the size of array.
The next line contains an array of integers, i.e., an array of citations
Constraints:
1 <= n <= 10^3
1 <= A[i] <= 10^3Output a single integer denoting the h-index.Input:
5
3 0 6 1 5
Output:
3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 1e3 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
int a[N];
signed main() {
IOS;
int n; cin >> n;
for(int i = 1; i <= n; i++){
int p; cin >> p;
a[p]++;
}
for(int i = N-2; i >= 0; i--){
a[i] += a[i+1];
if(a[i] >= i)
return cout << i, 0;
}
return 0;
}, In this Programming Language: C++, 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 traverse the list and print its elements.<b>User Task:</b>
Since this will be a functional problem, you don't have to take input. You just have to complete the function <b>printList()</b> that takes head node of the linked list as parameter.
<b>Constraints:</b>
1 <=N <= 1000
1 <=Node.data<= 1000For each testcase you need to print the elements of the linked list separated by space. The driver code will take care of printing new line.Sample input
5
2 4 5 6 7
Sample output
2 4 5 6 7
Sample Input
4
1 2 3 5
Sample output
1 2 3 5, I have written this Solution Code: public static void printList(Node head) {
while(head!=null)
{
System.out.print(head.val + " ");
head=head.next;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Abhas likes to play with numbers. He is given integers N and K. Find the number of triples (a, b, c) of positive integers not greater than N such that a+b, b+c, and c+a are all multiples of K. The order of a, b, and c does matter, and some of them can be the same.The input line contains N and K separated by space.
<b>Constraints</b>
1≤N, K≤2×10^5
N and K are integers.Print the number of triples (a, b, c) of positive integers not greater than N such that a+b, b+c, and c+a are all multiples of K.<b>Sample Input 1</b>
3 2
<b>Sample Output 1</b>
9
<b>Sample Input 2</b>
5 3
<b>Sample Output 2</b>
1
<b>Sample Input 3</b>
35897 932
<b>Sample Output 3</b>
114191, I have written this Solution Code: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll get(ll x) {
return x*x*x;
}
int main() {
ll n,k,ans=0;
cin>>n>>k;
if (k&1) {
cout<<get(n/k);
}
else {
cout<<get(n/k)+get(2*n/k-n/k);
}
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Walter white is considered very intelligent person. He has a problem to solve. As he is suffering from cancer, can you help him solve it?
Given two integer arrays C and S of length c and s respectively. Index i of array S can be considered good if a subarray of length c can be formed starting from index i which is complimentary to array C.
Two arrays A, B of same length are considered complimentary if any cyclic permutation of A satisfies the property (A[i]- A[i-1]=B[i]-B[i-1]) for all i from 2 to length of A (1 indexing).
Calculate number of good positions in S .
<a href="https://mathworld.wolfram.com/CyclicPermutation.html">Cyclic Permutation</a>
1 2 3 4 has 4 cyclic permutations 2 3 4 1, 3 4 1 2, 4 1 2 3,1 2 3 4First line contains integer s (length of array S).
Second line contains s space separated integers of array S.
Third line contain integer c (length of array C).
Forth line contains c space separated integers of array C.
Constraints:
1 <= s <=1000000
1 <= c <=1000000
1 <= S[i], C[i] <= 10^9
Print the answer.
Input :
9
1 2 3 1 2 4 1 2 3
3
1 2 3
Output :
4
Explanation :
index 1- 1 2 3 matches with 1 2 3
index 2- 2 3 1 matches with 2 3 1(2 3 1 is cyclic permutation of 1 2 3)
index 3- 3 1 2 matches with 3 1 2(3 1 2 is cyclic permutation of 1 2 3)
index 7- 1 2 3 matches with 1 2 3
Input :
4
3 4 3 4
2
1 2
Output :
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 2000015
#define qw1 freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout);
#define qw2 freopen("input2.txt", "r", stdin); freopen("output2.txt", "w", stdout);
#define qw3 freopen("input3.txt", "r", stdin); freopen("output3.txt", "w", stdout);
#define qw4 freopen("input4.txt", "r", stdin); freopen("output4.txt", "w", stdout);
#define qw5 freopen("input5.txt", "r", stdin); freopen("output5.txt", "w", stdout);
#define qw6 freopen("input6.txt", "r", stdin); freopen("output6.txt", "w", stdout);
#define qw freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
int A[sz],B[sz],C[sz],D[sz],E[sz],F[sz],G[sz];
int n,m;
signed main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>A[i];
F[i]=A[i];
}
cin>>m;
for(int i=0;i<m;i++)
{
cin>>B[i];
G[i]=B[i];
C[m-i-1]=B[i];
}
C[m]=-500000000;
for(int i=0;i<n;i++)
{
C[i+m+1]=A[n-i-1];
}
int l=0,r=0;
for(int i=1;i<=n+m;i++)
{
if(i<=r)
{
E[i]=min(r-i+1,E[i-l]);
}
while(i+E[i]<=n+m && C[E[i]]-C[0]==C[i+E[i]]-C[i])
E[i]++;
if(i+E[i]-1>r) {
l=i;r=i+E[i]-1;
}
}
for(int i=0;i<m;i++)
{
C[i]=B[i];
}
for(int i=0;i<n;i++)
{
C[i+m+1]=A[i];
}
for(int i=0;i<n;i++)
{
A[i]=E[n+m-i];
}
l=0;
r=0;
for(int i=1;i<=n+m;i++)
{
if(i<=r)
{
D[i]=min(r-i+1,D[i-l]);
}
while(i+D[i]<=n+m && C[D[i]]-C[0]==C[i+D[i]]-C[i])
D[i]++;
if(i+D[i]-1>r) {
l=i;r=i+D[i]-1;
}
}
// cout<<0<<" ";
for(int i=0;i<n;i++)
{
B[i]=D[i+m+1];
// cout<<A[i]<<" ";
}
// cout<<endl;
// for(int i=0;i<n;i++)
// {
// cout<<B[i]<<" ";
// }cout<<endl;
// for(int i=0;i<=n;i++)
// {
// cout<<i<<" ";
// }
// cout<<endl;
int cnt=0;
vector<pii> xx,yy;
for(int i=0;i<=n;i++){
int a=0;
int b=0;
if(i>0) a=A[i-1];
if(i<n) b=B[i];
// cout<<i<<" "<<a<<" "<<b<<endl;
if(a+b>=m && (a==0 || b==0 ||(F[i]-F[i-1]==G[0]-G[m-1])))
{xx.pu(mp(i-a,i+b-m)); }
if(a==m) xx.pu(mp(i-a,i-a));
if(b==m ) xx.pu(mp(i,i));
}
sort(xx.begin(),xx.end());
for(int i=0;i<xx.size();i++)
{ // cout<<xx[i].fi<<" "<<xx[i].se<<endl;
if(yy.size()==0) yy.pu(mp(xx[i].fi,xx[i].se));
else{
int p=yy.size()-1;
// cout<<i<<" "<<xx[i].fi<<" "<<xx[i].se<<" " <<yy[p].se<<endl;
if(yy[p].se>=xx[i].se) continue;
if(yy[p].se>=xx[i].fi) yy[p].se=xx[i].se;
else yy.pu(mp(xx[i].fi,xx[i].se));
}
}
for(int i=0;i<yy.size();i++)
{ // cout<<yy[i].fi<<" "<<yy[i].se<<endl;
cnt+=yy[i].se-yy[i].fi+1;
}
cout<<cnt<<endl;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: A number (n) is represented in Linked List such that each digit corresponds to a node in linked list. Add 1 to it.
<b>Note:-</b> Linked list representation of a number is from left to right i.e if the number is 123 than in linked list it is represented as 3->2->1<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>addOne()</b> that takes head node of the linked list as parameter.
Constraints:
1 <=length of n<= 1000Return the head of the modified linked list.Input 1:
456
Output 1:
457
Input 2:
999
Output 2:
1000, I have written this Solution Code:
static Node addOne(Node head)
{
Node res = head;
Node temp = null, prev = null;
int carry = 1, sum;
while (head != null) //while both lists exist
{
sum = carry + head.data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0) {
Node x=new Node(carry);
temp.next=x;}
return res;
} , In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: import java.io.*;
import java.util.*;
class Main {
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
int t=Integer.parseInt(str[0]);
while(t-->0){
str=br.readLine().split(" ");
int k=Integer.parseInt(str[0]);
int n=Integer.parseInt(str[1]);
int[] arr=new int[n];
str=br.readLine().split(" ");
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(str[i]);
}
String[] st=new String[k];
for(int i=0;i<k;i++){
st[i]="";
}
for(int i=0;i<n;i++){
st[arr[i]%k]+="->"+str[i];
}
for(int i=0;i<k;i++){
if(st[i]!=""){
System.out.println(i+st[i]);
}
}
System.out.println("~");
}
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: t=int(input())
for i in range(t):
rowNcol=input().split()
els=input().split()
ds=[]
for i in range(int(rowNcol[0])):
col=[]
ds.append(col)
for i in range(int(rowNcol[1])):
ds[int(int(els[i])%int(rowNcol[0]))].append(els[i])
for i in range(len(ds)):
if(len(ds[i])==0):
continue
else:
print(i,end="")
for j in range(len(ds[i])):
print("->"+ds[i][j],end="")
print()
print("~"), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Separate chaining technique in hashing allows us to use a linked list at each hash slot to handle the problem of collisions. That is, every slot of the hash table is a linked list, so whenever a collision occurs, the element can be appened as a node to the linked list at the slot.
In this question, we'll learn how to fill up the hash table using Separate chaining technique. You are given hash table size which you'll use to insert elements into their correct position in the hash table i.e.(arr[i]%hashSize). You are also given an array arr. The size of the array is denoted by sizeOfArray. You need to fill up the hash table using Separate chaining and print the resultant hash table.The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains 2 lines of input. The first line contains size of the hashtable and the size of the array. The next line contains elements of the array
Constraints:
1 <= T <= 100
2 <= hashSize <= 10^3
1 <= sizeOfArray <= 10^3
0 <= arr[i] <= 10^7For each testcase, in a new line, print the hash table. You need to print the hash table as represented in the example.
Note: Please print tilde ( '~') character at the end of every testcase for separation of list from one another. Given below in the example represent to how to separate each testcase by tilde character.Sample Input:
2
10 6
92 4 14 24 44 91
10 5
12 45 36 87 11
Sample Output:
1->91
2->92
4->4->14->24->44
~
1->11
2->12
5->45
6->36
7->87
~
Explanation:
Testcase1: 92%10=2 so 92 goes to slot 2. 4%10=4 so 4 goes to slot 4. 14%10=4. But 4 is already occupied so we make a linked list at this position and add 14 after 4 in slot 4 and so on.
Testcase2: 12%10=2 so 12 goes to slot 2. 45%10=5 goes to slot 5. 36%10=6 goes to slot 6. 87%10=7 goes to slot 7 and finally 11%10=1 goes to slot 1., I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
#define max1 1000001
int main(){
int t;
cin>>t;
while(t--){
int n,m,x;
cin>>m>>n;
vector<int> a[m];
for(int i=0;i<n;i++){
cin>>x;
a[x%m].push_back(x);
}
for(int i=0;i<m;i++){
if(a[i].size()==0){continue;}
cout<<i<<"->";
for(int j=0;j<a[i].size()-1;j++){
cout<<a[i][j]<<"->";
}
cout<<a[i][a[i].size()-1]<<endl;
}
cout<<"~"<<endl;
}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: // arr is the array of numbers, n is the number fo elements
function replaceArray(arr, n) {
// write code here
// do not console.log
// return the new array
const newArr = []
newArr[0] = arr[0] * arr[1]
newArr[n-1] = arr[n-1] * arr[n-2]
for(let i= 1;i<n-1;i++){
newArr[i] = arr[i-1] * arr[i+1]
}
return newArr
}
, In this Programming Language: JavaScript, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: n = int(input())
X = [int(x) for x in input().split()]
lst = []
for i in range(len(X)):
if i == 0:
lst.append(X[i]*X[i+1])
elif i == (len(X) - 1):
lst.append(X[i-1]*X[i])
else:
lst.append(X[i-1]*X[i+1])
for i in lst:
print(i,end=" "), In this Programming Language: Python, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code: #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long b[n],a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=1;i<n-1;i++){
b[i]=a[i-1]*a[i+1];
}
b[0]=a[0]*a[1];
b[n-1]=a[n-1]*a[n-2];
for(int i=0;i<n;i++){
cout<<b[i]<<" ";}
}
, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Given an array of N elements, your task is to update every element with multiplication of previous and next elements with following exceptions:-
a) First element is replaced by multiplication of first and second.
b) Last element is replaced by multiplication of last and second last.
See example for more clarityFirst line of input contains the size of array N, next line contains N space separated integers denoting values of array.
Constraints:-
2 < = N < = 100000
1 < = arr[i] < = 100000Print the modified arraySample Input :-
5
2 3 4 5 6
Sample Output:-
6 8 15 24 30
Explanation:-
{2*3, 2*4, 3*5, 4*6, 5*6}
Sample Input:-
2
3 4
Sample Output:-
12 12, I have written this Solution Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.print(a[0]*a[1]+" ");
for(int i=1;i<n-1;i++){
System.out.print(a[i-1]*a[i+1]+" ");
}
System.out.print(a[n-1]*a[n-2]);
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not? | Compilable |
For this Question: Numbers are awesome, larger numbers are more awesome!
Given an array A of size N, you need to find the maximum sum that can be obtained from the elements of the array (the selected elements need not be contiguous). You may even decide to take no element to get a sum of 0.The first line of the input contains the size of the array.
The next line contains N (white-space separated) integers denoting the elements of the array.
<b>Constraints:</b>
1 ≤ N ≤ 10<sup>4</sup>
-10<sup>7</sup> ≤ A[i] ≤10<sup>7</sup>For each test case, output one integer representing the maximum value of the sum that can be obtained using the various elements of the array.Input 1:
5
1 2 1 -1 1
output 1:
5
input 2:
5
0 0 -1 0 0
output 2:
0
<b>Explanation 1:</b>
In order to maximize the sum among [ 1 2 1 -1 1], we need to only consider [ 1 2 1 1] and neglect the [-1]., I have written this Solution Code: #include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;
#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
signed main() {
IOS;
int n; cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
int p; cin >> p;
if(p > 0)
sum += p;
}
cout << sum;
return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not? | Compilable |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.