📌  相关文章
📜  最小化每个元素与 (i+K)%N 处元素的位置乘积之和

📅  最后修改于: 2022-05-13 01:56:10.591000             🧑  作者: Mango

最小化每个元素与 (i+K)%N 处元素的位置乘积之和

给定一个大小为N的整数数组Arr[] ,任务是找到给定函数在应用于数组时的最小值

例子:

Naive Approach:解决该问题的基本方法是针对范围 [0, N-1] 内的所有 K 值计算函数的值。

请按照以下步骤解决问题:

  • 可以观察到,如果Arr的长度为N ,那么给定函数只能有N个不同的值。
  • k = 0 到 N – 1分别找出函数S(k)的所有可能值,并打印其中的最小值

下面是朴素方法的实现。

C++
// C++ Program to find minimum value of given function
#include 
using namespace std;
 
// Function to find value
// of given function at k
int fnValueAtk(vector Arr, int k)
{
  int sum = 0;
  int N = Arr.size();
  for (int i = 0; i < N; i++) {
    sum += (i + 1) * Arr[(i + k) % N];
  }
  return sum;
}
 
// Function to print minimum value
// of given function
void minValFn(vector Arr)
{
  int N = Arr.size();
  int min_val = INT_MAX;
  for (int k = 0; k < N; k++) {
    int fn_At_k = fnValueAtk(Arr, k);
    min_val = min(min_val, fn_At_k);
  }
  cout << min_val;
}
 
int main()
{
 
  vector Arr = { 2, 1, 4, 3 };
  minValFn(Arr);
  return 0;
}
 
// This code is contributed by hrithikgarg03188.


Java
// Java Program to find minimum value of given function
 
import java.util.*;
 
class GFG {
 
    // Function to print minimum value
    // of given function
    static void minValFn(int[] Arr)
    {
        int N = Arr.length;
        int min_val = Integer.MAX_VALUE;
        for (int k = 0; k < N; k++) {
            int fn_At_k = fnValueAtk(Arr, k);
            min_val = Math.min(min_val, fn_At_k);
        }
        System.out.println(min_val);
    }
 
    // Function to find value
    // of given function at k
    static int fnValueAtk(int[] Arr, int k)
    {
        int sum = 0;
        int N = Arr.length;
        for (int i = 0; i < N; i++) {
            sum += (i + 1) * Arr[(i + k) % N];
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] Arr = { 2, 1, 4, 3 };
        minValFn(Arr);
    }
}


Python3
# Python3 program to find the minimum value of given function
import sys
 
# function to find the value
# of given function at k
def fnValueAtk(Arr, k):
    sums = 0
    N = len(Arr)
    for i in range(N):
        sums += (i + 1) * Arr[(i + k) % N]
    return sums
 
# function to print minimum value of given function
def minValFn(Arr):
    N = len(Arr)
    min_val = sys.maxsize
    for k in range(N):
        fn_At_k = fnValueAtk(Arr, k)
        min_val = min(min_val, fn_At_k)
 
    print(min_val)
 
# Driver Code
Arr = [2, 1, 4, 3]
minValFn(Arr)
 
# This code is contributed by phasing17


C#
// C# Program to find minimum value of given function
using System;
 
public class GFG{
 
  // Function to print minimum value
  // of given function
  static void minValFn(int[] Arr)
  {
    int N = Arr.Length;
    int min_val = Int32.MaxValue;
    for (int k = 0; k < N; k++) {
      int fn_At_k = fnValueAtk(Arr, k);
      min_val = Math.Min(min_val, fn_At_k);
    }
    Console.WriteLine(min_val);
  }
 
  // Function to find value
  // of given function at k
  static int fnValueAtk(int[] Arr, int k)
  {
    int sum = 0;
    int N = Arr.Length;
    for (int i = 0; i < N; i++) {
      sum += (i + 1) * Arr[(i + k) % N];
    }
    return sum;
  }
 
