📌  相关文章
📜  创建一个大小为 N 且总和为 S 的数组,使得不存在总和为 S 或 SK 的子数组

📅  最后修改于: 2021-10-26 02:37:59             🧑  作者: Mango

给定一个数字N和一个整数S ,任务是创建一个由N 个整数组成的数组,使得所有元素的总和等于S并打印一个元素K ,其中 0 ≤ K ≤ S,这样就不存在总和等于的子数组K(S – K)
如果没有这样的数组,则打印“-1”
注意: K可以有多个值。您可以打印其中任何一个。
例子:

方法:要解决上面提到的问题,我们必须观察:

  1. 如果2 * N > S那么就不可能有数组。
    例如:
  2. 只有当2 * N <= S并且可以使用元素(N-1) 乘以 2并且最后一个元素为S – (2 * (N – 1))并且 K 始终为 1 时,才能创建数组。

下面是上述方法的实现:

C++
// C++ for the above approach
#include
using namespace std;
      
// Function to create an array with
// N elements with sum as S such that
// the given conditions satisfy
void createArray(int n, int s)
{
      
    // Check if the solution exists
    if (2 * n <= s)
    {
  
        // Print the array as
        // print (n-1) elements
        // of array as 2
        for(int i = 0; i < n - 1; i++) 
        {
           cout << "2" << " ";
           s -= 2;
        }
  
        // Print the last element
        // of the array
        cout << s << endl;
  
        // Print the value of k
        cout << "1" << endl;
    }
    else
      
        // If solution doesnot exists
        cout << "-1" << endl;
}
  
// Driver Code
int main()
{
      
    // Given N and sum S
    int N = 1;
    int S = 4;
  
    // Function call
    createArray(N, S);
}
  
// This code is contributed by Ritik Bansal


Java
// Java for the above approach
class GFG{
      
// Function to create an array with
// N elements with sum as S such that
// the given conditions satisfy
static void createArray(int n, int s)
{
  
    // Check if the solution exists
    if (2 * n <= s)
    {
  
        // Print the array as
        // print (n-1) elements
        // of array as 2
        for (int i = 0; i < n - 1; i++) 
        {
            System.out.print(2 + " ");
            s -= 2;
        }
  
        // Print the last element
        // of the array
        System.out.println(s);
  
        // Print the value of k
        System.out.println(1);
    }
    else
      
        // If solution doesnot exists
        System.out.print("-1");
}
  
// Driver Code
public static void main(String[] args) 
{
  
    // Given N and sum S
    int N = 1;
    int S = 4;
  
    // Function call
    createArray(N, S);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 for the above approach
  
# Function to create an array with
# N elements with sum as S such that
# the given conditions satisfy
def createArray(n, s):
   
    # Check if the solution exists
    if (2 * n<= s):             
          
        # Print the array as
        # print (n-1) elements 
        # of array as 2
        for i in range(n-1):
            print(2, end =" ")
            s-= 2
              
        # Print the last element
        # of the array 
        print(s) 
          
        # Print the value of k 
        print(1)
    else:
        # If solution doesnot exists
        print('-1')
  
  
# Driver Code 
  
# Given N and sum S  
N = 1
S = 4
  
# Function call
createArray(N, S)


C#
// C# program for the above approach
using System;
class GFG{
      
// Function to create an array with
// N elements with sum as S such that
// the given conditions satisfy
static void createArray(int n, int s)
{
      
    // Check if the solution exists
    if (2 * n <= s)
    {
  
        // Print the array as
        // print (n-1) elements
        // of array as 2
        for(int i = 0; i < n - 1; i++) 
        {
           Console.Write(2 + " ");
           s -= 2;
        }
  
        // Print the last element
        // of the array
        Console.WriteLine(s);
  
        // Print the value of k
        Console.WriteLine(1);
    }
    else
      
        // If solution doesnot exists
        Console.Write("-1");
}
  
// Driver Code
public static void Main() 
{
  
    // Given N and sum S
    int N = 1;
    int S = 4;
  
    // Function call
    createArray(N, S);
}
}
  
// This code is contributed by Code_Mech


Javascript


输出:
4
1

时间复杂度: O(N)
辅助空间: O(1)