Posts

Showing posts from 2020

Java Question Data structure 07

Image
Consider the following array of sorted integers.  10,15,25,30,33,34,46,55,78,84,96,99  Using binary search algorithm, search for 23. //Q7.java public class Q7 {      public static void binarySearch ( int [] arr , int num )     {                   //initialize low mid and high          int low = 0 ;          int high = arr . length - 1 ;          int mid = 0 ;                   //binary search logic          while (low <= high)         {             mid = (low + high) / 2 ;      ...

Java Question Data structure 04

Image
Given a two-dimensional array, write a program to print it out in spiral order,  Ex: 1 2   3   4        5 6   7   8         9 10 11 12         13 14 15 16  Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10     //Q4.java public class Q4 { public static void main ( String args []) { int [][] mat = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 } }; int top = 0 , bottom = mat . length - 1 ; int left = 0 , right = mat[ 0 ]. length - 1 ; while ( true ) { if (left > right) { break ; } for ( int i = left ; i <= right ; i ++ ) { System . out . print (mat[top][i] ...

Download source files