3. Sample Input: Sample Output:
Found
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
arr_size = 5
arr[] = {23, 82, 57, 45, 38}
search_elem = 45
4. import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
int i;
for(i = 0; i < arr_size; i++)
{
arr[i] = sc.nextInt();
}
int search_elem = sc.nextInt();
int is_matched = 0;
for(i = 0; i < arr_size; i++)
{
if(arr[i] == search_elem)
{
is_matched = 1;
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
6. Sample Input: Sample Output:
2
Question 2
Write a Java code to find the number of occurrences of a given number in
an array.
arr_size = 6
arr[] = {3, 82, 57, 45, 3, 8}
search_elem = 3
7. import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
int i;
for(i = 0; i < arr_size; i++){
arr[i] = sc.nextInt();
}
int search_elem = sc.nextInt();
int count = 0;
for(i = 0; i < arr_size; i++)
{
if(arr[i] == search_elem)
{
count++;
}
}
System.out.print(count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
8. Sample Input: Sample Output:
7
Question 3
Write a Java code to find the largest number in an array.
arr_size = 5
arr[] = {1, 7, 3, 4, 5}
9. import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[]=new int[a];
for(int i = 0; i < a; i++)
{
arr[i] = sc.nextInt();
}
int max = 0;
for(int i = 0; i < a; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
System.out.print(max);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
10. Sample Input: Sample Output:
10, 20
30, 0
Question 4
Write a Java code to find all pairs of elements whose sum is equal to the
given value.
arr_size = 5
arr[] = {50, 10, 30, 20, 0}
value = 30
11. import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[] = new int[a];
for(int i = 0; i < a; i++){
arr[i] = sc.nextInt();
}
int value = sc.nextInt();
int temp = 0;
for(int i = 0; i < a; i++) {
for(int j = i+1; j < a; j++) {
temp = arr[i]+arr[j];
if(temp == value){
System.out.print(arr[i]+ ", "+arr[j]);
System.out.println();
}
temp = 0;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
12. Sample Input: Sample Output:
3 4 5 1 2
Question 5
Write a Java code to rotate an array.
arr_size = 5
arr[] = {1, 2, 3, 4, 5}
no_of_rotations = 2
(HINT: You have to circularly left shift array by 2 positions)
13. import java.util.Scanner;
public class Main
{
public static void rotate_arr(int arr_size, int arr[], int no_of_rotate)
{
for(int i = 1; i <= no_of_rotate; i++)
{
int temp = arr[0];
for(int j = 1; j < arr_size; j++)
{
arr[j-1] = arr[j];
}
arr[arr_size-1] = temp;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
14. public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int index = 0; index < arr_size; index++)
{
arr[index] = sc.nextInt();
}
int no_of_rotate = sc.nextInt();
rotate_arr(arr_size, arr, no_of_rotate);
for(int i = 0; i < arr_size; i++)
{
System.out.print(arr[i] + " ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
15. Sample Input: Sample Output:
1 7
Question 6
Write a Java code to find the unique element in the given array.
arr_size = 6
arr[] = {1, 2, 3, 2, 3, 7}
16. import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int index = 0; index < arr_size; index++)
{
arr[index] = sc.nextInt();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
18. Sample Input: Sample Output:
Yes
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".
arr_size = 5
arr[] = {4, 5, 3, 5, 4}
19. import java.util.Scanner;
class Main{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int idx = 0; idx <= arr_size - 1; idx++)
{
arr[idx] = sc.nextInt();
}
int left = 0;
int right = arr_size - 1;
boolean is_palindrome = true;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Section End/Start
Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).
Input :
arr_size = 6
arr[] = {23, 82, 57, 45, 38, 63}
search_elem = 12
Output:
Not Found
Question+ Input + Output (Programming)
Question+ Input + Output (Programming)
Question+ Input + Output (Programming)
Output:
4
7
34
67
100
Output:
4
7
34
67
100
Section End/Start
Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).
We have passed the variable 'a' and array values to the function.
Inside the function, value 2 is added with the variable 'a' and array values in the index 0 and 1 are changed with different values.
The added value is not printed inside the main function. This is because 'a' is a single variable.
But for an array, when we try to print inside the main function the values get changed. This is because while passing an array it's reference will be passed.