Google Kickstart 2020 Round C: Countdown - Code Not Working - java

When I submit my code on Google Kickstart, I get a "wrong answer" for the 1st test set, even though the sample input and output match. I'm really not sure why Google does not accept the code.
The task is that given an array of N positive integers, find the number of K-countdowns, where a K-countdown is a contiguous subarray if it is of length K and contains the integers K, K-1, K-2, ..., 2, 1 in that order.
Input:
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integers N and K. The second line contains N integers. The i-th integer is Ai.
Output:
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of K-countdowns in her array.
Sample Input:
3
12 3
1 2 3 7 9 3 2 1 8 3 2 1
4 2
101 100 99 98
9 6
100 7 6 5 4 3 2 1 100
Sample Output:
Case #1: 2
Case #2: 0
Case #3: 1
My logic is pretty straightforward: I have a counter variable x which starts at K and decrements whenever an integer equals x. If a countdown is found (x=0 after decrementing) then the answer increases and x is set to equal K. Here is the crux of my code:
for(int i=0; i<n; i++) {
arr[i]=sc.nextLong();
if(arr[i]==x)
x--;
else
x=k;
if(x==0) {
ans++;
x=k;
}
}
Here is my entire code in case there are any trivial errors:
import java.util.Scanner;
public class Solution {
static int t;
static long n,k;
static long[] arr;
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
t=sc.nextInt();
for(int i=1; i<=t; i++) {
n=sc.nextLong();
k=sc.nextLong();
System.out.println("Case #"+i+": "+solve());
}
sc.close();
}
public static long solve() {
long x=k;
long ans=0;
arr=new long[(int)n];
for(int i=0; i<n; i++) {
arr[i]=sc.nextLong();
if(arr[i]==x)
x--;
else
x=k;
if(x==0) {
ans++;
x=k;
}
}
return ans;
}
}

here is my cpp code which is running in o(n)
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,m;
cin>>t;
m=t;
while(t--){
int n,k;
cin>>n>>k;
vector <int> v,v2;
for(int i=0;i<n;i++){
int tr;
cin>>tr;
v.push_back(tr);
}
for(int j=0;j<k;j++){
int tr=k-j;
v2.push_back(tr);
}
int fg=0;
for(int i=0;i<n;i++){
if(v[i]==k){
int count=0;
for(int j=0;j<k;j++){
if(v2[j]==v[i+j])
count++;
}
if(count==k)
fg++;
}
}
cout<<"Case #"<<m-t<<": "<<fg<<endl;
}
}

Suppose there is half of a countdown in the array and the following number is K.
Based on the code, that number will be neglected and so if there is a following countdown, it won't be counted. Here is what the main loop should look like:
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
if (arr[i] == x)
x--;
else if (arr[i] == k)
x = k-1;
else
x = k;
if (x == 0) {
ans++;
x = k;
}
}

Here is my Accepted code in java :
import java.util.*;
public class Solution {
public static int kCountDown(int [] nums, int k) {
int length = nums.length;
int endCounter = 0;
int result = 0;
for (int i=1; i<length; i++) {
if (nums[i - 1] - nums[i] == 1)
endCounter += 1;
else
endCounter = 0;
if (nums[i] == 1 && endCounter >= k - 1)
result += 1;
}
return result;
}
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
for (int t=1; t<=test; t++) {
int n = sc.nextInt();
int k = sc.nextInt();
int [] nums = new int [n];
for (int i=0; i<n; i++) {
nums[i] = sc.nextInt();
}
System.out.println("Case #" + t + ": " + kCountDown(nums, k));
}
}
}
Complexity is O(n) where n = length of the input array

Related

Construct an N input OR Gate. An OR Gate returns 0 if all its inputs are 0, otherwise 1 using java

Exact questionhttp://www.practice.geeksforgeeks.org/problem-page.php?pid=1335
Construct an N input OR Gate. An OR Gate returns 0 if all its inputs
are 0, otherwise 1.
Input:
3 //these are testcases
2 //these are number of inputs
1 1 //these are inputs
3 //these
0 0 0
4
1 1 1 0
output: 1 //o/p for 1st case
0 //o/p for second case
1
this code is causing run time error
errors such as Scanner java:907,java:1530,java:2160
class GFG {
static int or(int a[] , int n , int x){
for(int i = 0;i<n;i++)
{
if(x==0)
return 0;
}
return 1;
}
public static void main (String[] args) {
int a[] = new int[100];
int x= 0;
Scanner in =new Scanner(System.in);
int t = in.nextInt();
while(t>0){
int n = in.nextInt();
for(int i =0; i<n;i++){
a[i] = in.nextInt();
x =x+(a[i]|a[i+1]);
}
System.out.println(or(a,n,x));
t--;
}
}
}
Well your code works for the most part but:
for(int i = 0; i < n; i++) {
if(x == 0)
return 0;
}
return 1;
is equivalent to:
if(x == 0) return 0; else return 1;
What you want is:
for(int i = 0; i < n; i++) {
if(a[i] == 0)
return 0;
}
return 1;
In fact you could eliminate x altogether, but if you wanted to use x it wouldn't be necessary to store the numbers in a.
Version without array:
public static void main (String[] args) {
int x = 0; // assume x is 0.
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
int n = in.nextInt();
x = 0; // reinitialise x each time.
for(int i = 0; i < n; i++) {
// if and only if all inputs are 0 will x be 0.
// if any input is 1 -> x | 1 is 1 no matter what x. So the result will be 1.
x = x | in.nextInt();
}
System.out.println(x);
t--;
}
in.close();
}
The input dialog box:
You can use the sample input provided by the site.

Printing a particular value from an array

How can I print the value of a Fibonacci function for any particular index, say n, where n is the provided argument value?
1 import java.util.Arrays;
2
3 public class Fibonacci {
4
5 public static void main(String[] args) {
6 int n = Integer.parseInt(args[0]);
7 if(n<3){
8 return;
9 }else{
10 int[] f = new int[n];
11 f[0] = 1;
12 f[1] = 1;
13 int i= 0;
14 for(i=2; i<f.length; i++){
15 f[i]= f[i-1]+ f[i-2];
16 }
17 System.out.println(f[i]);
18 }
19
20 }
21
22 }
Your code is basically fine, but I tweaked a few things in my response:
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
if (n < 0) {
System.out.println("Cannot computer Fib() of a negative number.");
return(0);
} else if (n < 3) {
System.out.println("Fib[" + n + "] = 1");
} else {
int[] f = new int[n];
f[0] = 1;
f[1] = 1;
for(int i=2; i < f.length; ++i) {
f[i] = f[i-1]+ f[i-2];
}
System.out.println("Fib[" + n + "] = " + f[n - 1]);
}
}
The problem you have, I believe, is System.out.println(f[i]);
At this spot, i will be equals to length of f[]. If you use i as index, it will be out of bound.
If you are going to print the last value, it should be System.out.println(f[i-1]);
You are printing f[i] which will give you ArrayIndexOutOfBoundsException since at that time value of i crosses the boundary of array f.
A simple workaround would be to print array by taking a separate variable like this :
int n = Integer.parseInt(args[0]);
if(n<3){
return;
}else{
int[] f = new int[n];
f[0] = 1;
f[1] = 1;
int i= 0;
for(i=2; i<f.length; i++){
f[i]= f[i-1]+ f[i-2];
}
for (int j=0;j<n;j++){
System.out.println(f[j]);
}
}
If you want to print any particular index then you can make one boundary check before printing:
int index=5;
if(index<f.length){
System.out.println(f[index]);
}
I think It's better to have Fibonachi in the recursive way:
public int fib(int n) {
if (n < 2) {
return n;
}
else {
return fib(n-1)+fib(n-2);
}
System.out.println(n);
}

counting cosecutive numbers in arrays

Problem H [Longest Natural Successors]
Two consecutive integers are natural successors if the second is the successor of the first in the sequence of natural numbers (1 and 2 are natural successors). Write a program that reads a number N followed by N integers, and then prints the length of the longest sequence of consecutive natural successors. Example:
Input
7 2 3 5 6 7 9 10 Output 3
here is my code so far can anyone help me plz
import java.util.Scanner;
public class Conse {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int x=scan.nextInt();
int[] array= new int[x];
for(int i=0;i<array.length;i++)
array[i]=scan.nextInt();
System.out.println(array(array));
}
public static int array(int[] array){
int count=0,temp=0;
for(int i=0;i<array.length;i++){
count=0;
for(int j=i,k=i+1;j<array.length-1;j++,k++)
if(array[j]-array[k]==1)
count++;
else{if(temp<count)
temp=count;
break;}
}
return temp+1;
}
}
Try this
ArrayList<Integer> outList = new ArrayList<Integer>()
int lastNum = array[0];
for(int i = 1; i < array.length; i++;)
if((lastNum + 1) == array[i])
outList.add(array[i]);
I think the line i=counter; should be i += counter. otherwise, you're always resetting the loop-counter i to zero, and so it never progresses.
You don't need the inner for loop, as this can be done with one single scan through the array:
public static int consecutive(int[]array) {
int tempCounter = 1; //there will always be a count of one
int longestCounter = 1; //always be a count of one
int prevCell = array[0];
for(int i=1;i<array.length;i++) {
if( array[i] == (prevCell + 1)) {
tempCounter++; //consecutive count increases
} else {
tempCount =1; //reset to 1
}
if(tempCounter > longestCounter) {
longestCounter = tempCounter; //update longest Counter
}
prevCell = array[i];
}
return longestCounter;
}
int sequenceStart = 0;
int sequenceLength = 0;
int longestSequenceLength = 0;
for (int item: array) {
if (item == sequenceStart + sequenceLength) {
sequenceLength++;
} else {
sequenceStart = item;
sequenceLength = 1;
}
longestSequenceLength = Math.max(longestSequenceLength, sequenceLength);
}

