Codigo - 2016, CDGO1602 - Editorial

PROBLEM LINK :

Contest

Practice

Editorialist: Atul Gupta

DIFFICULTY:

Simple

PREREQUISITES:

Implementation

PROBLEM:

Given an array A. Count the non-zero elements divisible by a, then count remaining non-zero elements divisible by b. Print the answer accordingly.

QUICK EXPLANATION:

  • A number m (say) is multiple of n (say) iff m is divisible by n.
  • A number m is divisible by n, iff remainder when m is divide by n is zero.
  • Iterate through all elements(non-zero) of array first check if it is divisible is a count them and mark them.
  • Iterate through unmarked element of array check if it is divisible b and count them.
  • Now output answer according to problem statement.

EXPLANATION:

Given array on N elements and two Numbers M and F. Let us take two counter variable one for Multan (cm) and other for Fultan (cf). First Multan goes for fight then Fultan, hence if the element is multiple of both M and F we will increase cm as once a players is defeated it will no fight again. Now if number is multiple of only M we will increase sm in that case also, otherwise if number is multiple of F only in this case we will increase cf.

To check the if s[i] is is multiple of k or not, we will use modulus operator i.e. if s[i]%K == 0 then it is s[i] is multiple of k. Now the corner case in this case is when s[i] should be positive integer multiple of k, where as in case s[i] is zero then also s[i]%K will be zero but zero is not positive integer, hence we will check for s[i] not equal to zero.

Total defeats will be sum of cm and cf. Percentage defeat is ((total defeats)/n)x100. If it is less than 70 we will output No else Yes. In case of Yes followed by if cm equals cf output both otherwise output Multan or Fultan depending on cm is greater or cf is greater respectively. Pseudo code is as follows


for each element in S {
 if (element != 0) {
  if( element%M==0 && element%F==0 )
   cm++;
  else if(element%M==0)
   cm++
  else if(element%F==0)
   cf++
  }
}
total = cm+cf
percentage = (total/n)*100
if(percent > = 70) {
 output("Yes")
 if(cm==cf)
  output("Both")
 else if(cm > cf)
  output("Multan")
 else
  output("Fultan")
}
else
 output("No")

To get the actual code of the same, click on solution link below.
This is one of the many possible approaches. Feel free to share your approach :slight_smile:

Keep Coding.

AUTHOR’S SOLUTIONS:

Author’s Solution

1 Like
//