📜  将前N个自然数分成3个相等的和子集

📅  最后修改于: 2021-04-29 04:05:12             🧑  作者: Mango

给定一个整数N ,任务是检查范围[1,N]中的元素是否可以划分为三个非空等和子集。如果可能,请打印“是”,否则打印“否”

例子:

方法:有两种情况:

  1. 如果N≤3:在这种情况下,无法划分满足给定条件的子集中的元素。因此,请打印No。
  2. 如果N> 3:在这种情况下,仅当范围[1,N]的所有元素的和可被3整除时,可以很容易地将其计算为sum =(N *(N + 1))/ 2 。现在,如果总和%3 = 0,则打印“是”,否则打印“否”

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true
// if the subsets are possible
bool possible(int n)
{
 
    // If n <= 3 then it is not possible
    // to divide the elements in three subsets
    // satisfying the given conditions
    if (n > 3) {
 
        // Sum of all the elements
        // in the range [1, n]
        int sum = (n * (n + 1)) / 2;
 
        // If the sum is divisible by 3
        // then it is possible
        if (sum % 3 == 0) {
            return true;
        }
    }
    return false;
}
 
// Driver code
int main()
{
    int n = 5;
 
    if (possible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.math.*;
 
class GFG
{
 
    // Function that returns true
    // if the subsets are possible
    public static boolean possible(int n)
    {
     
        // If n <= 3 then it is not possible
        // to divide the elements in three subsets
        // satisfying the given conditions
        if (n > 3)
        {
     
            // Sum of all the elements
            // in the range [1, n]
            int sum = (n * (n + 1)) / 2;
     
            // If the sum is divisible by 3
            // then it is possible
            if (sum % 3 == 0)
            {
                return true;
            }
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
 
        if (possible(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Naman_Garg


Python3
# Python3 implementation of the approach
 
# Function that returns true
# if the subsets are possible
def possible(n) :
 
    # If n <= 3 then it is not possible
    # to divide the elements in three subsets
    # satisfying the given conditions
    if (n > 3) :
 
        # Sum of all the elements
        # in the range [1, n]
        sum = (n * (n + 1)) // 2;
 
        # If the sum is divisible by 3
        # then it is possible
        if (sum % 3 == 0) :
            return True;
     
    return False;
 
# Driver code
if __name__ == "__main__" :
 
    n = 5;
 
    if (possible(n)) :
        print("Yes");
    else :
        print("No");
         
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that returns true
// if the subsets are possible
public static bool possible(int n)
{
 
    // If n <= 3 then it is not possible
    // to divide the elements in three subsets
    // satisfying the given conditions
    if (n > 3)
    {
 
        // Sum of all the elements
        // in the range [1, n]
        int sum = (n * (n + 1)) / 2;
 
        // If the sum is divisible by 3
        // then it is possible
        if (sum % 3 == 0)
        {
            return true;
        }
    }
    return false;
}
 
// Driver code
static public void Main ()
{
    int n = 5;
 
    if (possible(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by ajit


Javascript


输出:
Yes