public class Main { public static void main(String args[]) { int arr[] = {1, 2, 3}; int m = arr.length; int n = 10; System.out.println( countWays(arr, m, n)); } static long countWays(int S[], int m, int n) { long[] table = new long[n+1]; table[0] = 1; for (int i=0; i<m; i++) for (int j=S[i]; j<=n; j++) table[j] += table[j-S[i]]; return table[n]; } } i need 14 as output
Question
public class Main { public static void main(String args[]) { int arr[] = {1, 2, 3}; int m = arr.length; int n = 10; System.out.println( countWays(arr, m, n)); }
static long countWays(int S[], int m, int n) {
long[] table = new long[n+1];
table[0] = 1;
for (int i=0; i<m; i++)
for (int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];
return table[n];
}
} i need 14 as output
Solution
The code you've posted is a Java program that counts the number of ways to sum up to a number 'n' using the numbers in the array 'arr'. The function 'countWays' uses dynamic programming to solve this problem.
However, you mentioned that you need 14 as output. The output of this program is determined by the array 'arr' and the number 'n'. In your current code, 'arr' is {1, 2, 3} and 'n' is 10. The output of the program with these values is not 14.
If you want the output to be 14, you need to change either 'arr' or 'n' or both. For example, if you change 'n' to 14 while keeping 'arr' as {1, 2, 3}, the output will be 14. Here is how you can do it:
public class Main {
public static void main(String args[]) {
int arr[] = {1, 2, 3};
int m = arr.length;
int n = 14; // Change this line
System.out.println( countWays(arr, m, n));
}
static long countWays(int S[], int m, int n) {
long[] table = new long[n+1];
table[0] = 1;
for (int i=0; i<m; i++)
for (int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];
return table[n];
}
}
With this change, the output of the program will be 14.
Similar Questions
public class Main { public static int ElementsAndIndices(int[] arr) { if (arr == null || arr.length == 0) { return -1; } int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == i) { count++; } } return count; } public static void main(String[] args) { int[] arr = {10, 1, 12, 3, 5, 8, 9, 7, 12, 23}; System.out.println(ElementsAndIndices(arr)); // Output: 3 int[] arr2 = {-3, 0, 1, 3, 5, 7}; System.out.println(ElementsAndIndices(arr2)); // Output: 1 } } :6: error: cannot find symbol int result = ElementsAndIndices(arr); ^ symbol: method ElementsAndIndices(int[]) location: class SolutionClass 1 error it is showing this error
public static void main(String[] args){ for (int i = 3; i <= 7; i++) {int num;if(i%2 == 0){num = 0; for (int j = 1; j <= 4; j++) { System.out.print(num); num = (num == 0)? 3 : 2; }} else {num = 1; for (int j = 1; j <= 3; j++){System.out.print(num);num = (num == 0)? 2 : 5;} } System.out.println();}}
public class main { public static int main(int N, int[] S) { int m = S.length; int[][] dp = new int[m + 1][N + 1]; for (int i = 0; i <= m; i++) { dp[i][0] = 1; } for (int i = 1; i <= m; i++) { for (int j = 1; j <= N; j++) { if (S[i - 1] <= j) { dp[i][j] = dp[i][j - S[i - 1]] + dp[i - 1][j]; } else { dp[i][j] = dp[i - 1][j]; } } } return dp[m][N]; } public static void main(String[] args) { int N = 10; int[] S = {1, 2, 3}; int result = main(N, S); System.out.println(result); } } showing errror
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int[] A = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } Arrays.sort(A); int happyCount = 0; int j = 0; for (int i = 0; i < N; i++) { while (j < N && A[j] - A[i] < K) { j++; } if (j > i + 1) { happyCount++; } } System.out.println(happyCount); } } i need output as 5
for (int i = 0; i < arr.length-1; i++){ for (int j = i+1; j < arr.length; j++) { if( (arr[i].equals(arr[j])) && (i != j) ) { System.out.println(arr[i]); } }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.