Related Articles

Related Articles

Print elements that can be added to form a given sum
  • Difficulty Level : Hard
  • Last Updated : 03 Oct, 2018

Given an array arr[] of positive integers and a sum, the task is to print the elements that will be included to get the given sum.
Note:

  1. Consider the elements in the form of queue i.e. Elements to be added from starting and up to the sum of elements is lesser or becomes equal to the given sum.
  2. Also, it is not necessary that the sum of array elements should be equal to the given sum.

As the task is to check that element can be included or not.

Examples:

Input: arr[] = {3, 5, 3, 2, 1}, Sum = 10
Output: 3 5 2
By adding 3, 5 and 3, sum becomes 11 so remove last 3.
Then on adding 2, sum becomes 10. So no other element
needs to be added.

Input:
arr[] = {7, 10, 6, 4}, Sum = 12
Output: 7 4
As, 7+10 and 7+6 sums to a higher value than 12
but 7+4 = 11 which is smaller than 12.
So, 7 and 4 can be included

Approach:



  1. Check if on adding the current element, the sum is less than the given sum.
  2. If yes, the add it.
  3. Else go to next element and repeat the same until their sum is less than or equals to the given sum.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that finds whether an element
// will be included or not
void includeElement(int a[], int n, int sum)
{
    for (int i = 0; i < n; i++) {
  
        // Check if the current element
        // will be incuded or not
        if ((sum - a[i]) >= 0) {
            sum = sum - a[i];
            cout << a[i]<< " ";
        }
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 3, 5, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 10;
  
    includeElement(arr, n, sum);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation 
// of above approach
class GFG
{
  
// Function that finds whether 
// an element will be included
// or not 
static void includeElement(int a[], 
                           int n, int sum) 
    for (int i = 0; i < n; i++) 
    
  
        // Check if the current element 
        // will be included or not 
        if ((sum - a[i]) >= 0)
        
            sum = sum - a[i]; 
            System.out.print(a[i] + " "); 
        
    
  
// Driver code 
public static void main(String[] args) 
{
    int arr[] = { 3, 5, 3, 2, 1 }; 
    int n = arr.length; 
    int sum = 10
  
    includeElement(arr, n, sum); 
}
}
  
// This code is contributed by Bilal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach 
  
# Function that finds whether an element 
# will be included or not 
def includeElement(a, n, sum) :
  
    for i in range(n) :
  
        # Check if the current element 
        # will be incuded or not 
        if sum - a[i] >= 0 :
  
            sum = sum - a[i]
  
            print(a[i],end = " ")
  
# Driver code
if __name__ == "__main__" :
  
    arr = [ 3, 5, 3, 2, 1]
    n = len(arr)
    sum = 10
  
    includeElement(arr, n, sum)
                         
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation 
// of above approach
using System;
  
class GFG
{
// Function that finds whether 
// an element will be included
// or not 
static void includeElement(int[] a, 
                           int n, int sum) 
    for (int i = 0; i < n; i++) 
    
  
        // Check if the current element 
        // will be included or not 
        if ((sum - a[i]) >= 0)
        
            sum = sum - a[i]; 
            Console.Write(a[i] + " "); 
        
    
  
// Driver code 
static void Main() 
{
    int[] arr = new int[]{ 3, 5, 3, 2, 1 }; 
    int n = arr.Length; 
    int sum = 10; 
  
    includeElement(arr, n, sum); 
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation of above approach
  
// Function that finds whether an 
// element will be included or not
function includeElement(&$a, $n, $sum)
{
    for ($i = 0; $i < $n; $i++) 
    {
        // Check if the current element
        // will be incuded or not
        if (($sum - $a[$i]) >= 0)
        {
            $sum = $sum - $a[$i];
            echo $a[$i] . " ";
        }
    }
}
  
// Driver Code
$arr = array( 3, 5, 3, 2, 1 );
$n = sizeof($arr);
$sum = 10;
  
includeElement($arr, $n, $sum);
  
// This code is contributed 
// by ChitraNayal
?>

chevron_right


Output:

3 5 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :