Posts

Showing posts with the label Array Exercises

Find Second Smallest Element In An Array | Java Programs

Output: *** Second Smallest Number In Array *** Original Array is: [-10, 42, 90, 21, 72, -3] Second smallest number is : -3 Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class SecondSmallestInArray { public static void main ( String [] args ) { int [] array = {-10, 42, 90, 21, 72, -3}; System . out . println ( "*** Second Smallest Number In Array ***" + "\n" ); System . out . println ( "Original Array is: " + Arrays . toString ( array )); int min = Integer . MAX_VALUE ; int secondMin = Integer . MAX_VALUE ; for ( int i = 0; i < array . length ; i ++) { if ( array [ i ] == min ) { secondMin = min ; } else if ( array [ i ] < min ) { secondMin = min ; min = array [ i ]; } else if ( array [ i ] < secon...

Find Second Largest Element In An Array | Java Programs

Output: *** Second Largest Number In An Array *** Original array is: [1, 2, 4, 7, 8, 3, 5, 10] Second largest number is : 8 Click Here For  Java Online Compiler Solution: import java.util.Arrays ; class SecondLargestInArray { public static void main ( String [] args ) { int [] array = {1, 2, 4, 7, 8, 3, 5, 10}; int max = array [0]; int secondMax = array [0]; System . out . println ( "*** Second Largest Number In An Array ***" + "\n" ); System . out . println ( "Original array is: " + Arrays . toString ( array )); for ( int i = 0; i < array . length ; i ++) { if ( array [ i ] > max ) { secondMax = max ; max = array [ i ]; } else if ( array [ i ] > secondMax ) { secondMax = array [ i ]; } } System . out . println...

Add Two Matrices (of same size) | Java Programs

Output: *** Addition Of Two Matrices *** Input number of rows of matrix: 2 Input number of columns of matrix: 2 Input elements of first matrix: 1 2 4 5 Input the elements of second matrix: 3 1 4 7 Sum of the matrices: - 4 3 8 12 Click Here For  Java Online Compiler Solution: import java.util.Scanner ; public class AddTwoMatrices { public static void main ( String args []) { int rows , columns , i , d ; Scanner scanner = new Scanner ( System . in ); System . out . println ( "*** Addition Of Two Matrices ***" + "\n" ); System . out . print ( "Input number of rows of matrix: " ); rows = scanner . nextInt (); System . out . print ( "Input number of columns of matrix: " ); columns = scanner . nextInt (); System . out . println (); int array1 [][] = new int [ rows ][ columns ]; int array2 [][]...

Find The Difference Between The Largest And Smallest Values In An Array Of Integers | Java Programs

Output: *** Difference Between Largest And Smallest Number In Array *** Original Array: [5, 7, 2, 4, 9] The Largest Number is: 9 The Smallest Number is: 2 Difference between the largest and smallest values of array: 7 Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class DifferenceLargestSmallest { public static void main ( String [] args ) { int [] array = {5, 7, 2, 4, 9}; System . out . println ( "*** Difference Between Largest And Smallest Number In Array ***" + "\n" ); System . out . println ( "Original Array: " + Arrays . toString ( array )); int max = array [0]; int min = array [0]; for ( int i = 1; i < array . length ; i ++) { if ( array [ i ] > max ) { max = array [ i ]; } else if ( array [ i ] < min ) { min = array [ i ]; ...

Check Whether An Array Contains An Specific Number | Java Programs

Output: *** Check Whether An Array Contain An Specific Number *** Array Elements Are: [1, 2, 3, 4, 5] Array Contains 2? Answer: true Array Contains 200? Answer: false Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class ArraySpecificValue { public static boolean contains ( int [] arr , int item ) { for ( int n : arr ) { if ( item == n ) { return true ; } } return false ; } public static void main ( String [] args ) { int [] array = {1, 2, 3, 4, 5}; System . out . println ( "*** Check Whether An Array Contain An Specific Number ***" + "\n" ); System . out . println ( "Array Elements Are: " + Arrays . toString ( array )); System . out . println ( "Array Contains 2?" ); System . out . println ( "Answer: " + contains ( array ,...

Bubble Sort Algorithm | Java Programs

Output: *** Bubble Sort Algorithm *** The Original Array is: [10, 2, 7, 11, 9] After applying BUBBLE SORT algorithm . The Array becomes: [2, 7, 9, 10, 11] Click Here For  Java Online Compiler Solution: import java.util.Arrays ; public class BubbleSortAlgorithm { public static void main ( String [] args ) { int [] myArray = {10, 2, 7, 11, 9}; System . out . println ( "*** Bubble Sort Algorithm ***" + "\n" ); System . out . println ( "The Original Array is: " + Arrays . toString ( myArray )); for ( int i = 0; i < myArray . length ; i ++) { for ( int j = 0; j < myArray . length - 1; j ++) { if ( myArray [ j ] > myArray [ j + 1]) { int a = myArray [ j + 1]; myArray [ j + 1] = myArray [ j ]; myArray [ j ] = a ; } } ...

Bring Zeros To The Front Of An Array - 2 | Java Programs

Output: *** Move Zeros Of An Array To Front *** Given array is: 1, 0, 2, 0, 3, 0, 4, 0, 5, Now it becomes: 0, 0, 0, 0, 1, 2, 3, 4, 5, Click Here For  Java Online Compiler Solution: public class MoveZerosToFront { static void moveZerosToFront ( int inputArray []) { int counter = inputArray . length - 1; for ( int i = inputArray . length - 1; i >= 0; i --) { if ( inputArray [ i ] != 0) { //Assigning inputArray[i] to inputArray[counter] inputArray [ counter ] = inputArray [ i ]; //Decrementing the counter by 1 counter --; } } //Assigning 0 to remaining elements while ( counter >= 0) { inputArray [ counter ] = 0; counter --; } System . out . print ( "Now it becomes: " ); for ( int i = 0; i < inputArray . leng...

Move Zeros To Front Of An Array | Java Programs

Output: *** Move Zeros Of An Array To Front *** Given array is: 1, 0, 2, 0, 3, 0, 4, 0, 5, Now it becomes: 0, 0, 0, 0, 1, 2, 3, 4, 5, Click Here For  Java Online Compiler Solution: public class MoveZerosToFront { static void moveZerosToFront ( int inputArray []) { int counter = inputArray . length - 1; for ( int i = inputArray . length - 1; i >= 0; i --) { if ( inputArray [ i ] != 0) { //Assigning inputArray[i] to inputArray[counter] inputArray [ counter ] = inputArray [ i ]; //Decrementing the counter by 1 counter --; } } //Assigning 0 to remaining elements while ( counter >= 0) { inputArray [ counter ] = 0; counter --; } System . out . print ( "Now it becomes: " ); for ( int i = 0; i < inputArray . leng...

Move Zeros To End Of An Array - 2 | Java Programs

Output: *** Move Zeros In An Array To End -2 *** Given array is: 1, 0, 2, 0, 3, 0, 4, 0, 5, Now it becomes: 1, 2, 3, 4, 5, 0, 0, 0, 0, Click Here For  Java Online Compiler Solution: class MoveZerosToEnd { static void moveZerosToEnd ( int inputArray []) { int counter = 0; for ( int i = 0; i < inputArray . length ; i ++) { if ( inputArray [ i ] != 0) { //Assigning inputArray[i] to inputArray[counter] inputArray [ counter ] = inputArray [ i ]; counter ++; } } while ( counter < inputArray . length ) { inputArray [ counter ] = 0; counter ++; } for ( int i = 0; i < inputArray . length ; i ++) { System . out . print ( inputArray [ i ] + ", " ); } } public static void main ( String [] args ) { int [] ...

Move Zeros To End Of An Array | Java Programs

Output: *** Move Zeros In An Array To End *** Given array is: 1, 0, 2, 0, 3, 0, 4, 0, 5, Now it becomes: 1, 2, 3, 4, 5, 0, 0, 0, 0, Click Here For  Java Online Compiler Solution: public class MoveZerosToEnd { public static void main ( String [] args ) { System . out . println ( "*** Move Zeros In An Array To End ***" + "\n" ); int [] array = {1, 0, 2, 0, 3, 0, 4, 0, 5}; int countZero = 0; System . out . print ( "Given array is: " ); for ( int i = 0; i < array . length ; i ++) { System . out . print ( array [ i ] + ", " ); } System . out . println (); System . out . print ( "Now it becomes: " ); for ( int i = 0; i < array . length ; i ++) { if ( array [ i ] == 0) { countZero ++; } else { System . out . p...