-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCS263_Lab_4.java
More file actions
32 lines (28 loc) · 1.33 KB
/
CS263_Lab_4.java
File metadata and controls
32 lines (28 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Question: Animal Candy Problem
import java.util.Scanner;
class solution {
static int maximizeProfit(int arr[], int k, int s, int ind) {
if (ind == arr.length) {
return s; //After checking with all animals the profit will be the answer
}
if (arr[ind] == k) return maximizeProfit(arr, k, s + 1, ind + 1); //if same candy encountered then sum is incremented
int exchange = maximizeProfit(arr, arr[ind], s - 1, ind + 1); //if candy exchanged then sum is decremented
int notExchange = maximizeProfit(arr, k, s, ind + 1); //if candy is not sexhanged then no chaangesto sum
return Math.max(exchange, notExchange); //maximum of the exchange and notExchanged will be returned
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scan.nextInt();
int arr[] = new int[n];
System.out.println("Enter the array of candy numbers: ");
for (int i = 0; i < n; i++) arr[i] = scan.nextInt();
System.out.print("Enter the initial candy number: ");
int k = scan.nextInt();
int s = 0;
System.out.print("Maximum score obtained is: ");
System.out.println(maximizeProfit(arr, k, s, 0));
scan.close();
}
}
// Since we are exploring all branches of the recurssion tree the time complexity of the code is O(2^n)