0% found this document useful (0 votes)
5 views11 pages

Question Array3

The document contains a collection of Java programming exercises focused on array manipulation, searching algorithms, sorting techniques, and matrix operations. Each exercise includes a brief description and corresponding code snippets to implement various functionalities such as counting even/odd numbers, finding the second largest element, and performing binary search. It serves as a practical guide for learning core Java concepts and improving programming skills.

Uploaded by

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

Question Array3

The document contains a collection of Java programming exercises focused on array manipulation, searching algorithms, sorting techniques, and matrix operations. Each exercise includes a brief description and corresponding code snippets to implement various functionalities such as counting even/odd numbers, finding the second largest element, and performing binary search. It serves as a practical guide for learning core Java concepts and improving programming skills.

Uploaded by

singhr61685
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Core java by Mahendra Sir

[Link] a Java program to count the number of even and odd elements in
an array.
int even = 0, odd = 0;
for(int num : arr){
if(num % 2 == 0) even++;
else odd++;
}
[Link]("Even: " + even + ", Odd: " + odd);

i
[Link] a program to find the second largest element in an array.

ah
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for(int num : arr){
if(num > first){
second = first;
first = num;

M
} else if(num > second && num != first){
second = num;
}
}
ith
[Link](second);

[Link] a program to reverse an array without using another array.


for(int i=0;i<[Link]/2;i++){
eW

int temp = arr[i];


arr[i] = arr[[Link]-i-1];
arr[[Link]-i-1] = temp;
}

[Link] a program to check whether a given element exists in the array


(linear search).
od

int key = 5, index = -1;


for(int i=0;i<[Link];i++){
if(arr[i]==key){
index = i;
C

break;
}
}
[Link](index);

[Link] a program to count how many times a given number appears in an


array.
Input : {2,3,4,2,5,2,7,2,8} target : 2
output : 4
int count = 0;

Don’t give up. Finish what you have [Link] 1


Core java by Mahendra Sir

for(int num : arr){


if(num == key) count++;
}
[Link](count);

[Link] a program to copy elements from one array to another.


int[] copy = new int[[Link]];
for(int i=0;i<[Link];i++){
copy[i] = arr[i];

i
}

ah
[Link] a program to print all elements present at even index
positions.
for(int i=0;i<[Link];i+=2){
[Link](arr[i] + " ");

M
}

[Link] a program to find the average of array elements.


int sum = 0;
ith
for(int num : arr) sum += num;
double avg = (double)sum / [Link];
[Link](avg);
eW

[Link] a program to find the difference between largest and smallest


element.
int max = arr[0], min = arr[0];
for(int num : arr){
if(num > max) max = num;
if(num < min) min = num;
}
od

[Link](max - min);

[Link] a program to check whether an array is sorted in ascending


order.
C

boolean sorted = true;


for(int i=0;i<[Link]-1;i++){
if(arr[i] > arr[i+1]){
sorted = false;
break;
}
}
[Link](sorted);

Don’t give up. Finish what you have [Link] 2


Core java by Mahendra Sir

Searching & Basic Patterns

[Link] a program to perform binary search on a sorted array.


int left = 0, right = [Link]-1;
while(left <= right){
int mid = (left+right)/2;
if(arr[mid] == key) return mid;
else if(arr[mid] < key) left = mid+1;

i
else right = mid-1;

ah
}
return -1;

[Link] a program to find the first repeating element.

M
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i]==arr[j]){
ith
[Link](arr[i]);
return;
}
}
eW

[Link] a program to find the first non-repeating element.


for(int i=0;i<[Link];i++){
int count = 0;
for(int j=0;j<[Link];j++){
od

if(arr[i]==arr[j]) count++;
}
if(count==1){
[Link](arr[i]);
C

break;
}
}

Array Manipulation

[Link] a program to rotate array left by k positions.


int k = 2;
for(int i=0;i<k;i++){
Don’t give up. Finish what you have [Link] 3
Core java by Mahendra Sir

int first = arr[0];


for(int j=0;j<[Link]-1;j++){
arr[j] = arr[j+1];
}
arr[[Link]-1] = first;
}

[Link] a program to rotate array right by k positions.

i
for(int i=0;i<k;i++){

ah
int last = arr[[Link]-1];
for(int j=[Link]-1;j>0;j--){
arr[j] = arr[j-1];
}

M
arr[0] = last;
} ith
[Link] a program to move all zeroes to the end of the array.
int index = 0;
for(int num : arr){
if(num != 0) arr[index++] = num;
eW

}
while(index < [Link]){
arr[index++] = 0;
}

[Link] a program to move all negative numbers to one side.


od

int j = 0;
for(int i=0;i<[Link];i++){
if(arr[i] < 0){
int temp = arr[i];
C

arr[i] = arr[j];
arr[j] = temp;
j++;
}
}

Duplicate / Unique

Don’t give up. Finish what you have [Link] 4


Core java by Mahendra Sir

[Link] a program to remove duplicates from a sorted array.


int j = 0;
for(int i=0;i<[Link]-1;i++){
if(arr[i] != arr[i+1]){
arr[j++] = arr[i];
}
}
arr[j++] = arr[[Link]-1];

i
ah
[Link] a program to print all duplicate elements.
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i]==arr[j]){

M
[Link](arr[i]);
break;
}
}
ith
}

[Link] a program to count unique elements.


eW

int count = 0;
for(int i=0;i<[Link];i++){
boolean unique = true;
for(int j=0;j<[Link];j++){
if(i!=j && arr[i]==arr[j]){
unique = false;
od

break;
}
}
if(unique) count++;
C

}
[Link](count);

