Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Split array into K-length subsets to minimize sum of second smallest element of each subset
Next article icon

Minimize sum of smallest elements from K subsequences of length L

Last Updated : 12 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of size N, the task is to find the minimum possible sum by extracting the smallest element from any K subsequences from arr[] of length L such that each of the subsequences have no shared element. If it is not possible to get the required sum, print -1.

Examples: 

Input: arr[] = {2, 15, 5, 1, 35, 16, 67, 10}, K = 3, L = 2 
Output: 8 
Explanation: 
Three subsequences of length 2 can be {1, 35}, {2, 15}, {5, 16} 
Minimum element of {1, 35} is 1. 
Minimum element of {2, 15} is 2. 
Minimum element of {5, 16} is 5. 
Their Sum is equal to 8 which is the minimum possible.

Input: arr[] = {19, 11, 21, 16, 22, 18, 14, 12}, K = 3, L = 3 
Output: -1 
Explanation: 
It is not possible to construct 3 subsequences of length 3 from arr[]. 

Approach: 
To optimize the above approach, we need to observe the following details: 

  • The K smallest elements of the array contribute to finding the minimum sum of the smallest elements of K subsequences.
  • The length of the array must be greater than or equal to (K * L) in order to form K subsequences of length L.

Follow the steps below to solve the problem: 

  • Check if the size of the array arr[] is greater than equal to (K * L).
  • If so, sort the array arr[] and print the sum of the first K elements of the array after sorting.
  • Otherwise, return -1.

Below is the implementation of the above approach:

C++




// C++ Program to find the minimum
// possible sum of the smallest
// elements from K subsequences
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum sum
int findMinSum(int arr[], int K,
               int L, int size)
{
 
    if (K * L > size)
        return -1;
 
    int minsum = 0;
 
    // Sort the array
    sort(arr, arr + size);
 
    // Calculate sum of smallest
    // K elements
    for (int i = 0; i < K; i++)
        minsum += arr[i];
 
    // Return the sum
    return minsum;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
 
    int length = sizeof(arr)
                / sizeof(arr[0]);
 
    cout << findMinSum(arr, K,
                       L, length);
 
    return 0;
}
 
 

Java




// Java program to find the minimum
// possible sum of the smallest
// elements from K subsequences
import java.util.Arrays;
 
class GFG{
 
// Function to find the minimum sum
static int findMinSum(int []arr, int K,
                      int L, int size)
{
    if (K * L > size)
        return -1;
 
    int minsum = 0;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Calculate sum of smallest
    // K elements
    for(int i = 0; i < K; i++)
        minsum += arr[i];
 
    // Return the sum
    return minsum;
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
    int length = arr.length;
 
    System.out.print(findMinSum(arr, K,
                                L, length));
}
}
 
// This code is contributed by Ritik Bansal
 
 

Python3




# Python3 program to find the minimum
# possible sum of the smallest
# elements from K subsequences
 
# Function to find the minimum sum
 
 
def findMinSum(arr, K, L, size):
 
    if (K * L > size):
        return -1
 
    minsum = 0
 
    # Sort the array
    arr.sort()
 
    # Calculate sum of smallest
    # K elements
    for i in range(K):
        minsum += arr[i]
 
    # Return the sum
    return minsum
 
 
# Driver code
if __name__ == '__main__':
 
    arr = [2, 15, 5, 1,
           35, 16, 67, 10]
    K = 3
    L = 2
 
    length = len(arr)
 
    print(findMinSum(arr, K, L, length))
 
# This code is contributed by Shivam Singh
 
 

C#




// C# program to find the minimum
// possible sum of the smallest
// elements from K subsequences
using System;
   
class GFG{
   
// Function to find the minimum sum
static int findMinSum(int []arr, int K,
                      int L, int size)
{
    if (K * L > size)
        return -1;
   
    int minsum = 0;
   
    // Sort the array
    Array.Sort(arr); 
   
    // Calculate sum of smallest
    // K elements
    for(int i = 0; i < K; i++)
        minsum += arr[i];
   
    // Return the sum
    return minsum;
}
   
// Driver Code
public static void Main() 
{
    int[] arr = { 2, 15, 5, 1,
                  35, 16, 67, 10 };
    int K = 3;
    int L = 2;
    int length = arr.Length;
   
    Console.Write(findMinSum(arr, K,
                             L, length));
}
}
 
// This code is contributed by code_hunt
 
 

Javascript




<script>
// Javascript program to find the minimum
// possible sum of the smallest
// elements from K subsequences
 
