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

📅  最后修改于: 2021-04-24 17:47:11             🧑  作者: 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) elments
        // 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) elments
        // 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) elments 
        # 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) elments
        // 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


输出:
4
1

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