Open In App

Sum of all elements between k1’th and k2’th smallest elements

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of integers and two numbers k1 and k2. Find the sum of all elements between given two k1’th and k2’th smallest elements of the array. It may be assumed that (1 <= k1 < k2 <= n) and all elements of array are distinct.

Examples : 

Input : arr[] = {20, 8, 22, 4, 12, 10, 14},  k1 = 3,  k2 = 6  
Output : 26          
         3rd smallest element is 10. 6th smallest element 
         is 20. Sum of all element between k1 & k2 is
         12 + 14 = 26

Input : arr[] = {10, 2, 50, 12, 48, 13}, k1 = 2, k2 = 6 
Output : 73 

Method 1 (Sorting): First sort the given array using an O(n log n) sorting algorithm like Merge Sort, Heap Sort, etc and return the sum of all element between index k1 and k2 in the sorted array.

Implementation:

C++




// C++ program to find sum of all element between
// to K1'th and k2'th smallest elements in array
#include <bits/stdc++.h>
 
using namespace std;
 
// Returns sum between two kth smallest elements of the array
int sumBetweenTwoKth(int arr[], int n, int k1, int k2)
{
    // Sort the given array
    sort(arr, arr + n);
 
    /* Below code is equivalent to
     int result = 0;
     for (int i=k1; i<k2-1; i++)
      result += arr[i]; */
    return accumulate(arr + k1, arr + k2 - 1, 0);
}
 
// Driver program
int main()
{
    int arr[] = { 20, 8, 22, 4, 12, 10, 14 };
    int k1 = 3, k2 = 6;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << sumBetweenTwoKth(arr, n, k1, k2);
    return 0;
}


Java




// Java program to find sum of all element
// between to K1'th and k2'th smallest
// elements in array
import java.util.Arrays;
 
class GFG {
 
    // Returns sum between two kth smallest
    // element of array
    static int sumBetweenTwoKth(int arr[],
                                int k1, int k2)
    {
        // Sort the given array
        Arrays.sort(arr);
 
        // Below code is equivalent to
        int result = 0;
 
        for (int i = k1; i < k2 - 1; i++)
            result += arr[i];
 
        return result;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int arr[] = { 20, 8, 22, 4, 12, 10, 14 };
        int k1 = 3, k2 = 6;
        int n = arr.length;
 
        System.out.print(sumBetweenTwoKth(arr,
                                          k1, k2));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to find sum of
# all element between to K1'th and
# k2'th smallest elements in array
 
# Returns sum between two kth
# smallest element of array
def sumBetweenTwoKth(arr, n, k1, k2):
 
    # Sort the given array
    arr.sort()
 
    result = 0
    for i in range(k1, k2-1):
        result += arr[i]
    return result
 
# Driver code
arr = [ 20, 8, 22, 4, 12, 10, 14 ]
k1 = 3; k2 = 6
n = len(arr)
print(sumBetweenTwoKth(arr, n, k1, k2))
 
 
# This code is contributed by Anant Agarwal.


C#




// C# program to find sum of all element
// between to K1'th and k2'th smallest
// elements in array
using System;
 
class GFG {
 
    // Returns sum between two kth smallest
    // element of array
    static int sumBetweenTwoKth(int[] arr, int n,
                                int k1, int k2)
    {
        // Sort the given array
        Array.Sort(arr);
 
        // Below code is equivalent to
        int result = 0;
 
        for (int i = k1; i < k2 - 1; i++)
            result += arr[i];
 
        return result;
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 20, 8, 22, 4, 12, 10, 14 };
        int k1 = 3, k2 = 6;
        int n = arr.Length;
 
        Console.Write(sumBetweenTwoKth(arr, n, k1, k2));
    }
}
 
// This code is contributed by nitin mittal.


PHP




<?php
// PHP program to find sum of all element between
// to K1'th and k2'th smallest elements in array
 
// Returns sum between two kth smallest elements of the array
function sumBetweenTwoKth($arr, $n, $k1, $k2)
{
    // Sort the given array
    sort($arr);
 
    // Below code is equivalent to
        $result = 0;
  
        for ($i = $k1; $i < $k2 - 1; $i++)
            $result += $arr[$i];
  
        return $result;
}
 
// Driver program
 
    $arr = array( 20, 8, 22, 4, 12, 10, 14 );
    $k1 = 3;
    $k2 = 6;
    $n = count($arr);;
    echo sumBetweenTwoKth($arr, $n, $k1, $k2);
     
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program to find sum of all element
// between to K1'th and k2'th smallest
// elements in array
 
// Returns sum between two kth smallest
// element of array
function sumBetweenTwoKth(arr, k1 , k2)
{
     
    // Sort the given array
    arr.sort(function(a, b){return a - b});
 
    // Below code is equivalent to
    var result = 0;
 
    for(var i = k1; i < k2 - 1; i++)
        result += arr[i];
 
    return result;
}
 
// Driver code
var arr = [ 20, 8, 22, 4, 12, 10, 14 ];
var k1 = 3, k2 = 6;
var n = arr.length;
 
document.write(sumBetweenTwoKth(arr,
                                k1, k2));
 
// This code is contributed by shikhasingrajput
 
</script>


Output

26

Time Complexity: O(n log n) 
Auxiliary Space: O(1)

Method 2 (Using Min Heap):

We can optimize the above solution by using a min-heap. 

  1. Create a min heap of all array elements. (This step takes O(n) time) 
  2. Do extract minimum k1 times (This step takes O(K1 Log n) time) 
  3. Do extract minimum k2 – k1 – 1 time and sum all extracted elements. (This step takes O ((K2 – k1) * Log n) time)

Time Complexity Analysis: 