// Function to find the minimum sum
function findMinSum(arr, K, L, size)
{
    if (K * L > size)
        return -1;
 
    let minsum = 0;
 
    // Sort the array
    arr.sort((a, b) => a - b);
 
    // Calculate sum of smallest
    // K elements
    for(let i = 0; i < K; i++)
        minsum += arr[i];
 
    // Return the sum
    return minsum;
}
 
    // Driver Code
     
    let arr = [ 2, 15, 5, 1,
                  35, 16, 67, 10 ];
    let K = 3;
    let L = 2;
    let length = arr.length;
 
   document.write(findMinSum(arr, K,
                                L, length));
 
</script>
 
 
Output
8

Time Complexity: O(N * log(N)) 
Space Complexity: O(1)
 



Next Article
Split array into K-length subsets to minimize sum of second smallest element of each subset
author
animesh_ghosh
Improve
Article Tags :
  • Algorithms
  • Arrays
  • Data Structures
  • DSA
  • Greedy
  • Mathematical
  • Sorting
  • optimization-technique
  • subsequence
Practice Tags :
  • Algorithms
  • Arrays
  • Data Structures
  • Greedy
  • Mathematical
  • Sorting

Similar Reads

  • Split array into K-length subsets to minimize sum of second smallest element of each subset
    Given an array arr[] of size N and an integer K (N % K = 0), the task is to split array into subarrays of size K such that the sum of 2nd smallest elements of each subarray is the minimum possible. Examples: Input: arr[] = {11, 20, 5, 7, 8, 14, 2, 17, 16, 10}, K = 5Output: 13Explanation: Splitting a
    5 min read
  • Length of Smallest Subsequence such that sum of elements is greater than equal to K
    Given an array arr[] of size N and a number K, the task is to find the length of the smallest subsequence such that the sum of the subsequence is greater than or equal to number K.Example: Input: arr[] = {2, 3, 1, 5, 6, 3, 7, 9, 14, 10, 2, 5}, K = 35 Output: 4 Smallest subsequence with the sum great
    6 min read
  • Maximum sum subsequence of length K | Set 2
    Given an array sequence arr[] i.e [A1, A2 …An] and an integer k, the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3………<=Sk. Examples: Input: arr[] = {-1, 3, 4, 2, 5}, K = 3Output: 3 4 5Explanation: Subsequence 3 4 5 with sum 12 is the s
    7 min read
  • Minimum sum of medians of all possible K length subsequences of a sorted array
    Given a sorted array arr[] consisting of N integers and a positive integer K(such that N%K is 0), the task is to find the minimum sum of the medians of all possible subsequences of size K such that each element belongs to only one subsequence. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 2Output
    7 min read
  • Sum of all subsequences of length K
    Given an array arr[]and an integer K, the task is to find the sum of all K length subsequences from the given array. Example: Input: arr[] = {2, 3, 4}, K = 2 Output: 18 Explanation: There are 3 possible subsequences of length 2 which are {2, 3}, {2, 4} and {3, 4} The sum of all 2 length subsequences
    6 min read
  • Minimize steps to form string S from any random string of length K using a fixed length subsequences
    Given a string S consisting of N characters and a positive integer K, the task is to find the minimum number of operations required to generate the string S from a random string temp of size K and inserting the subsequence of any fixed length from the random string in the random string. If it is imp
    12 min read
  • Number of K length subsequences with minimum sum
    Given an array arr[] of size N and an integer K, the task is to find the number of K length subsequences of this array such that the sum of these subsequences is the minimum possible. Examples: Input: arr[] = {1, 2, 3, 4}, K = 2 Output: 1 Subsequences of length 2 are (1, 2), (1, 3), (1, 4), (2, 3),
    8 min read
  • Maximize subsequences having array elements not exceeding length of the subsequence
    Given an array arr[] consisting of N positive integers, the task is to maximize the number of subsequences that can be obtained from an array such that every element arr[i] that is part of any subsequence does not exceed the length of that subsequence. Examples: Input: arr[] = {1, 1, 1, 1} Output: 4
    6 min read
  • Maximum Bitwise AND value of subsequence of length K
    Given an array a of size N and an integer K. The task is to find the maximum bitwise and value of elements of any subsequence of length K. Note: a[i] <= 109 Examples: Input: a[] = {10, 20, 15, 4, 14}, K = 4 Output: 4 {20, 15, 4, 14} is the subsequence with highest '&' value. Input: a[] = {255
    15+ min read
  • Minimum cost for constructing the subsequence of length K from given string S
    Given a string S consisting of N lowercase English alphabets, and an integer K and, an array cost[] of size 26 denoting the cost of each lowercase English alphabet, the task is to find the minimum cost to construct a subsequence of length K from the characters of the string S. Examples: Input: S = "
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences