0% found this document useful (0 votes)
117 views28 pages

1D Array Programs

The document contains 7 questions related to arrays in Java. Each question provides a sample input, sample output, and the code to find the output for an array problem. The problems include searching for a number in an array, finding the number of occurrences of a number, finding the largest number, finding pairs with a given sum, rotating an array, finding the unique element, and checking if an array is a palindrome. The code solutions use techniques like loops, if-else statements, breaking out of loops, and printing outputs.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views28 pages

1D Array Programs

The document contains 7 questions related to arrays in Java. Each question provides a sample input, sample output, and the code to find the output for an array problem. The problems include searching for a number in an array, finding the number of occurrences of a number, finding the largest number, finding pairs with a given sum, rotating an array, finding the unique element, and checking if an array is a palindrome. The code solutions use techniques like loops, if-else statements, breaking out of loops, and printing outputs.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Programming

Question 1
Write a Java code to search a given number in an array. If the element is found then
print Found, else print Not Found

Sample Input: Sample Output:


arr_size = 5 Found
arr[] = {23, 82, 57, 45, 38}
search_elem = 45
1 import java.util.Scanner;
2 public class MyClass {
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int arr_size = sc.nextInt();
6 int arr[] = new int[arr_size];
7 int i;
8 for(i = 0; i < arr_size; i++)
9 {
10 arr[i] = sc.nextInt();
11 }
12 int search_elem = sc.nextInt();
13 int is_matched = 0;
14 for(i = 0; i < arr_size; i++)
15 {
16 if(arr[i] == search_elem)
17 {
18 is_matched = 1;
19 break;
20 }
21 }
22
1 if(is_matched == 1)
2 {
3 System.out.print("Found");
4 }
5 else
6 {
7 System.out.print("Not Found");
8 }
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 2
Write a Java code to find the number of occurrences of a given number in an array.

Sample Input: Sample Output:


arr_size = 6 2
arr[] = {3, 82, 57, 45, 3, 8}
search_elem = 3
1 import java.util.Scanner;
2 public class MyClass {
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int arr_size = sc.nextInt();
6 int arr[] = new int[arr_size];
7 int i;
8 for(i = 0; i < arr_size; i++){
9 arr[i] = sc.nextInt();
10 }
11 int search_elem = sc.nextInt();
12 int count = 0;
13 for(i = 0; i < arr_size; i++)
14 {
15 if(arr[i] == search_elem)
16 {
17 count++;
18 }
19 }
20 System.out.print(count);
21 }
22 }
Question 3
Write a Java code to find the largest number in an array.

Sample Input: Sample Output:


arr_size = 5 7
arr[] = {1, 7, 3, 4, 5}
1 import java.util.*;
2 public class Main{
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int a = sc.nextInt();
6 int arr[]=new int[a];
7 for(int i = 0; i < a; i++)
8 {
9 arr[i] = sc.nextInt();
10 }
11 int max = 0;
12 for(int i = 0; i < a; i++)
13 {
14 if(arr[i] > max)
15 {
16 max = arr[i];
17 }
18 }
19 System.out.print(max);
20 }
21 }
22
Question 4
Write a Java code to find all pairs of elements whose sum is equal to the given
value.

Sample Input: Sample Output:


arr_size = 5 10, 20
arr[] = {50, 10, 30, 20, 0} 30, 0
value = 30
1 import java.util.Scanner;
2 public class Main{
3 public static void main(String args[]) {
4 Scanner sc = new Scanner(System.in);
5 int a = sc.nextInt();
6 int arr[] = new int[a];
for(int i = 0; i < a; i++){
7
arr[i] = sc.nextInt();
8
}
9
int value = sc.nextInt();
10 int temp = 0;
11 for(int i = 0; i < a; i++) {
12 for(int j = i+1; j < a; j++) {
13 temp = arr[i]+arr[j];
14 if(temp == value){
15 System.out.print(arr[i]+ ", "+arr[j]);
16 System.out.println();
17 }
18 temp = 0;
19 }
20 }
21 }
22 }
Question 5
Write a Java code to rotate an array.

Sample Input: Sample Output:


arr_size = 5 34512
arr[] = {1, 2, 3, 4, 5}
no_of_rotations = 2

(HINT: You have to circularly left shift array by 2 positions)