  // Driver code
  static public void Main (){
 
    int[] Arr = { 2, 1, 4, 3 };
    minValFn(Arr);
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript
// JavaScript program to find the
// minimum value of the given function
 
// function to find the value of
// given function at k
function fnValueAtk(Arr, k)
{
  var sum = 0;
  var N = Arr.length;
  for (var i = 0; i < N; i++)
  {
    sum += (i + 1) * Arr[(i + k) % N];
  }
  return sum;
}
 
// function to print the minimum value
// of given function
function minValFn(Arr)
{
  var N = Arr.length;
  var min_val = Number.MAX_SAFE_INTEGER;
  for (var k = 0; k < N; k++)
  {
    var fn_At_k = fnValueAtk(Arr, k);
    min_val = Math.min(min_val, fn_At_k);
  }
  document.write(min_val);
}
 
// Driver Code
var Arr = [2, 1, 4, 3];
minValFn(Arr);
 
// This code is contributed by phasing17


C++
// C++ Program to find minimum value
// Of given function
 
#include 
using namespace std;
 
void minValFn(vector Arr)
{
    int N = Arr.size();
    int min_val;
 
    // To store sum of all elements
    int sum = 0;
 
    // Finding S(0)
    int s0 = 0;
    for (int i = 0; i < N; i++) {
        s0 += Arr[i] * (i + 1);
        sum += Arr[i];
    }
 
    min_val = s0;
 
    // To store s(k-1)
    int sk_min_1 = s0;
    for (int k = 1; k < N; k++) {
 
        // Using formula
        int sk = sk_min_1 - sum + Arr[(k - 1) % N] * N;
        min_val = min(sk, min_val);
        sk_min_1 = sk;
    }
    cout << min_val << "\n";
}
 
// Driver code
int main()
{
    vector Arr = { 2, 1, 4, 3 };
    minValFn(Arr);
}
 
// This code is contributed by Taranpreet


Java
// Java Program to find minimum value
// Of given function
 
import java.io.*;
 
public class GFG {
    static void minValFn(int[] Arr)
    {
        int N = Arr.length;
        int min_val;
 
        // To store sum of all elements
        int sum = 0;
 
        // Finding S(0)
        int s0 = 0;
        for (int i = 0; i < N; i++) {
            s0 += Arr[i] * (i + 1);
            sum += Arr[i];
        }
 
        min_val = s0;
 
        // To store s(k-1)
        int sk_min_1 = s0;
        for (int k = 1; k < N; k++) {
 
            // Using formula
            int sk = sk_min_1 - sum
                     + Arr[(k - 1) % N] * N;
            min_val = Math.min(sk, min_val);
            sk_min_1 = sk;
        }
        System.out.println(min_val);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] Arr = { 2, 1, 4, 3 };
        minValFn(Arr);
    }
}


Python3
# Python3 program to find the minimum value of
# the given function
 
# function to find the minimum value
def minValFn(Arr):
    N = len(Arr)
 
    # to store sum of all elements
    sums = 0
 
    # finding S(0)
    s0 = 0
    for i in range(N):
        s0 += Arr[i] * (i + 1)
        sums += Arr[i]
 
    min_val = s0
 
    # To store s(k - 1)
    sk_min_1 = s0
    for k in range(1, N):
        # using formula for minimum value
        sk = sk_min_1 - sums + Arr[(k - 1) % N] * N
        min_val = min(sk, min_val)
        sk_min_1 = sk
 
    print(min_val)
 
# Driver code
Arr = [2, 1, 4, 3]
minValFn(Arr)
 
# This code is contributed by phasing17


C#
// C# Program to find minimum value
// Of given function
 
using System;
 
public class GFG {
  static void minValFn(int[] Arr)
  {
    int N = Arr.Length;
    int min_val;
 
    // To store sum of all elements
    int sum = 0;
 
    // Finding S(0)
    int s0 = 0;
    for (int i = 0; i < N; i++) {
      s0 += Arr[i] * (i + 1);
      sum += Arr[i];
    }
 
    min_val = s0;
 
    // To store s(k-1)
    int sk_min_1 = s0;
    for (int k = 1; k < N; k++) {
 
      // Using formula
      int sk = sk_min_1 - sum
        + Arr[(k - 1) % N] * N;
      min_val = Math.Min(sk, min_val);
      sk_min_1 = sk;
    }
    Console.Write(min_val);
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int[] Arr = { 2, 1, 4, 3 };
    minValFn(Arr);
  }
}
 
// This code is contributed by ukasp.


JavaScript



输出
20

时间复杂度: O(N 2 )。
辅助空间: O(1)

高效方法:解决问题的有效方法基于S(k)S(k-1) 之间的以下关系:

请参阅下图以获得更好的理解。

插图:

按照下面提到的步骤来实现上述观察:

