给定一个数字N和一个整数S ,任务是创建一个N个整数的数组,以使所有元素的总和等于S并打印一个元素K ,其中0≤K≤S,从而不存在总和等于的子数组K或(S – K) 。
如果没有这样的数组,则打印“ -1” 。
注意: K可以有多个值。您可以打印其中任何一个。
例子:
Input: N = 1, S = 4
Output: {4}
K = 2
Explanation:
There exists an array {4} whose sum is 4.
From all possible value of K i.e., 0 ≤ K ≤ 4, K = 1, 2, and 3 satisfy the given conditions.
For K = 2, there is no subarray whose sum is 2 or S – K i.e., 4 – 2 = 2.
Input: N = 3, S = 8
Output: {2, 2, 4}
K = 1
Explanation:
There exists an array {2, 2, 4} and there exists K as 1 such that there is no subarray whose sum is 1 and S – K i.e., 8 – 1 = 7.
方法:要解决上述问题,我们必须注意:
- 如果2 * N> S,则不可能有数组。
例如:For N = 3 and S = 4, then the possible arrays are {1, 2, 1}, {1, 1, 2}, {2, 1, 1}.
The possible values for K are 0, 1, 2, 3 (0 < = k < = S).
But there is no value for K which satisfy the condition.
So the solution to this is not possible. - 仅当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)