stuck in contraints in a easy exercise

I am trying to solve this exercise, It seems easy this, but I can not understand the contraints -rules, It says:
the number may be represented on one or two hands;
if the number is represented on two hands, the larger number is given first
The rule number 2 I can not understand for example if it says 3, I have 3, 2+1, 1+2 (this not because its repeated), if it says 6 we have 6, 5+1, 4+2, 3+3, 2+4 + 1+5 but the correct output is 3, can someone guide me in this problem?? for 7 is 2, and 8 is 2, 9 is 1, and 10 is 1.
this is my code:
import java.util.Scanner;
class j1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int tot = 5;
int n = sc.nextInt();
int sum = 0;
int count = 1;
for (int i = 1; i <= tot; i++) {
for (int j = 1; j <= tot; j++) {
sum = i + j;
if (sum == n) {
System.out.println(i);
System.out.println(j);
count++;
}
}
}
System.out.println(count);
sc.close();
}
}
Its simple - if you are going to give the number using both the hands (2 hands) then you will first need to give the larger number which comprises the overall number -
eg for 7 (4+3 OR 5+2) when represented using 2 hands - give 4 first !
other option for 7 (3+4, 2+5) are invalid since it will make us to list the smaller number first which violates the rule #2
The number of the second hand must always be less than or equal to the number of the first hand. I believe the code below will work.
import java.util.Scanner;
class j1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int tot = 5;
int n = sc.nextInt();
int sum = 0;
int count = 1;
for (int i = 1; i <= tot; i++) {
for (int j = 1; j <= i; j++) {
sum = i + j;
if (sum == n) {
System.out.println(i);
System.out.println(j);
count++;
}
}
}
System.out.println(count);
sc.close();
}
}

How can i generate all subsets of a variable length set?

I am trying to write a program that generates all the subsets of an entered set in java. I think i nearly have it working.
I have to use arrays (not data structures)
The entered array will never be greater than 20
Right now when i run my code this is what i get:
Please enter the size of A: 3
Please enter A: 1 2 3
Please enter the number N: 3
Subsets:
{ }
{ 1 }
{ 1 2 }
{ 1 2 3 }
{ 2 3 }
{ 2 3 }
{ 2 }
{ 1 2 }
this is the correct number of subsets (2^size) but as you can see it prints a few duplicates and not some of the subsets.
Any ideas where I am going wrong in my code?
import java.util.Scanner;
public class subSetGenerator
{
// Fill an array with 0's and 1's
public static int [] fillArray(int [] set, int size)
{
int[] answer;
answer = new int[20];
// Initialize all elements to 1
for (int i = 0; i < answer.length; i++)
answer[i] = 1;
for (int a = 0; a < set.length; a++)
if (set[a] > 0)
answer[a] = 0;
return answer;
} // end fill array
// Generate a mask
public static void maskMaker(int [] binarySet, int [] set, int n, int size)
{
int carry;
int count = 0;
boolean done = false;
if (binarySet[0] == 0)
carry = 0;
else
carry = 1;
int answer = (int) Math.pow(2, size);
for (int i = 0; i < answer - 1; i++)
{
if (count == answer - 1)
{
done = true;
break;
}
if (i == size)
i = 0;
if (binarySet[i] == 1 && carry == 1)
{
binarySet[i] = 0;
carry = 0;
count++;
} // end if
else
{
binarySet[i] = 1;
carry = 1;
count++;
//break;
} // end else
//print the set
System.out.print("{ ");
for (int k = 0; k < size; k++)
if (binarySet[k] == 1)
System.out.print(set[k] + " ");
System.out.println("}");
} // end for
} // maskMaker
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int[] set;
set = new int[20];
int size = 0;
int n = 0;
// take input for A and B set
System.out.print("Please enter the size of A: ");
size = scan.nextInt();
if (size > 0)
{
System.out.print("Please enter A: ");
for (int i = 0; i < size; i++)
set[i] = scan.nextInt();
} // end if
System.out.print("Please enter the number N: ");
n = scan.nextInt();
//System.out.println("Subsets with sum " + n + ": ");
System.out.println("Subsets: ");
System.out.println("{ }");
maskMaker(fillArray(set, size), set, n, size);
} // end main
} // end class
The value of i always goes from 0 to N-1 and then back to 0. This is not useful to generate every binary mask you need only one time. If you think about it, you need to move i only when you have generate all possible masks up to i-1.
There is a much easier way to do this if you remember every number is already internally represented in binary in the computer and everytime you increment it Java is doing the adding and carrying by itself. Look for bitwise operators.

Categories