📌  相关文章
📜  检查N是否可以分为K个连续元素,总和等于N

📅  最后修改于: 2021-04-27 05:37:40             🧑  作者: Mango

给定一个整数N ,我们的任务是检查N是否可以分为总和等于N的K个连续元素。如果无法以这种方式进行除法,则打印-1 ,否则打印值K。
例子:

方法:为了解决上述问题,让我们将整数N划分为i个连续的数字。序列的项看起来像(d + 1),(d + 2),(d + 3)…..(d + i) ,其中d是每个整数中存在的公共差及其总和序列应等于N。
因此,这些数字的总和也可以表示为:

随着总和= i *(i + 1)/ 2平方增长,我们得到N – sum = i * d 。因此,对于一个存在的解决方案,整数的数量应均匀地除以数量N – sum。步骤如下:

  1. 从索引2迭代索引(例如i )。
  2. 找出第一个i数之和(例如sum )。
  3. 对于任何迭代,如果(N – sum)可被i整除,则输出i的值。
  4. 对于任何迭代,如果N超过总和,则打印“ -1”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the K consecutive
// elements with a sum equal to N
void canBreakN(long long n)
{
    // Iterate over [2, INF]
    for (long long i = 2;; i++) {
 
        // Store the sum
        long long m = i * (i + 1) / 2;
 
        // If the sum exceeds N
        // then break the loop
        if (m > n)
            break;
 
        long long k = n - m;
 
        // Common difference should be
        // divisible by number of terms
        if (k % i)
            continue;
 
        // Print value of i & return
        cout << i << endl;
        return;
    }
 
    // Print "-1" if not possible
    // to break N
    cout << "-1";
}
 
// Driver Code
int main()
{
    // Given N
    long long N = 12;
 
    // Function Call
    canBreakN(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the K consecutive
// elements with a sum equal to N
public static void canBreakN(long n)
{
     
    // Iterate over [2, INF]
    for(long i = 2;; i++)
    {
         
        // Store the sum
        long m = i * (i + 1) / 2;
 
        // If the sum exceeds N
        // then break the loop
        if (m > n)
            break;
 
        long k = n - m;
 
        // Common difference should be
        // divisible by number of terms
        if (k % i != 0)
            continue;
 
        // Print value of i & return
        System.out.println(i);
        return;
    }
 
    // Print "-1" if not possible
    // to break N
    System.out.println("-1");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N
    long N = 12;
 
    // Function call
    canBreakN(N);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 program for the above approach
 
# Function to find the K consecutive
# elements with a sum equal to N
def canBreakN(n):
     
    # Iterate over [2, INF]
    for i in range(2, n):
 
        # Store the sum
        m = i * (i + 1) // 2
 
        # If the sum exceeds N
        # then break the loop
        if (m > n):
            break
 
        k = n - m
 
        # Common difference should be
        # divisible by number of terms
        if (k % i):
            continue
 
        # Print value of i & return
        print(i)
        return
     
    # Print "-1" if not possible
    # to break N
    print("-1")
 
# Driver Code
 
# Given N
N = 12
 
# Function call
canBreakN(N)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to find the K consecutive
// elements with a sum equal to N
public static void canBreakN(long n)
{
      
    // Iterate over [2, INF]
    for(long i = 2;; i++)
    {
          
        // Store the sum
        long m = i * (i + 1) / 2;
  
        // If the sum exceeds N
        // then break the loop
        if (m > n)
            break;
  
        long k = n - m;
  
        // Common difference should be
        // divisible by number of terms
        if (k % i != 0)
            continue;
  
        // Print value of i & return
        Console.Write(i);
        return;
    }
  
    // Print "-1" if not possible
    // to break N
    Console.Write("-1");
}
  
// Driver Code
public static void Main(string[] args)
{
      
    // Given N
    long N = 12;
  
    // Function call
    canBreakN(N);
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
3

时间复杂度: O(K),其中K是总和为K的元素数。
辅助空间: O(1)