Given an array, arr[] of size N and an integer T. The task is to find for each index the minimum number of indices that should be skipped if the sum till the ith index should not exceed T.
Examples:
Input: N = 7, T = 15, arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 0 0 0 0 0 2 3
Explanation: No indices need to be skipped for the first 5 indices: {1, 2, 3, 4, 5}, since their sum is 15 and <= T.
For the sixth index, indices 3 and 4 can be skipped, that makes its sum = (1+2+3+6) = 12.
For the seventh, indices 3, 4 and 5 can be skipped which makes its sum = (1+2+3+7) = 13.
Input: N = 2, T = 100, arr[] = {100, 100}
Output: 0 1
Approach: The idea is to use a map to store the visited elements in increasing order while traversing. Follow the steps below to solve the problem:
- Create an ordered map, M to keep a count of the elements before the ith index.
- Initialize a variable sum as 0 to store the prefix sum.
- Traverse the array, arr[] using the variable i
- Store the difference of sum+arr[i] and T in a variable, d.
- If the value of d>0, traverse the map from the end and select the indices with the largest elements until the sum becomes less than T. Store the number of elements required in a variable k.
- Add arr[i] to sum and increment A[i] in M by 1.
- Print the value of k.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void skipIndices( int N, int T, int arr[])
{
int sum = 0;
map< int , int > count;
for ( int i = 0; i < N; i++) {
int d = sum + arr[i] - T;
int k = 0;
if (d > 0) {
for ( auto u = count.rbegin(); u != count.rend();
u++) {
int j = u->first;
int x = j * count[j];
if (d <= x) {
k += (d + j - 1) / j;
break ;
}
k += count[j];
d -= x;
}
}
sum += arr[i];
count[arr[i]]++;
cout << k << " " ;
}
}
int main()
{
int N = 7;
int T = 15;
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
skipIndices(N, T, arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
class GFG {
public static void skipIndices( int N, int T, int arr[])
{
int sum = 0 ;
TreeMap<Integer, Integer> count = new TreeMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
int d = sum + arr[i] - T;
int k = 0 ;
if (d > 0 ) {
for (Map.Entry<Integer, Integer> u : count.descendingMap().entrySet()) {
int j = u.getKey();
int x = j * count.get(j);
if (d <= x) {
k += (d + j - 1 ) / j;
break ;
}
k += count.get(j);
d -= x;
}
}
sum += arr[i];
if (count.containsKey(arr[i])){
count.put(arr[i], count.get(arr[i]) + 1 );
} else {
count.put(arr[i], 1 );
}
System.out.print(k + " " );
}
}
public static void main(String args[])
{
int N = 7 ;
int T = 15 ;
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
skipIndices(N, T, arr);
}
}
|
Python3
def skipIndices(N, T, arr):
sum = 0
count = {}
for i in range (N):
d = sum + arr[i] - T
k = 0
if (d > 0 ):
for u in list (count.keys())[:: - 1 ]:
j = u
x = j * count[j]
if (d < = x):
k + = (d + j - 1 ) / / j
break
k + = count[j]
d - = x
sum + = arr[i]
count[arr[i]] = count.get(arr[i], 0 ) + 1
print (k, end = " " )
if __name__ = = '__main__' :
N = 7
T = 15
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
skipIndices(N, T, arr)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
public static void skipIndices( int N, int T, int [] arr)
{
int sum = 0;
SortedDictionary< int , int > count = new SortedDictionary< int , int >();
for ( int i = 0; i < N; i++)
{
int d = sum + arr[i] - T;
int k = 0;
if (d > 0)
{
foreach (KeyValuePair< int , int > u in count.Reverse())
{
int j = u.Key;
int x = j * count[j];
if (d <= x)
{
k += (d + j - 1) / j;
break ;
}
k += count[j];
d -= x;
}
}
sum += arr[i];
if (count.ContainsKey(arr[i]))
{
count[arr[i]] = count[arr[i]] + 1;
}
else
{
count[arr[i]] = 1;
}
Console.Write(k + " " );
}
}
public static void Main( string [] args)
{
int N = 7;
int T = 15;
int [] arr = { 1, 2, 3, 4, 5, 6, 7 };
skipIndices(N, T, arr);
}
}
|
Javascript
<script>
function skipIndices(N, T, arr)
{
let sum = 0;
let count = new Map();
for (let i = 0; i < N; i++)
{
let d = sum + arr[i] - T;
let k = 0;
if (d > 0)
{
for (let u of [...count.keys()].reverse())
{
let j = u;
let x = j * count.get(j);
if (d <= x)
{
k += Math.floor((d + j - 1) / j);
break ;
}
k += count.get(j);
d -= x;
}
}
sum += arr[i];
if (count.has(arr[i]))
{
count.set(arr[i], count.get(i) + 1)
}
else
{
count.set(arr[i], 1)
}
document.write(k + " " );
}
}
let N = 7;
let T = 15;
let arr = [ 1, 2, 3, 4, 5, 6, 7 ];
skipIndices(N, T, arr);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)