  • By doing a simple analysis, we can observe that time complexity of step3 [ Determining step for overall time complexity ] can reach to O(nlogn) also. 
  • Take a look at the following description:
    • Time Complexity of step3 is:  O((k2-k1)*log(n)) . 
    • In worst case, (k2-k1) would be almost O(n) [ Assume situation when k1=0  and k2=len(arr)-1 ]
    • When O(k2-k1) =O(n) then overall complexity will be O(n* Log n ) .
    • but in most cases…it will be lesser than O(n Log n) which is equal to sorting approach described above.

Implementation:

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
int n = 7;
 
void minheapify(int a[], int index)
{
 
    int small = index;
    int l = 2 * index + 1;
    int r = 2 * index + 2;
 
    if (l < n && a[l] < a[small])
        small = l;
 
    if (r < n && a[r] < a[small])
        small = r;
 
    if (small != index) {
        swap(a[small], a[index]);
        minheapify(a, small);
    }
}
 
int main()
{
    int i = 0;
    int k1 = 3;
    int k2 = 6;
 
    int a[] = { 20, 8, 22, 4, 12, 10, 14 };
 
    int ans = 0;
 
    for (i = (n / 2) - 1; i >= 0; i--) {
        minheapify(a, i);
    }
 
    // decreasing value by 1 because we want min heapifying k times and it starts
    // from 0 so we have to decrease it 1 time
    k1--;
    k2--;
 
    // Step 1: Do extract minimum k1 times (This step takes O(K1 Log n) time)
    for (i = 0; i <= k1; i++) {
        // cout<<a[0]<<endl;
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    /*Step 2: Do extract minimum k2 – k1 – 1 times and sum all
   extracted elements. (This step takes O ((K2 – k1) * Log n) time)*/
    for (i = k1 + 1; i < k2; i++) {
        // cout<<a[0]<<endl;
        ans += a[0];
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    cout << ans;
 
    return 0;
}


Java




// Java implementation of above approach
class GFG
{
     
static int n = 7;
 
static void minheapify(int []a, int index)
{
 
    int small = index;
    int l = 2 * index + 1;
    int r = 2 * index + 2;
 
    if (l < n && a[l] < a[small])
        small = l;
 
    if (r < n && a[r] < a[small])
        small = r;
 
    if (small != index)
    {
        int t = a[small];
        a[small] = a[index];
        a[index] = t;
        minheapify(a, small);
    }
}
 
// Driver code
public static void main (String[] args)
{
    int i = 0;
    int k1 = 3;
    int k2 = 6;
 
    int []a = { 20, 8, 22, 4, 12, 10, 14 };
 
    int ans = 0;
 
    for (i = (n / 2) - 1; i >= 0; i--)
    {
        minheapify(a, i);
    }
 
    // decreasing value by 1 because we want
    // min heapifying k times and it starts
    // from 0 so we have to decrease it 1 time
    k1--;
    k2--;
 
    // Step 1: Do extract minimum k1 times
    // (This step takes O(K1 Log n) time)
    for (i = 0; i <= k1; i++)
    {
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    for (i = k1 + 1; i < k2; i++)
    {
        // cout<<a[0]<<endl;
        ans += a[0];
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    System.out.println(ans);
}
}
 
// This code is contributed by mits


Python3




# Python 3 implementation of above approach
n = 7
 
def minheapify(a, index):
    small = index
    l = 2 * index + 1
    r = 2 * index + 2
 
    if (l < n and a[l] < a[small]):
        small = l
 
    if (r < n and a[r] < a[small]):
        small = r
 
    if (small != index):
        (a[small], a[index]) = (a[index], a[small])
        minheapify(a, small)
     
# Driver Code
i = 0
k1 = 3
k2 = 6
 
a = [ 20, 8, 22, 4, 12, 10, 14 ]
ans = 0
 
for i in range((n //2) - 1, -1, -1):
    minheapify(a, i)
 
# decreasing value by 1 because we want
# min heapifying k times and it starts
# from 0 so we have to decrease it 1 time
k1 -= 1
k2 -= 1
 
# Step 1: Do extract minimum k1 times
# (This step takes O(K1 Log n) time)
for i in range(0, k1 + 1):
    a[0] = a[n - 1]
    n -= 1
    minheapify(a, 0)
 
# Step 2: Do extract minimum k2 – k1 – 1 times and
# sum all extracted elements.
# (This step takes O ((K2 – k1) * Log n) time)*/
for i in range(k1 + 1, k2) :
    ans += a[0]
    a[0] = a[n - 1]
    n -= 1
    minheapify(a, 0)
 
print (ans)
 
# This code is contributed
# by Atul_kumar_Shrivastava


C#




// C# implementation of above approach
using System;
 
class GFG
{
     
static int n = 7;
 
static void minheapify(int []a, int index)
{
 
    int small = index;
    int l = 2 * index + 1;
    int r = 2 * index + 2;
 
    if (l < n && a[l] < a[small])
        small = l;
 
    if (r < n && a[r] < a[small])
        small = r;
 
    if (small != index)
    {
        int t = a[small];
        a[small] = a[index];
        a[index] = t;
        minheapify(a, small);
    }
}
 
// Driver code
static void Main()
{
    int i = 0;
    int k1 = 3;
    int k2 = 6;
 
    int []a = { 20, 8, 22, 4, 12, 10, 14 };
 
    int ans = 0;
 
    for (i = (n / 2) - 1; i >= 0; i--)
    {
        minheapify(a, i);
    }
 
    // decreasing value by 1 because we want
    // min heapifying k times and it starts
    // from 0 so we have to decrease it 1 time
    k1--;
    k2--;
 
    // Step 1: Do extract minimum k1 times
    // (This step takes O(K1 Log n) time)
    for (i = 0; i <= k1; i++)
    {
        // cout<<a[0]<<endl;
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    /*Step 2: Do extract minimum k2 – k1 – 1 times
    and sum all extracted elements. (This step
    takes O ((K2 – k1) * Log n) time)*/
    for (i = k1 + 1; i < k2; i++)
    {
        // cout<<a[0]<<endl;
        ans += a[0];
        a[0] = a[n - 1];
        n--;
        minheapify(a, 0);
    }
 
    Console.Write(ans);
}
}
 
// This code is contributed by mits


Javascript




<script>
 
// Javascript implementation of above approach
let n = 7;
 
function minheapify(a, index)
{
    let small = index;
    let l = 2 * index + 1;
    let r = 2 * index + 2;
 
    if (l < n && a[l] < a[small])
        small = l;
 
    if (r < n && a[r] < a[small])
        small = r;
 
    if (small != index)
    {
        let t = a[small];
        a[small] = a[index];
        a[index] = t;
        minheapify(a, small);
    }
}
 
// Driver code
let i = 0;
let k1 = 3;
let k2 = 6;
 
let a = [ 20, 8, 22, 4, 12, 10, 14 ];
 
let ans = 0;
 
for(i = parseInt(n / 2, 10) - 1; i >= 0; i--)
{
    minheapify(a, i);
}
 
// decreasing value by 1 because we want
// min heapifying k times and it starts
// from 0 so we have to decrease it 1 time
k1--;
k2--;
 
// Step 1: Do extract minimum k1 times
// (This step takes O(K1 Log n) time)
for(i = 0; i <= k1; i++)
{
    a[0] = a[n - 1];
    n--;
    minheapify(a, 0);
}
 
for(i = k1 + 1; i < k2; i++)
{
     
    // cout<<a[0]<<endl;
    ans += a[0];
    a[0] = a[n - 1];
    n--;
    minheapify(a, 0);
}
 
document.write(ans);
 
// This code is contributed by vaibhavrabadiya117
 
</script>


Output

26

Time Complexity: O(n + k2 Log n)
Auxiliary Space: O(1)

Method 3 : (Using Max Heap – most optimized )

The Below Idea uses the Max Heap Strategy to find the solution.

Algorithm:

  1. The idea is to find the Kth Smallest element for the K2 . 
  2. Then just keep an popping the elements until the size of heap is K1, and make sure to add the elements to a variable before popping the elements.

Now the idea revolves around Kth Smallest Finding:

  1.  The CRUX over here is that, we are storing the K smallest elements in the MAX Heap
  2.  So while every push, if the size goes over K, then we pop the Maximum value.
  3.  This way after whole traversal. we are left out with K elements.
  4.  Then the N-K th Largest Element is Popped and given, which is as same as  K’th Smallest element.

So by this manner we can write a functional code with using the C++ STL Priority_Queue, we get the most time and space optimized solution.

C++




// C++ program to find sum of all element between
// to K1'th and k2'th smallest elements in array
#include <bits/stdc++.h>
using namespace std;
long long sumBetweenTwoKth(long long A[], long long N,
                           long long K1, long long K2)
{
    // Using max heap to find K1'th and K2'th smallest
    // elements
    priority_queue<long long> maxH;
 
    // Using this for loop we eliminate the extra elements
    // which are greater than K2'th smallest element as they
    // are not required for us
 
    for (int i = 0; i < N; i++) {
 
        maxH.push(A[i]);
 
        if (maxH.size() > K2) {
            maxH.pop();
        }
    }
    // popping out the K2'th smallest element
    maxH.pop();
 
    long long ans = 0;
    // adding the elements to ans until we reach the K1'th
    // smallest element
    while (maxH.size() > K1) {
 
        ans += maxH.top();
        maxH.pop();
    }
 
    return ans;
}
 
int main()
{
    long long arr[] = { 20, 8, 22, 4, 12, 10, 14 };
    long long k1 = 3, k2 = 6;
    long long n = sizeof(arr) / sizeof(arr[0]);
    cout << sumBetweenTwoKth(arr, n, k1, k2);
    return 0;
}


Java




// Java program to find sum of all element between
// to K1'th and k2'th smallest elements in array
import java.util.*;
public class GFG {
  static long sumBetweenTwoKth(long A[], long N, long K1,
                               long K2)
  {
    // Using max heap to find K1'th and K2'th smallest
    // elements
    PriorityQueue<Long> maxH = new PriorityQueue<>(
      Collections.reverseOrder());
 
    // Using this for loop we eliminate the extra
    // elements which are greater than K2'th smallest
    // element as they are not required for us
 
    for (int i = 0; i < N; i++) {
 
      maxH.add(A[i]);
 
      if (maxH.size() > K2) {
        maxH.remove();
      }
    }
    // popping out the K2'th smallest element
    maxH.remove();
 
    long ans = 0;
    // adding the elements to ans until we reach the
    // K1'th smallest element
    while (maxH.size() > K1) {
 
      ans += maxH.peek();
      maxH.remove();
    }
 
    return ans;
  }
 
  public static void main(String[] args)
  {
    long arr[] = { 20, 8, 22, 4, 12, 10, 14 };
    long k1 = 3, k2 = 6;
    long n = arr.length;
    System.out.println(
      sumBetweenTwoKth(arr, n, k1, k2));
  }
}
// This code is contributed by karandeep1234


Python3




# Python3 program to find sum of all element between
# to K1'th and k2'th smallest elements in array
def sumBetweenTwoKth(A, N, K1, K2):
   
    # Using max heap to find K1'th and K2'th smallest
    # elements
    maxH = []
 
    # Using this for loop we eliminate the extra elements
    # which are greater than K2'th smallest element as they
    # are not required for us
 
    for i in range(0,N):
        maxH.append(A[i])
        maxH.sort(reverse=True)
 
        if (len(maxH) > K2):
            maxH.pop(0)
    # popping out the K2'th smallest element
    maxH.pop(0)
 
    ans = 0
    # adding the elements to ans until we reach the K1'th
    # smallest element
    while (len(maxH) > K1):
        ans += maxH[0]
        maxH.pop(0)
 
    return ans
 
 
arr = [ 20, 8, 22, 4, 12, 10, 14 ]
k1 = 3
k2 = 6
n = len(arr)
print(sumBetweenTwoKth(arr, n, k1, k2))
 
# This code is contributed by akashish__


C#




using System;
using System.Collections.Generic;
 
public class GFG {
  public static long sumBetweenTwoKth(long[] A, long N,
                                      long K1, long K2)
  {
     
    // Using max heap to find K1'th and K2'th smallest
    // elements
    SortedSet<long> maxH = new SortedSet<long>();
 
    // Using this for loop we eliminate the extra
    // elements which are greater than K2'th smallest
    // element as they are not required for us
 
    for (int i = 0; i < N; i++) {
 
      maxH.Add(A[i]);
 
      if (maxH.Count > K2) {
        maxH.Remove(maxH.Max);
      }
    }
    // popping out the K2'th smallest element
    maxH.Remove(maxH.Max);
 
    long ans = 0;
     
    // adding the elements to ans until we reach the
    // K1'th smallest element
    while (maxH.Count > K1) {
 
      ans += maxH.Max;
      maxH.Remove(maxH.Max);
    }
 
    return ans;
  }
 
  static public void Main()
  {
 
    long[] arr = { 20, 8, 22, 4, 12, 10, 14 };
    long k1 = 3, k2 = 6;
    long n = arr.Length;
    Console.WriteLine(sumBetweenTwoKth(arr, n, k1, k2));
  }
}
 
// This code is contributed by akashish__


Javascript




// JS program to find sum of all element between
// to K1'th and k2'th smallest elements in array
 
function PriorityQueue () {
    let collection = [];
    this.printCollection = function() {
      (console.log(collection));
    };
    this.enqueue = function(element){
        if (this.isEmpty()){
            collection.push(element);
        } else {
            let added = false;
            for (let i=0; i<collection.length; i++){
                 if (element[1] < collection[i][1]){ //checking priorities
                    collection.splice(i,0,element);
                    added = true;
                    break;
                }
            }
            if (!added){
                collection.push(element);
            }
        }
    };
    this.dequeue = function() {
        let value = collection.shift();
        return value[0];
    };
    this.front = function() {
        return collection[0];
    };
    this.size = function() {
        return collection.length;
    };
    this.isEmpty = function() {
        return (collection.length === 0);
    };
}
 
function sumBetweenTwoKth(A, N, K1, K2)
{
    // Using max heap to find K1'th and K2'th smallest
    // elements
    let maxH = new PriorityQueue();
 
    // Using this for loop we eliminate the extra elements
    // which are greater than K2'th smallest element as they
    // are not required for us
 
    for (let i = 0; i < N; i++) {
 
        maxH.enqueue(A[i]);
 
        if (maxH.size() > K2) {
            maxH.dequeue();
        }
    }
    // popping out the K2'th smallest element
    maxH.dequeue();
 
    let ans = 0;
    // adding the elements to ans until we reach the K1'th
    // smallest element
    while (maxH.size() > K1) {
 
        ans += maxH.front();
        maxH.dequeue();
    }
 
    return ans;
}
 
let arr = [ 20, 8, 22, 4, 12, 10, 14 ];
let k1 = 3, k2 = 6;
let n = arr.length;
console.log(sumBetweenTwoKth(arr, n, k1, k2));
 
// This code is contributed by akashish__


Output

26

Time Complexity: ( N * log K2 ) + ( (K2-K1) * log (K2-K1) ) + O(N) = O(NLogK2) (Dominant Term)

Reasons:

  • The Traversal O(N) in the function
  • Time Complexity for finding K2’th smallest element is ( N * log K2 )
  • Time Complexity for popping ( K2-K1 ) elements is ( (K2-K1) * log (K2-K1) )
  • As 1 Insertion takes O(LogK) where K is the size of Heap.
  • As 1 Deletion takes  O(LogK) where K is the size of Heap.

Extra Space Complexity: O(K2), As we use Heap / Priority Queue and we only store at max K elements, not more than that.
The above Method-3 Idea, Algorithm, and Code are contributed by Balakrishnan R (rbkraj000 – GFG ID).

References : https://www.geeksforgeeks.org/heap-sort 
other Geeks. 



Previous Article
Next Article

Similar Reads

For all Array elements find Product of Sum of all smaller and Sum of all greater elements
Given an array arr[] of integers of length N, the task is to find the product of the sum of all the numbers larger than that number with the sum of all the numbers less than that number for each number in the array. Examples: Input: arr[] = {8, 4, 9, 3}, N = 4Output:- 63, 51, 0, 0Explanation:For first number 8: Sum of elements smaller than this is
15 min read
Maximum sum of smallest and second smallest in an array
Given an array, find maximum sum of smallest and second smallest elements chosen from all possible subarrays. More formally, if we write all (nC2) subarrays of array of size &gt;=2 and find the sum of smallest and second smallest, then our answer will be maximum sum among them. Examples: Input : arr[] = [4, 3, 1, 5, 6]Output : 11Subarrays with smal
10 min read
Queries to return the absolute difference between L-th smallest number and the R-th smallest number
Given an array arr[] of N unique elements and Q queries. Every query consists of two integers L and R. The task is to print the absolute difference between the indices of the Lth smallest and the Rth smallest element. Examples: Input: arr[] = {1, 5, 4, 2, 8, 6, 7}, que[] = {{2, 5}, {1, 3}, {1, 5}, {3, 6}} Output: 2 2 5 4 For the first query the sec
7 min read
Find the smallest and second smallest elements in an array
Given an array arr[] of size N, find the smallest and second smallest element in an array. Examples: Input: arr[] = {12, 13, 1, 10, 34, 1} Output: 1 10Explanation: The smallest element is 1 and second smallest element is 10. Input: arr[] = {111, 13, 25, 9, 34, 1}Output: 1 9Explanation: The smallest element is 1 and second smallest element is 9. Rec
14 min read
Sort Array such that smallest is at 0th index and next smallest it at last index and so on
Given an array, arr[] of N integers, the task is to rearrange the array elements such that the smallest element is at the 0th position, the second smallest element is at the (N-1)th position, the third smallest element at 1st position, 4th smallest element is at the (N-2)th position, and so on for all integers in arr[]. Examples: Input: arr[] = {10
12 min read
Length of smallest subarray consisting of all occurrences of all maximum occurring elements
Given an array arr[] of size N, The task is to find the length of the smallest subarray consisting of all the occurrences of maximum occurring elementsExamples: Input: arr[] = {1, 2, 1, 3, 2}Output: 5Explanation: Elements with maximum frequency (=2) are 1 &amp; 2. Therefore, the length of smallest subarray consisting of all the occurrences of 1 and
6 min read
Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K
Given an array arr[] of size N and a positive integer K, the task is to find the smallest positive integer such that the sum of remaining array elements obtained by dividing all array elements by that smallest positive integer does not exceed K. Note: Dividing an array element by the smallest positive integer must be of Ceil type. Examples: Input:
8 min read
For each A[i] find smallest subset with all elements less than A[i] sum more than B[i]
Given two arrays A[] and B[] of N integers, the task is to find for each element A[i], the size of the smallest subset S of indices, such that : Each value corresponding to the indices in subset S is strictly less than A[i].Sum of elements corresponding to the indices in B is strictly greater than B[i]. Examples: Input: N = 5, A = {3, 2, 100, 4, 5}
10 min read
Smallest subset with sum greater than all other elements
Given an array of non-negative integers, the task is to find the minimum number of elements such that their sum should be greater than the sum of the rest of the elements of the array. Example: Input: arr[] = [ 3 , 1 , 7, 1 ]Output: 1Explanation: Smallest subset is {7}. Sum of this subset is greater than the sum of all other elements left after rem
6 min read
Smallest number that can replace all -1s in an array such that maximum absolute difference between any pair of adjacent elements is minimum
Given an array arr[] consisting of N positive integers and some elements as -1, the task is to find the smallest number, say K, such that replacing all -1s in the array by K minimizes the maximum absolute difference between any pair of adjacent elements. Examples: Input: arr[] = {-1, 10, -1, 12, -1}Output: 11Explanation:Consider the value of K as 1
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg