Codeforces educational round 84

import java.util.*;
class Rextester{
  static boolean odd(int n){
    if(n%2==0)
      return false;
    return true;
  }
  static boolean solve(int n, int k)
  {
    if (n >= k*k) {
      return true;
    }
    return false;
  }
  public static void main(String[] args) {
    Scanner in= new Scanner(System.in);
    int test=in.nextInt();
    while(test-- >0){
      int n, k;
      n=in.nextInt();
      k=in.nextInt();
      if(odd(n) && odd(k)){
        if(solve(n, k)){
          System.out.println("YES");
          continue;
        }
      }
      if(!odd(n) && !odd(k)){
        if(solve(n, k)){
          System.out.println("YES");
          continue;
        }
      }
      System.out.println("NO");
    }
  }
}

Problem - A - Codeforces

I cannot understand which test case if failing. Can someone please help me.

The answer would be “YES” iff n and k both would be even or both would be odd and n >= k*k else answer would be “NO”
This is what I did.

1 Like

1
1000000 1100000

take k long

isn’t that what I did? I think it should work.

getting yes for this test case. I think that is correct…

OK i was getting buffer overflow error… should be long instead of int. THanks for help

Was it a pretty known trick? I see so many solutions using this but I didn’t know that earlier

1 + 3 + 5 + 7...2n-1 is n^2

1 Like

Why this condition doesn’t work :-
if( (n%2==0 && k%2==0) || (n%2!=0 && k%2!=0) && ( (n/k) >= k) )

But this one works :-
if( (n%2==k%2) && ((n/k) >= k) )

Because hierarchy of logical operators says that in a statement, first all the logical NOT(!) should be processed, then logical AND(&&), and then logical OR(||).

So according to the first thing, condition 2 AND condition 3 should work. After that, if condition 1 OR the other two condition work, it will process the statements under it. This problem can be resolved by putting a bracket before condition 1, and after condition 2.

2 Likes

No, Most of us derived it quickly, it comes from practice !

k>=sqrt(n)
answer would be no;

So it breaks down to - >
You have to represent N using K distinct odd digits
now if K is odd then N should be odd and if N is even K should be even.
You can check so by (N-K)%2 == 0. Then the minimum Number you can represent using K distinct odd digits is 1 + 3 + 5 + 7 + . … 2 * K + 1, by applying the Arithmetic Progression Formula you obtain that that series is equal to K^2, so now if N < K ^ 2 you can’t represent it where as if N >= K^2 you can always represent it let’s say N > K ^ 2 , so you can just change the last digit 2 * K + 1 by N - K^2 making it 2 * K + 1 + N - K^2, it will remain odd because N and K have the same parity so we will be doing odd + even. Hope this clears any doubts you had.

1 Like