Smallest index in the given array that satisfies the given condition
Given an array arr[] of size N and an integer K, the task is to find the smallest index in the array such that:
floor(arr[i] / 1) + floor(arr[i + 1] / 2) + floor(arr[i + 2] / 3) + ….. + floor(arr[n – 1] / n – i ) ? K
If no such index is found then print -1.
Examples:
Input: arr[] = {6, 5, 4, 2}, K = 8
Output: 1
(6 / 1) + (5 / 2) + (4 / 3) + (2 / 4) = 6 + 2 + 1 + 0 = 9 which is > K
(5 / 1) + (4 / 2) + (2 / 3) = 5 + 2 + 0 = 7 < K
Hence i = 1 is the required index.
Input: arr[] = {5, 4, 2, 3, 9, 1, 8, 7}, K = 7
Output: 5
Approach: For every index i starting from 0, find the sum of the given series, and the first index which given a sum greater than or equal to K is our required index. If there is no such index then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getIndex( int arr[], int n, int K)
{
for ( int i = 0; i < n; i++) {
int sum = 0;
int den = 1;
for ( int j = i; j < n; j++) {
sum += (arr[j] / den);
den++;
if (sum > K)
break ;
}
if (sum <= K)
return i;
}
return -1;
}
int main()
{
int arr[] = { 6, 5, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int K = 8;
cout << getIndex(arr, n, K);
return 0;
}
|
Java
class GFG {
static int getIndex( int arr[], int n, int K)
{
for ( int i = 0 ; i < n; i++) {
int sum = 0 ;
int den = 1 ;
for ( int j = i; j < n; j++) {
sum += (arr[j] / den);
den++;
if (sum > K)
break ;
}
if (sum <= K)
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 6 , 5 , 4 , 2 };
int n = arr.length;
int K = 8 ;
System.out.print(getIndex(arr, n, K));
}
}
|
Python
def getIndex(array, k):
n = len (array)
for ind in range (n):
sum = 0
div = 1
for item in array:
sum + = item / / div
div + = 1
if sum > k:
break
if sum < = k:
return ind
return - 1
arr = [ 6 , 5 , 4 , 2 ]
K = 8
print (getIndex(arr, K))
arr = [ 5 , 4 , 2 , 3 , 9 , 1 , 8 , 7 ]
K = 7
print (getIndex(arr, K))
|
C#
using System;
class GFG
{
static int getIndex( int []arr, int n, int K)
{
for ( int i = 0; i < n; i++)
{
int sum = 0;
int den = 1;
for ( int j = i; j < n; j++)
{
sum += (arr[j] / den);
den++;
if (sum > K)
break ;
}
if (sum <= K)
return i;
}
return -1;
}
static public void Main ()
{
int []arr = { 6, 5, 4, 2 };
int n = arr.Length;
int K = 8;
Console.WriteLine(getIndex(arr, n, K));
}
}
|
PHP
<?php
function getIndex( $arr , $n , $K )
{
for ( $i = 0; $i < $n ; $i ++)
{
$sum = 0;
$den = 1;
for ( $j = $i ; $j < $n ; $j ++)
{
$sum += floor ( $arr [ $j ] / $den );
$den ++;
if ( $sum > $K )
break ;
}
if ( $sum <= $K )
return $i ;
}
return -1;
}
$arr = array ( 6, 5, 4, 2 );
$n = sizeof( $arr );
$K = 8;
echo getIndex( $arr , $n , $K );
?>
|
Javascript
<script>
function getIndex(arr, n, K)
{
for (let i = 0; i < n; i++)
{
let sum = 0;
let den = 1;
for (let j = i; j < n; j++)
{
sum += parseInt((arr[j] / den), 10);
den++;
if (sum > K)
break ;
}
if (sum <= K)
return i;
}
return -1;
}
let arr = [ 6, 5, 4, 2 ];
let n = arr.length;
let K = 8;
document.write(getIndex(arr, n, K));
</script>
|
Time complexity: O(N2)
Auxiliary space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...