Skip to main content

Posts

Showing posts from September, 2021

Sherlock and Squares Hackerrank Solution - java

Sherlock and Squares 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  squares( int  a,  int  b) {          int  first = ( int )Math.ceil(Math.sqrt(a));          int  last = ( int )Math.floor(Math.sqrt(b));          return  last - first + 1 ;     } } public   class  Solution {      public   static   void  main(String[] args)  throws  IOException {         BufferedReader bufferedReader =  new  BufferedReader( new  InputStreamReader(System.in));         BufferedWriter bufferedWriter =  new  BufferedWriter( new  FileWriter(System.getenv( "OUTPUT_PATH" )));          int  q = Integer.parseInt(buffe

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }

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

Diagonal Difference Hackerrank Solution - java

 Diagonal Difference 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 'diagonalDifference' function below.      *      * The function is expected to return an INTEGER.      * The function accepts 2D_INTEGER_ARRAY arr as parameter.      */      public   static   int  diagonalDifference(List<List<Integer>> arr) {          int  size = arr.get( 0 ).size();          int  sum1 =  0 ;          for ( int  i= 0 ;i<size;i++){              for ( int  j= 0 ;j<size;j++){                  if (i==j){                     sum1 = sum1 + arr.get(i).get(j);                 }             }         }

The Blunder Hackerrank Solution - SQL

 The Blunder Hackerrank Solution - SQL For explanation watch video :: Code: select ceil(avg(salary)-avg(replace(salary,'0',''))) from Employees;

java hashset hackerrank solution - java 15

 java hashset hackerrank solution - java 15 for explanation watch video: code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int  t = scn.nextInt();         scn.nextLine();         HashSet<String> hs =  new  HashSet<>();          for ( int  i= 0 ;i<t;i++){             String s = scn.nextLine();             hs.add(s);             System.out.println(hs.size());         }     } }

java map hackerrank solution - Java 15

   java map hackerrank solution - Java 15 for explanation watch video : Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int  n = scn.nextInt();         scn.nextLine();         HashMap<String,Integer> hm =  new  HashMap<>();          for ( int  i= 0 ;i<n;i++){             String name = scn.nextLine();              int  mobile = scn.nextInt();             scn.nextLine();             hm.put(name,mobile);         }          while (scn.hasNext()){             String name = scn.nextLine();              if (hm.containsKey(name)){                 System.out.println(name+ "=" +hm.get(name));             } else {                 System.out.println( "Not found" );             }         }        } }

Welcome to Java! hackerrank solution - java

  Welcome to Java! hackerrank solution - java  for explanation watch video: Code: import java.io.*; import java.util.*; public class Solution {     public static void main(String[] args) {        System.out.println("Hello, World.");         System.out.println("Hello, Java.");     } }

Java Stdin and Stdout I hackerrank solution - java

 Java Stdin and Stdout I hackerrank solution - java  for explanation watch video: Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int  a = scn.nextInt();          int  b = scn.nextInt();          int  c = scn.nextInt();         System.out.println(a);         System.out.println(b);         System.out.println(c);     } }

Java Varargs - Simple Addition hackerrank Solution - Java

 Java Varargs - Simple Addition hackerrank Solution - Java  for explanation watch video : Code: import  java.io.*; import  java.util.*; public   class  Solution {      public   static   void  add( int ... arr){          int  sum =  0 ;          for ( int  i= 0 ;i<arr.length;i++){             sum = sum + arr[i];              if (i==arr.length- 1 ){                 System.out.print(arr[i]+ "=" );             } else {                 System.out.print(arr[i]+ "+" );             }         }         System.out.println(sum);     }      public   static   void  main(String[] args) {         Scanner scn =  new  Scanner(System.in);          int [] arr =  new   int [ 6 ];          for ( int  i= 0 ;i< 6 ;i++){             arr[i] = scn.nextInt();         }         add(arr[ 0 ],arr[ 1 ]);         add(arr[ 0 ],arr[ 1 ],arr[ 2 ]);         add(arr[ 0 ],arr[ 1 ],arr[ 2 ],arr[ 3 ],arr[ 4 ]);         add(arr[ 0 ],arr[ 1 ],arr[ 2 ],arr[ 3 ],arr[ 4 ],arr[ 5 ]);     } }

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 )){            

Weather Observation Station 13 Hackerrank Solution - SQL

 Weather Observation Station 13 Hackerrank Solution - SQL For Explanation Watch Video :  Code : select round(sum(LAT_N),4) from station where LAT_N > 38.7880 and LAT_N < 137.2345;

Top Earners Hackerrank Solution - SQL | Hackerrank SQL

 Top Earners Hackerrank Solution - SQL for explanation watch video : Code :: select max(salary * months ),count(*) from Employee where salary*months = (select max(salary*months) from Employee);

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++){      

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

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

Sum and Difference of Two Numbers Hackerrank Solution - C Language

 Sum and Difference of Two Numbers Hackerrank Solution - C Language  Code: #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > int  main() {      int  a,b;      float  c,d;     scanf( "%d %d %f %f" ,&a,&b,&c,&d);      int  sum1 = a+b;      int  diff1 = a-b;      float  sum2 = c+d;      float  diff2 = c-d;     printf( "%d %d\n" ,sum1,diff1);     printf( "%.1f %.1f" ,sum2,diff2);      return   0 ; }

Conditional Statements in C Hackerrank Solution - C Language

 Conditional Statements in C Hackerrank Solution - C Language Code: #include   < assert.h > #include   < limits.h > #include   < math.h > #include   < stdbool.h > #include   < stddef.h > #include   < stdint.h > #include   < stdio.h > #include   < stdlib.h > #include   < string.h > char * readline(); int  main() {      char * n_endptr;      char * n_str = readline();      int  n = strtol(n_str, &n_endptr,  10 );      if  (n_endptr == n_str || *n_endptr !=  ' \ 0 ' ) { exit(EXIT_FAILURE); }      // Write Your Code Here      if (n== 1 ){         printf( "one" );     } else   if (n== 2 ){         printf( "two" );     } else   if (n== 3 ){         printf( "three" );     } else   if (n== 4 ){         printf( "four" );     } else   if (n== 5 ){         printf( "five" );     } else   if (n== 6 ){         printf( "six" );     } else   if (n== 7 ){         printf( "s

1D Arrays in C Hackerrank Solution - C Language

  1D Arrays in C Hackerrank Solution - C Language  Code : #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > int  main() {      int  n;     scanf( "%d" ,&n);      int  arr[n];      int  sum =  0 ;      for ( int  i= 0 ;i<n;i++){         scanf( "%d" ,&arr[i]);         sum = sum + arr[i];     }       printf( "%d" ,sum);      return   0 ; }

Print the Elements of a Linked List Hackerrank Solution - java

 Print the Elements of a Linked List 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 {      static   class  SinglyLinkedListNode {          public   int  data;          public  SinglyLinkedListNode next;          public  SinglyLinkedListNode( int  nodeData) {              this .data = nodeData;              this .next = null;         }     }      static   class  SinglyLinkedList {          public  SinglyLinkedListNode head;          public  SinglyLinkedListNode tail;          public  SinglyLinkedList() {              this .head = null;              this .tail = null;         }          public   void  insertNode( int  nodeData) {             SinglyLinkedListNode node =  new  SinglyLinkedListNode(nodeData);              if  ( this .head == null) {                  this .head = node;   

Print in Reverse Hackerrank Solution - java

 Print in Reverse 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 {      static   class  SinglyLinkedListNode {          public   int  data;          public  SinglyLinkedListNode next;          public  SinglyLinkedListNode( int  nodeData) {              this .data = nodeData;              this .next = null;         }     }      static   class  SinglyLinkedList {          public  SinglyLinkedListNode head;          public  SinglyLinkedListNode tail;          public  SinglyLinkedList() {              this .head = null;              this .tail = null;         }          public   void  insertNode( int  nodeData) {             SinglyLinkedListNode node =  new  SinglyLinkedListNode(nodeData);              if  ( this .head == null) {                  this .head = node;             }  else  {

For Loop in C Hackerrank Solution - C Language

 For Loop in C Hackerrank Solution - C Language Code: #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > int  main()  {      int  a, b;     scanf( "%d\n%d" , &a, &b);      for ( int  i=a;i<=b;i++){       if (i>= 1  && i<= 9 ){           if (i== 1 ){              printf( "one\n" );          } else   if (i== 2 ){              printf( "two\n" );          } else   if (i== 3 ){              printf( "three\n" );          } else   if (i== 4 ){              printf( "four\n" );          } else   if (i== 5 ){              printf( "five\n" );          } else   if (i== 6 ){              printf( "six\n" );          } else   if (i== 7 ){              printf( "seven\n" );          } else   if (i== 8 ){              printf( "eight\n" );          } else   if (i== 9 ){              printf( "nine\n" );          }    

Sum of Digits of a Five Digit Number Hackerrank Solution - C

 Sum of Digits of a Five Digit Number Hackerrank Solution - C Code : #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > int  main() {           int  n;     scanf( "%d" , &n);      //Complete the code to calculate the sum of the five digits on n.      int  sum =  0 ;      while (n!= 0 ){          int  r = n% 10 ;         n = n/ 10 ;         sum = sum + r;     }     printf( "%d" ,sum);      return   0 ; }