DEV Community

Neelakandan R
Neelakandan R

Posted on

Missing number,Reverse Number,subarray with a given sum

Missing number:

package afterfeb13; public class missingnum { public static void main(String[] args) { int[] arr = { 10, 20, 30, 40, 50, 70, 80, 90, 110 }; for (int i = 1; i < arr.length; i++) { if (arr[i] == arr[i - 1] + 10) { continue; } else { System.out.println(arr[i] - 10); break; } } } } 
Enter fullscreen mode Exit fullscreen mode

Output:60

Reverse Number: using new array

package afterfeb13; public class reversenumber { public static void main(String[] args) { int[] a = { 10, 20, 30, 40, 50 }; int[] b = new int[a.length]; int j = a.length - 1; for (int i = 0; i < a.length; i++) { b[i] = a[j]; j--; } for (int i = 0; i < a.length; i++) { System.out.print(b[i] + " "); } } } 
Enter fullscreen mode Exit fullscreen mode

Output:

50 40 30 20 10

Reverse Number

package afterfeb13; public class reversenumber1 { public static void main(String[] args) { int[] a = { 10, 20, 30, 40, 50 }; int len = a.length - 1; int i = 0; while (i < len) { int tem = a[i]; a[i] = a[len]; a[len] = tem; i++; len--; } for (i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } } 
Enter fullscreen mode Exit fullscreen mode

Output:

50 40 30 20 10

Find subarray with a given sum

package afterfeb13; public class subarry { public static void main(String[] args) { int[] a = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; int num = 110; for (int j = 0; j < a.length; j++) { for (int i = j + 1; i < a.length; i++) { if (a[j] + a[i] == num) { System.out.println(a[j] + " + " + a[i] + " =" + num); } } } } } 
Enter fullscreen mode Exit fullscreen mode

Output:
10 + 100 =110
20 + 90 =110
30 + 80 =110
40 + 70 =110
50 + 60 =110

Roating Rigth to left

package afterfeb13; public class roatingrl { public static void main(String[] args) { int[] arr = { 10, 20, 30, 40, 50 }; int temp = arr[arr.length - 1]; for (int i = arr.length - 1; i > 0; i--) { arr[i] = arr[i - 1];// rigth to left arr[4]=arr[3],arr[3]=arr[2] } arr[0] = temp; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } 
Enter fullscreen mode Exit fullscreen mode

Output:50 10 20 30 40

Roating left to right

package afterfeb13; public class roatinglr { public static void main(String[] args) { int[] arr = { 10, 20, 30, 40, 50 }; int temp=arr[0];//0 for(int i =0; i < arr.length-1; i++) { arr[i]=arr[i+1];// left to rigth arr[0]=arr[1],arr[1]=arr[2] } arr[arr.length-1]=temp;//10 for(int i=0;i<arr.length;i++) { System.out.print(arr[i]+" "); } } } 
Enter fullscreen mode Exit fullscreen mode

output:20 30 40 50 10

Top comments (0)