0

# Write a program to find lowest number of integers in the array that sums up the given number. In java,c,c++ only no python

Write a program to find lowest number of integers in the array that sums up the given number. The program should ask user to input array of integers (“Input Array”) and the required sum (“Required Sum”). The output (“Output”) should list the lowest number of integers from the input array that sums up the “Required Sum”. Refer to examples given below. Input : Array of integers Required sum Output : Elements from array which makes sum equals to given value Example: Input Array : [10, 0, -1, 20, 25, 30] Required Sum: 45 Output: [20, 25] Required Sum: 59 Output: [10, -1, 20, 30] Required Sum: 60 Output: [10, 20, 30]

8th Oct 2019, 5:35 AM
D€√Anand Gutte
5 ответов
0
i did following program but it work only for fixed 4 integers.i need help to code for selecting minimum elements automatically... import java.util.Arrays; public class assign4{ /* A sorting based solution to print all combination of 4 elements in A[] with sum equal to X */ void find4Numbers(int A[], int n, int X) { int l, r; // Sort the array in increasing order, using library // function for quick sort Arrays.sort(A); /* Now fix the first 2 elements one by one and find the other two elements */ for (int i = 0; i < n - 3; i++) { for (int j = i + 1; j < n - 2; j++) { // Initialize two variables as indexes of the first and last // elements in the remaining elements l = j + 1; r = n - 1; // To find the remaining two elements, move the index // variables (l & r) toward each other. while (l < r) { if (A[i] + A[j] + A[l] + A[r] == X) { System.out.println(A[i]+" "+A[j]+" "+A[l]+" "+A[r]); l++; r--; } else if (A[i] + A[j] + A[l] + A[r] < X) l++; else // A[i] + A[j] + A[l] + A[r] > X r--; } // end of while } // end of inner for loop } // end of outer for loop } // Driver program to test above functions public static void main(String[] args) { assign4 findfour = new assign4(); int A[] = {1, 4, 45, 6, 10, 12}; int n = A.length; int X = 21; findfour.find4Numbers(A, n, X); } }
19th Nov 2019, 8:38 AM
Ranjit
0
No one has given correct answer
15th Feb 2020, 9:06 AM
D€√Anand Gutte
0
7th Feb 2021, 4:06 PM
Puneet Verma
- 1