Professional Documents
Culture Documents
Write a program to rotate array elements by a given number of rotation in the given
range .
Example 1: If there are 5 houses with values 5, 5, 10, 1, 8 then the maximum that
can be stolen is 23.
Example 2: If there are 5 houses with values 5, 5, 5, 10, 1 then the maximum that
can be stolen is 15.
Question 2
Sample Input1: Sample Output1:
5 19
1 5 8 3 10
1 import java.util.*;
2 public class Main {
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int n, i;
6 int house_values[]=new int[50];
7 n = sc.nextInt();
8 for(i = 0; i < n; i++)
9 {
10 house_values[i] = sc.nextInt();
11 }
12 System.out.print(max_stealing_opt_wrapper(house_values, n));
13 }
14 public static int max_stealing_wrapper(int house_values[], int n)
15 {
16 return max_stealing(house_values,n, 0);
17 }
18
19
20
21
22
1 public static int max_stealing(int house_values[],int n,int
2 current_house)
3 {
4 if(current_house >= n)
5 {
6 return 0;
7 }
8 int if_stolen = house_values[current_house]
9 +max_stealing(house_values, n, current_house + 2);
10 int if_not_stolen = max_stealing(house_values,n,current_house + 1);
11 return MAX(if_stolen, if_not_stolen);
12 }
13 public static int max_stealing_opt_wrapper(int house_values[], int n)
14 {
15 int i;
16 int results_cache[]=new int[n];
17 for(i = 0; i < n; i++)
18 {
19 results_cache[i] = -1;
20 }
21 return max_stealing_opt(house_values, n, 0, results_cache);
22 }
1 public static int max_stealing_opt(int house_values[], int n, int
2 current_house, int results_cache[]){
3 if(current_house >= n){
4 return 0;
5 }
6 if(results_cache[current_house] != -1){
7 return results_cache[current_house];
8 }
9 int if_stolen = house_values[current_house] +
10 max_stealing_opt(house_values, n ,current_house + 2,
11 results_cache);
12
13 int if_not_stolen = max_stealing_opt(house_values, n ,
14 current_house+1, results_cache);
15
16 results_cache[current_house] = MAX(if_stolen, if_not_stolen);
17 return results_cache[current_house];
18 }
19 public static int MAX(int a,int b){
20 return a>b ? a:b;}
21 }
22 }
Question 3
Write a Java code to find the inverse run length encoding of the given input.