Skip to main content

Posts

Showing posts with the label Algorithm

Modified Kaprekar Numbers Hackerrank Solution - java

 Modified Kaprekar Numbers Hackerrank Solution - java For explanation watch video : Code:: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'kaprekarNumbers' function below.      *      * The function accepts following parameters:      *  1. INTEGER p      *  2. INTEGER q      */     public static void kaprekarNumbers(int p, int q) {         ArrayList<Long> al = new ArrayList<>();         for(long i=p;i<=q;i++){             long sq = i*i;             String num = String.valueOf(i);             String s = String.valueOf(sq);             int leftLength = s.length() - num.length();             long leftNum = 0;             if(leftLength == 0){                 lef

equalize the array hackerrank solution in java

 equalize the array hackerrank solution in java for explanation watch video :: code:: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'equalizeArray' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY arr as parameter.      */     public static int equalizeArray(List<Integer> arr) {                 HashMap<Integer,Integer> hm = new HashMap<>();         for(int i=0;i<arr.size();i++){             hm.put(arr.get(i),hm.getOrDefault(arr.get(i),0)+1);         }                  int max = Integer.MIN_VALUE;         for(Map.Entry<Integer,Integer> e : hm.entrySet()){             int value

Library Fine Hackerrank Solution - java

 Library Fine Hackerrank Solution - java for explanation watch video : code: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'libraryFine' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER d1      *  2. INTEGER m1      *  3. INTEGER y1      *  4. INTEGER d2      *  5. INTEGER m2      *  6. INTEGER y2      */      public   static   int  libraryFine( int  d1,  int  m1,  int  y1,  int  d2,  int  m2,  int  y2) {          if (y1>y2){              return   10000 ;         } else   if (m1>m2 && y1==y2){              return   500  *(m1-m2);         } else  

Chocolate Feast Hackerrank Solution - java

Chocolate Feast Hackerrank Solution - java for explanation watch video : code: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'chocolateFeast' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER n      *  2. INTEGER c      *  3. INTEGER m      */     public static int chocolateFeast(int n, int c, int m) {         //no of  chocolate          int num = n/c;         int wrapper = num;         if(wrapper<m){             return num;         }else{             while(wrapper>=m){                 num = num + 1;                 wrapper = wrapper - m + 1;             }             ret

Grid Challenge Hackerrank Solution - java | Hackerrank Algorithm

 Grid Challenge Hackerrank Solution - java  For Explanation Watch Video :: Code :: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {           public   static  String sort(String s){          char [] ch = s.toCharArray();         Arrays.sort(ch);          return   new  String(ch);     }      public   static  String gridChallenge(List<String> grid) {                  ArrayList<String> al =  new  ArrayList<>();          for ( int  i= 0 ;i<grid.size();i++){             String s = sort(grid.get(i));             al.add(s);         }                 int  size = al.get( 0 ).length();          for ( int  i= 1 ;i<al.size();i++){              for ( int  j= 0 ;j<size;j++){      

Marc's Cakewalk Hackerrank Solution - Java | Hackerrank Algorithms

 Marc's Cakewalk Hackerrank Solution - Java  For Explanation watch video : Code : import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'marcsCakewalk' function below.      *      * The function is expected to return a LONG_INTEGER.      * The function accepts INTEGER_ARRAY calorie as parameter.      */      public   static   long  marcsCakewalk(List<Integer> calorie) {                Collections.sort(calorie,Collections.reverseOrder());          long  min_miles =  0 ;          for ( int  i= 0 ;i<calorie.size();i++){             min_miles = min_miles + (( long )Math.pow( 2 ,i)*calorie.get(i));         }          return  min_miles;     } } public   cl

Jumping on the Clouds Hackerrank Solution - java

 Jumping on the Clouds Hackerrank Solution - java for explanation Watch video : Code :  import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {         Scanner scn =  new  Scanner(System.in);          int  n = scn.nextInt();          int [] arr =  new   int [n];          for ( int  i= 0 ;i<n;i++){             arr[i] = scn.nextInt();         }          if (n== 2  || n== 3 ){             System.out.println( 1 );         } else { // n=6  0 0 0 0 1 0              int  i= 0 ;              int  count =  0 ;              while (i<=n- 2 ){                  if ((i+ 2 )<n && arr[i+ 2 ]!= 1 ){                    

Repeated String Hackerrank Solution - java

 Repeated String Hackerrank Solution - java  For Explanation Watch Video : Code :  import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      public   static   int  noOfA(String s, long  rem){          int  count =  0 ;          for ( int  i= 0 ;i<rem;i++){              if (s.charAt(i)== 'a' ){                 count++;             }         }          return  count;     }      public   static   long  repeatedString(String s,  long  n) {          int  a_count =  0 ;          for ( int  i= 0 ;i<s.length();i++){              if (s.charAt(i)== 'a' ){                 a_count++;             }         }          long  rem = n % s.length();          if (rem== 0 ){              return

Apple and Orange Hackerrank Solution - Java

 Apple and Orange Hackerrank Solution - Java For Explanation Watch Video: Code:: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'countApplesAndOranges' function below.      *      * The function accepts following parameters:      *  1. INTEGER s      *  2. INTEGER t      *  3. INTEGER a      *  4. INTEGER b      *  5. INTEGER_ARRAY apples      *  6. INTEGER_ARRAY oranges      */      public   static   void  countApplesAndOranges( int  s,  int  t,  int  a,  int  b, List<Integer> apples, List<Integer> oranges) {          int  count =  0 ;          for ( int  i= 0 ;i<apples.size();i++){              if (apples.get(i)+a>=s && apples.g

Mini-Max Sum Hackerrank Solution - java

 Mini-Max Sum Hackerrank Solution - java For Explanation Watch the video: Code:: import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'miniMaxSum' function below.      *      * The function accepts INTEGER_ARRAY arr as parameter.      */     public static void miniMaxSum(List<Integer> arr) {         Collections.sort(arr);         long sum = 0;         for(int i=0;i<arr.size();i++){             sum += arr.get(i);         }         long low_sum = sum - arr.get(arr.size()-1);         long hig_sum = sum - arr.get(0);         System.out.println(low_sum+" "+hig_sum);     } } public class Solution {     public static void main(String[] args) throws IOException {    

Mars Exploration Hackerrank Solution - java

 Mars Exploration Hackerrank Solution - java For Explanation watch video:  Code::   import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result {     /*      * Complete the 'marsExploration' function below.      *      * The function is expected to return an INTEGER.      * The function accepts STRING s as parameter.      */     public static int findDifferentLetters(String s){         int count = 0;         if(s.charAt(0)!='S'){             count++;         }          if(s.charAt(1)!='O'){             count++;         }          if(s.charAt(2)!='S'){             count++;         }         return count;     }     public static int marsExploration(String s) {         int ans = 0;         for

Picking Numbers Hackerrank Solution - java

 Picking Numbers Hackerrank Solution - java  Code:: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {          int [] arr =  new   int [ 100 ];         Scanner scn =  new  Scanner(System.in);          int  n = scn.nextInt();          for ( int  i= 0 ;i<n;i++){              int  num = scn.nextInt();             arr[num]++;         }          int  max =  0 ;          for ( int  i= 0 ;i< 99 ;i++){             max = Math.max(max,arr[i]+arr[i+ 1 ]);         }         System.out.println(max);     } }

Hackerrank Designer PDF Viewer Solution - Java

 Hackerrank Designer PDF Viewer Solution - Java For Explanation Watch Video:: Code:: import  java.io.*; import  java.math.*; import  java.security.*; import  java.text.*; import  java.util.*; import  java.util.concurrent.*; import  java.util.function.*; import  java.util.regex.*; import  java.util.stream.*; import   static  java.util.stream.Collectors.joining; import   static  java.util.stream.Collectors.toList; class  Result {      /*      * Complete the 'designerPdfViewer' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER_ARRAY h      *  2. STRING word      */      public   static   int  designerPdfViewer(List<Integer> h, String word) {          int  max = - 1 ;          for ( int  i= 0 ;i<word.length();i++){              char  ch = word.charAt(i);              int  ascii = ( int )ch -  97 ;              int  val = h.get(ascii);              if (val>max){