Chef and Bored Games - CodeChef

It's time for another game, a "bored" one indeed, this problem is part of the practice problems of CodeChef which I usually solve in my free time, and this particular question was interesting because it just needs few slices of focus with slight garnishing of endurance on top of it.
For this problem your programming understanding is insignificant, you may be having fair experience with standard problems, but fact is you'll be at the same position as a person with a smattering of programming understanding.
All that matters is how easily you can dive in the depths of this problem and come out alive.


yupp 🙂


Let's see if you can get this one :) ?
Problem Statement:-

Chef has recently been playing a lot of chess in preparation for the ICCT (International Chef Chess Tournament).

Since putting in long hours is not an easy task, Chef's mind wanders elsewhere. He starts counting the number of squares with odd side length on his chessboard..

However, Chef is not satisfied. He wants to know the number of squares of odd side length on a generic 𝑁𝑁 chessboard.

###Input:

  • The first line will contain a single integer 𝑇, the number of test cases.
  • The next 𝑇 lines will have a single integer 𝑁, the size of the chess board.

###Output: For each test case, print a integer denoting the number of squares with odd length.

###Constraints

  • 1𝑇100
  • 1𝑁1000

Sample 1:

Input
Output
2
3
8
10
120

Solution:-
It's just some observation that is required in this problem. You should first draw some matrices(chess boards) and try this on your own, try finding the number of squares with odd side lengths for different size matrices. 
Now, you will observe that for every odd length like, 1,3,5,7 the number of squares in the 2D matrix are equal to (x+1)^2, where x is the difference between n and that odd number(1,3,5,7,etc)
For example, 3 length squares in a matrix of size 5, will be 3*3, difference from 5 is 2 and +1 means 3 now it's square = 9. 
So, taking this even further, when we find total number of squares of odd side length in a matrix of size 5, it will be --> For 1-->(5*5) , for 3-->(3*3), for 5-->(1*1). 

Now, if we observe then there is a pattern in the products, that is 5,3,1, which is like the ans is always going from N to 1 with an interval of 2 and for every number we need to get it's square. This is what I have used in the code.

That's it!! Try more samples on your own and follow this pattern to explore it, or you can also find the number of squares by just taking every odd length from 1 to size also, this is also fine but then every time you will have to perform Subtraction operation and then add 1 to it. So it is little time consuming, and we can conclude that the abstracted method where we made use of the pattern is better.

If you could make it on your own then it's amazing, if you read the solution, it's wonderful!

 Code Implementation:-

#include <iostream>
using namespace std;

int main() {
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        int n,ans=0;
        cin>>n;
        for(int j=n;j>=1;j-=2){
            ans+=(j*j);
        }
        cout<<ans<<endl;
    }
    return 0;
}



It's 10 at night, the dusk of an amazing day which I just wanna conclude with a blog and some music, what's better than blogging while relaxing to Tides on Spotify? Well, a cold coffee I'd say! But I don't wanna incinerate my sleep right now, which is next in the queue so maybe tomorrow morning... cya. 

Comments

  1. Nice one! And I loved it involved the two passions we have in common: programming and chess.

    After reading the explanation, it is almost obvious, but honestly, it is not at all instinctive, and I could only solve after drawing some examples, as you suggested haha very cool!

    ReplyDelete

Post a Comment

Popular Posts