给定一个由N 个整数组成的循环数组arr[] ,任务是找到循环数组的起始索引 这样来自该索引的前缀和总是非负的。如果不存在这样的索引,则打印“-1” 。
例子:
Input: arr[] = {3, -6, 7, -1, -4, 5, -1}
Output: 2
Explanation:
Consider the index 2 from where the prefix sum of the given circular array is being calculated, then the prefix sum is given by {9, 3, 7, 6, 2, 7, 6}
Input: arr[] = {3, -5, -1}
Output: -1
方法:根据以下观察可以解决给定的问题:
- 如果数组元素的总和为负,则不存在这样的索引,其从该点开始的前缀总和为非负。
- 否则,索引是具有最小前缀总和的索引之后的索引。
请按照以下步骤解决问题:
- 初始化一个变量,比如sum为0 ,它存储数组元素的总和。
- 初始化一个变量,例如在如0,其存储用于所述圆形遍历的起始索引。
- 初始化一个变量,比如min作为INT_MAX ,它存储数组arr[]的最小前缀和。
- 遍历给定数组并执行以下步骤:
- 将sum的值更新为sum和当前元素arr[i]的总和。
- 如果总和的值小于分钟,然后更新分钟作为总和和在作为第(i + 1)。
- 如果数组的总和为负,则打印-1 。否则,打印(以 % N 为单位)的值作为结果可能的索引。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the starting index
// of the given circular array s.t.
// prefix sum array is non negative
int startingPoint(int A[], int N)
{
// Stores the sum of the array
int sum = 0;
// Stores the starting index
int in = 0;
// Stores the minimum prefix
// sum of A[0..i]
int min = INT_MAX;
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update the value of sum
sum += A[i];
// If sum is less than min
if (sum < min) {
// Update the min as the
// value of prefix sum
min = sum;
// Update in
in = i + 1;
}
}
// Otherwise, no such index is
// possible
if (sum < 0) {
return -1;
}
return in % N;
}
// Driver Code
int main()
{
int arr[] = { 3, -6, 7, -4, -4, 6, -1 };
int N = (sizeof(arr) / sizeof(arr[0]));
cout << startingPoint(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the starting index
// of the given circular array s.t.
// prefix sum array is non negative
static int startingPoint(int A[], int N)
{
// Stores the sum of the array
int sum = 0;
// Stores the starting index
int in = 0;
// Stores the minimum prefix
// sum of A[0..i]
int min = Integer.MAX_VALUE;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Update the value of sum
sum += A[i];
// If sum is less than min
if (sum < min)
{
// Update the min as the
// value of prefix sum
min = sum;
// Update in
in = i + 1;
}
}
// Otherwise, no such index is
// possible
if (sum < 0)
{
return -1;
}
return in % N;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, -6, 7, -4, -4, 6, -1 };
int N = arr.length;
System.out.print(startingPoint(arr, N));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to find the starting index
# of the given circular array
# prefix sum array is non negative
import sys
def startingPoint(A, N):
# Stores the sum of the array
sum = 0
# Stores the starting index
startingindex = 0
# Stores the minimum prefix
# sum of A[0..i]
min = sys.maxsize
# Traverse the array
for i in range(0, N):
# Update the value of sum
sum += A[i]
# If sum is less than minimum
if (sum < min):
# Update the min as
# the value of prefix sum
min = sum
# Update starting index
startingindex = i + 1
# Otherwise no such index is possible
if (sum < 0):
return -1
return startingindex % N
# Driver code
arr = [ 3, -6, 7, -4, -4, 6, -1 ]
N = len(arr)
print(startingPoint(arr,N))
# This code is contributed by Virusbuddah
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the starting index
// of the given circular array s.t.
// prefix sum array is non negative
static int startingPoint(int[] A, int N)
{
// Stores the sum of the array
int sum = 0;
// Stores the starting index
int ind = 0;
// Stores the minimum prefix
// sum of A[0..i]
int min = Int32.MaxValue;
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Update the value of sum
sum += A[i];
// If sum is less than min
if (sum < min)
{
// Update the min as the
// value of prefix sum
min = sum;
// Update in
ind = i + 1;
}
}
// Otherwise, no such index is
// possible
if (sum < 0)
{
return -1;
}
return ind % N;
}
// Driver Code
public static void Main()
{
int[] arr = { 3, -6, 7, -4, -4, 6, -1 };
int N = arr.Length;
Console.Write(startingPoint(arr, N));
}
}
// This code is contributed by ukasp
Javascript
输出:
5
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live