Skip to content

Sankych25/Java-basic-DSA-Programs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java-basic-DSA-Programs

Index program001 Hello world program in java Hello World!! program002 Addition of two numbers input given by user 15 25 15+25=40 program003 Get input from user as a string and print it. Enter a string : sanket sanket program004 Print number from 0 upto n using for loop. Enter value for n : 6 0 1 2 3 4 5 6 program005 Print number from 0 to 10 using while loop. Enter value for n : 5 0 1 2 3 4 5 program005_1 Print number from 0 to 10 using do while loop. Enter value for n : 5 0 1 2 3 4 5 program006 print sum of first n natural numbers Enter value for n : 10 Sum of first 10 natural number is 55 program006_1 print sum of first n natural numbers(recursion) Enter value for n : 5 sum of first 5 natural number is : 15 program007 print table of a number input by the user Which number`s table you want : 15 Table of a 15 number is : 15 30 45 60 75 90 105 120 135 150 program008 Rectangle * pattern input by the user Please give dimension of rectangle Number of rows : 4 Number of columns : 9 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * program009 Hollow Rectangle * pattern input by the user Please give dimension of rectangle Number of rows : 8 Number of columns : 9 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * program010 half pyramid * pattern input by the user Enter Number of rows & columns : 6 * * * * * * * * * * * * * * * * * * * * * program011 inverted half pyramid * pattern input by the user Number of rows & columns : 6 * * * * * * * * * * * * * * * * * * * * * program012 mirror half pyramid * pattern input by the user
Number of rows & columns : 5
* ** *** **** ***** program013 half pyramid number pattern input by the user Number of rows & columns : 6 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 program014 inverted half pyramid of number pattern input by the user Number of rows & columns : 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 program015 floydsTriangle pattern input by the user Number of rows & columns : 4 1 2 3 4 5 6 7 8 9 10 program016 half pyramid of 0 and 1 input by the user Number of rows & columns : 4 1 0 1 1 0 1 0 1 0 1 program017 butterfly * pattern input by the user Number of rows & columns : 4 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * program018 solid rhombus * pattern input by the user Number of rows & columns : 5 * * * * * * * * * * * * * * * * * * * * * * * * * program019 number pyramid pattern input by the user Number of rows & columns : 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 program020 palindromic pyramid pattern input by the user Number of rows & columns : 5 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 program021 Diamond * pattern input by the user Number of rows & columns : 4 * * * * * * * * * * * * * * * * * * * * * * * * * program022 Print factorial of a given number Enter a Number : 5 Factorial of 5 is : 120 program022_1 Print factorial of a given number(recursion) Enter a Number : 5 Factorial of 5 is : 120 program023 Get input from user for 2D array and print it. Enter the number of rows : 2 Enter the number of cols : 2 Enter element at position (0,0): 10 Enter element at position (0,1): 20 Enter element at position (1,0): 30 Enter element at position (1,1): 12 The 2D array is : 10 20 30 12 program024 Get input from user as string and reverse it. Enter a string to reverse : sanket Reverse string is : teknas program024_1 Get input from user as string and reverse it.(use recursion) Enter a string to reverse : sanket Reverse string is : teknas program025 Bubble sort algorithm Bubble Sort Enter the size of array : 5 Enter the element at position 0 : 45 Enter the element at position 1 : 25 Enter the element at position 2 : 68 Enter the element at position 3 : 62 Enter the element at position 4 : 54 pass 1 : 25 45 62 54 68 pass 2 : 25 45 54 62 68 pass 3 : 25 45 54 62 68 pass 4 : 25 45 54 62 68 Sorted array is : 25 45 54 62 68 program026 Selection sort algorithm Selection Sort Enter the size of array : 3 Enter the element at position 0 : 45 Enter the element at position 1 : 25 Enter the element at position 2 : 36 pass 1 : 25 45 36 pass 2 : 25 36 45 Sorted array is : 25 36 45 program027 Insertion sort algorithm Insrtion Sort Enter the size of array : 4 Enter the element at position 0 : 25 Enter the element at position 1 : 35 Enter the element at position 2 : 65 Enter the element at position 3 : 45 pass 1 : 25 35 65 45 pass 2 : 25 35 65 45 pass 3 : 25 35 65 45 pass 4 : 25 35 45 65 Sorted array is : 25 35 45 65 program028 Quick sort algorithm Quick Sort Enter the size of array : 6 Enter the element at position 0 : 6 Enter the element at position 1 : 3 Enter the element at position 2 : 9 Enter the element at position 3 : 5 Enter the element at position 4 : 2 Enter the element at position 5 : 8 Sorted array is : 2 3 5 6 8 9 program029 Merge sort algorithm Merge Sort Enter the size of array : 6 Enter the element at position 0 : 6 Enter the element at position 1 : 3 Enter the element at position 2 : 9 Enter the element at position 3 : 5 Enter the element at position 4 : 2 Enter the element at position 5 : 8 Sorted array is : 2 3 5 6 8 9 program030 Tower of Honoi Enter the number of disc : 3 Transfer disc 1 from S to D Transfer disc 2 from S to H Transfer disc 1 from D to H Transfer disc 3 from S to D Transfer disc 1 from H to S Transfer disc 2 from H to D Transfer disc 1 from S to D program031 First and last occurance of an element in string(recursion) for ex : aabfvaabgdffdgvsvbdg Enter a string : entertainment Enter a character for finding occurance : e First occurance of a character e is at position : 0 Last occurance of a character e is at position : 10 program032 Check if array is sorted or not (Strictly increasing) Enter size of arr : 5 Enter element at position 0 : 12 Enter element at position 1 : 45 Enter element at position 2 : 67 Enter element at position 3 : 89
Enter element at position 4 : 99 Array is sorted!! program033 move all "X" to the end of the string(recursion) Enter a string : entertainment Enter a character which move at the end : e Output : ntrtainmnteee program034 remove duplicate characters from a string(recursion) Enter a string : entertainment Output : entraim program035 Print all the subsequents of a string(recursion) Enter a string : abc abc ab ac a bc b c ' ' program036 Print all the unique subsequences of a string(recursion) aaa aa a ' ' program037 Print keypad combinations(recursion) Press keys of a keypad : 23 dg dh di eg eh ei fg fh fi program038 print fibonacci series upto nth term.(recursion) Enter value for n : 8 Fibonacci series upto 8 th term : 0 1 1 2 3 5 8 13 program039 print X^n(recursion)(stack hight n) Enter value for X : 3 Enter value for n : 3 3^3 is : 27 program039_1 print X^n(recursion)(stack hight log n) Enter value for X : 2 Enter value for n : 5 2^5 is : 32 program040 print all combinations of a string(recursion & backtracking) Enter a string : abc abc acb bac bca cab cba program041 count total path in maze to move from (0,0) to (n,m)(recursion & backtracking) Enter a Dimension for maze Enter number of rows : 3 Enter number of columns : 3 Total paths are 6 to move from (0,0) to (3,3) program042 Place tiles of size 1m in a floor of size nm(recursion & backtracking) Enter a Dimension of floor Total length of floor : 6 Total width of floor : 2 Total 13 ways are available program043 Find the number of ways in which you can invite n people to your party, single or pair(recursion & backtracking) Enter number of guest you want to invite : 4 Total 10 ways are available to invite guest. progarm044 Print all the subset of first n natural numbers(recursion & backtracking) Enter value for n : 3 3 2 1 3 2 3 1 3 2 1 7 2 1 '' program045 Print all solutions where N queens are safe in N*N chessboard 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 program046 Create simple binary tree Data of root node is : 2 program047 write a code to solve sudoku 3 1 6 5 7 8 4 9 2 5 2 9 1 3 4 7 6 8 4 8 7 6 2 9 5 3 1 2 6 3 4 1 5 9 8 7 9 7 4 8 6 3 1 2 5 8 5 1 7 9 2 6 4 3 1 3 8 9 4 7 2 5 6 6 9 2 3 5 1 8 7 4 7 4 5 2 8 6 3 1 9 program048 write a code to print tree data in preorder Tree data in preorder : 1-2-4-5-3-6- program049 write a code to print tree data in inorder Tree data in inorder : 4-2-5-1-3-6- program050 Write a code to print tree data in postorder Tree data in postorder : 4-5-2-6-3-1- program051 write a code to print tree data in level order Tree data in level order : 1 2 3 4 5 6 program052 write a code to count nodes of a binary tree Total nodes are : 6 program053 write a code to sum of nodes of a binary tree Total sum of nodes are : 21 program054 write a code to print height of a binary tree Total height of tree is : 3 program055 write a code to print diameter of a binary tree Total diameter of tree is : 5 program055_1 write a code to print diameter of a binary tree Total diameter of tree is : 5 program056 queue using array(add,remove,peek function) Enter size of a queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 1 --> 2 --> 3 --> 4 --> 5 program057 circular queue using array Enter size of a queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 1 --> 2 --> 3 --> 4 --> 5 program058 queue using linkedlist Enter size of a queue : 6 Enter element at position 0 : 1 Enter element at position 1 : 4 Enter element at position 2 : 5 Enter element at position 3 : 2 Enter element at position 4 : 3 Enter element at position 5 : 6 1 --> 4 --> 5 --> 2 --> 3 --> 6 program059 queue using linkedlist 2 Enter size of a queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 6 Enter element at position 4 : 9 1 --> 2 --> 3 --> 6 --> 9 program060 queue using ArrayDeque Enter size of a queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 6 Enter element at position 4 : 5 1 --> 2 --> 3 --> 6 --> 5 program061 Queue using 2 stack Enter size of a queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 1 --> 2 --> 3 --> 4 --> 5 program062 stack using LinkedList Enter size of a Queue : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 5 --> 4 --> 3 --> 2 --> 1 program063 stack using ArrayList Enter size of a stack : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 5 --> 4 --> 3 --> 2 --> 1 program064 Stack using java collection framework Enter size of a stack : 5 Enter element at position 0 : 12 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 5 --> 4 --> 3 --> 2 --> 12 program065 Write a code to push the element at the bottom of stack Enter size of a stack : 4 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element which you want to push at bottom : 5 4 --> 3 --> 2 --> 1 --> 5 program066 Write a code to Reverse stack Enter size of a stack : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 1 --> 2 --> 3 --> 4 --> 5 program067 create linkedList and perform basic operation deleteFirst --> addFirst --> addLast --> printList --> deleteLast --> Null Size of linkedList is : 5 addFirst --> addLast --> printList --> Null program068 write a code to reverse a linked List(iterative method) deleteFirst --> addFirst --> addLast --> printList --> deleteLast --> Null deleteLast --> printList --> addLast --> addFirst --> deleteFirst --> Null OKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKKOKOKOKOKOKOKJOKOKOKOKOK OKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKOKKOKOKOKOKOKOKJOKOKOKOKOK program068_1 write a code to reverse a linked List(recursive method) deleteFirst --> addFirst --> addLast --> printList --> deleteLast --> Null deleteLast --> printList --> addLast --> addFirst --> deleteFirst --> Null program068_2 write a code to reverse a linked List(collection framework method) How many element you want to add ? : 5 Enter element at position 0 :1 Enter element at position 1 :2 Enter element at position 2 :3 Enter element at position 3 :4 Enter element at position 4 :5 Your linledlist is : [1, 2, 3, 4, 5] reverse linkedlist is : [5, 4, 3, 2, 1] program069 write a code to n th node fron last int linkedList Original List: 1 2 3 4 5 List After Removing 2nd Node From End: 1 2 3 5

program070 write a code to check list is palindrome or not Linked List: 1 2 2 1 Is Palindrome: true program071 write a code to detect loop in linkedList Has Cycle: true Has Cycle: false program072 Basic function of ArrayList How many number you want to add : 4 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 6 3 4 Sorted list is : 3 4 6 program073 write a code to perform basic funcion on hashmap(collection framework) {USA=30, China=150, UK=100, India=120} True 150 USA : 30 China : 150 UK : 100 India : 160 program074 write a code to perform basic function on hashmap(iternally) using array of linkedlist US 150 India 150 China 140 program074_1 write a code to perform basic function on hashmap(iternally) using array of linkedlist US 150 India 150 China 140 After removing 'India': US 150 China 140 program075 Mejoruty element --> give an array of size n, find all elements that appears more than n/3 times How many elements you have to enter : 9 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 1 Enter element at position 3 : 3 Enter element at position 4 : 4 Enter element at position 5 : 1 Enter element at position 6 : 5 Enter element at position 7 : 1 Enter element at position 8 : 6 Majority element is : 1 program076 Union of 2 array using hashset
How many elements you have to enter in array1 : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 1 Enter element at position 4 : 4 How many elements you have to enter in array2 : 5 Enter element at position 0 : 4 Enter element at position 1 : 5 Enter element at position 2 : 6 Enter element at position 3 : 7 Enter element at position 4 : 7 Union of array1 & array2 is : [1, 2, 3, 4, 5, 6, 7] total unique elements are in array1 & array2 : 7 program077 Intersection of 2 arrays using hashset How many elements you have to enter in array1 : 5 Enter element at position 0 : 1 Enter element at position 1 : 2 Enter element at position 2 : 3 Enter element at position 3 : 4 Enter element at position 4 : 5 How many elements you have to enter in array2 : 5 Enter element at position 0 : 3 Enter element at position 1 : 4 Enter element at position 2 : 5 Enter element at position 3 : 6 Enter element at position 4 : 7 Common elements are : 3, 4, 5, total common elements are in array1 & array2 : 3 program078 find itinerary path from tickets using hashmap How many route you have : 5 Enter source for route 1 : chennai Enter a destination for chennai : bengaluru Enter source for route 2 : mumbai Enter a destination for mumbai : pune Enter source for route 3 : pune Enter a destination for pune : delhi Enter source for route 4 : goa Enter a destination for goa : chennai Enter source for route 5 : delhi Enter a destination for delhi : goa mumbai --> pune --> delhi --> goa --> chennai --> bengaluru program079 find sub-array which sum is equal to k How many element you have to add in array : 10 Enter element at 0 position : 1 Enter element at 1 position : 2 Enter element at 2 position : 3 Enter element at 3 position : 4 Enter element at 4 position : 5 Enter element at 5 position : 6 Enter element at 6 position : 7 Enter element at 7 position : 8 Enter element at 8 position : 9 Enter element at 9 position : 10 Enter sum of sub-Array : 9 There are total 3 subarray which sum is equal to 9 program080 write a code to implement trie datastructure and insert element into it and print Trie element are : the, a, there, ap, app, thee, appl, like, apply, samsung, apple, program081 write a code to search element in trie datastructure Trie element are : the, a, there, ap, app, thee, appl, like, apply, samsung, apple, Enter element to search in trie : their false Enter element to search in trie : apple true program082 create function for trie datastructure which returns true if there is a previously inserted string word that has the prefix, and false otherwise. Trie element are : a, banana, app, appl, apply, ap, apple, Enter string for Starts with problem : app true Enter string for Starts with problem : grapes false program083 given an input string and a dictionary of words . find out if the input string can be broken into a space - separated sequence of dictionary words. Trie element are : the, a, there, their, any, thee, i, like, sam, samsung, mobile, Enter string for Starts with problem : ilikesam true Enter string for Starts with problem : ilikeanything false program084 given a string of length n of lowercase alphabets characters, we need to count total number of distinct substrings of this string. Enter string for count distinct substring : qwerty 22 program085 find longest string in words such that every prefix of its also in words. Trie element are : the, a, there, ap, app, thee, appl, like, apply, samsung, apple, Longest word is apple which all substring are present in Trie program086 write a code to create nodes of graph and print nodes using bfs method in graph(Breadth first search) Output of graph using BFS(Connected components) : 0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6 -- program087 write a code to create nodes of graph and print nodes using bfs method in graph(Breadth first search) Output of graph using BFS(Disconnected components) : 0 -- 1 -- 2 -- 3 -- 4 -- program088 write a code to create nodes of graph and print nodes using dfs method in graph(depth first search)[connected graph] Output of graph using DFS(Connected components) : 0 -- 1 -- 3 -- 4 -- 2 -- 5 -- 6 -- program089 write a code to find all paths from source to target using dfs method in graph(depth first search) All paths from 0 to 5 : 0 -- 1 -- 3 -- 4 -- 5 0 -- 1 -- 3 -- 5 0 -- 2 -- 4 -- 3 -- 5 0 -- 2 -- 4 -- 5 program090 write a code to detect Cycle in directed graph Cycle is detected in directed graph
Cycle is not detected in directed graph program091 write a code for topological sorting in graph(only for directed acyclic graph[DAG]) Topological sorting order for directed graph : 5 4 2 3 1 0 program092 write a code to detect cycle in undirected graph Cycle is not present in undirected garph
Cycle id detected in undirected graph program093 Shortest path algorithm(Dijkstra's algorithm) in directed weighted graph Shortest path using Dijkstras algorithm is : 0 2 3 8 6 9 program094 Shortest distance in directed weighted graph using Bellman Ford algorithm Shortest distance form 0 to each node : 0 2 -2 0 4
program095 Minimum Spanning Tree(MST) Min cost of MST : 55 program096 Strongly Connected components SCC : 0 1 2 SCC : 3 SCC : 4 program097 Bridge in graphs Bridge is : 0 ---- 3 program098 Articulation point Ap is : 0 Ap is : 3

About

JAVA DSA programs on graph, array list, map, set , etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages