You are on page 1of 17

Question 1

Write a program to rotate array elements by a given number of rotation in the given
range .

Sample Input: Sample Output:


Size - 6 2 4 10 6 8 12
Arr[] - {2 4 6 8 10 12}
Number of rotation - 1
Range - 2 4
1 import java.util.*;
2 public class Main{
3 public static void main(String args[]){
4 int n, no_of_rotation, s, e;
5 Scanner st=new Scanner(System.in);
6 n=st.nextInt();
7 int arr[]=new int[n];
8 for(int i = 0; i < n; i++)
9 {
10 arr[i]=st.nextInt();
11 }
12 no_of_rotation=st.nextInt();
13 s=st.nextInt();
14 e=st.nextInt();
15 int temp;
16 for(int i = 0; i < no_of_rotation; i++){
17 temp = arr[e];
18 for(int j = e; j > s; j--){
19 arr[j] = arr[j - 1];
20 }
21 arr[s] = temp;
22 }
1 for(int i = 0; i < n; i++)
2 {
3 System.out.println(arr[i]);
4 }
5 }
6 }
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 2
A line of houses are stashed with valuables and you want to steal from these houses.
The constraint is you should not steal from adjacent houses. Compute the maximum
you can steal from these houses.

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.

Sample Input: Sample Output:


a1b10 abbbbbbbbbb
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main (String[] args)
5 {
6 Scanner in = new Scanner(System.in);
7 String str = in.nextLine();
8 int temp[] = new int[26];
9 int len = str.length();
10 for(int i = 0; i <= len - 2;)
11 {
12 if(str.charAt(i + 1) >= '0' && str.charAt(i+ 1) <= '9')
13 {
14 if((i + 2) < len && str.charAt(i + 2) >= '0' &&
15 str.charAt(i + 2) <= '9')
16 {
17 int num1 = str.charAt(i + 1) - '0';
18 int num2 = str.charAt(i + 2) - '0';
19 int n = 0;
20 if(num2 == 0){
21 n = num1 * 10;
22 }
1 else
2 {
3 n = num1 * 10 + num2;
4 }
5 int idx = str.charAt(i) - 'a';
6 temp[idx] = n;
7 i = i + 3;
8 }
9 else
10 {
11 int num1 = str.charAt(i + 1) - '0';
12 int idx = str.charAt(i) - 'a';
13 temp[idx] = num1;
14 i = i + 2;
15 }
16 }
17 }
18 for(int i = 0; i < 26; i++)
19 {
20 if(temp[i] != 0)
21 {
22 int x = 1;
1 int t = temp[i];
2 while(x <= t)
3 {
4 char ch = (char)('a' + i);
5 System.out.print(ch);
6 x++;
7 }
8 }
9 }
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 4
Write java program to find all pairs of elements whose sum is equal to the given
value.

Sample Input: Sample Output:


5 10, 20
50 10 20 40 30
30
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner in = new Scanner(System.in);
7 int arr_size = in.nextInt();
8 int arr[] = new int[arr_size];
9 for(int index = 0; index <= arr_size - 1; index++)
10 {
11 arr[index] = in.nextInt();
12 }
13 int value = in.nextInt();
14 perfect_couple(arr_size, arr, value);
15 }
16
17
18
19
20
21
22
1 public static void perfect_couple(int arr_size, int arr[], int
2 value)
3 {
4 for(int index1 = 0; index1 <= arr_size - 1; index1++)
5 {
6 for(int index2 = index1 + 1; index2 <= arr_size - 1; index2++)
7 {
8 int sum = arr[index1] + arr[index2];
9 if(sum == value)
10 {
11 System.out.println(arr[index1] + "," + " " +
12 arr[index2]);
13 }
14 }
15 }
16 }
17 }
18
19
20
21
22
THANK YOU

You might also like