📌  相关文章
📜  从1到N除去两个连续的整数,以使总和等于S

📅  最后修改于: 2021-05-07 00:56:01             🧑  作者: Mango

给定总和S和整数N,任务是从1到N除去两个连续的整数,以使总和等于S。

例子:

Input: N = 4, S = 3
Output: Yes
sum(1, 2, 3, 4) = 10, 
remove 3 and 4 from 1 to N
now sum(1, 2) = 3 = S
Hence by removing 3 and 4 we got sum = S

Input: N = 5, S =3
Output: No
Its not possible to remove two elements
from 1 to N such that new sum is 3
  • 方法1:
    • 创建一个数组,其元素的范围是1到N。
    • 每次删除两个连续的元素,并检查N个自然数之和与两个删除的元素之和之间的差为S。
    • 可以使用公式计算N个自然数的总和
      sum = (n)(n + 1) / 2
    Time complexity: O(n)
    Space complexity: O(n)
    
  • 方法2:
    • 我们知道,可以使用公式计算N个自然数的总和
      sum = (n)(n + 1) / 2
    • 根据问题陈述,我们需要从1到N中删除两个整数,以使剩余整数之和为S。
    • 令要删除的两个连续整数为ii + 1
    • 所以,
      required sum = S = (n)(n + 1) / 2 - (i) - (i + 1)
                     S = (n)(n + 1) / 2 - (2i - 1)
      
      Therefore,
      i = ((n)(n + 1) / 4) - ((S + 1) / 2)
      
    • 因此找到我使用上面的公式
    • 如果我是整数,则答案为是,否则为否

    下面是上述方法的实现:

    C++
    // C++ program remove two consecutive integers
    // from 1 to N to make sum equal to S
      
    #include 
    using namespace std;
      
    // Function to find the numbers
    // to be removed
    float findNumber(int N, int S)
    {
      
        // typecast appropriately
        // so that answer is float
        float i = (((float)(N) * (float)(N + 1)) / 4)
                  - ((float)(S + 1) / 2);
      
        // return the obtained result
        return i;
    }
      
    void check(int N, int S)
    {
      
        float i = findNumber(N, S);
      
        // Convert i to integer
        int integerI = (int)i;
      
        // If i is an integer is 0
        // then answer is Yes
        if (i - integerI == 0)
            cout << "Yes: "
                 << integerI << ", "
                 << integerI + 1
                 << endl;
        else
            cout << "No"
                 << endl;
    }
      
    // Driver code
    int main()
    {
      
        int N = 4, S = 3;
        check(N, S);
      
        N = 5, S = 3;
        check(N, S);
      
        return 0;
    }


    Java
    // Java program remove two consecutive integers 
    // from 1 to N to make sum equal to S 
      
    class GFG 
    {
          
        // Function to find the numbers 
        // to be removed 
        static float findNumber(int N, int S) 
        { 
          
            // typecast appropriately 
            // so that answer is float 
            float i = (((float)(N) * (float)(N + 1)) / 4) 
                    - ((float)(S + 1) / 2); 
          
            // return the obtained result 
            return i; 
        } 
          
        static void check(int N, int S) 
        { 
          
            float i = findNumber(N, S); 
          
            // Convert i to integer 
            int integerI = (int)i; 
          
            // If i is an integer is 0 
            // then answer is Yes 
            if (i - integerI == 0) 
                System.out.println("Yes: " + integerI + 
                                    ", " + (integerI + 1)) ;
            else
                System.out.println("No");
        } 
          
        // Driver code 
        public static void main (String[] args)
        { 
          
            int N = 4, S = 3; 
            check(N, S); 
          
            N = 5; S = 3; 
            check(N, S); 
      
        } 
    }
      
    // This code is contributed by AnkitRai01


    Python3
    # Python3 program remove two consecutive integers 
    # from 1 to N to make sum equal to S 
      
    # Function to find the numbers 
    # to be removed 
    def findNumber(N, S) :
      
        # typecast appropriately 
        # so that answer is float 
        i = (((N) * (N + 1)) / 4) - ((S + 1) / 2); 
      
        # return the obtained result 
        return i; 
      
    def check(N, S) :
      
        i = findNumber(N, S); 
      
        # Convert i to integer 
        integerI = int(i); 
      
        # If i is an integer is 0 
        # then answer is Yes 
        if (i - integerI == 0) :
            print("Yes:", integerI,
                     ",", integerI + 1); 
        else :
            print("No");
      
    # Driver code 
    if __name__ == "__main__" : 
      
        N = 4;
        S = 3; 
        check(N, S); 
      
        N = 5;
        S = 3; 
        check(N, S); 
      
    # This code is contributed by AnkitRai01


    C#
    // C# program remove two consecutive integers 
    // from 1 to N to make sum equal to S 
    using System;
      
    class GFG 
    { 
          
        // Function to find the numbers 
        // to be removed 
        static float findNumber(int N, int S) 
        { 
          
            // typecast appropriately 
            // so that answer is float 
            float i = (((float)(N) * (float)(N + 1)) / 4) 
                    - ((float)(S + 1) / 2); 
          
            // return the obtained result 
            return i; 
        } 
          
        static void check(int N, int S) 
        { 
            float i = findNumber(N, S); 
          
            // Convert i to integer 
            int integerI = (int)i; 
          
            // If i is an integer is 0 
            // then answer is Yes 
            if (i - integerI == 0) 
                Console.WriteLine("Yes: " + integerI + 
                                    ", " + (integerI + 1)) ; 
            else
                Console.WriteLine("No"); 
        } 
          
        // Driver code 
        public static void Main() 
        { 
          
            int N = 4, S = 3; 
            check(N, S); 
          
            N = 5; S = 3; 
            check(N, S); 
        } 
    } 
      
    // This code is contributed by AnkitRai01


    输出:
    Yes: 3, 4
    No
    

    时间复杂度: O(1)
    空间复杂度: O(1)