Skip to main content

Posts

Showing posts from September, 2020

Find the nth term in the mixture of two series

 Consider the series:- 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64 ...........and so on. Find the nth term in the given series ?  ex:-   9 th term       print 16            6 th term       print  9 Note:- We have to print only nth term not the whole series up to nth term. Explanation:- The given series is a combination of two geometric progression series. First series ( odd positions ) :-  1, 2, 4, 8, 16, 32, 64, ........and so on.                                                               2 0  ,   2 1  ,   2 2  ,   2 3  ,   2 4  ,   2 5  ,……. Second series (  even positions ) :- 1, 3, 9, 27, 81, 243,..... and so on.                                                             3 0  ,  3 1  ,   3 2  ,   3 3  ,   3 4  ,   3 5 …….     Java 1 2 3 4 5 6 7 8 9 10 11 import java.util.Scanner; public class Series{ public static void main (S

Quick Sort Algorithm

  QuickSort is a Divide and Conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. Java public class Sort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 , 8 }; quickSort(arr, 0 , arr. length - 1 ); for ( int x:arr) System. out . print (x+ " " ); } public static int partition ( int arr[], int low, int high){ int pivot = arr[high]; int i = (low- 1 ); for ( int j=low; j<high; j++){ if (arr[j] <= pivot){ i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i+ 1 ]; arr[i+ 1 ] = arr[high]; arr[high] = temp; return i+ 1 ; } public static void quickSort (

Insertion Sort Algorithm

The array is virtually split into a sorted and an unsorted part. Values from the unsorted part is picked and placed at the correct position in the sorted part. Complexity:- Worst-case and average-case:-  O ( n 2  ) best case:- O(n) Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class InsertionSort{ public static void main (String []args){ int [] arr = { 3 , 5 , 1 , 6 , 2 , 4 }; for ( int i= 1 ; i<arr. length ; i++){ int temp = arr[i]; int j = i - 1 ; while (j>= 0 && arr[j]>temp){ arr[j+ 1 ] = arr[j]; j--; } arr[j+ 1 ] = temp; } for ( int x:arr) System. out . print (x+ " " ); } } Python def insertion_sort(arra

Binary Search Algorithm

Binary search compares the target value to the middle element of the array.  If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half,  again taking the middle element to compare to the target value, and repeating this until the target value is found.  If the search ends with the remaining half being empty, the target is not in the array. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 public class BinarySearch{ public static void main (String []args){ int [] arr = { 1 , 2 , 4 , 5 , 7 , 11 , 12 , 15 }; int target = 7 ; System. out . println (binarySearchRec(arr, target, 0 , arr. length - 1 )); System. out . println (binarySearchIter(arr, target)); } // Using recursion public static boolean binarySearchRec ( int [] arr, int t

Linear Search Algorithm

It is the simplest searching algorithm. A sequential search is made over all items one by one. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Serach{ public static void main (String[] args){ int [] arr = { 3 , 2 , 5 , 4 , 7 , 9 , 1 }; int target = 9 ; System. out . println (linearSearch(arr,target)); } public static String linearSearch ( int [] arr, int target){ for ( int i= 0 ; i<arr. length - 1 ; i++){ if (arr[i]==target) return "found at index " +i; } return "not found" ; } }

Bubble Sort Algorithm

 Bubble sort is one of the simplest sorting algorithms.    It is a comparison-based sorting algorithm.  In this sorting algorithm, each pair of adjacent elements is compared and swapped if they are not in order.  Disadvantage:-   Not suitable for large data sets. Average and worst-case complexity = O(n^2) Normal code:- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class BubbleSort{ public static void main (String[] args){ int [] arr = { 3 , 2 , 5 , 4 , 7 , 9 , 1 }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j= 0 ; j<arr. length - 1 -i; j++){ if (arr[j]>arr[j+ 1 ]){ int temp = arr[j]; arr[j] = arr[j+ 1 ]; arr[j+ 1 ] = temp; } } } for ( int x:arr) System. out . print (x+ " " ); } } Optimize Code:- public class B

Reverse each word of sentence without changing their order

   Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import java.util.Scanner; public class RevWordSen{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); String sentence = sc. nextLine (); String[] arr = sentence. split ( " " ); for ( int i= 0 ; i<=arr. length - 1 ; i++) arr[i] = reverse(arr[i]); String result = String. join ( " " ,arr); System. out . println (result); } public static String reverse (String str){ String rev = "" ; for ( int i=str. length ()- 1 ; i>= 0 ; i--) rev += str. charAt (i) ; return rev; } }  Python sentence = input ( "Enter a sentence" ); str_arr = list (sentence . split( " " )) result = "" for item in str_arr: result += item[:: - 1 ] + " "

Check whether a number is Seed of another number

 A number P is said to be the seed of another number Q if multiplying P with its digits equates to Q. Ex:-  123 is seed of 738 as    123 * 1 * 2 * 3 = 738  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Seed{ public static void main (String[] args){ int num1 = 123 , num2 = 738 ; num1 = Math. min (num1,num2); num2 = Math. max (num1,num2); System. out . println (checkSeed(num1, num2)); } public static boolean checkSeed ( int num1, int num2){ int seed = num1; while (num1> 0 ){ seed = seed * (num1% 10 ); num1 = num1 / 10 ; } if (seed==num2) return true ; else return false ; } } Python def checkSeed(num1,num2): seed = num1 while num1 > 0 : seed = seed * (num1 % 10 ) num1 = num1 // 10 if seed == n

Print all possible rotations of a string

 Ex:- "code" ==>   "code"  "odec"  "deco"  "ecod"          "heist"  ==>   "heist"  "eisth"  "isthe"  "sthei"  "theis" Java 1 2 3 4 5 6 7 8 9 public class Rotation{ public static void main (String[] args){ String str = "codeheist" ; for ( int i= 0 ; i<str. length (); i++){ String ans = str. substring (i,str. length ()) + str. substring ( 0 ,i); System. out . println (ans); } } } Python input_str = "codeheist" for i in range ( 0 , len (input_str)): print ( input_str [ i : len (input_str) ] + input_str [ 0 : i ] )

Write a method to Mirror Encrypt a String

 Mirror Encrypt: a -- z       b -- y     c -- x    d -- w  ............... y -- b     z -- a A -- Z      B --- Y   C -- X   D -- W  ................ Y -- B     Z -- A EX:-    abcd ==> zyxw              AnT   ==> ZmG Ternary or conditional Operator Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class MirrorString{ public static void main (String[] args){ String str = "codeheist" ; System. out . println (mirrorEncrypt(str)); } public static String mirrorEncrypt (String str){ String ans = "" ; for ( int i= 0 ; i<str. length (); i++){ char ch = str. charAt (i); ans += Character. isUpperCase (ch) ? ( char )( 'Z' + 'A' -ch) : ( char )( 'z' + 'a' -ch); } return ans; } } Python def mirrorEncrypt(input_str): ans = ""

Char sum

                         Java 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.Scanner; public class CharSum{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "Enter a string:" ); String str = sc. next (); int sum = 0 ; for ( int i= 0 ; i<str. length (); i++){ sum += (( int )str. charAt (i) - 96 ); } System. out . print ( "Char sum: " + sum); } } Python input_str = input ( "Enter a string:" ) sum = 0 for char in input_str: sum += ord (char) - 96 print ( "char sum : " , sum )

Find the Nth root of a number

Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.Scanner; public class Root{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int a = sc. nextInt (); int n = sc. nextInt (); System. out . println (nthRoot(n,a)); } public static int nthRoot ( int n, int a){ for ( int i= 1 ; i<=Math. sqrt (a); i++){ if (a==( int )Math. pow (i,n)) return i; } return - 1 ; } } Python import math def nthRoot(n,a): for i in range ( 1 , int (math . sqrt(a) + 1 )): if math . pow(i, n) == a: return i return - 1 a = int ( input ( "Enter n:" )) n = int ( input ( "Enter a:" )) print (nthRoot(n,a))

Find the trailing zeroes in factorial of a given number

Ex:- 5 ! = 120 here, the trailing zero is only 1.         10 ! = 3628800 here, trailing zeroes are 2.   Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.Scanner; public class FactTrailZero{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); System. out . println ( "enter a number:" ); int num = sc. nextInt (); int ans = 0 ; while (num!= 0 ){ ans += (num / 5 ); num = num / 10 ; } System. out . println ( "Trailing zeroes:" +ans); } } Python num = int ( input ( "Enter a number:" )) ans = 0 while num != 0 : ans += (num // 5 ) num = num // 10 print ( "Trailing zeroes:" ,ans)

Write a program to generate all Pythagorean triplets within a given limit

 For a, b, c to be a Pythagorean triplet  a*a + b*b should be equals to c*c  i.e.  a*a + b*b == c*c Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.Scanner; public class PythagoreanTriplet{ public static void main (String []args){ Scanner sc = new Scanner(System. in ); int limit = sc. nextInt (); for ( int a= 1 ; a<limit; a++) { for ( int b=a+ 1 ; b<limit; b++) { for ( int c=b+ 1 ; c<=limit; c++) { if (a*a+b*b==c*c) { System. out . println (a+ " " +b+ " " +c); break ; } } } } } } Python limit = int ( input ( "Enter a limit:"