Find position forming AP with prefix and suffix sum of Array with common difference D
Last Updated :
24 Mar, 2022
Given an array, arr[] of size N and a positive integer D, the task is to find the position i of an element in arr[] such that the prefix sum, arr[i] and suffix sum is in arithmetic progression with common difference D, i.e. arr[i] – sum(arr[0, . . ., i-1]) = sum(arr[i+1, . . ., N-1]) – arr[i] = D. If no such position exists return -1.
Examples:
Input: arr[] = { 4, 6, 20, 10, 15, 5 }, D = 10
Output: 3
Explanation: Sum till 3rd position is 4+6 = 10.
Element at 3rd position is 20.
Suffix sum is 10 + 15 + 5 = 30.
So 10, 20, 30 is forming an AP whose common difference is 10.
Input: arr[] ={ 1, 3, 5 }, D = 7
Output: -1
Approach: The given problem can be solved by using Linear Search approach based on the below observation:
- If the size of array is less than 3 then no sequence is possible so simply return -1.
- Calculate sum of array arr[] and store it in Sum.
- if Sum % 3 != 0, then return 0.
- Initialize a variable say Mid = Sum / 3 to store the middle element of the AP series.
- Iterate arr[] from index i = 1 to N – 2:
- Calculate the prefix sum till i-1 (say temp)
- If temp is equal to Mid – D then suffix sum is Mid + D. So return the position i+1.
- Else return -1.
- If loop terminates and no element in arr[] is equal to mid then simply return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkArray( int arr[], int N, int d)
{
if (N < 3)
return -1;
int i, Sum = 0, temp = 0;
for (i = 0; i < N; i++)
Sum += arr[i];
if (Sum % 3 != 0)
return 0;
int Mid = Sum / 3;
for (i = 1; i < N - 1; i++) {
temp += arr[i - 1];
if (arr[i] == Mid) {
if (temp == Mid - d)
return i + 1;
else
return 0;
}
}
return 0;
}
int main()
{
int arr[] = { 4, 6, 20, 10, 15, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int D = 10;
cout << checkArray(arr, N, D) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int checkArray( int arr[], int N, int d)
{
if (N < 3 )
return - 1 ;
int i, Sum = 0 , temp = 0 ;
for (i = 0 ; i < N; i++)
Sum += arr[i];
if (Sum % 3 != 0 )
return 0 ;
int Mid = Sum / 3 ;
for (i = 1 ; i < N - 1 ; i++) {
temp += arr[i - 1 ];
if (arr[i] == Mid) {
if (temp == Mid - d)
return i + 1 ;
else
return 0 ;
}
}
return 0 ;
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 20 , 10 , 15 , 5 };
int N = arr.length;
int D = 10 ;
System.out.println(checkArray(arr, N, D));
}
}
|
Python3
def checkArray(arr, N, d):
if (N < 3 ):
return - 1
i = 0
Sum = 0
temp = 0
for i in range (N):
Sum + = arr[i]
if ( Sum % 3 ! = 0 ):
return 0
Mid = Sum / 3
for i in range ( 1 , N - 1 ):
temp + = arr[i - 1 ]
if (arr[i] = = Mid):
if (temp = = Mid - d):
return i + 1
else :
return 0
return 0
if __name__ = = "__main__" :
arr = [ 4 , 6 , 20 , 10 , 15 , 5 ]
N = len (arr)
D = 10
print (checkArray(arr, N, D))
|
C#
using System;
class GFG
{
static int checkArray( int []arr, int N, int d)
{
if (N < 3)
return -1;
int i, Sum = 0, temp = 0;
for (i = 0; i < N; i++)
Sum += arr[i];
if (Sum % 3 != 0)
return 0;
int Mid = Sum / 3;
for (i = 1; i < N - 1; i++) {
temp += arr[i - 1];
if (arr[i] == Mid) {
if (temp == Mid - d)
return i + 1;
else
return 0;
}
}
return 0;
}
public static void Main()
{
int []arr = { 4, 6, 20, 10, 15, 5 };
int N = arr.Length;
int D = 10;
Console.WriteLine(checkArray(arr, N, D));
}
}
|
Javascript
<script>
const checkArray = (arr, N, d) => {
if (N < 3)
return -1;
let i, Sum = 0, temp = 0;
for (i = 0; i < N; i++)
Sum += arr[i];
if (Sum % 3 != 0)
return 0;
let Mid = parseInt(Sum / 3);
for (i = 1; i < N - 1; i++) {
temp += arr[i - 1];
if (arr[i] == Mid) {
if (temp == Mid - d)
return i + 1;
else
return 0;
}
}
return 0;
}
let arr = [4, 6, 20, 10, 15, 5];
let N = arr.length;
let D = 10;
document.write(checkArray(arr, N, D));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...