Oracle Interview Question
Applications DevelopersCountry: India
8.void main()
9.{
10. int i, n, sum = 0;
11. int *a;
12.
13. printf("Enter the size of array A \n");
14. scanf("%d", &n);
15. a = (int *) malloc(n * sizeof(int));
16. printf("Enter Elements of First List \n");
17. for (i = 0; i < n; i++)
18. {
19. scanf("%d", a + i);
20. }
21. </* Compute the sum of all elements in the given array */
22. for (i = 0; i < n; i++)
23. {
24. sum = sum + *(a + i);
25. }
26. printf("Sum of all elements in array = %d\n", sum);
27.}
8.void main()
9.{
10. int i, n, sum = 0;
11. int *a;
12.
13. printf("Enter the size of array A \n");
14. scanf("%d", &n);
15. a = (int *) malloc(n * sizeof(int));
16. printf("Enter Elements of First List \n");
17. for (i = 0; i < n; i++)
18. {
19. scanf("%d", a + i);
20. }
21. </* Compute the sum of all elements in the given array */
22. for (i = 0; i < n; i++)
23. {
24. sum = sum + *(a + i);
25. }
26. printf("Sum of all elements in array = %d\n", sum);
27.}
The following cases must be eliminated.
Case 1) Only K songs are present => (NcK)xKx(K-1)...x2
Case 2) Only K+1 sings are present =>(Nc(K+1)) x(K+1)xK...x2
......
.....
Case N-K) Only N-1 songs are present => N x (N-1)x(N-2)x(N-K)x(N-1-K)^(L-K)
Let the sum be S
The answer is Nx(N-1)x...x(N-K+1)x(N-K)^(L-K) - S
(N!/(N-K)!) * (N-K)^(L-K+1) assuming L > K and N >= K
For the first K songs, we have a decreasing list of possibilities
1st song - N
2nd song - (N-1)
3rd song - (N-2)
...
ith song (i < K) - (N-i)
Kth song - (N-K)
After this, for each song, one of the songs from the earlier choice is also valid thus always ensuring that the available songs is (N-K) for the remaining (L-K) songs
Formula is:
- Amauris February 07, 2015((N-i)! Up to i=K)*((N-k)^(N-i)) where i Is iterating from 0 to N