[Solved] Equally Happy Contest Problem

Equally Happy: Ninja has ‘N’ friends, and it’s the Ninjafest again.

A person is ‘K’ units happy if he/she receives ‘K’ candy as a gift. Ninja has exactly ‘M’ candies.

Your task is to tell if Ninja can make all his friends equally happy after distributing all ‘M’ candies among his friends.

Example :

N = 5 
M = 20
If Ninja distributes ‘4’ candies to each of his friends, all friends will be 4 units happy, so the answer is ‘1’.

Input Format :

The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.

The first line of each test case contains two integers ‘N’ and ‘M’ denoting the number of friends and number of candies Ninja have.

Output Format :

For each test case, print ‘1’ if Ninja can make each friend equally happy after distributing exactly ‘M’ candy else print ‘0’.

Note :

You don’t need to print anything. It has already been taken care of. Just implement the given function.

Constraints :

1 <= T <= 10
1 <= N  <= 10^5
0 <= U, V <= N - 1
Sum of N <= 10^5

Time Limit: 1 sec

Sample Input 1 :

2
5 5
5 13    

Sample Output 1 :

1
0

Explanation Of Sample Input 1 :

For test case 1, 
If Ninja distributes ‘1’ candy to each friend all his friends will have ‘1’units of happiness so the answer is ‘1’.
For test case 2,
There is No way Ninja can distribute all ‘13’ candies equally among his ‘5’ friends so the answer is ‘0’.

Sample Input 2 :

2
7 14
5 0

Sample Output 2 :

1
1

Solution

APPROACH : 

  • We want to distribute all ‘M’ candies equally among all ‘N’ friends.
  • If ‘M’ is divisible by ‘N’ we can distribute each friend’s ‘N / M’ candies, for this case answer is ‘1’.
  • For all other cases, the answer is ‘0’.

ALGORITHM :

  • Let us initialise a variable ‘REM’ and set value as ‘M%N’.
  • Return ‘1’ if ‘REM’ equals ‘0’, else return ‘1’.
Time Complexity

O( 1 ), where ‘1’ is constant time.

We will be using constant operations, so the complexity is O( 1 ).

Space Complexity

O( 1 ), where ‘1’ is constant space.

We are using constant space so overall space complexity is O( 1 ).

/*
    Time Complexity : O( 1 )
    Space Complexity : O( 1 )
    
    where '1' represents constant. 
*/

int equalHappy(int n, int m)
{
    // 'REM' stores the remainder.
    int rem = m % n;

    // If 'REM' is '0' return '1'.
    if (rem == 0) {
        return 1;
    }

    // Else return '0'.
    return 0;
}
/*
    Time Complexity : O( 1 )
    Space Complexity : O( 1 )
    
    where '1' represents constant. 
*/

public class Solution {
    public static int equalHappy(int n, int m) {
		// 'REM' stores the remainder. 
		int rem = m % n;
	  
		// If 'REM' is '0' return '1'.
		if(rem == 0) {
			return 1;
		}
	  
		// Else return '0'.
		return 0;
	}
}
"""
    Time Complexity : O( 1 )
    Space Complexity : O( 1 )

    where '1' represents constant.
"""


def equalHappy(n, m):
    
    # 'REM' stores the remainder.
    rem = m % n

    # If 'REM' is '0' return '1'.
    if rem == 0:
        return 1

    # Else return '0'.
    return 0

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *