📌  相关文章
📜  在给定数组中查找 S 的值范围,其值满足 [ arr[i] = floor((i*S)/K) ]

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

在给定数组中查找 S 的值范围,其值满足 [ arr[i] = floor((i*S)/K) ]

给定一个由N个正整数组成的数组arr[]和一个正整数K ,任务是找到范围[L, R]使得对于这个范围内的所有元素,比如说S ,每个数组元素arr[i]floor( (i*S)/K)

例子:

方法:按照以下步骤解决给定问题:

  • 将两个变量LR初始化为INT_MININT_MAX ,分别存储左侧范围和右侧范围的值。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 找到第i数组元素左侧范围的可能值ceil(1.0*arr[i]*K/(i + 1))
    • 找到第i数组元素的正确范围的可能值ceil((1.0 + arr[i])*K/(i + 1)) – 1
    • L的值更新为max(L, l) ,将R的值更新为min(R, r)
  • 完成上述步骤后,打印LR的值作为结果范围。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the range of values
// for S in a given array that satisfies
// the given condition
void findRange(int arr[], int N, int K)
{
 
    // Stores the left range value
    int L = INT_MIN;
 
    // Stores the right range value
    int R = INT_MAX;
 
    for (int i = 0; i < N; i++) {
        // Find the current left range
        // value for S
        int l = (int)ceil(1.0 * arr[i] * K / (i + 1));
 
        // Find the current right range
        // value for S
        int r = (int)ceil((1.0 + arr[i]) * K / (i + 1)) - 1;
 
        // Updating L value
        L = max(L, l);
 
        // Updating R value
        R = min(R, r);
    }
 
    cout << L << " " << R;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 2, 4, 6, 9, 11 };
    int K = 10;
    int N = sizeof(arr) / sizeof(int);
 
    findRange(arr, N, K);
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
 
import java.util.*;
import java.lang.*;
 
class Codechef {
 
    // Function to find the range of values
    // for S in a given array that satisfies
    // the given condition
    static void findRange(int arr[], int N,
                          int K)
    {
 
        // Stores the left range value
        int L = Integer.MIN_VALUE;
 
        // Stores the right range value
        int R = Integer.MAX_VALUE;
 
        for (int i = 0; i < N; i++) {
            // Find the current left range
            // value for S
            int l = (int)Math.ceil(
                1.0 * arr[i] * K / (i + 1));
 
            // Find the current right range
            // value for S
            int r = (int)Math.ceil(
                        (1.0 + arr[i]) * K / (i + 1))
                    - 1;
 
            // Updating L value
            L = Math.max(L, l);
 
            // Updating R value
            R = Math.min(R, r);
        }
 
        System.out.println(L + " " + R);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 4, 6, 9, 11 };
        int K = 10;
        int N = arr.length;
 
        findRange(arr, N, K);
    }
}


Python3
# Python 3 program for the above approach
from math import ceil,floor
import sys
 
# Function to find the range of values
# for S in a given array that satisfies
# the given condition
def findRange(arr, N, K):
   
    # Stores the left range value
    L = -sys.maxsize-1
 
    # Stores the right range value
    R = sys.maxsize
 
    for i in range(N):
       
        # Find the current left range
        # value for S
        l = ceil(1.0 * arr[i] * K / (i + 1))
 
        # Find the current right range
        # value for S
        r = ceil((1.0 + arr[i]) * K / (i + 1) - 1)
 
        # Updating L value
        L = max(L, l)
 
        # Updating R value
        R = min(R, r)
 
    print(L,R)
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 4, 6, 9, 11]
    K = 10
    N = len(arr)
    findRange(arr, N, K)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
 
using System;
 
class GFG {
 
    // Function to find the range of values
    // for S in a given array that satisfies
    // the given condition
    static void findRange(int[] arr, int N, int K)
    {
 
        // Stores the left range value
        int L = Int32.MinValue;
 
        // Stores the right range value
        int R = Int32.MaxValue;
 
        for (int i = 0; i < N; i++)
        {
           
            // Find the current left range
            // value for S
            int l = (int)Math.Ceiling(1.0 * arr[i] * K
                                      / (i + 1));
 
            // Find the current right range
            // value for S
            int r = (int)Math.Ceiling((1.0 + arr[i]) * K
                                      / (i + 1))
                    - 1;
 
            // Updating L value
            L = Math.Max(L, l);
 
            // Updating R value
            R = Math.Min(R, r);
        }
 
        Console.WriteLine(L + " " + R);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 4, 6, 9, 11 };
        int K = 10;
        int N = arr.Length;
 
        findRange(arr, N, K);
    }
}
 
// This code is contributed by subham348.


Javascript


输出:
23 23

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