I applied a similar logic related to the pattern of 2,4,8,6.
I ran a loop till the first occurrence of digit ‘2’ in the number and computed the sum till that position, for the rest of the digits(if any) i find the sum directly using sum of 2,4,8,6 and dividing the remaining digits by 4 and later modulo to find if anythings left.
I think you are getting Runtime Error because the value of N can be as large as 10^9 which I don’t think JAVA will be able to allocate.
When I ran your program I get the following message for the given test-case:
1
1
1 1000000000 1
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at CoinFlip.playGame(coin_flip.java:11)
at CoinFlip.main(coin_flip.java:67)
NOTE: I am not a JAVA programmer, so I am not sure, but maybe that is the sure bug in your program.
Test-Cases: The following are the test-cases on which you can run your program, if you get the correct output, then you can be sure that your solution is correct.
Here is my code of CARVANS. Please tell me about where I am wrong.
try:
a = int(input())
for i in range(a):
e = 0
b = int(input())
c = list(map(int, input().split()))
if(len(c) == 1):
print(1)
else:
for i in range(len(c)-1):
if(i == 0):
e += 1
elif(c[i] < c[i-1] and c[i] < c[i+1]):
e += 1
print(e)
except EOFError:
pass
I suggest you to have a look on the editorial for the problem as explained here, and update your source-code accordingly and then run on the test-cases mentioned here.
If you are getting correct answers for all the test-cases then you can claim that your solution is correct.
I’m not able to understand why I’m getting wrong answer for my submission. I’ve tripled checked my logic and have referred to a detailed tutorial on geeksforgeeks. Here’s the link:
Hi, I am stuck at the multiple of 3 problem.
my thought process behind the solution is :
case 1-- if (d0 +d1)%3 ==0 then the number will be a multiple of 3.
case 2 – if (k-3)*sum%3 ==0 then also the number will be will be multiple of 3
( case 2 can happen because of any of the two a-- k is multiple of 3 b-- sum is multiple 3.)
also this code works fine on the given test cases, but is giving WA for the solution.
I am getting Wrong answer. I tried something different. Is my solution approach, wrong? Any suggestion or help will be appreciated. Thank you
#include<bits/stdc++.h>
using namespace std;
bool solve(){
ll k,d0,d1;
cin>>k>>d0>>d1;
int d2 = d0 + d1;
//corner case
if(k==2){
if((d0*10+d1)%3==0)
return true;
return false;
}
//corner case
if(k==3){
if((d0*100+d1*10+d2)%3==0)
return true;
return false;
}
vector<int> pattern; // pattern
/*
example : 13 8 1
the pattern we are looking to built is
pattern [ 2 2 1 2 1 1 0 1 0 0 2 0 ]
remainder of 4 5 6 7 8 9 10 11 12 13 14 15 th digit,
so, since the number ought to be 13(k) we check the 13th digits remainder.
*/
int rem = (d0*100+d1*10+d2)%3;
for (int i = 0; i < 12; i++)
{
d2 = (d2*2)%10;
rem = (rem*10 +d2)%3;
pattern.push_back(rem);
}
// debug(pattern)
/*
Since the pattern is repeating
[ 2 2 1 2 1 1 0 1 0 0 2 0 2 2 1 2 1 1 0 1 0 0 2 0 ]
it start to repeat at every 12 remainder,
so (k-3-1)%12 since we reduced the first 3 remainders which is not
repetitive. also we have checked in the corner cases.
*/
if(pattern[(k-3-1)%12]==0) return true;
return false;
}
int main() {
fastio();
ll t=1;
cin >> t;
while(t--) {
if(solve())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
I’m sorry i am new to cp but what does " the constraints imposed on K i.e. 1 <= K <= 10^{12}, means you cannot go for the O(K)O(K) approach." mean in the problem “Multiple of 3” ?