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++
#include <bits/stdc++.h>
using namespace std;
int minimumSum( int n, vector< int > arr)
{
long long ans = 0;
int diff = 0;
for ( int i = 1; i < n; i++) {
long long temp = pow ( abs (arr[i] - arr[i - 1]), 2);
ans += temp;
diff = max(diff, abs (arr[i] - arr[i - 1]));
}
int one = diff / 2, two = diff / 2;
if (diff % 2) {
two++;
}
long long t = pow (one, 2) + pow (two, 2);
t = pow (diff, 2) - t;
ans -= t;
return ans;
}
int main()
{
int N = 4;
vector< int > arr = { 4, 7, 1, 4 };
cout << minimumSum(N, arr);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static long minimumSum( int n, List<Integer> arr)
{
long ans = 0 ;
int diff = 0 ;
for ( int i = 1 ; i < n; i++) {
long temp = ( long )Math.pow(
Math.abs(arr.get(i) - arr.get(i - 1 )), 2 );
ans += temp;
diff = Math.max(
diff,
Math.abs(arr.get(i) - arr.get(i - 1 )));
}
int one = diff / 2 , two = diff / 2 ;
if (diff % 2 != 0 ) {
two++;
}
long t = ( long )Math.pow(one, 2 )
+ ( long )Math.pow(two, 2 );
t = ( long )Math.pow(diff, 2 ) - t;
ans -= t;
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
List<Integer> arr
= new ArrayList<>(Arrays.asList( 4 , 7 , 1 , 4 ));
System.out.println(minimumSum(N, arr));
}
}
|
Python3
def minimumSum(n, arr):
ans = 0
diff = 0
for i in range ( 1 , n):
temp = pow ( abs (arr[i] - arr[i - 1 ]), 2 )
ans + = temp
diff = max (diff, abs (arr[i] - arr[i - 1 ]))
one = diff / / 2
two = diff / / 2
if diff % 2 :
two + = 1
t = pow (one, 2 ) + pow (two, 2 )
t = pow (diff, 2 ) - t
ans - = t
return ans
if __name__ = = "__main__" :
N = 4
arr = [ 4 , 7 , 1 , 4 ]
print (minimumSum(N, arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static long MinimumSum( int n, List< int > arr)
{
long ans = 0;
int diff = 0;
for ( int i = 1; i < n; i++) {
long temp = ( long )Math.Pow(
Math.Abs(arr[i] - arr[i - 1]), 2);
ans += temp;
diff = Math.Max(diff,
Math.Abs(arr[i] - arr[i - 1]));
}
int one = diff / 2, two = diff / 2;
if (diff % 2 != 0) {
two++;
}
long t = ( long )Math.Pow(one, 2)
+ ( long )Math.Pow(two, 2);
t = ( long )Math.Pow(diff, 2) - t;
ans -= t;
return ans;
}
static void Main( string [] args)
{
int N = 4;
List< int > arr = new List< int >{ 4, 7, 1, 4 };
Console.WriteLine(MinimumSum(N, arr));
}
}
|
Javascript
function minimumSum(n, arr) {
let ans = 0;
let diff = 0;
for (let i = 1; i < n; i++) {
let temp = Math.pow(Math.abs(arr[i] - arr[i - 1]), 2);
ans += temp;
diff = Math.max(diff, Math.abs(arr[i] - arr[i - 1]));
}
let one = Math.floor(diff / 2);
let two = Math.floor(diff / 2);
if (diff % 2 === 1) {
two++;
}
let t = Math.pow(one, 2) + Math.pow(two, 2);
t = Math.pow(diff, 2) - t;
ans -= t;
return ans;
}
const N = 4;
const arr = [4, 7, 1, 4];
console.log(minimumSum(N, arr));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...