Subarray Concepts
[Link] a program to find the sum of all subarrays.
int total = 0;
for(int i=0;i<[Link];i++){
for(int j=i;j<[Link];j++){

Don’t give up. Finish what you have [Link] 5


Core java by Mahendra Sir

for(int k=i;k<=j;k++){
total += arr[k];
}
}
}
[Link](total);

[Link] a program to find the maximum subarray sum (Kadane’s

i
concept).

ah
int maxSum = arr[0], curr = arr[0];
for(int i=1;i<[Link];i++){
curr = [Link](arr[i], curr+arr[i]);
maxSum = [Link](maxSum, curr);

M
}
[Link](maxSum); ith
[Link] a program to find the subarray with given sum.
for(int i=0;i<[Link];i++){
int sum = 0;
for(int j=i;j<[Link];j++){
eW

sum += arr[j];
if(sum == target){
[Link](i + " to " + j);
}
}
}
od

🔹 Sorting Basics
[Link] a program to sort an array using bubble sort.
C

for(int i=0;i<[Link]-1;i++){
for(int j=0;j<[Link]-i-1;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}

Don’t give up. Finish what you have [Link] 6


Core java by Mahendra Sir

[Link] a program to sort using selection sort.


for(int i=0;i<[Link];i++){
int min = i;
for(int j=i+1;j<[Link];j++){
if(arr[j] < arr[min]) min = j;
}

i
int temp = arr[min];

ah
arr[min] = arr[i];
arr[i] = temp;
}

M
[Link] a program to sort using insertion sort.
for(int i=1;i<[Link];i++){
int key = arr[i];
int j = i-1;
ith
while(j>=0 && arr[j]>key){
arr[j+1] = arr[j];
j--;
eW

}
arr[j+1] = key;
}

Pair / Combination Logic


od

[Link] a program to find all pairs whose sum equals a given number.
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i]+arr[j]==target){
C

[Link](arr[i]+" "+arr[j]);
}
}
}

[Link] a program to find a pair with minimum difference.


int minDiff = Integer.MAX_VALUE;
for(int i=0;i<[Link];i++){

Don’t give up. Finish what you have [Link] 7


Core java by Mahendra Sir

for(int j=i+1;j<[Link];j++){
minDiff = [Link](minDiff, [Link](arr[i]-arr[j]));
}
}
[Link](minDiff);

🔹 Frequency / Counting

i
[Link] a program to find the frequency of each element.

ah
boolean[] visited = new boolean[[Link]];
for(int i=0;i<[Link];i++){
if(visited[i]) continue;
int count = 1;

M
for(int j=i+1;j<[Link];j++){
if(arr[i]==arr[j]){
count++;
visited[j] = true;
ith
}
}
[Link](arr[i]+" -> "+count);
eW

}
[Link] a program to find the majority element (> n/2 times).
for(int i=0;i<[Link];i++){
int count = 0;
for(int j=0;j<[Link];j++){
if(arr[i]==arr[j]) count++;
od

}
if(count > [Link]/2){
[Link](arr[i]);
break;
C

}
}

Misc Logical Problems

[Link] a program to find missing number in array (1 to n).


int n = [Link] + 1;
int sum = n*(n+1)/2;
for(int num : arr) sum -= num;

Don’t give up. Finish what you have [Link] 8


Core java by Mahendra Sir

[Link](sum);

[Link] a program to find intersection of two arrays.


for(int i=0;i<[Link];i++){
for(int j=0;j<[Link];j++){
if(arr1[i]==arr2[j]){
[Link](arr1[i]);
break;

i
}

ah
}
}

[Link] a program to find union of two arrays.

M
for(int num : arr1) [Link](num+" ");
for(int num : arr2){
boolean found = false;
for(int x : arr1){
ith
if(x==num){ found=true; break; }
}
if(!found) [Link](num+" ");
eW

[Link] a program to check if two arrays are equal.


boolean equal = true;
for(int i=0;i<[Link];i++){
if(arr1[i]!=arr2[i]){
od

equal = false;
break;
}
}
C

[Link](equal);

[Link] a program to merge two sorted arrays.


int i=0,j=0,k=0;
int[] res = new int[[Link]+[Link]];
while(i<[Link] && j<[Link]){
if(arr1[i]<arr2[j]) res[k++]=arr1[i++];
else res[k++]=arr2[j++];

Don’t give up. Finish what you have [Link] 9


Core java by Mahendra Sir

}
while(i<[Link]) res[k++]=arr1[i++];
while(j<[Link]) res[k++]=arr2[j++];

2D Arrays (Important for interviews)

[Link] a program to find row-wise sum in a matrix.


for(int i=0;i<[Link];i++){

i
int sum=0;

ah
for(int j=0;j<mat[i].length;j++){
sum += mat[i][j];
}
[Link](sum);

M
}

[Link] a program to find column-wise sum.


ith
for(int j=0;j<mat[0].length;j++){
int sum=0;
for(int i=0;i<[Link];i++){
sum += mat[i][j];
eW

}
[Link](sum);
}

[Link] a program to transpose a matrix.


for(int i=0;i<[Link];i++){
od

for(int j=i;j<mat[0].length;j++){
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
C

}
}

🔹 Pattern-based Thinking
[Link] a program to find leaders in array (element greater than all
right elements).
int max = arr[[Link]-1];
[Link](max+" ");

Don’t give up. Finish what you have [Link] 10


Core java by Mahendra Sir

for(int i=[Link]-2;i>=0;i--){
if(arr[i] > max){
max = arr[i];
[Link](max+" ");
}
}

[Link] a program to find peak element.

i
for(int i=0;i<[Link];i++){

ah
if((i==0 || arr[i]>=arr[i-1]) &&
(i==[Link]-1 || arr[i]>=arr[i+1])){
[Link](arr[i]);
break;

M
}
} ith
eW
od
C

Don’t give up. Finish what you have [Link] 11

You might also like