📌  相关文章
📜  打印可以添加以形成给定总和的元素

📅  最后修改于: 2022-05-13 01:57:47.392000             🧑  作者: Mango

打印可以添加以形成给定总和的元素

给定一个正整数数组arr[]和一个sum ,任务是打印将包含的元素以获得给定的总和。
笔记:

  1. 考虑队列形式的元素,即从开始到元素总和要添加的元素小于或等于给定总和。
  2. 此外,数组元素的总和不必等于给定的总和。

因为任务是检查是否可以包含元素。

例子:

方法:

  1. 检查在添加当前元素时,总和是否小于给定总和。
  2. 如果是,则添加它。
  3. 否则转到下一个元素并重复相同的操作,直到它们的总和小于或等于给定的总和。

以下是上述方法的实现:

C++
// C++ implementation of above approach
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


PHP
= 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
?>


Javascript


输出:
3 5 2