Skip to main content

Posts

Showing posts with the label list

Find the second smallest element from an array or list

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Array2ndSmallest{ public static void main (String []args){ int [] arr = { 6 , 2 , 3 , 4 , 5 , 1 }; int first = Integer. MAX_VALUE ; int second = Integer. MIN_VALUE ; for ( int i= 0 ; i<arr. length ; i++){ if (arr[i]<first){ second = first; first = arr[i]; } else if (arr[i]<second && arr[i]!=first) second = arr[i]; } System. out . println ( "second smallest:" +second); } } Python import sys arr = [ 6 , 2 , 3 , 4 , 5 , 1 ]; first = sys . maxsize second = sys . maxsize for i in range ( 0 , len (arr)): if arr[i] < first: second = first first = arr[i] elif arr[i] < second and arr[i] != first: second = arr[i] print ( "second smal...

Reverse an array in place in Java or list in Python

Ex:-      [ 1, 2, 3, 4, 5]  after reversing  [ 5, 4, 3, 2, 1]   Here, " in place "   means you can't take another array to reverse. Java 1 2 3 4 5 6 7 8 9 10 11 12 public class ArrayReverse{ public static void main (String []args){ int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 }; for ( int i= 0 ; i<arr. length / 2 ; i++){ int temp = arr[i]; arr[i] = arr[arr. length -i- 1 ]; arr[arr. length -i- 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python given_list = [ 1 , 2 , 3 , 4 , 5 , 6 ] given_list . reverse() print (given_list)

Find all pairs of an integer array or list whose sum is equal to a given number

Ex:-   [ 2, 4, 3, 5, 1, 6, 0 ]     and  5                      (2, 3)  ==> 2 + 3 = 5                      (4, 1) ==> 4 + 1 = 5                       (5, 0) ==> 5 + 0 = 5 Java 1 2 3 4 5 6 7 8 9 10 public class ArraySum { public static void main (String []args){ int [] arr = { 2 , 4 , 3 , 5 , 1 , 6 , 0 }; int given_sum = 5 ; for ( int i= 0 ; i<arr. length ; i++) for ( int j=i+ 1 ; j<arr. length ; j++) if (arr[i]+arr[j]==given_sum) System. out . println (arr[i]+ " " +arr[j]); } } Python given_list = [ 2 , 4 , 3 , 5 , 1 , 6 , 0 ] given_sum = 5 for i in rang...

Remove the duplicate number from a given integer array or list.

Removing the duplicate elements from an array is the same as printing the unique elements of an array. Notes:- If we write              boolean[ ]  flag = new boolean[ size ] ;               in java, it creates  an array of booleans where false present in all positions by default.               We don't have to assign value to flag array elements exclusively.  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class RemoveDuplicate{ public static void main (String []args){ int [] arr = { 1 , 2 , 3 , 6 , 3 , 3 , 3 , 4 , 2 }; boolean [] flag = new boolean [arr. length ]; for ( int i= 0 ; i<arr. length ; i++) if (!flag[i]) for ( int j=i+ 1 ; j<arr. length ; j++) ...

Find all the missing number in a given integer array(or list) of 1 to 10.

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Array1to10{ public static void main (String []args){ int [] arr = { 4 , 5 , 1 , 3 , 6 , 7 , 9 }; int j; for ( int i= 1 ; i<= 10 ; i++){ for (j= 0 ; j<arr. length ; j++){ if (i==arr[j]){ break ; } } if (j==arr. length ) System. out . print (i+ " " ); } } } Python arr = [ 4 , 5 , 1 , 3 , 6 , 7 , 9 ] for i in range ( 1 , 11 ): if i not in arr: print (i, end = " " )