0% found this document useful (0 votes)
3 views

day10ArrayTask

The document contains multiple Java programming tasks, including sorting an array in ascending and descending order, finding the nth maximum and minimum values, inserting an element at a specified index, counting the frequency of elements in an array, and identifying missing elements in a range of consecutive integers. Each task is accompanied by code implementations and example inputs/outputs. The overall focus is on array manipulation and basic algorithmic operations in Java.

Uploaded by

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

day10ArrayTask

The document contains multiple Java programming tasks, including sorting an array in ascending and descending order, finding the nth maximum and minimum values, inserting an element at a specified index, counting the frequency of elements in an array, and identifying missing elements in a range of consecutive integers. Each task is accompanied by code implementations and example inputs/outputs. The overall focus is on array manipulation and basic algorithmic operations in Java.

Uploaded by

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

Que->1

------
Sort the array In Ace and Dec order
Input as : [5,1,2,3,9,13,7]
In ACE
Output as: [1,2,3,5,7,9,13]
In DEC
Output as:[13,9,7,5,3,2,1]
Ans:
import java.util.Scanner;
import java.util.Arrays;
public class MainClass{
public static void sortArray(int []arr){
for(int i=0; i<arr.length; i++) for(int j=i; j<arr.length; j++)
if(arr[i]>arr[j]){
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
System.out.println("Asscending order array: " + Arrays.toString(arr));
for(int i=0; i<arr.length/2; i++){
int t=arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = t;
}
System.out.println("Descending order array: " + Arrays.toString(arr));
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: "); int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + size + " elements:");
for(int i=0; i<size; i++) arr[i] = sc.nextInt();
sortArray(arr);
}
}
-----------------------------------------------------------------------------------
----------------
Que->2
------
Find nth max and nth min
[5,1,2,3,9,13,7]
nth min=3
Output as : 3
nth max=4
Output as : 5
Ans:
import java.util.Scanner;
public class MainClass{
public static void findNthMaxMin(int []arr, int nthMin, int nthMax){
int t = nthMin;
int min = arr[0];
int max = arr[0];
for(int i=1; i<arr.length; i++){
if(min>arr[i]) min = arr[i];
if(max<arr[i]) max = arr[i];
}
while(min<=max){
for(int i=0; i<arr.length; i++) if(arr[i] == min) {t--; break;}
if(t==0) {System.out.println(nthMin + " min value: " + min);
break;}
min++;
}
t = nthMax;
while(max>=min){
for(int i=0; i<arr.length; i++) if(arr[i] == max) {t--; break;}
if(t==0) {System.out.println(nthMax + " max value: " + max);
break;}
max--;
}
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: "); int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + size + " elements:");
for(int i=0; i<size; i++) arr[i] = sc.nextInt();
System.out.println("Enter nth min to find: "); int nthMin =
sc.nextInt();
System.out.println("Enter nth max to find: "); int nthMax =
sc.nextInt();
findNthMaxMin(arr, nthMin, nthMax);
}
}
-----------------------------------------------------------------------------------
----------------
Que->3
------
Insert the element based on the user choice element and user choice index ??
Input as [ 1,2,3,4,5,6 ]
int element=100;
int index=3;
Ouput as [ 1 ,2 100 ,3,4,5,6]
Ans:
import java.util.Scanner;
import java.util.Arrays;
public class MainClass{
public static int[] insertValueAtGivenIndex(int []arr, int index, int value){
int []result = new int[arr.length+1];
int j=0;
for(int i=0; i<result.length; i++){
if(i==index-1) result[i] = value;
else result[i] = arr[j++];
}
return arr=result;
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: "); int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + size + " elements:");
for(int i=0; i<size; i++) arr[i] = sc.nextInt();
System.out.println("Enter index number: "); int index = sc.nextInt();
System.out.println("Enter value: "); int value = sc.nextInt();
System.out.println("Array after inserting value: " +
Arrays.toString(insertValueAtGivenIndex(arr, index, value)));
}
}
-----------------------------------------------------------------------------------
----------------
Que->4
------
Find the Frequency of the array element (without sorting the array element)
Input as:[ 1,2,3,1,5,2,6,1]
Output as: 1 -------- 3 times
2 -------- 2 times
3 -------- 1 times
5 -------- 1 times
6 -------- 1 times
Ans:
import java.util.Scanner;
public class MainClass{
public static void findFrequencyOfElements(int []arr){
for(int i=0; i<arr.length; i++){
int count = 1;
for(int j=i+1; j<arr.length; j++) if(arr[i]!='*' &&
arr[i]==arr[j]) {count++; arr[j]='*';}
if(arr[i]!='*') System.out.println(arr[i] + " ------- " + count +
" times.");
}
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: "); int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + size + " elements:");
for(int i=0; i<size; i++) arr[i] = sc.nextInt();
findFrequencyOfElements(arr);
}
}
-----------------------------------------------------------------------------------
----------------
Que-> 5
-------
Write a Java program to find and display the missing elements in a given integer
array, where the array contains consecutive numbers except for some missing
elements.
You are provided with an unsorted array of integers, and your task is to:
Sort the array.
Identify and print all missing elements between the minimum and maximum value of
the array.
If the smallest number in the array is greater than 1, prepend 1 to the array and
treat it as the starting point to find missing numbers.
Requirements:
The array is unsorted and may have gaps between consecutive elements.
If the smallest number in the array is greater than 1, include 1 and check for
missing numbers starting from 1.
Print all the missing numbers between the smallest and largest elements in the
sorted array.
The program should handle arrays of varying sizes.
Example:
Input 1:
int [] arr = {2, 10, 5, 8, 3, 9};
Output 1:
Original array: [2, 10, 5, 8, 3, 9]
Sorted array: [2, 3, 5, 8, 9, 10]
Modified array (if 1 is missing): [1, 2, 3, 5, 8, 9, 10]
The missing element is 1
The missing element is 4
The missing element is 6
The missing element is 7
Input 2:
int [] arr = {1, 2, 4, 6};
Output:
Original array: [1, 2, 4, 6]
Sorted array: [1, 2, 4, 6]
The missing element is 3
The missing element is 5
Ans:

import java.util.Scanner;
import java.util.Arrays;
public class MainClass{
public static void printMissingElements(int []arr){
Arrays.sort(arr);
int value = 1;
for(int i=0; i<arr.length; i++){
while(!(value==arr[i])) System.out.println("The missing element
is " + value++);
value++;
}
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size: "); int size = sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter " + size + " elements:");
for(int i=0; i<size; i++) arr[i] = sc.nextInt();
printMissingElements(arr);
}
}

You might also like