Find range of values for S in given Array with values satisfying [ arr[i] = floor((i*S)/K) ]
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:
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++
#include <bits/stdc++.h>
using namespace std;
void findRange( int arr[], int N, int K)
{
int L = INT_MIN;
int R = INT_MAX;
for ( int i = 0; i < N; i++) {
int l = ( int ) ceil (1.0 * arr[i] * K / (i + 1));
int r = ( int ) ceil ((1.0 + arr[i]) * K / (i + 1)) - 1;
L = max(L, l);
R = min(R, r);
}
cout << L << " " << R;
}
int main()
{
int arr[] = { 2, 4, 6, 9, 11 };
int K = 10;
int N = sizeof (arr) / sizeof ( int );
findRange(arr, N, K);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class Codechef {
static void findRange( int arr[], int N,
int K)
{
int L = Integer.MIN_VALUE;
int R = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int l = ( int )Math.ceil(
1.0 * arr[i] * K / (i + 1 ));
int r = ( int )Math.ceil(
( 1.0 + arr[i]) * K / (i + 1 ))
- 1 ;
L = Math.max(L, l);
R = Math.min(R, r);
}
System.out.println(L + " " + R);
}
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
from math import ceil,floor
import sys
def findRange(arr, N, K):
L = - sys.maxsize - 1
R = sys.maxsize
for i in range (N):
l = ceil( 1.0 * arr[i] * K / (i + 1 ))
r = ceil(( 1.0 + arr[i]) * K / (i + 1 ) - 1 )
L = max (L, l)
R = min (R, r)
print (L,R)
if __name__ = = '__main__' :
arr = [ 2 , 4 , 6 , 9 , 11 ]
K = 10
N = len (arr)
findRange(arr, N, K)
|
C#
using System;
class GFG {
static void findRange( int [] arr, int N, int K)
{
int L = Int32.MinValue;
int R = Int32.MaxValue;
for ( int i = 0; i < N; i++)
{
int l = ( int )Math.Ceiling(1.0 * arr[i] * K
/ (i + 1));
int r = ( int )Math.Ceiling((1.0 + arr[i]) * K
/ (i + 1))
- 1;
L = Math.Max(L, l);
R = Math.Min(R, r);
}
Console.WriteLine(L + " " + R);
}
public static void Main()
{
int [] arr = { 2, 4, 6, 9, 11 };
int K = 10;
int N = arr.Length;
findRange(arr, N, K);
}
}
|
Javascript
<script>
function findRange(arr, N, K)
{
let L = Number.MIN_VALUE;
let R = Number.MAX_VALUE;
for (let i = 0; i < N; i++) {
let l = Math.ceil(1.0 * arr[i] * K / (i + 1));
let r = Math.ceil((1.0 + arr[i]) * K / (i + 1)) - 1;
L = Math.max(L, l);
R = Math.min(R, r);
}
document.write(L + " " + R);
}
let arr = [ 2, 4, 6, 9, 11 ];
let K = 10;
let N = arr.length;
findRange(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...