  • 声明一个变量'min_val'来存储给定函数的最小值。
  • 在数组的单次遍历中找到S(0)并将其存储在'min_val'中。
  • 然后使用上面的等式找到k = 1 到 N – 1的函数S(k)的所有可能值。
    • 如果此值小于“min_val”,则更新“ min_val”
  • min_val的最终值是所需的答案。

下面是上述方法的实现:

C++

// C++ Program to find minimum value
// Of given function
 
#include 
using namespace std;
 
void minValFn(vector Arr)
{
    int N = Arr.size();
    int min_val;
 
    // To store sum of all elements
    int sum = 0;
 
    // Finding S(0)
    int s0 = 0;
    for (int i = 0; i < N; i++) {
        s0 += Arr[i] * (i + 1);
        sum += Arr[i];
    }
 
    min_val = s0;
 
    // To store s(k-1)
    int sk_min_1 = s0;
    for (int k = 1; k < N; k++) {
 
        // Using formula
        int sk = sk_min_1 - sum + Arr[(k - 1) % N] * N;
        min_val = min(sk, min_val);
        sk_min_1 = sk;
    }
    cout << min_val << "\n";
}
 
// Driver code
int main()
{
    vector Arr = { 2, 1, 4, 3 };
    minValFn(Arr);
}
 
// This code is contributed by Taranpreet

Java

// Java Program to find minimum value
// Of given function
 
import java.io.*;
 
public class GFG {
    static void minValFn(int[] Arr)
    {
        int N = Arr.length;
        int min_val;
 
        // To store sum of all elements
        int sum = 0;
 
        // Finding S(0)
        int s0 = 0;
        for (int i = 0; i < N; i++) {
            s0 += Arr[i] * (i + 1);
            sum += Arr[i];
        }
 
        min_val = s0;
 
        // To store s(k-1)
        int sk_min_1 = s0;
        for (int k = 1; k < N; k++) {
 
            // Using formula
            int sk = sk_min_1 - sum
                     + Arr[(k - 1) % N] * N;
            min_val = Math.min(sk, min_val);
            sk_min_1 = sk;
        }
        System.out.println(min_val);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] Arr = { 2, 1, 4, 3 };
        minValFn(Arr);
    }
}

Python3

# Python3 program to find the minimum value of
# the given function
 
# function to find the minimum value
def minValFn(Arr):
    N = len(Arr)
 
    # to store sum of all elements
    sums = 0
 
    # finding S(0)
    s0 = 0
    for i in range(N):
        s0 += Arr[i] * (i + 1)
        sums += Arr[i]
 
    min_val = s0
 
    # To store s(k - 1)
    sk_min_1 = s0
    for k in range(1, N):
        # using formula for minimum value
        sk = sk_min_1 - sums + Arr[(k - 1) % N] * N
        min_val = min(sk, min_val)
        sk_min_1 = sk
 
    print(min_val)
 
# Driver code
Arr = [2, 1, 4, 3]
minValFn(Arr)
 
# This code is contributed by phasing17

C#

// C# Program to find minimum value
// Of given function
 
using System;
 
public class GFG {
  static void minValFn(int[] Arr)
  {
    int N = Arr.Length;
    int min_val;
 
    // To store sum of all elements
    int sum = 0;
 
    // Finding S(0)
    int s0 = 0;
    for (int i = 0; i < N; i++) {
      s0 += Arr[i] * (i + 1);
      sum += Arr[i];
    }
 
    min_val = s0;
 
    // To store s(k-1)
    int sk_min_1 = s0;
    for (int k = 1; k < N; k++) {
 
      // Using formula
      int sk = sk_min_1 - sum
        + Arr[(k - 1) % N] * N;
      min_val = Math.Min(sk, min_val);
      sk_min_1 = sk;
    }
    Console.Write(min_val);
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int[] Arr = { 2, 1, 4, 3 };
    minValFn(Arr);
  }
}
 
// This code is contributed by ukasp.

JavaScript



输出
20

时间复杂度: O(N)
辅助空间: O(1)