Array Program 1.1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Question 1

India has organized a blind folded ball passing game for two players.

They have a special kind of ground where player B can throw the ball in any
four directions (up, down, right, left).
The ground consists of some slant poles on which, when the ball strikes the
ball changes its direction by 90 degrees.

Consider the below field of 2 x 2 where the ball is at location 0,0 and being
thrown to its right. After hitting slant pole "\", it will change direction and go
to 1,1.
B\
--
Question 1
If suppose you have thrown a ball in a direction in which there is no pole
then the ball goes out of the stadium in that direction.
Player B has the ball initially. The objective is to output the minimum steps
in which B can pass the ball to A and also the count of poles the ball has
struck.
If he can never pass the ball to A, then your output should be -1.

If you get multiple minimum steps then output those steps in which the ball
hits minimum no of barriers
Question 1
Input Format:

First line containing the integer K indicating the size of the field
Next two lines each having a pair of integers separated by space giving the
row and column numbers of
the positions of A and B respectively K lines of K integers each, separated
by space, giving the positions of
the barriers in the field - 0 indicating no barrier, 1 indicating a / barrier and 2
indicating a \ barrier.

Output Format:
Minimum number of steps required for the ball to reach A
Number of barriers ball on which the ball bounces
Question 1
Sample Input: Sample Output:
field_size = 4 4
3
A's position = 1 3

B's position = 3 3

Field[][]
0000
1110
2222
1110
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 int K = sc.nextInt();
8 int a_pos_row = sc.nextInt();
9 int a_pos_col = sc.nextInt();
10 int b_pos_row = sc.nextInt();
11 int b_pos_col = sc.nextInt();
12 int play_field[][] = new int[K][K];
13 for(int i = 0; i < K; i++)
14 {
15 for(int j = 0; j < K; j++)
16 {
17 play_field[i][j] = sc.nextInt();
18 }
19 }
20 int min_step = -1;
21 int min_barriers = -1;
22
1 for(int i = 0; i <= 3; i++)
2 {
3 int direction = i;
4 int curr_row = b_pos_row, curr_col = b_pos_col;
5 int step = 0, barrier_count = 0;
6 while(true)
7 {
8 if(curr_col < 0 || curr_col >= K || curr_row < 0 ||
9 curr_row >= K)
10 {
11 break;
12 }
13 if(curr_row == a_pos_row && curr_col == a_pos_col)
14 {
15 if(min_step > step || min_step == -1)
16 {
17 min_step = step;
18 min_barriers = barrier_count;
19 }
20 else if (min_step == step && min_barriers >
21 barrier_count)
22
1 for(int i = 0; i <= 3; i++)
2 {
3 min_barriers = barrier_count;
4 }
5 break;
6 }
7 step++;
8 if(play_field[curr_row][curr_col] == 1 ||
9 play_field[curr_row][curr_col] == 2)
10 {
11 barrier_count++;
12 }
13 switch(direction)
14 {
15 case 0:
16 if(play_field[curr_row][curr_col] == 1)
17 {
18 curr_col++;
19 direction = 2;
20 }
21 else if(play_field[curr_row][curr_col] == 2)
22
1 curr_col--;
2 direction = 3;
3 }
4 else
5 {
6 curr_row--;
7 }
8 break;
9 case 1:
10 if(play_field[curr_row][curr_col] == 1)
11 {
12 curr_col--;
13 direction = 3;
14 }
15 else if(play_field[curr_row][curr_col] == 2)
16 {
17 curr_col++;
18 direction = 2;
19 }
20 else
21 {
22
1 curr_row++;
2 }
3 break;
4 case 2:
5 if(play_field[curr_row][curr_col] == 1)
6 {
7 curr_row--;
8 direction = 0;
9 }
10 else if(play_field[curr_row][curr_col] == 2)
11 {
12 curr_row++;
13 direction = 1;
14 }
15 else
16 {
17 curr_col++;
18 }
19 break;
20 case 3:
21
22
1 if(play_field[curr_row][curr_col] == 1){
2 curr_row++;
3 direction = 1;
4 }
5 else if(play_field[curr_row][curr_col] == 2){
6 curr_row--;
7 direction = 0;
8 }
9 else{
10 curr_col--;
11 }break;
12 }
13 }
14 }
15 if(min_step != -1) {
16 System.out.println(min_step);
17 System.out.print(min_barriers);
18 }
19 else{
20 System.out.print("-1");}
21 }
22 }
Question 2
Write a Java program to implement a flames program.

FLAMES is a silly game which given 2 names calculates a compatibility


score. Given 2 names,
a flame score f is obtained by removing characters from both the names and
counting the characters.
Use FLAMES word and eliminate every f%6 th character. Map the character
to it's result and print the result.
Question 2
F -> Friends
L -> Lovers
A -> Anger
M -> Marriage
E -> Engaged
S -> Sweethearts
Question 2
Sample Input: Sample Output:
Xxxx Affection
Yyyy
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner scan = new Scanner(System.in);
7 String name1 = scan.nextLine();
8 String name2 = scan.nextLine();
9 String string1 = name1.toLowerCase();
10 String string2 = name2.toLowerCase();
11 StringBuilder sb1 = new StringBuilder(string1);
12 StringBuilder sb2 = new StringBuilder(string2);
13 int sb1_len = sb1.length();
14 int sb2_len = sb2.length();
15 int uncommon_char_count = uncommon_characters_count(sb1, sb1_len,
16 sb2, sb2_len);
17 StringBuilder str = new StringBuilder("FLAMES");
18 int str_len = str.length();
19 int idx = 0, flames_count = 0;
20 while(str_len > 0)
21 {
22 flames_count++;
1 if(idx == str_len-1)
2 {
3 idx = 0;
4 }
5 else{
6 idx++;
7 }
8 if(flames_count == uncommon_char_count)
9 {
10 int j = 0;
11 if(idx == 0)
12 {
13 j = str_len - 1;
14 }
15 else{
16 j = --idx;
17 }
18 while(j < str_len - 1)
19 {
20 str.setCharAt(j, str.charAt(j + 1));
21 j++;
22 }
1 flames_count = 0;
2 str_len--;
3 }
4 }
5 char ch = str.charAt(0);
6 switch(ch)
7 {
8 case 'F':
9 {
10 System.out.print("Friends");
11 break;
12 }
13 case 'L':
14 {
15 System.out.print("Love");
16 break;
17 }
18 case 'A':
19 {
20 System.out.print("Affection");
21 break;
22 }
1 case 'M':
2 {
3 System.out.print("Marriage");
4 break;
5 }
6 case 'E':
7 {
8 System.out.print("Enemy");
9 break;
10 }
11 case 'S':
12 {
13 System.out.print("Sibling");
14 break;
15 }
16 }
17 }
18 public static int uncommon_characters_count(StringBuilder sb1,
19 int sb1_len, StringBuilder sb2, int sb2_len)
20 {
21 for(int i = 0; i < sb1_len; i++)
22 {
1 for(int j = 0; j < sb2_len; j++)
2 {
3 if(sb1.charAt(i)== sb2.charAt(j)){
4 sb1.setCharAt(i,'$');
5 sb2.setCharAt(j,'$');
6 break;
7 }
8 }
9 }int count = 0;
10 for(int i =0; i < sb1_len; i++){
11 if(sb1.charAt(i) != '$'){
12 count++;
13 }
14 }
15 for(int i =0; i < sb2_len; i++){
16 if(sb2.charAt(i) != '$'){
17 count++;
18 }
19 }
20 return count;
21 }
22 }
Question 3
Write a java program to find the array - Max consecutive 1's after flipping.

Given an array consisting of zeros and ones, you are allowed to flip at most
1 element from 0 to 1.
Print the size of the sub array which consists of maximum number of
consecutive 1's .

Note:
First input is an size of array
Second input is an array element
Question 3
Sample Input: Sample Output:
14 7
11110101110111
1 import java.util.*;
2 public class Main
3 {
4 public static int max_element(int x, int y)
5 {
6 if(x >= y)
7 {
8 return x;
9 }
10 return y;
11 }
12 public static void get_max_consecutive1(int arr[], int N)
13 {
14 int i, count = 0, flip_count = 0, max_count = 0;
15 for(i = 0; i < N; i++)
16 {
17 if(arr[i] == 1)
18 {
19 count++;
20 }
21 if(arr[i] == 0)
22 {
1 if(arr[i+1] == 0){
2 flip_count = 1;
3 }
4 else{
5 flip_count = count + 1;
6 count = 0;
7 }
8 }
9 max_count = max_element(count + flip_count, max_count);
10 }
11 System.out.println(max_count);
heirt
12 }
13 public static void main(String [] args){
14 Scanner s=new Scanner(System.in);
15 int arr[]=new int[100];
16 int i, N;
17 N=s.nextInt();
18 for(i = 0; i < N; i++)
19 arr[i]=s.nextInt();
20 get_max_consecutive1(arr, N);
21 }
22 }
Question 4
Write a java program to print spiral pattern.

Sample Input: Sample Output:


5 12345
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 int n = sc.nextInt();
8 int a[][] = new int[n][n];
9 print_spirally(a, n);
10 for(int i = 0; i < n; i++)
11 {
12 for(int j = 0; j < n; j++)
13 {
14 System.out.print(a[i][j] + " ");
15 }
16 System.out.println();
17 }
18 }
19 public static void print_spirally(int a[][], int n)
20 {
21 int val = 1;
22 int r_min = 0, c_min = 0;
1 int r_max = n-1, c_max = n-1;
2 while ((r_min <= r_max) && (c_min <= c_max)){
3 for(int i = c_min; (i <= c_max); i++)
4 {
5 a[r_min][i] = val++;
6 }
7 for(int i = r_min + 1; (i <= r_max); i++)
8 {
9 a[i][c_max] = val++;
10 }
11 for(int i = c_max-1; (i >= c_min); i--)
12 {
13 a[r_max][i] = val++;
14 }
15 for(int i = r_max - 1; (i >= r_min+1); i--)
16 {
17 a[i][c_min] = val++;
18 }
19 r_min++; c_min++; r_max--; c_max--;
20 }
21 }
22 }
THANK YOU

You might also like