Penalty Shooters | Practice | GeeksforGeeks

Geek and Nerd are strikers playing football with their friend the Goalie. Their energies are denoted by X,Y and Z respectively. 
Strikers can only score a goal if the Goalie's energy is a factor of their energy. For every goal scored the energy of the respective player is decreased by 1. Each save decreses the Goalie's energy by 1. The game ends when the Goalie's energy is reduced to 1. The strikers can try to goal repeatedly in any order in order to optimise the total number of goals. Geek takes the lead whenever in doubt. 
Find the number of goals made by Geek and Nerd. 

Example 1:
Input: 
X = 4, Y = 9, Z = 5

Output: 3 2

Explaination:
1st Kick: X=4, Y=9, Z=4
Z is not a factor of X or Y. So the Goalie 
will save the first kick and his energy will 
reduce by 1. 

2nd Kick: X=3, Y=9, Z=4
Z is a factor of X. So Geek will take the 
goal and reduce X by 1. 

3rd Kick: X=3, Y=9, Z=3. Goalie saves. 

4th Kick: X=2, Y=9, Z=3. 
Z is a factor of both X and Y. 
When in doubt, Geek takes the lead. 

5th Kick: X=2, Y=8, Z=3. Nerd goals.
6th Kick: X=2, Y=8, Z=2. Goalie saves.
7th Kick: X=1, Y=8, Z=2. Geek goals.
8th Kick: X=1, Y=7, Z=2. Nerd goals.
9th Kick: X=1, Y=7, Z=1. 
Goalie saves and ends the game.

Example 2:
Input: 
X = 13, Y = 10, Z = 7

Output: 0 3

Explaination: Since X is a prime number, 
Geek will never goal. 

Your Task:
You do not need to read input or print anything. Your task is to complee the function goals() which takes X, Y and Z as input parameters and returns a list of integers containing two elements denoting the number of goals scored by Geek and Nerd respectively. 

Expected Time Complexity: O(Z)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ X, Y, Z ≤ 105
+1
imohdalam2 months ago
Java | O(n)
class Solution{
    static List<Integer> goals(int X, int Y, int Z)
    {
        ArrayList<Integer> list = new ArrayList<Integer>();
        
        int Geek = 0;
        int Nerd = 0;
        
        while(Z != 1){
            if( X%Z == 0 && Y%Z == 0){ Geek++; X--; } // Goal by Geek
            else if(X%Z != 0 && Y%Z != 0){ Z--; } // Goal saved by Goalie
            else if(X%Z != 0 && Y%Z == 0){ Nerd++; Y--; } // Goal by Nerd
            else if(X%Z == 0 && Y%Z != 0){ Geek++; X--; } // Goal by Geek
        }
        
        list.add(Geek);
        list.add(Nerd);
        
        return list;
    }
}
+1
wsurajml0073 months ago
vector<int> goals(int x, int y, int z){              int geek = 0, nerd = 0;       while(z != 1){           if(x % z == 0){               geek++;               x = x-1;           }else if(y % z == 0){               nerd++;               y = y-1;           }else{               z = z-1;           }       }              vector<int> v;       v.push_back(geek);       v.push_back(nerd);              return v;   }
0
vishwajeetofficial20223 months ago
//JAVA SOLUTION

class Solution{
    static List<Integer> goals(int X, int Y, int Z)
    {
        // code here
      ArrayList<Integer> aL = new ArrayList<>();
       int a = 0 , b = 0;
       while(Z > 1)
       {
           if(X % Z == 0)
           {
               ++a;
               --X;
           }
           else if(Y % Z == 0)
           {
               ++b;
               --Y;
           }
           else
               --Z;
       }
       aL.add(a);
       aL.add(b);
       return aL;
    }
}
0
diptendunandi3 months ago
vector<int> goals(int X, int Y, int Z){
        // code here
        int Xi = X;
       int Yi = Y;
       int Zi = Z;
       
       while(Z > 1) {
           if(X%Z == 0){
               X--;
           }
           else if(Y%Z == 0) {
               Y--;
           }
           else {
               Z--;
           }
       }
       
       return {Xi - X, Yi - Y};
    }
0
jacksonbusch333 months ago
//C++ Solution
class Solution{public:   vector<int> goals(int X, int Y, int Z){       int Xi = X;       int Yi = Y;       int Zi = Z;              while(Z > 1) {           if(X%Z == 0){               --X;           }           else if(Y%Z == 0) {               --Y;           }           else {               --Z;           }       }              return {Xi - X, Yi - Y};   }};
0
shivkantpandey704 months ago
def goals(self, X, Y, Z):       a=c=0       b=Z       while(b>1):           if(X%b==0):               a+=1               X-=1           elif Y%b==0:               c+=1               Y-=1           else:               b-=1       return a,c 
0
saivadamodalu4 months ago
PLC-22809-13-74074
0
proakash2566 months ago
static List<Integer> goals(int X, int Y, int Z)   {       // code here       ArrayList<Integer> ar = new ArrayList<>();       int a = 0 , b = 0;       while(Z > 1)       {           if((X % Z) == 0)           {               ++a;               --X;           }           else if(Y % Z == 0)           {               ++b;               --Y;           }           else               --Z;       }       ar.add(a);       ar.add(b);       return ar;   }
-1
Rohit Vishwakarma8 months ago
Rohit Vishwakarma
can someone please tell me where I'm wrong here, its giving me segfault error
class Solution{public:    vector<int> goals(int x, int y, int z){        // code here        if (z == 1)        {            return vector<int>{0, 0};        }        int geek, nerd;        geek = nerd = 0;        if (x % z == 0)        {            geek++;            x--;        }        else if (y % z == 0)        {            nerd++;            y--;        }        else        {            z--;        }        vector<int> res = goals(x, y, z);        int p=res[0],q=res[1];        res.clear();         return vector<int>{geek + p, nerd + q};    }};
-1
ANKIT SINHA9 months ago
ANKIT SINHA
https://uploads.disquscdn.c...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.