1 import java.util.Scanner;
2 public class Main
3 {
4 public static void rotate_arr(int arr_size, int arr[], int no_of_rotate)
5 {
6 for(int i = 1; i <= no_of_rotate; i++)
7 {
8 int temp = arr[0];
9 for(int j = 1; j < arr_size; j++)
10 {
11 arr[j-1] = arr[j];
12 }
13 arr[arr_size-1] = temp;
14 }
15 }
16
17
18
19
20
21
22
1 public static void main(String args[])
2 {
3 Scanner sc = new Scanner(System.in);
4 int arr_size = sc.nextInt();
5 int arr[] = new int[arr_size];
6 for(int index = 0; index < arr_size; index++)
7 {
8 arr[index] = sc.nextInt();
9 }
10 int no_of_rotate = sc.nextInt();
11 rotate_arr(arr_size, arr, no_of_rotate);
12 for(int i = 0; i < arr_size; i++)
13 {
14 System.out.print(arr[i] + " ");
15 }
16 }
17 }
18
19
20
21
22
Question 6
Write a Java code to find the unique element in the given array.

Sample Input: Sample Output:


arr_size = 6 17
arr[] = {1, 2, 3, 2, 3, 7}
1 import java.util.Scanner;
2 class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 int arr_size = sc.nextInt();
8 int arr[] = new int[arr_size];
9 for(int index = 0; index < arr_size; index++)
10 {
11 arr[index] = sc.nextInt();
12 }
13
14
15
16
17
18
19
20
21
22
1 for(int index1 = 0; index1 <= arr_size-1; index1++)
2 {
3 int has_occurred = 0;
4 for(int index2 = 0; index2 <= arr_size-1; index2++)
5 {
6 if((index1 != index2) && (arr[index1] == arr[index2]))
7 {
8 has_occurred = 1;
9 break;
10 }
11 }
12 if(has_occurred == 0)
13 {
14 System.out.print(arr[index1] + " ");
15 }
16 }
17 }
18 }
19
20
21
22
Question 7
Write a Java code to check whether the given array is a palindrome or not. If the
given array is a palindrome, then print "Yes". Otherwise, print "No".

Sample Input: Sample Output:


arr_size = 5 Yes
arr[] = {4, 5, 3, 5, 4}
1 import java.util.Scanner;
2 class Main{
3 public static void main(String args[])
4 {
5 Scanner sc = new Scanner(System.in);
6 int arr_size = sc.nextInt();
7 int arr[] = new int[arr_size];
8 for(int idx = 0; idx <= arr_size - 1; idx++)
9 {
10 arr[idx] = sc.nextInt();
11 }
12 int left = 0;
13 int right = arr_size - 1;
14 boolean is_palindrome = true;
15
16
17
18
19
20
21
22
1 while(left <= right)
2 {
3 if(arr[left] != arr[right])
4 {
5 is_palindrome = false;
6 break;
7 }
8 left++;
9 right--;
10 }
11 if(is_palindrome == true){
12 System.out.print("Yes");
13 }
14 else{
15 System.out.print("No");
16 }
17 }
18 }
19
20
21
22
1 // Predict the output
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 int arr[] = {10, 20, 30, 40, 50};
8 System.out.print(arr[2]);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
MCQ
Question 1
A) No output

B) ArrayIndexOutOfBoundsException

C) 40

D) 30
1 // Predict the output
2 import java.util.Scanner;
3 class Main
4 {
5 public static void main (String[] args)
6 {
7 int arr[] = {10, 20, 30, 40};
int a = 50;
8
call(a,arr);
9
System.out.println(a);
10
System.out.println(arr[0]);
11 System.out.println(arr[1]);
12 }
13 public static void call(int a, int arr[])
14 {
15 a = a + 2;
16 arr[0] = 100;
17 arr[1] = 200;
18 }
19 }
20
21
22
Question 2
A) 50
100
200
B) 52
100
200
C) 50
10
20
D) 52
10
20
1 // Predict the output
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 int arr[2];
8 System.out.println(arr[0]);
9 System.out.println(arr[1]);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 3
A) Garbage value
Garbage value

B) ArrayIndexOutOfBoundsException

C) Compilation error

D) 0
0
THANK YOU

You might also like