Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Find range of values for S in given Array with values satisfying [ arr[i] = floor((i*S)/K) ]

  • Last Updated : 29 Sep, 2021

Given an array arr[] of N positive integers and a positive integer K, the task is to find the range [L, R] such that for all the elements in this range, say S each array element arr[i] is floor((i*S)/K).

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 5, K = 10, arr[] = {2, 4, 6, 9, 11}
Output: 23 23
Explanation: 
When S = 23, substituting in the equation gives the same array values:
arr[1] = floor((1*23)/10) = 2
arr[2] = floor((2*23/10)) = 4
arr[3] = floor((3*23/10)) = 6
arr[4] = floor((4*23/10)) = 9
arr[5] = floor((5*23/10)) = 11



Input: N = 5, K = 100, arr[] = {0, 0, 0, 0, 1}
Output: 20 24

Approach: Follow the below steps to solve the given problem:

  • Initialize two variables say L and R as INT_MIN and INT_MAX that stores the value of the left range and the right range respectively.
  • Traverse the given array arr[] and perform the following steps:
    • Find the possible value of the left range for the ith array element as ceil(1.0*arr[i]*K/(i + 1)).
    • Find the possible value of the right range for the ith array element as ceil((1.0 + arr[i])*K/(i + 1)) – 1.
    • Update the value of L as max(L, l) and the value of R as min(R, r).
  • After completing the above steps, print the values of L and R as the resultant range.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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




<script>
// JavaScript program for the above approach
 
// Function to find the range of values
// for S in a given array that satisfies
// the given condition
function findRange(arr,  N,  K)
{
 
    // Stores the left range value
    let L = Number.MIN_VALUE;
 
    // Stores the right range value
    let R = Number.MAX_VALUE;
 
    for (let i = 0; i < N; i++) {
        // Find the current left range
        // value for S
        let l = Math.ceil(1.0 * arr[i] * K / (i + 1));
 
        // Find the current right range
        // value for S
        let r = 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);
    }
 
    document.write(L  + " " + R);
}
 
    // Driver code
    let  arr = [ 2, 4, 6, 9, 11 ];
    let K = 10;
    let N = arr.length;
 
    findRange(arr, N, K);
 
// This code is contributed by AnkThon
</script>
Output: 
23 23

 

Time Complexity: O(N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :