给定两个数字N和S ,任务是找到范围(1,N)中最小子数组的长度,以使这些选择的数字之和大于S。
例子:
Input: N = 5, S = 11
Output: 3
Explanation:
Smallest subarray with sum > 11 = {5, 4, 3}
Input: N = 4, S = 7
Output: 3
Explanation:
Smallest subarray with sum > 7 = {4, 3, 2}
天真的方法:蛮力方法是按相反的顺序选择元素,直到所有选定元素的总和小于或等于给定数量。
下面是上述方法的实现。
C++
// C++ implementation of the above implementation
#include
using namespace std;
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S) {
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
int main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
cout << count << endl;
return 0;
}
Java
// Java implementation of the above implementation
class GFG
{
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
static int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
public static void main (String[] args)
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
System.out.println(count);
}
}
// This code is contributed by AnkitRai01
Python
# Python implementation of the above implementation
# Function to return the count
# of minimum elements such that
# the sum of those elements is > S.
def countNumber(N, S):
countElements = 0;
currentSum = 0
currSum = 0;
# Loop from N to 1 to add the numbers
# and check the condition.
while (currSum <= S) :
currSum += N;
N = N - 1;
countElements=countElements + 1;
return countElements;
# Driver code
N = 5;
S = 11;
count = countNumber(N, S);
print(count) ;
# This code is contributed by Shivi_Aggarwal
C#
// C# implementation of the above implementation
using System;
class GFG
{
// Function to return the count
// of minimum elements such that
// the sum of those elements is > S.
static int countNumber(int N, int S)
{
int countElements = 0;
// Initialize currentSum = 0
int currSum = 0;
// Loop from N to 1 to add the numbers
// and check the condition.
while (currSum <= S)
{
currSum += N;
N--;
countElements++;
}
return countElements;
}
// Driver code
public static void Main()
{
int N, S;
N = 5;
S = 11;
int count = countNumber(N, S);
Console.WriteLine(count);
}
}
// This code is contributed by AnkitRai01
Javascript
C++
// C++ implementation of the above approach.
#include
using namespace std;
// Function to do a binary search
// on a given range.
int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S) {
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
int main()
{
int N, S;
N = 5;
S = 11;
cout << (N - usingBinarySearch(1, N, N, S) + 1)
<< endl;
return 0;
}
Java
// Java implementation of the above approach.
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void main (String[] args)
{
int N, S;
N = 5;
S = 11;
System.out.println(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach.
# Function to do a binary search
# on a given range.
def usingBinarySearch(start, end, N, S) :
if (start >= end) :
return start;
mid = start + (end - start) // 2;
# Total sum is the sum of N numbers.
totalSum = (N * (N + 1)) // 2;
# Sum until mid
midSum = (mid * (mid + 1)) // 2;
# If remaining sum is < the required value,
# then the required number is in the right half
if ((totalSum - midSum) <= S) :
return usingBinarySearch(start, mid, N, S);
return usingBinarySearch(mid + 1, end, N, S);
# Driver code
if __name__ == "__main__" :
N = 5;
S = 11;
print(N - usingBinarySearch(1, N, N, S) + 1) ;
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach.
using System;
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void Main()
{
int N, S;
N = 5;
S = 11;
Console.WriteLine(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
3
时间复杂度: O(N)
高效的方法:想法是使用二进制搜索概念来解决该问题。
从二元搜索概念,已知当知道问题中存在顺序时可以应用该概念。就是说,对于每次迭代,如果可以确定是否需要的答案位于上半部分或后半部分(即)中,则该问题中存在一个模式。
因此,可以通过以下方式对范围应用二进制搜索:
- 初始化开始= 1,结束=N。
- 查找中点=开始+(结束–开始)/ 2。
- 如果从最后一个元素到中间元素的所有元素的总和小于或等于给定的总和,则end = mid否则start = mid +1。
- 当开始时间小于结束时间时,请重复步骤2。
下面是上述方法的实现。
C++
// C++ implementation of the above approach.
#include
using namespace std;
// Function to do a binary search
// on a given range.
int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S) {
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
int main()
{
int N, S;
N = 5;
S = 11;
cout << (N - usingBinarySearch(1, N, N, S) + 1)
<< endl;
return 0;
}
Java
// Java implementation of the above approach.
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void main (String[] args)
{
int N, S;
N = 5;
S = 11;
System.out.println(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach.
# Function to do a binary search
# on a given range.
def usingBinarySearch(start, end, N, S) :
if (start >= end) :
return start;
mid = start + (end - start) // 2;
# Total sum is the sum of N numbers.
totalSum = (N * (N + 1)) // 2;
# Sum until mid
midSum = (mid * (mid + 1)) // 2;
# If remaining sum is < the required value,
# then the required number is in the right half
if ((totalSum - midSum) <= S) :
return usingBinarySearch(start, mid, N, S);
return usingBinarySearch(mid + 1, end, N, S);
# Driver code
if __name__ == "__main__" :
N = 5;
S = 11;
print(N - usingBinarySearch(1, N, N, S) + 1) ;
# This code is contributed by AnkitRai01
C#
// C# implementation of the above approach.
using System;
class GFG
{
// Function to do a binary search
// on a given range.
static int usingBinarySearch(int start, int end,
int N, int S)
{
if (start >= end)
return start;
int mid = start + (end - start) / 2;
// Total sum is the sum of N numbers.
int totalSum = (N * (N + 1)) / 2;
// Sum until mid
int midSum = (mid * (mid + 1)) / 2;
// If remaining sum is < the required value,
// then the required number is in the right half
if ((totalSum - midSum) <= S)
{
return usingBinarySearch(start, mid, N, S);
}
return usingBinarySearch(mid + 1, end, N, S);
}
// Driver code
public static void Main()
{
int N, S;
N = 5;
S = 11;
Console.WriteLine(N - usingBinarySearch(1, N, N, S) + 1) ;
}
}
// This code is contributed by AnkitRai01
Java脚本
输出:
3
时间复杂度: O(log N)