Posts

Showing posts from April, 2021

I PATTERN SUM IN 2D ARRAY

  Context Given a    2D Array ,  : 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  representation: a b c d e f g Output Format Print the maximum hourglass sum in  .  Input 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 2 4 4 0 0 0 0 2 0 0 0 0 1 2 4 0  Output 19 Explanation  contains the following hourglasses: 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 2 0 2 4 2 4 4 4 4 0 1 1 1 1 1 0 1 0 0 0 0 0 0 2 4 4 0 0 0 0 0 2 0 2 0 2 0 0 0 0 2 0 2 4 2 4 4 4 4 0 0 0 2 0 0 0 1 0 1 2 1 2 4 2 4 0 The hourglass with the maximum sum ( ) is: 2 4 4 2 1 2 4 SOLUTION: package   T wo DA rrays ; import   java . util .*; public   class   IpatternSum  {      public   static   void   main ( String   args []...

TWIN STRINGS PROBLEM

 TWIN STRINGS PROBLEM I/P: 3 cdab dcba abcd 3 abcd abcd abcdcd O/P: Yes No No SOLUTION: package   S trings ; import   java . util .*; public   class   TwinStrings  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   iNum = sc . nextInt ();          String   s []=  new   String [ iNum ];          for ( int   i = 0 ; i < iNum ; i ++)         {              s [ i ]= sc . next ();         }          int   a = sc . nextInt ();    ...

MERGE TWO STRINGS ALTERNATIVELY

 MERGE TWO STRINGS ALTERNATIVELY I/P: az bsd O/P: abzsd SOLUTION: package   S trings ; import   java . util .*; public   class   MergeString  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          String   s1 = sc . next ();          String   s2 = sc . next ();          int   i = 0 , j = 0 ;          while ( i < s1 . length () ||  j < s2 . length ())         {              if ( i < s1 . length ())             {    ...

JUMPING CLOUD PROBLEM IN JAVA

 JUMPING CLOUD PROBLEM IN JAVA I/P: 7 0 0 1 0 0 1 0 O/P: 4 I/P: 6 0 0 0 0 1 0 O/P: 3 SOLUTION: package   A rrays ; import   java . util .*; class   JumpingCloud   {       public   static   void   main  ( String []  args )      {          Scanner   sc = new   Scanner ( System . in );          int   iNum = sc . nextInt ();          int   arr []= new   int [ iNum ];          for ( int   i = 0 ; i < arr . length ; i ++)         {              arr [ i ]= sc . nextInt ();         }          int   count = 0 ;  ...

PRINT THE INDEX OF SECOND LARGEST NUMBER IN THE ARRAY

 Print the index of the second largest number in the positive array.If second largest number duplicates       print -1 Scenerio 1: I/P: 6 18 11 12 13 16 11 O/P: 4 I/P : 6 1 3 3 2 5 5 O/P: -1 SOLUTION: package   A rrays ; import   java . util .*; public   class   SecondLargest  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   l = sc . nextInt ();          int   arr []= new   int [ l ];          int   a2 []= new   int [ l ];          for ( int   i = 0 ; i < l ; i ++)         {        ...

SPIRAL MATRIX IN CLOCKWISE DIRECTION

 SPIRAL MATRIX IN CLOCKWISE DIRECTION I/P: 3 3 1 2 3 4 5 6 7 8 9 O/P: 4 1 2 7 5 3 8 9 6 SOLUTION: package   A rrays ; import   java . util .*; public   class   SpiralClockwise  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   iR = sc . nextInt ();          int   iC = sc . nextInt ();          int   t = iR , t1 = iC ;          int   arr [][]= new   int [ iR ][ iC ];          for ( int   i = 0 ; i < iR ; i ++)         {              for ( int...

SPIRAL MATRIX PROBLEM

 SPIRAL MATRIX PROBLEM IN JAVA 4 4 1   2   3   4 5   6   7   8 9 10  11  12 13 14 15 16 O/P: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 SOLUTION: package   A rrays ; import   java . util .*; public   class   SpiralMatrix  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   iR = sc . nextInt ();          int   iC = sc . nextInt ();          int   arr [][]= new   int [ iR ][ iC ];          for ( int   i = 0 ; i < iR ; i ++)         {         ...

Some prime numbers can be expressed as a sum of other consecutives prime numbers

 Some prime numbers can be expressed as a sum of other consecutives prime numbers for example, 5=2+3, 17=2+3+5+7 41=2+3+5+7+11+13 You task is to find out how many prime numbers which satisfy the property are present in the range 3 to N subject to a constraint that summation should always start with number 2. Write a code to find out the number of prime numbers that satisfy the above mentioned property in a given range I/P: 13 O/P: 2 5 17 41 SOLUTION: package   N umbers ; import   java . util .*; public   class   primewithinrange  {      public   static   boolean   isPrime ( int   n ) {          int   count  =  0 ;          for ( int   i  =  2  ;  i  <=  n  ;  i ++) {              if ( n  %  i  ...

SORT THE STRING BASED ON FREQUENCY

 PROGRAM TO SORT THE STRING BASED ON FREQUENCY I/P: hi hello hi everyone O/P: everyone hello hi hi SOLUTION: import   java . util .*; public   class   SortTheStringbasedonFrequency  {       public   static   void   main ( String []  args ) {       Scanner   sc = new   Scanner ( System . in );       String   s1 = sc . nextLine ();       String   sArr []= s1 . split ( " " );       ArrayList < String >  a1 = new   ArrayList <>();       ArrayList < Integer >  a2 = new   ArrayList <>();       for ( int   i = 0 ; i < sArr . length ; i ++)      {        a1 . add ( sArr [ i ]);      }       for ...

PROGRAM TO COUNT CONSECUTIVE BINARY ONES IN A STRING

PROGRAM TO COUNT CONSECUTIVE BINARY ONES IN A STRING  Input 1 5 Output 1 1  Input 2 13  Output 2 2 SOLUTION: package   N umbers ; import   java . util .*; public   class   Countofconsecutive1sinBinary  {      public   static   void   main ( String   args [])     {          Scanner   sc = new   Scanner ( System . in );          int   n = sc . nextInt ();          char []  binary  =  Integer . toBinaryString ( n ). toCharArray ();          int   tmpCount  =  0 ;          int   maxCount  =  0 ;          for ( int   i  =  0 ;  i  <  binary . length ;  i ++){ ...