Minimize sum of squares of adjacent elements difference
Last Updated :
10 Aug, 2023
Given an Array arr[] or N elements, the task is to minimize the sum of squares of difference of adjacent elements by adding one element at any position of the array.
Examples:
Input: N = 4, arr = [4, 7, 1, 4]
Output: 36
Explanation: The sum of squares of difference of adjacent element before inserting any element is (4-7)^2 + (7-1)^2 + (1-4)^2 = 9+36+9 = 54. After insertion 4 in between 7 and 1 array become
arr = [4, 7, 4, 1, 4] and sum of square of adjacent element will become 9+9+9+9 = 36.
Input:- N = 4, arr = [3, 5, 1, 8]
Output: 45
Explanation:-Sum of squares of differences of adjacent elements before insertion is 4+16+49 = 69. After insertion 4 in between 1 and 8 the array will become arr = [3, 5, 1, 4, 8] the sum will become 4+16+9+16 = 45. That is the minimum sum which you can make.
Approach: To solve the problem follow the below observations:
- So first of all we have to observe that we are adding the sum of squares of differences of adjacent elements, so to minimize we need to minimize the maximum square so that answer will get more minimized.
- We will check for all adjacent element differences and find the maximum difference so that we can minimize it.
- After it, we will add an element between the elements which are forming the maximum square.
- We will add the element which is close to both the elements if we have to add an element in (1, 9) then we will add 5. So the difference of 1, 5 will be 4 and of 5, 9 will be 4.
- And if we have to add an element in between elements in which the difference is odd then we will do that like this If we have to add between (1, 8) then we will add 5 or 4. Because of we add 5 then the difference between (1, 5) will be 4 and (5, 8) will be 3, If we add 4 then the difference between (1, 4) is 3, and between (4, 8) will be 4. So we can add either 4 or 5.
Below is the code for the above approach:
C++
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum sum
int minimumSum(int n, vector<int> arr)
{
// To store answer
long long ans = 0;
// To store maximum difference
// between to adjacent elements
int diff = 0;
// Iterating over array
for (int i = 1; i < n; i++) {
// Sqaure of current adjacent
// element difference
long long temp = pow(abs(arr[i] - arr[i - 1]), 2);
// Adding to answer
ans += temp;
// Taking maximum difference
diff = max(diff, abs(arr[i] - arr[i - 1]));
}
// Difference of elements
// in between element adding
// after adding element
int one = diff / 2, two = diff / 2;
// If diff is odd
if (diff % 2) {
two++;
}
long long t = pow(one, 2) + pow(two, 2);
t = pow(diff, 2) - t;
// Decresing the sum after adding element
ans -= t;
return ans;
}
// Driver code
int main()
{
int N = 4;
vector<int> arr = { 4, 7, 1, 4 };
// Function call
cout << minimumSum(N, arr);
return 0;
}
Java
// JAVA code for the above approach:
import java.util.*;
public class GFG {
// Function to find minimum sum
public static long minimumSum(int n, List<Integer> arr)
{
// To store answer
long ans = 0;
// To store maximum difference between two adjacent
// elements
int diff = 0;
// Iterating over array
for (int i = 1; i < n; i++) {
// Square of current adjacent element difference
long temp = (long)Math.pow(
Math.abs(arr.get(i) - arr.get(i - 1)), 2);
// Adding to answer
ans += temp;
// Taking maximum difference
diff = Math.max(
diff,
Math.abs(arr.get(i) - arr.get(i - 1)));
}
// Difference of elements in between element adding
// after adding element
int one = diff / 2, two = diff / 2;
// If diff is odd
if (diff % 2 != 0) {
two++;
}
long t = (long)Math.pow(one, 2)
+ (long)Math.pow(two, 2);
t = (long)Math.pow(diff, 2) - t;
// Decreasing the sum after adding element
ans -= t;
return ans;
}
// Driver code
public static void main(String[] args)
{
int N = 4;
List<Integer> arr
= new ArrayList<>(Arrays.asList(4, 7, 1, 4));
// Function call
System.out.println(minimumSum(N, arr));
}
}
// This code is contributed by rambabuguphka
Python3
# Python3 code for the above approach:
# Function to find minimum sum
def minimumSum(n, arr):
# To store answer
ans = 0
# To store maximum difference
# between two adjacent elements
diff = 0
# Iterating over array
for i in range(1, n):
# Square of current adjacent
# element difference
temp = pow(abs(arr[i] - arr[i - 1]), 2)
# Adding to answer
ans += temp
# Taking maximum difference
diff = max(diff, abs(arr[i] - arr[i - 1]))
# Difference of elements
# in between element adding
# after adding element
one = diff // 2
two = diff // 2
# If diff is odd
if diff % 2:
two += 1
t = pow(one, 2) + pow(two, 2)
t = pow(diff, 2) - t
# Decreasing the sum after adding element
ans -= t
return ans
# Driver code
if __name__ == "__main__":
N = 4
arr = [4, 7, 1, 4]
# Function call
print(minimumSum(N, arr))
# This code is contributed by rambabuguphka
C#
using System;
using System.Collections.Generic;
class GFG {
// Function to find minimum sum
static long MinimumSum(int n, List<int> arr)
{
// To store answer
long ans = 0;
// To store maximum difference between two adjacent
// elements
int diff = 0;
// Iterating over array
for (int i = 1; i < n; i++) {
// Square of current adjacent element difference
long temp = (long)Math.Pow(
Math.Abs(arr[i] - arr[i - 1]), 2);
// Adding to answer
ans += temp;
// Taking maximum difference
diff = Math.Max(diff,
Math.Abs(arr[i] - arr[i - 1]));
}
// Difference of elements in between element adding
// after adding element
int one = diff / 2, two = diff / 2;
// If diff is odd
if (diff % 2 != 0) {
two++;
}
long t = (long)Math.Pow(one, 2)
+ (long)Math.Pow(two, 2);
t = (long)Math.Pow(diff, 2) - t;
// Decreasing the sum after adding element
ans -= t;
return ans;
}
static void Main(string[] args)
{
int N = 4;
List<int> arr = new List<int>{ 4, 7, 1, 4 };
// Function call
Console.WriteLine(MinimumSum(N, arr));
}
}
// This code is contributed by shivamgupta0987654321
JavaScript
// Function to find minimum sum
function minimumSum(n, arr) {
// To store answer
let ans = 0;
// To store maximum difference
// between two adjacent elements
let diff = 0;
// Iterating over the array
for (let i = 1; i < n; i++) {
// Square of the current adjacent
// element difference
let temp = Math.pow(Math.abs(arr[i] - arr[i - 1]), 2);
// Adding to the answer
ans += temp;
// Taking the maximum difference
diff = Math.max(diff, Math.abs(arr[i] - arr[i - 1]));
}
// Difference of elements
// in between elements adding
// after adding elements
let one = Math.floor(diff / 2);
let two = Math.floor(diff / 2);
// If diff is odd
if (diff % 2 === 1) {
two++;
}
let t = Math.pow(one, 2) + Math.pow(two, 2);
t = Math.pow(diff, 2) - t;
// Decreasing the sum after adding element
ans -= t;
return ans;
}
// Driver code
const N = 4;
const arr = [4, 7, 1, 4];
// Function call
console.log(minimumSum(N, arr));
// Contributed by Aditi Tyagi
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Rearrange Array to minimize difference of sum of squares of odd and even index elements Given an array arr[] of size N (multiple of 8) where the values in the array will be in the range [a, (a+8*N) -1] (a can be any positive integer), the task is to rearrange the array in a way such that the difference between the sum of squares at odd indices and sum of squares of the elements at even
15+ min read
Sum of minimum difference between consecutive elements of an array Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive elements of an array where the array is filled in the below manner: Each element of an array lies in the range given at its corresponding index in the range array
10 min read
Minimize the absolute difference of sum of two subsets Given a number n, divide first n natural numbers (1, 2, â¦n) into two subsets such that difference between sums of two subsets is minimum.Examples: Input : n = 4 Output : First subset sum = 5, Second subset sum = 5. Difference = 0 Explanation: Subset 1: 1 4 Subset 2: 2 3 Input : n = 6 Output: First s
10 min read
Minimize difference after changing all odd elements to even Given an array arr[] of N positive integers. We have to perform one operation on every odd element in the given array i.e., multiply every odd element by 2 in the given array, the task is to find the minimum difference between any two elements in the array after performing the given operation.Exampl
5 min read
Find minimum difference with adjacent elements in Array Given an array A[] of N integers, the task is to find min(A[0], A[1], ..., A[i-1]) - min(A[i+1], A[i+2], ..., A[n-1]) for each i (1 ⤠i ⤠N). Note: If there are no elements at the left or right of i then consider the minimum element towards that part zero. Examples: Input: N = 4, A = {8, 4, 2, 6}Out
12 min read