Skip to main content

Posts

Showing posts with the label Algorithms

Selection Sort Program in java

 Selection Sort Program in java Code: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ int[] arr = {7,2,4,1,5,3}; selection(arr); for(int i : arr){ System.out.print(i+ " "); } } public static void selection(int[] arr){ for(int i=0;i<arr.length;i++){ int minVal = arr[i]; int ind = i; for(int j= i+1;j<arr.length;j++){ if(arr[j] < minVal){ ind = j; minVal = arr[j]; } } //swap with minimum  int temp = arr[i]; arr[i] = minVal; arr[ind] = temp; } } }

Sequence Equation Hackerrank Solution - java

 Sequence Equation 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 'permutationEquation' function below.      *      * The function is expected to return an INTEGER_ARRAY.      * The function accepts INTEGER_ARRAY p as parameter.      */      public   static  List<Integer> permutationEquation(List<Integer> p) {          // p = [4 3 5 1 2]           //     1 2 3 4 5          //1)size of arry          int  n = p.size(); //5          //2)find index of 1 to n in arraylist p         ArrayList<Integer> al1 =  new  ArrayList<>();          for ( int  i= 1 ;i<=n;i++){       

A Very Big Sum Hackerrank Solution - java

 A Very Big Sum 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 'aVeryBigSum' function below.      *      * The function is expected to return a LONG_INTEGER.      * The function accepts LONG_INTEGER_ARRAY ar as parameter.      */     public static long aVeryBigSum(List<Long> ar) {         long sum = 0;         for(int i=0;i<ar.size();i++){             sum = sum + ar.get(i);         }         return sum;     } } public class Solution {     public static void main(String[] args) throws IOException {         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));         Buf

Maximum Perimeter Triangle Hackerrank Solution - java

 Maximum Perimeter Triangle 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 'maximumPerimeterTriangle' function below.      *      * The function is expected to return an INTEGER_ARRAY.      * The function accepts INTEGER_ARRAY sticks as parameter.      */      public   static  List<Integer> maximumPerimeterTriangle(List<Integer> sticks) {         Collections.sort(sticks);         ArrayList<Integer> al =  new  ArrayList<>();          int  i = sticks.size()- 3 ;          while (i>= 0 ){              if (sticks.get(i)+sticks.get(i+ 1 )>sticks.get(i+ 2 )){            

Two Strings Hackerrank Solution - java

 Two Strings 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 'twoStrings' function below.      *      * The function is expected to return a STRING.      * The function accepts following parameters:      *  1. STRING s1      *  2. STRING s2      */     public static String twoStrings(String s1, String s2) {         HashSet<Character> hs = new HashSet<>();         for(Character ch : s1.toCharArray()){             hs.add(ch);         }         for(Character ch : s2.toCharArray()){             if(hs.contains(ch)){                 return "YES";             }         }         return "N

Minimum Absolute Difference in an Array HackerRank Solution - java

Minimum Absolute Difference in an Array HackerRank Solution - java   For Explanation Watch Video :  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 'minimumAbsoluteDifference' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY arr as parameter.      */      public   static   int  minimumAbsoluteDifference(List<Integer> arr) {         Integer min = Integer.MAX_VALUE;         Collections.sort(arr);          for ( int  i= 1 ;i<arr.size();i++){              int  diff = Math.abs(arr.get(i- 1 )-arr.get(i));              if (diff<min){                 min = diff;             }         

Max Min Hackerrank Solution - java

 Max Min 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 'maxMin' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER k      *  2. INTEGER_ARRAY arr      */      public   static   int  maxMin( int  k, List<Integer> arr) {         Collections.sort(arr);         Integer min = Integer.MAX_VALUE;          for ( int  i= 0 ;i<=arr.size()-k;i++){              int  diff = arr.get(i+k- 1 ) - arr.get(i);              if (diff<min){                 min = diff;             }         }          retur

Cats and a Mouse Hackerrank Solution - java

Cats and a Mouse 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.regex.*; public class Solution {     // Complete the catAndMouse function below.     static String catAndMouse(int x, int y, int z) {         int aCat = Math.abs(z-x);         int bCat = Math.abs(z-y);         if(aCat==bCat){             return "Mouse C";         }else if(aCat<bCat){             return "Cat A";         }else{             return "Cat B";         }     }     private static final Scanner scanner = new Scanner(System.in);     public static void main(String[] args) throws IOException {         BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));         int q = scanner.nextInt();         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");         for (in

CamelCase Hackerrank Solution - java

CamelCase 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 'camelcase' function below.      *      * The function is expected to return an INTEGER.      * The function accepts STRING s as parameter.      */     public static int camelcase(String s) {//saveChangesInTheEditor        long count = s.chars().filter(ch->ch>=65 && ch<=90).count();        return (int)(count+1);     } } public class Solution {     public static void main(String[] args) throws IOException {         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));         BufferedWriter bufferedWriter

Simple Array Sum Hackerrank Solution - java

 Simple Array Sum 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 'simpleArraySum' function below.      *      * The function is expected to return an INTEGER.      * The function accepts INTEGER_ARRAY ar as parameter.      */     public static int simpleArraySum(List<Integer> ar) {     // Write your code here     int sum = 0;     for(int i=0;i<ar.size();i++){         sum = sum + ar.get(i);     }     return sum;     } } public class Solution {     public static void main(String[] args) throws IOException {         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));  

Drawing Book Hackerrank Solution - Java

Drawing Book 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 'pageCount' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER n      *  2. INTEGER p      */      public   static   int  pageCount( int  n,  int  p) {          return  Math.min(p/ 2 ,(n/ 2 )-(p/ 2 ));     } } public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {         BufferedReader bufferedReader =  new  BufferedReader( new  InputStreamReader(System.in));         BufferedWriter bufferedWriter =

Counting Valleys Hackerrank Solution - Java

 Counting Valleys 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 'countingValleys' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER steps      *  2. STRING path      */      public   static   int  countingValleys( int  steps, String path) {          int  sum =  0 ;          int  vally =  0 ;          for ( int  i= 0 ;i<steps;i++){              if (path.charAt(i)== 'U' ){                  if (sum==- 1 ){                     vally++;                 }                 sum = sum +  1 ;       

Hackerrank Grading Students Solution - Java

Hackerrank Grading Students 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 'gradingStudents' function below.      *      * The function is expected to return an INTEGER_ARRAY.      * The function accepts INTEGER_ARRAY grades as parameter.      */      public   static  List<Integer> gradingStudents(List<Integer> grades) {         ArrayList<Integer> al =  new  ArrayList<>();          for ( int  i= 0 ;i<grades.size();i++){              int  mark = grades.get(i);              if (mark< 38 ){                 al.add(mark);             } else {                  int  multiple = (mark

Hackerrank Breaking the Records Solution - Java

Hackerrank Breaking the Records 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 'breakingRecords' function below.      *      * The function is expected to return an INTEGER_ARRAY.      * The function accepts INTEGER_ARRAY scores as parameter.      */      public   static  List<Integer> breakingRecords(List<Integer> scores) {          int  max_count =  0 ;          int  min_count =  0 ;          int  max_value = scores.get( 0 );          int  min_value = scores.get( 0 );          for ( int  i= 1 ;i<scores.size();i++){              int  val = scores.get(i);              if (val>max_value

Hackerrank Subarray Division Solution - Java

 Hackerrank Subarray Division 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 'birthday' function below.      *      * The function is expected to return an INTEGER.      * The function accepts following parameters:      *  1. INTEGER_ARRAY s      *  2. INTEGER d      *  3. INTEGER m      */      public   static   int  birthday(List<Integer> s,  int  d,  int  m) {          int  ans =  0 ;          for ( int  i= 0 ;i<=s.size()-m;i++){              int  sum =  0 ;              for ( int  j=i;j<i+m;j++){                 sum = sum + s.get(j);             }              if (sum==d){