Which is the best app to practice C programming offline
Want know about C
SURVIVE - Editorial
PROBLEM LINK:
Author:Sidhant Bansal
Testers:Hasan Jaddouh
Editorialist:Sidhant Bansal
DIFFICULTY:
easy
PREREQUISITES:
basic math
PROBLEM:
In this problem, the greedy approach of buying the box of chocolate for some consecutive early days is the right direction.
Let $A = K * S$ and $B = N * (S - S/7)$, here $A$ denotes the total number of chocolates we need to eat and $B$ denotes the maximum no. of chocolates we can buy. Here $S - S/7$ denotes the number of days the shop is open.
So if $A > B$ or ($(N - K) * 6 < K$ and $S \geq 7$), then the answer is $-1$. otherwise the answer is ceil($\frac{A}{N}$), where ceil($x$) denotes ceiling function on $x$.
The first condition i.e $A > B$, for $-1$ is fairly obvious that if the total no. of chocolates we need to eat is more than we can buy at max then it is invalid.
The second condition i.e $(N - K) * 6 < K$ and $S \geq 7$ is a bit tricky, it is basically the contrapositive of the statement, "if you can survive the first $7$ days, then you can survive any given number of days". So the contrapositive (i.e same statement in different words) is "if you cannot survive the first $7$ days then you won't be able to survive for $S \geq 7$". The condition for being able to survive on the $7^{th}$ day is basically if we add our remaining chocolates from the first 6 days, i.e $(N - K) * 6$ and it is still smaller than $K$, i.e the chocolates we need for the $7^{th}$ day, then we don't survive. But we only need to test this when $S \geq 7$.
Incase, we are able to survive, then the answer is ceil($\frac{A}{N}$), which is basically total number of chocolates we need to eat divided by the number of chocolates we can buy in a single day (and if a remainder exists, then we need to buy one more day). This portion is pretty straightforward.
The above reasoning to check for $-1$ is obviously tricky and a simpler approach exists which is to just simulate the days once we know the value of ceil($\frac{A}{N}$).
SOLUTIONS
a puzzle game
can anyone explain me this problem https://www.codechef.com/problems/H1
HRACE :need help
https://sapphireengine.com/@/blgws I have tried many test cases but my answer is still wrong. kindly help me where I am wrong
dynamic programming
how to solve this problem? https://www.spoj.com/problems/NAJKRACI/
Why do i get a Run Time Error here? I can use dynamic memory allocation (malloc, free, ..) in C right?
Here is my submission - https://www.codechef.com/viewsolution/19460938 It gives runtime error on codechef but it works on my computer. Why am I getting this runtime error? I can use dynamic memory allocation, Right?
Can anyone help me with Great Chase from TCS Mockvita 2018 Contest?
Hi Guys can someone tell me how to solve this problem ?
@vijju123@taran_1407@vivek_1998299@meooow@john_smith_3@aryanc403@vbt_95
My Approach : I tried using Extended Euclid's Algorithm for this but I am not able to solve this problem.
NOTE : This question is not from an ongoing contest. It's a past contest problem :)
NOT GETTING RATING PREDICTOR
Hello everyone,
I want to ask as why the rating predictor for August long challenge is not working, earlier it used to be from the very first day of the contest?
CLKLZM - Editorial
PROBLEM LINK:
Author and Editorialist:Soumik Sarkar
Tester:Sarthak Manna
DIFFICULTY:
EASY
PREREQUISITES:
Sorted set/Priority queue/Heap, Difference array
PROBLEM:
There is an array $Z$ of size $N$ denoting the health of $N$ zombies. There also $M$ types of beams of the form $(L, R, K)$ where $K$ denotes the number of times the beam can be used to decrease every element in $Z[L..R]$ by $1$. Find the minimum number of beam drops needed to reduce all elements in $Z$ to $0$ or determine if it is impossible.
EXPLANATION:
Consider the first zombie $Z[1]$. This zombie can only be affected by beams whose $L = 1$. Let's gather all such beams into a set $U$. If we want to reduce $Z[1]$ to $0$, we have to drop some beams from $U$. Of course we should drop the number of beams exactly equal to its health and not waste any beams. The question is.. which beams should we drop?
Here it makes sense to make a greedy choice and drop the beams from $U$ with largest $R$. That way we are affecting maximum zombies to the right of $L$ at the same time.
After killing the first zombie let us move to $Z[2]$. Of course $Z[2]$ may already have been reduced to $0$ after being affected by the beams that we used on $Z[1]$, but let us assume it is still non-zero. Now we are faced with a similar situation as before. Only those beams with $L \le 2$ can affect $Z[2]$. But all beams with $L = 1$ were added to $U$, and some of them are even already used. So we must add all the beams that have $L=2$ to the leftover beams in $U$, and make greedy choices again to reduce $Z[2]$.
Generalizing, the solution involves maintaining an active set of beams $U$. At any index $i$, all beams that can affect $i$, ie. beams that have $L \le i$, are either in set $U$ or have been used up. To kill zombie $i$, only as many beams as required are used, the beams with largest $R$ being used first. The process is carried out for $i = 1$ to $N$. The algorithm can be described as follows:
U = empty set
ans = 0
for i in [1..N]:
add all beams with L = i to U
while Z[i] > 0:
let b be the beam with largest R from U
if U is empty or b.R < i:
zombie i cannot be killed
exit loop
if b.K > Z[i]:
ans = ans + Z[i]
b.K = b.K - Z[i]
reduce every element in Z[i..b.R] by Z[i]
else:
ans = ans + b.K
Z[i] = Z[i] - b.K
reduce every element in Z[i..b.R] by b.K
remove b from U
The above strategy is optimal, but how can we implement it efficiently?
The two procedures that are non-trivial are retrieving the update with largest $R$ from $U$ and applying decrement operations on a range $Z[L..R]$.
For the first we can maintain $U$ as a sorted set or a priority queue. This allows retrieval of largest element in $\mathcal{O}(\log N)$ time. Most languages have implementations of these structures in the standard library.
For the second the simplest solution is to use a difference array D
. Generally speaking, to apply an update on $[L..R]$ we perform D[L] += X
and D[R + 1] -= X
and then get the original array using prefix sums. Here we need to apply the updates as we go along. Note that the updates to be applied only affect elements to the right of the current index, so we can maintain the prefix sum in a variable and just modify D[R + 1]
as required.
The complexity of the solution is $\mathcal{O}((N + M)\log M)$.
AUTHOR'S AND TESTER'S SOLUTION:
Author's solution can be found here
Tester's solution can be found here.
Please help me solving this problem
Welcome to the Planet Zandar, the second most prominent planet in the Milky Way Galaxy (of course after our own Earth).
The planet is in a distress condition, a Group of Galactic pirates, Zorons have stolen the Trident Crystal, which is the main source of energy of the planet, and are escaping the Galaxy. The Nova Corps, the military agency of Zandar, have gathered intelligence that the Zoronion space craft can run in cosmic leaps of exactly D units, (it means that the space craft will move D units from its position in every leap/turn) and is currently I units away from Zandar.
The Zandarian Space crafts can run in cosmic leaps of exactly Z units. The Commander of Nova Corps wants to know the smallest number of leaps required to catch Zorons (Note that it is possible to catch the pirates only when they will be at the same point in the cosmic universe). The Zorons, even though are clever thieves, travel in one direction, and keep jumping exactly D units without stopping at any point.
The Nova Corps can dial in the number of jumps they need to make (each of them exactly Z units), and reach the place almost instantly. They can then wait there until the Zorons arrive, and recover the Trident Crystal.
However, their wizard has told them that there may be situations where it is impossible for the Nova corps to be at the same distance as the Zorons.
As the planet is out of power currently, their supercomputers are shut down and they are not able to calculate the required information. As you are there from Earth they have approached you for help.
Note: Assume that the Cosmic universe is one dimensional.
Constraints
1 <= I,D <= 10^12
1<= Z <= 10^9
Input Format
An integer T for number of test cases, followed by T test cases each one consisting of three numbers
1) I :- initial distance of Zorons
2) D:- distance covered in a single cosmic leap by Zoronion space craft.
3) Z:- distance covered by Zandarian space crafts.
Output
Single number, the number of leaps required to catch the pirates, and if it is not possible to catch them, output will be -1
Explanation
Example 1
Input
2 9 5 12 5 7 9
Output
2 6
Explanation
The first line is 2, so T is 2, and there are 2 test cases.
In the first test case, The Zorons will initially be at 9 and then they will leap to 14,19 24..... The Nova Corps can take leaps of 12 and will catch them nearest at a distance 24, taking 2 leaps 12 and 24.
In the second test case, The Zorons will initially be at 5 and then they will leap to 12 19 26 33, 40, 47, 54..... The Nova Corps can take leaps of 9 and will catch them nearest at 54, taking 6 leaps.
Hence the output has 2 lines, 2 and 6.
Example 2
Input
1
10 15 20
Output
2
Explanation
The first line is 1, so T is 1, and there is 1 test case.
The Zorons will initially be at 10, and jump in jumps of 15, landing at 25,40,
The Nova Corps take leaps of 20, and arrive at 20, 40. Hence, they can meet at 40 after 2 leaps. The output is 2.
Invitation for IOI Training Camp (IOITC) Replay Contest #2
The International Olympiad in Informatics is one such event that we all look forward to, not only as players but also as spectators. An annual competition that began in 1989 in Bulgaria, the IOI has risen from its humble beginnings to become one of the largest worldwide competitions for school students that tests their skills in programming and problem solving.
CodeChef is conducting the next replay of the Indian IOI Training Camp. This contest is the second among the three replays that we plan to conduct. There will be three challenging problems and a five-hours to solve them.
The authors of the round are:
- Kushagra Juneja (kushagra1729), Praveen Dhinwa (dpraveen), and Sreejata Bhattacharya (AnonymousBunny)
The testers panel consists of:
- Rajat De (rajat1603), Sampriti Panda (sampriti), Sidhant Bansal (sidhant007), Arjun Arul (arjunarul), Swapnil Gupta (born2rule), Sreejata Bhattacharya (AnonymousBunny), Animesh Fatehpuria (animesh_f), and Arjun P (Superty)
How to participate? It’s simple, just register at CodeChef if you haven’t done so already, and check out the details below! We hope that you will find the contest very interesting. We wish that you will enjoy the round as much as we enjoyed preparing this round.
Start Date: Saturday, 21st July, 2018 at 19:00 HRS (IST)
You may check your timezone here.
Contest Link: www.codechef.com/IOITC182
Good luck!
Cheers,
Team CodeChef
Can anyone help me with Distinct Partition Squares from TCS Mockvita 2018 Contest?
Hi Guys can someone tell me how to approach this problem ?
@vijju123@taran_1407@vivek_1998299@meooow@john_smith_3@aryanc403@vbt_95
NOTE : This question is not from an ongoing contest. It's a past contest problem :)
Invitation to Encoding August '18
Hello everyone!
We are excited to invite you all to Encoding August '18. The contest is hosted by Netaji Subhash Engineering College, Kolkata. The contest will have challenges for all - beginner to advanced programmers. We hope all of you will love to join us and spend the Sunday evening solving exciting coding challenges.
The authors of the contest are:
1. Arkapravo Ghosh
2. Amit Kumar Pandey
3. Rahul Ojha
4. Md Shahid
Contest Details
Date : 5th August
Time : 19:30 hrs - 22:30 hrs(IST)
Contest Link : https://www.codechef.com/ENAU2018
Registration : You just need to have a CodeChef handle to participate. For all those, who are interested and do not have a CodeChef handle, are requested to register in order to participate.
Good Luck!
See you on the ranklist! Happy Coding!
CHO1 - Editorial
PROBLEM LINK:
Author:Manisha Reddy Thirumala Reddy
Tester:Manisha Reddy Thirumala Reddy
DIFFICULTY:
CAKEWALK.
PREREQUISITES:
Strings, Arrays.
PROBLEM:
Given a string s with n characters, we need to perform the following steps:
- swap adjacent characters.
- swap every alphabet with its ASCII reverse alphabet. Ex: For 'a' the reverse ASCII value is 'z'. we can do this by ASCII values (char)(122-'a'+97) = 'z'.
EXPLANATION:
As it is very easy question, the code snippet will guide you.
temp = en[i+1];
en[i+1]=en[i];
en[i]=temp;
}for(int i=0; i<len; i = i+2){
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
CBARG - Editorial
PROBLEM LINK:
Author:Pavel Sheftelevich
Tester:Mahbubul Hasan and Sunny Aggarwal
Editorialist:Balajiganapathi Senthilnathan
Russian Translator:Sergey Kulik
Mandarian Translator:Minako Kojima
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
You are given $N$ memory requests. The system starts with no memory allocated. For each request, the system either allocates or deallocates the memory to exactly match the requested memory. Calculate the total amount of memory allocated.
SHORT EXPLANATION
Keep track of the amount of memory currently allocated. For each request, if the requested memory is more than the current memory, then add the difference to the answer.
EXPLANATION:
This is a fairly straightforward simulation problem. Observe that the memory allocated or deallocated only depends on two things:
- The memory during the previous task (or zero initially) (call it $last$)
- The memory requested during the current task (let us call it $cur$)
So, given these two values, when will the system have to allocate more memory? When the current task requests more memory than the previous one. How much memory should we allocate? Suppose $a$ is the amount of memory we have to allocate. Note that allocation is in addition to the memory the system had before this task. So, the total memory after allocation would be $last + a$. We want this to be equal to $cur$. So, $last + a = cur$. Which gives $a = cur - last$. For each request that requires allocation, we add the amount allocated to the total.
Pseudocode:
total = 0 // Stores the amount of memory allocated so far
last = 0 // Stores the amount of memory allocated to the last task (initially 0)
repeat n times:
input cur // Get the amount of memory requested by the current task
if cur > last:
ans += cur - last // If more memory is required, then we must allocate the difference
last = cur
output total
A word on implementation
For some language like Python, we can translate the pseudocode above easily. For languages like C/C++ or Java we need to decide the type of each variable. Note that 32 bits are not enough to store the total. Why? Suppose the requests are $10^5$ $1$ $10^5$ $1$ $10^5$ $1$ ... then for each pair of requests of $1$ followed by $10^5$, we need to allocate $(10^5 - 1)$ memory. Since $n$ can be upto $10^5$, the total amount will exceed $32$ bits. So we need to use $64$ bit data type to store the total.
Time Complexity:
$O(N)$
AUTHOR'S AND TESTER'S SOLUTIONS:
Invitation to ProCon Junior 2018
"Code to fight, Code to survive, Code to win!"
Hello Everyone!
I would like to invite you all to participate in ProCon Junior to be held on CodeChef. It will begin at 19:30 IST on August 11, 2018.
The problems have been made by me (prakhar17252), Madhav Sainanee (madhav_1999) and Tanmay Bansal (tanmay28).
You will be given 7 problems to solve in 2.5 hours. The competition is open for all and rated for Div-2.
Top school students studying between classes VI-XII (or equivalent) in an Indian school will then be invited to the onsite finals to be held at Esya'18, the Technical Fest of IIIT-Delhi.
School Students, register here for onsite finals: bit.ly/PRCNJR18
Prizes worth Rs. 20000 for the top rankers in the onsite finals.
This is my first time hosting a contest, and that too a rated one! Hope you all like the problems!
Hope everyone has high ratings!
Good Luck!
Happy Coding!
Explicitly same code got AC in python and PA(with partial WA not TLE) in CPP
I am looking for any possible overflows in my code but not able to find any. Will someone please share what are the possible errors I should be looking for in my code.
Easy bitwise OR
TAPALIN - Editorial
PROBLEM LINKS
DIFFICULTY
SIMPLE
PREREQUISITES
Simple Math, Matrix Exponentiation
PROBLEM
Find the number of palindromic strings that can be made of length N, or less.
QUICK EXPLANATION
We can fix the first ciel(N/2) characters of the string as we want, and the rest of the string is simply forced.
Also, each string we get by fixing the first ciel(N/2) characters is a unique palindromic string.
Thus, we can find the number of palindromic strings of each length
F(n) = 26ceil(n/2)
Now, we need to find the summation of this result for all n from 1 to N to find all the palindromic strings of length N, or less.
SUMMA( F(n), 1 ≤ n ≤ N ) = 2*SUMMA( 26n, 1 ≤ n ≤ ceil(N/2) ), for even N 2*SUMMA( 26n, 1 ≤ n ≤ floor(N/2) ) + 26ceil(N/2) for odd N
We know that we can find ab for a large b by repeated squaring. The problem now reduces to calculating the following
S(n) = SUMMA( 26n, 1 ≤ n ≤ T )
EXPLANATION
There are two ways to find the result.
METHOD 1: Matrix Exponentiation
S(n) = 26, for n = 1 = 26*S(n-1) + 26
We can build the matrix equation as below
| S(n+1) | = | 26 26 | * | S(n) | | 1 | | 0 1 | | 1 |
Thus, to find the result, we can calculate
| 26 26 |T | 0 1 |
By repeated squaring of matrices. The results need to be found modulo 1000000007. Since all calculations are sum or product only, the drill should be straightforward.
METHOD 2: Sum of Geometric Progression
S(T) = 26*(26T - 1) / 25
Calculating the numerator modulo 1000000007 is pretty straight forward. We use repeated squaring for the exponent part.
But, to the uninitiated it might be confusing as to why can we get away with calculating the residue of the numerator considering we are yet to divide it by 25.
Here, we apply the concept of modular arithmetic inverses. These are well defined for residues modulo primes. 1000000007 happens to be a prime (and is often the reason it is chosen in problems by several authors). The modular arithmetic inverse of 25, modulo 1000000007 is
251000000005
We arrive at this result by virtue of Fermat's Little Theorem. Since
251000000006 = 1, modulo 1000000007 25 * 251000000005 = 1, modulo 1000000007 Thus, 251000000005 = 25-1, modulo 1000000007
Now, by using repeated squaring, we can find the modular inverse of 25 and multiply it to the numerator to find our answer.
SETTER'S SOLUTION
Can be found here.
TESTER'S SOLUTION
Can be found here.
why does NOT using Scanner class in Java give TLE?
I submitted two solution for a problem which only differ by two lines of seemingly inconsequential nature. One solution gives TLE but other one passes. The solution that gives TLE is NOT using Scanner class. The solution that passes has two additional lines which import and initialize a Scanner class. The additional lines are given below. If anyone can provide a reason for this, it will help a lot. Thanks.
import java.util.Scanner;
Scanner in = new Scanner(System.in);
Problem:
https://www.codechef.com/problems/COPS
Solution which initializes Scanner obj (passes):
https://www.codechef.com/viewsolution/19479471
Solution without Scanner obj (gives TLE):
https://www.codechef.com/viewsolution/19479329