├── .github
├── pull_request_template.md
└── workflows
│ └── maven.yml
├── .gitignore
├── Admission
├── pom.xml
└── src
│ └── main
│ └── java
│ └── admission.java
├── Algorithms
├── BreadthFirstSearch.java
├── BubbleRecursion.java
├── CyclicSort.java
├── DepthFirstSearch.java
├── Dijkstra-Algorathim.java
├── Exponential_search.java
├── Fibonacci_search.java
├── Interpolation_search.java
├── Jump_search.java
├── KadaneAlgo.java
├── MountainArray.java
├── MutualRecursion.java
├── RotatedBinarySearchRecursion.java
├── SpecialNumber.java
├── bellman-ford-algorithm.java
├── boyer-more-majority-vote-algorithm.java
├── checkarrayrotation.java
├── flood-fill-algorithm.java
├── geometricSum.java
├── johnson-algorithm.java
├── leaderElement.java
├── minimumLengthWord.java
├── optimal_page_replacement.java
├── pom.xml
├── prims-algorithm.java
├── rabin-karp.java
├── selectionsort.java
├── src
│ └── main
│ │ └── java
│ │ ├── BinarySearch.java
│ │ ├── Bresenham_Line.java
│ │ ├── BubbleSort.java
│ │ ├── DDA_Line.java
│ │ ├── EuclieanDistance.java
│ │ ├── FindMinimumInRotatedSortedArray.java
│ │ ├── NKnights.java
│ │ ├── NQueens.java
│ │ ├── ThreeSum.java
│ │ ├── dynamic_programing
│ │ ├── 0 1 KNAPSACK.JAVA
│ │ ├── FactorialDP.java
│ │ ├── FibonacciNumber.java
│ │ ├── HouseRobber.java
│ │ └── ReadMe.md
│ │ ├── encryption
│ │ └── SimpleDESEncryptionAlgorithm.java
│ │ ├── kadane.java
│ │ ├── linearSearch.java
│ │ ├── matrix_operation
│ │ ├── Find2DMatrixSum.java
│ │ └── Matrix.java
│ │ ├── numbers
│ │ ├── ArmstrongNum.java
│ │ ├── CalculatePi.java
│ │ ├── Factorial.java
│ │ ├── HCF.java
│ │ ├── LCM.java
│ │ ├── NumberSquare.java
│ │ ├── PalindromeNumber.java
│ │ ├── add_fibonacci_numbers
│ │ │ ├── BruteForce.java
│ │ │ ├── DPEfficient.java
│ │ │ └── Efficient.java
│ │ └── find_prime
│ │ │ ├── FindPrime.java
│ │ │ └── Solution.java
│ │ ├── sorting
│ │ ├── BubbleSort.java
│ │ ├── BubbleSort2.java
│ │ ├── CyclicSort.java
│ │ ├── HeapSort.java
│ │ ├── InsertionSort.java
│ │ ├── MergeSort.java
│ │ ├── QuickSort.java
│ │ ├── RadixSort.java
│ │ └── SelectionSort.java
│ │ ├── strings
│ │ ├── LongestPalindromicSubString.java
│ │ ├── MaxRepeatingChar.java
│ │ ├── Pangram.java
│ │ ├── PermutationOfString.java
│ │ ├── ReverseOnlyVowels.java
│ │ ├── ReverseString.java
│ │ └── convert_to_palindrome.java
│ │ └── trees
│ │ ├── MirrorBinaryTree.java
│ │ ├── SegmentTrees.java
│ │ └── tree
│ │ ├── Node.java
│ │ ├── Tree.java
│ │ ├── Tree_Set.java
│ │ └── tree
│ │ ├── avltree
│ │ └── Linkedlist.java
│ │ └── binarytree
│ │ └── BinarySearchTree.java
└── union-find-algorithm.java
├── AllIndicesOfNumber
└── allindicesofnumber.java
├── AlternatePrimeNumber
└── Apn.java
├── Arrange_Buildings
├── Main.java
└── Problem_Statement
├── BinarySearch
├── BSInInfiniteArray.java
├── BinaryRecurse.java
├── BinarySearch.iml
├── BinarySearch.java
├── BinarySearch2D.java
├── HowManyTimesRotated.java
├── OrderAgnosticBinarySearch.java
└── RotatedBinarySearch.java
├── Blockchain_Implementation
├── Block.java
├── BlockStore.java
├── Crypt.java
└── isChainValid.java
├── Buy and Sell Stock
├── Main.java
└── ProblemStatement.txt
├── Buy_Seel_Stock_TA
├── Main.java
└── Problem_Statement.txt
├── CONTRIBUTING.md
├── Calculator
└── calculator.java
├── CharArrayToString
└── Main.java
├── Check a Leap Year
└── Main.java
├── CherryPickup
├── Main.java
└── ProblemStatement.txt
├── CodeQuotient2
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── Student.java
│ ├── TimeSpan.java
│ ├── add1toeachDigit.java
│ ├── aggMarks.java
│ ├── classRectangle.java
│ ├── cmpareReverse.java
│ ├── evenodd.java
│ ├── factorial.java
│ ├── happy.java
│ ├── hra.java
│ ├── kaperker.java
│ ├── leapYear.java
│ ├── matrixMultiplication.java
│ ├── max.java
│ ├── maxElement.java
│ ├── multiplication.java
│ ├── primenum.java
│ ├── sumArray.java
│ ├── sumofn.java
│ └── swap.java
├── CodechefQuestions
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── Codechef.java
│ ├── EOEO.java
│ ├── ShufflingPatries.java
│ ├── SortExample.java
│ └── Zomcav.java
├── Codeforces-Solution
├── A._Next Round.java
├── Andrew_and_stones.java
├── BuyingTorches.java
├── ColorBlindness.java
├── Counterexample.java
├── DeadlyLaser.java
├── MainakAndArray1.java
├── NewBusRoute.java
├── Sorting_Parts_1637A.java
├── TeaWithTangerines.java
├── WorkingWeek.java
└── YaroslavAndPermutations.java
├── Codejam2022
└── 3DPrinting.java
├── Constant_enum
└── src
│ └── main
│ └── java
│ └── Main.java
├── Cool Map of India
└── map_of_india.java
├── Count_Binary_Strings
├── Main.java
└── Problem_Statement
├── DigitalClock
└── DigitalClock.java
├── Fibonacciseries
├── FibonacciJavaStreams.java
├── FibonacciRecurse.java
└── Main.java
├── FindFirstIndexOfNumber
└── findfirstindexofnumber.java
├── GUI
├── BasicChatAppInterface.class
└── BasicChatAppInterface.java
├── Games
├── GuessTheMovieGame
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ ├── Game.java
│ │ ├── GuessTheMovie.java
│ │ └── Movie.java
│ │ └── resources
│ │ └── movies.txt
├── GuessTheNumber.java
├── GuessingNumberGame
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ └── Guessing_Game_Human_vs_CPU.java
│ │ └── resources
│ │ ├── Guessing_Game_Human_vs_CPU.class
│ │ └── Guessing_Game_Human_vs_CPU.ctxt
├── Hangman.java
├── MiniTennis
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ ├── java
│ │ ├── entities
│ │ │ ├── Ball.java
│ │ │ ├── Entity.java
│ │ │ └── Raquet.java
│ │ └── game
│ │ │ ├── Display.java
│ │ │ ├── Game.java
│ │ │ ├── KeyManager.java
│ │ │ └── Launch.java
│ │ └── resources
│ │ └── README.md
├── Pin_Ball.java
├── Rock Paper Scissor.java
├── SnakeGameApplet
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ ├── Direction.java
│ │ ├── SnakeAppletCode.java
│ │ └── SnakeCanvas.java
├── StonePaperScissor.java
├── Sudoko.java
├── guess_number_oops.java
├── pom.xml
└── tictactoe.java
├── Goldmine
├── Main.java
└── Problem_Statement
├── Graphs
├── 0_1_BFS.java
├── Bellman_ford.java
├── BreadthFirstSearch.java
├── ConnectedComponents.java
├── DepthFirstSearch.java
├── Dijkstra_Algorithm.java
├── Floyd_warshall.java
├── GraphRepresentation.java
├── Graph_implementation.java
├── IsGraphBipartite.java
├── Karps.java
├── Kosaraju.java
├── Kruskal_Algorithm.java
├── Peterson_graph_problem.java
└── TopologicalSort.java
├── GridUniquePath
├── Main.java
└── ProblemStatement.txt
├── GuessNumberGame
└── GuessNumber.java
├── Hackerrank
├── Encryption.java
├── Java_Date_and_Time.java
├── Java_currency_formatter.java
├── LeftRotation.java
├── MatrixLayerRotation.java
├── NonDivisibleSubset.java
├── OrganisingContainersOfBalls.java
├── RainWaterTrapping.java
└── towerofhanoi.java
├── HashTables
├── pom.xml
└── src
│ └── main
│ └── java
│ └── LinearProbingHashTable.java
├── Hashing_Implementation
└── Hashing.java
├── Interface
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── Driver.java
│ ├── Instrument.java
│ ├── Percussion.java
│ ├── Stringed.java
│ └── Wind.java
├── Intresting_games
└── Number_of_ways_in_a_Maze.java
├── KickStart2019
├── pom.xml
└── src
│ └── main
│ └── java
│ └── Solution.java
├── LICENSE.md
├── LeetCodeSolutions
├── 24 Game
│ └── 24_Game.java
├── Algorithm_Day2_SquaresofaSortedArray.java
├── Algorithm_Day3_MoveZeroes.java
├── Algorithm_Day3_TwoSumIIInputArrayIsSorted.java
├── Allocate_Mailboxes.java
├── Array_TwoSum.java
├── Basic calculator IV.java
├── BinarySearch.java
├── Concatenated Words.java
├── ContainerMaxWater.java
├── Couples Holding Hands.java
├── Cut Off Trees for Golf Event.java
├── Decode Ways2.java
├── Distinct Subsequences.java
├── Edit_Distance.java
├── FirstBadVersion.java
├── First_Missing_Positive.java
├── Freedom Trail.java
├── LFU Cache.java
├── Longest consecutive sequence.java
├── LongestSubstringWithoutRepeatingCharacters.java
├── MaxSubarray.java
├── Maximal Rectangle.java
├── MergeTwoBinaryTrees.java
├── MiddleoftheLinkedList.java
├── NextPermutation.java
├── Number_of_ways_of_cutting_a_pizza.java
├── OccurenceOfCharInString.java
├── PalindromeNumber.java
├── PermutationinString.java
├── PowerofFour.java
├── Prefix and suffix Search.java
├── Race Car.java
├── Random_pick_with_blacklist.java
├── Reconstruct Itinerary.java
├── ReverseInteger.java
├── ReverseString.java
├── ReverseWordsinaStringiII.java
├── Russian Doll Envelopes
│ └── Russian_Doll_Envelopes.java
├── SearchInsertPosition.java
├── SetMismatch.java
├── SplitArray.java
├── Stickers to Spell Word.java
├── Strange Printer .java
├── Sudoku.java
├── Tallest Billboard.java
├── ThreeSum.java
├── Trailing_zeroes.java
├── TrappingRainWater.java
├── Trapping_rain_water_problem.java
├── TwoSum.java
├── ValidParentheses.java
├── Wildcard Matching.java
├── Word Break2.java
├── Word Ladder2.java
├── Word Search2.java
├── binary-search-tree-iterator.java
├── lengthOfLastWord.java
├── n-Queens.java
├── pom.xml
├── search-in-a-binary-search-tree.java
├── src
│ └── main
│ │ ├── PhoneLetterCombinations.java
│ │ ├── java
│ │ ├── AddTwoNumbers.java
│ │ ├── Arranging_Coins.java
│ │ ├── CombinationSum.java
│ │ ├── FindPeakInMountArray.java
│ │ ├── HouseRobber.java
│ │ ├── JumpGame.java
│ │ ├── KMissingPositiveNumber.java
│ │ ├── KthLargestInArray.java
│ │ ├── LRUCache.java
│ │ ├── LongestCommonPrefix.java
│ │ ├── LongestSubstringWithoutRepeatingCharacters.java
│ │ ├── MaxConsecutiveOnes.java
│ │ ├── MedianOfTwoSortedArray.java
│ │ ├── MinimumPathSum.java
│ │ ├── NQueens.class
│ │ ├── NQueens.java
│ │ ├── PeakIndex.java
│ │ ├── RemoveElement.java
│ │ ├── RottingOranges.java
│ │ ├── SearchInRotatedSortedArray.java
│ │ ├── SmallestLetterGreaterThanTarget.java
│ │ ├── Sort_Array_By_Parity.java
│ │ ├── TrappingRainWaterProblem.java
│ │ ├── TwoSumII.java
│ │ ├── bestTimeToBuySellStocksCooldown.java
│ │ ├── bestTimeToBuySellStocksI.java
│ │ ├── bestTimeToBuySellStocksII.java
│ │ ├── bestTimeToBuySellStocksIII.java
│ │ ├── bestTimeToBuySellStocksIV.java
│ │ ├── findLargestNumberInRotatedArrayWithDuplicates.java
│ │ ├── kokoEatingBananas.java
│ │ └── searchInMountainArray.java
│ │ └── main.iml
└── validAnagram.java
├── LinkedList
├── pom.xml
└── src
│ └── main
│ └── java
│ └── SinglyLinkedLists
│ └── SinglyLinkedList.java
├── LinkedLists
├── CircularLinkedList
│ └── circularLinkedList.java
├── DoublyLinkedList
│ ├── Merge_sort_for_doubly_linked_list.java
│ └── doublyLinkedList.java
├── SinglyLinkedLists
│ ├── Delete_linked_list.java
│ ├── DetectLoop_UsingHashMethod.java
│ ├── MergeLLatKthPos.java
│ ├── Merge_sort_for_singly_linked_list.java
│ └── ReverseLoop.java
└── reverse_a_linked_list.java
├── Longest Palindromic Subsequence
├── Main.java
└── ProblemStatement.txt
├── Matrix
└── Rotate90.java
├── MatrixOperations
├── MatrixChainMultiplicationImplementation.java
├── MatrixUtil.java
├── identitymatrix.java
├── matrix_border.java
├── matrix_largest_row.java
├── matrixmultiplication.java
├── pom.xml
└── spiralshift.java
├── NoMatcher
├── pom.xml
└── src
│ └── main
│ └── java
│ └── NoMatcher.java
├── Number2Words
└── n2w.java
├── Numbers Programs
├── AlternatePrimeNumber.java
├── Armstrong Number.java
├── CatalanNumber.java
├── FascinatingNumber.java
├── HappyNumber.java
├── MersenneNumber.java
├── Palindrome Number.java
├── PerfectNumber.java
└── all_subsets_of_first_n_natural_numbers.java
├── OTPGenerator
├── pom.xml
└── src
│ └── main
│ └── java
│ └── OtpGenerator.java
├── PassGenerator
├── README.md
├── pom.xml
└── src
│ └── main
│ └── java
│ └── PasswordGenerator.java
├── PasswordValidationInJava
├── pom.xml
└── src
│ └── main
│ └── java
│ └── Password.java
├── PrintOneToHundredWithoutLoop
└── PrintOneToHundredWithoutLoopAndRecursion.java
├── Queue
└── Main.java
├── README.md
├── RMI
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── Client.java
│ ├── HelloImpl.java
│ ├── HelloInt.java
│ └── Server.java
├── RainWaterProblem
├── TrappingRainwaterProblem.java
└── trap.java
├── Random interview questions
└── Top K frequent
│ └── Interpolation Search.java
├── RandomInterviewQuestions
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── TopKFrequentElements.java
│ └── resources
│ └── ReadMe.md
├── Recursion
├── AllSubsets.java
├── BinarySearch.java
├── BubbleSort.java
├── Count_zeros.java
├── FactorialOfNumber.java
├── FibonacciSeries.java
├── JosephusProblem.java
├── Power.java
├── Prime.java
├── RecursiveSumOfAllNumbers.java
├── ReverseNumber.java
├── permutationString.java
└── string.java
├── ReverseOfTheString
└── ReverseOfTheString.java
├── ReverseaNumber
└── main.java
├── Small project
├── BMI-Checker.java
├── Calculator.java
├── Multiplication.java
├── Temperature converter.java
├── TemperatureConverter.java
├── Text-Binary Code Interconvertible utility
│ ├── binary_string_converter.java
│ └── string_binary_converter.java
├── currency_convert.java
├── hotel_AP.java
└── ip_validator.java
├── Small_Gui_Projects
├── FontViewer.java
├── InvestmentFrame.java
└── MovingTwoRectangle.java
├── Snake Game with Various Features and GUI
├── GameFrame.java
├── GamePanel.java
├── README.md
└── SnakeGamer.java
├── Soduku_Solver
└── Sudoku.java
├── Sorting
├── 3_way_merge_sort.java
├── Bitonic_sort.java
├── BubbleSort.java
├── Bucket_sort.java
├── Comb_sort.java
├── ComboSort.java
├── ComboSorting.java
├── CountingSort.java
├── HeapSort.java
├── InPlaceSort.java
├── InsertionSort.java
├── MergeSort.java
├── Odd_even_sort.java
├── Pigeonhole_sort.java
├── QuickSort.java
├── Radix Sort.java
├── Recursive_bubble_sort.java
├── Recursive_insertion_sort.java
├── SelectionSort.java
├── Shell_sort.java
├── SleepSort.java
├── Stooge_sort.java
├── Tim_sort.java
└── WaveSort.java
├── Stacks
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── StackDemo.java
│ └── StackExceptionHandling.java
├── Steganography
├── pom.xml
└── src
│ └── main
│ ├── java
│ ├── LSB_decode.java
│ └── LSB_encode.java
│ └── resources
│ └── README.md
├── String
├── DecodeString.java
└── shortestpath.java
├── SubArrayQuestion
└── maximumSumSubArray.java
├── Timer
├── pom.xml
└── src
│ └── main
│ └── java
│ ├── Main.java
│ └── Timer.java
├── TowersOfHanoi
├── pom.xml
└── src
│ └── main
│ └── java
│ └── TowersOfHanoi.java
├── Tree
├── Red-BlackTree.java
├── fenwickTree.java
└── segmentTree.java
├── Wildcard Matching
├── Main.java
└── ProblemStatement.txt
├── Zomcav
├── pom.xml
└── src
│ └── main
│ └── java
│ └── Codechef.java
├── armstrongNumberChecker
└── IsArmstrongNum.java
├── arrays in java
├── UniquePath.java
├── array_binary_char.java
├── array_binary_int.java
├── array_binary_s.java
├── array_bubble_char.java
├── array_bubble_int.java
└── array_bubble_s.java
├── atm
└── atm.java
├── calculate rectangle
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── let
│ │ └── us
│ │ └── java
│ │ └── App.java
│ └── test
│ └── java
│ └── com
│ └── let
│ └── us
│ └── java
│ └── AppTest.java
├── checkpalindrome
└── checkpalindrome.java
├── competitive programming
├── FastIO.java
├── GFG.java
└── README.md
├── copy_array
├── Genrate_random_number
│ └── random.java
└── array.java
├── dfs
└── dfs.java
├── dynamic programming topics
└── Gold Mine Problem.java
├── interviewQuestions
├── ArrayOccurencesMinMoves.java
└── LeapingFrog.java
├── linearsearch
└── Main.java
├── numberstarpattern
└── numberstarpattern.java
├── pom.xml
└── romanArabicCalculator
└── romanArabicCalculator.java
/.github/pull_request_template.md:
--------------------------------------------------------------------------------
1 | ## Code contribution
2 | - [ ] Read through [contributing.md](https://github.com/CodeNerve/Let-Us-Java/blob/master/CONTRIBUTING.md)
3 | - [ ] All the code files are in a relevant directory
4 | - [ ] Code is only in java (and the files have .java extension), no other files (e.g. .class files) are included
5 | - [ ] The code is not already present in the repository
6 | - [ ] The contribution is meaninful and not just a simple print statement
7 | - [ ] The build passes for your PR
8 |
9 | ---
10 |
11 | ## ReadMe contribution
12 | - [ ] Formatting is good and not messy
13 | - [ ] The PR has meaninful contribution to make the repository better and not just typos or such small changes
14 |
--------------------------------------------------------------------------------
/.github/workflows/maven.yml:
--------------------------------------------------------------------------------
1 | # This workflow will build a Java project with Maven
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3 |
4 | name: Java CI with Maven
5 |
6 | on:
7 | push:
8 | branches: [ master ]
9 | pull_request:
10 | branches: [ master ]
11 |
12 | jobs:
13 | build:
14 |
15 | runs-on: ubuntu-latest
16 |
17 | steps:
18 | - uses: actions/checkout@v2
19 | - name: Set up JDK 1.8
20 | uses: actions/setup-java@v1
21 | with:
22 | java-version: 1.8
23 | - name: Build with Maven
24 | run: mvn -P doc
25 | - name: Zip Release
26 | # You may pin to the exact commit or the version.
27 | # uses: TheDoctor0/zip-release@a1afcab9c664c9976ac398fa831eac67bed1eb0e
28 | uses: TheDoctor0/zip-release@0.4.1
29 | with:
30 | # Filename for archive
31 | filename: site.zip
32 | # Base path for archive files
33 | path: target/staging
34 | - uses: actions/upload-artifact@v2
35 | with:
36 | name: Artifact upload
37 | path: site.zip
38 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | */.classpath
3 | */.project
4 | */.settings
5 | .settings
6 | .project
7 | .classpath
8 | .vscode/
9 | .idea/
--------------------------------------------------------------------------------
/Admission/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | Admission
15 | jar
16 |
17 | Admission
18 |
19 |
20 |
--------------------------------------------------------------------------------
/Algorithms/BubbleRecursion.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | /**
4 | * BubbleRecursion
5 | */
6 | public class BubbleRecursion {
7 |
8 | public static void main(String[] args) {
9 | int[] arr={6,3,2,9,99};
10 | System.out.println(Arrays.toString(bubbles(arr, arr.length-1, 0)));
11 | }
12 | static int[] bubbles(int[] arr,int r,int c){
13 | if(r==0){
14 | return arr;
15 | }
16 | if(carr[c+1]){
18 | int temp=arr[c];
19 | arr[c]=arr[c+1];
20 | arr[c+1]=temp;
21 | }
22 | return bubbles(arr, r, c+1);
23 | }
24 | else{
25 | return bubbles(arr, r-1, 0);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Algorithms/CyclicSort.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | public class CyclicSort {
4 | public static void main(String[] args) {
5 | int[] arr = {5, 4, 3, 2, 1};
6 | sort(arr);
7 | System.out.println(Arrays.toString(arr));
8 | }
9 |
10 | static void sort(int[] arr) {
11 | int i = 0;
12 | while (i < arr.length) {
13 | int correct = arr[i] - 1;
14 | if (arr[i] != arr[correct]) {
15 | swap(arr, i , correct);
16 | } else {
17 | i++;
18 | }
19 | }
20 | }
21 |
22 | static void swap(int[] arr, int first, int second) {
23 | int temp = arr[first];
24 | arr[first] = arr[second];
25 | arr[second] = temp;
26 | }
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Algorithms/Exponential_search.java:
--------------------------------------------------------------------------------
1 |
2 | import java.util.Arrays;
3 |
4 | class GFG
5 | {
6 | // Returns position of
7 | // first occurrence of
8 | // x in array
9 | static int exponentialSearch(int arr[],
10 | int n, int x)
11 | {
12 | // If x is present at first location itself
13 | if (arr[0] == x)
14 | return 0;
15 |
16 | // Find range for binary search by
17 | // repeated doubling
18 | int i = 1;
19 | while (i < n && arr[i] <= x)
20 | i = i*2;
21 |
22 | // Call binary search for the found range.
23 | return Arrays.binarySearch(arr, i/2,
24 | Math.min(i, n-1), x);
25 | }
26 |
27 | // Driver code
28 | public static void main(String args[])
29 | {
30 | int arr[] = {2, 3, 4, 10, 40};
31 | int x = 10;
32 | int result = exponentialSearch(arr,
33 | arr.length, x);
34 |
35 | System.out.println((result < 0) ?
36 | "Element is not present in array" :
37 | "Element is present at index " +
38 | result);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Algorithms/KadaneAlgo.java:
--------------------------------------------------------------------------------
1 | package Arrays;
2 |
3 | public class KadaneAlgo {
4 | public static void main(String[] args) {
5 | System.out.println(kadaneAlgo(new int[]{1,2,3,4,-10, 11, -1}));
6 | }
7 |
8 | public static int kadaneAlgo(int[] arr){
9 | int max = arr[0], sum = 0;
10 | int len = 0;
11 | for (int i = 0; i < arr.length; i++) {
12 | sum+=arr[i];
13 | len++;
14 | if (sum > max) {
15 | max = sum;
16 | }
17 | if (sum <= 0) {
18 | sum = 0;
19 | len = 0;
20 | }
21 | }
22 | return len;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Algorithms/MountainArray.java:
--------------------------------------------------------------------------------
1 | public class MountainArray {
2 | public static void main(String[] args) {
3 | int[] arr={1,3,5,7,4,2};
4 | System.out.println(findPivot(arr));
5 | }
6 | static int findPivot(int[] arr){
7 | int start=0;
8 | int end=arr.length-1;
9 | while(startarr[mid+1]){
12 | end=mid;
13 | }
14 | else {
15 | start=mid+1;
16 |
17 | }
18 |
19 | }
20 | return arr[start];
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Algorithms/MutualRecursion.java:
--------------------------------------------------------------------------------
1 | package Sample;
2 | import java.util.Scanner;
3 |
4 | public class MutualRecursion {
5 |
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | System.out.println("Enter the number : ");
10 | int n = scanner.nextInt();
11 |
12 | if(even(n)){
13 | System.out.println(n + " is even number.");
14 | }
15 | else {
16 | System.out.println(n + " is odd number.");
17 | }
18 | scanner.close();
19 | }
20 |
21 | public static boolean even(int n){
22 | if(n==0)
23 | return true;
24 | else
25 | return odd(n-1);
26 | }
27 |
28 | public static boolean odd(int n){
29 | if(n==0)
30 | return false;
31 | else
32 | return even(n-1);
33 | }
34 |
35 | }
36 |
37 |
38 |
--------------------------------------------------------------------------------
/Algorithms/SpecialNumber.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | class SpecialNumber {
4 | public static void main(String args[]) {
5 | int num, number, last_digit, sum_Of_Fact = 0;
6 | Scanner sc = new Scanner(System.in);
7 | System.out.print("Enter a number: ");
8 | number = sc.nextInt();
9 | num = number;
10 | while (number > 0) {
11 | last_digit = number % 10;
12 | int fact = 1;
13 | for (int i = 1; i <= last_digit; i++) {
14 | fact = fact * i;
15 | }
16 | sum_Of_Fact = sum_Of_Fact + fact;
17 | number = number / 10;
18 | }
19 | if (num == sum_Of_Fact) {
20 | System.out.println(num + " is a special number.");
21 | } else {
22 | System.out.println(num + " is not a special number.");
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/Algorithms/boyer-more-majority-vote-algorithm.java:
--------------------------------------------------------------------------------
1 | //by ajay
2 |
3 | class Main
4 | {
5 |
6 | public static int findMajorityElement(int[] nums)
7 | {
8 |
9 | int m = -1;
10 |
11 |
12 | int i = 0;
13 |
14 |
15 | for (int j = 0; j < nums.length; j++)
16 | {
17 |
18 | if (i == 0)
19 | {
20 |
21 | m = nums[j];
22 |
23 |
24 | i = 1;
25 | }
26 |
27 | else if (m == nums[j]) {
28 | i++;
29 | }
30 |
31 | else {
32 | i--;
33 | }
34 | }
35 |
36 | return m;
37 | }
38 |
39 | public static void main (String[] args)
40 | {
41 | int[] nums = { 1, 8, 7, 4, 1, 2, 2, 2, 2, 2, 2 };
42 |
43 | System.out.println("The majority element is " + findMajorityElement(nums));
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Algorithms/geometricSum.java:
--------------------------------------------------------------------------------
1 | import java.text.DecimalFormat;
2 | import java.util.Scanner;
3 |
4 | public class runner {
5 | public static void main(String[] args) {
6 | Scanner s = new Scanner(System.in);
7 | int k = s.nextInt();
8 | double ans = solution.findGeometricSum(k);
9 | DecimalFormat dec = new DecimalFormat("#0.00000");
10 | System.out.println(dec.format(ans));
11 | }
12 | }
13 |
14 |
15 | class solution {
16 | public static double findGeometricSum(int k){
17 | if(k == 0){
18 | return 1;
19 | }
20 | return findGeometricSum(k - 1) + 1 / Math.pow(2, k);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Algorithms/leaderElement.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 |
4 | public class runner{
5 |
6 | public static void main(String[] args) {
7 | Scanner s = new Scanner(System.in);
8 | int n = s.nextInt();
9 | int[] input = new int[n];
10 |
11 | for(int i=0; i
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | Algorithms
13 |
--------------------------------------------------------------------------------
/Algorithms/rabin-karp.java:
--------------------------------------------------------------------------------
1 | // by-ajay
2 |
3 | public class RabinKarp {
4 | public final static int d = 10;
5 |
6 | static void search(String pattern, String txt, int q) {
7 | int m = pattern.length();
8 | int n = txt.length();
9 | int i, j;
10 | int p = 0;
11 | int t = 0;
12 | int h = 1;
13 |
14 | for (i = 0; i < m - 1; i++)
15 | h = (h * d) % q;
16 |
17 | for (i = 0; i < m; i++) {
18 | p = (d * p + pattern.charAt(i)) % q;
19 | t = (d * t + txt.charAt(i)) % q;
20 | }
21 |
22 | for (i = 0; i <= n - m; i++) {
23 | if (p == t) {
24 | for (j = 0; j < m; j++) {
25 | if (txt.charAt(i + j) != pattern.charAt(j))
26 | break;
27 | }
28 |
29 | if (j == m)
30 | System.out.println("Pattern is found at position: " + (i + 1));
31 | }
32 |
33 | if (i < n - m) {
34 | t = (d * (t - txt.charAt(i) * h) + txt.charAt(i + m)) % q;
35 | if (t < 0)
36 | t = (t + q);
37 | }
38 | }
39 | }
40 |
41 | public static void main(String[] args) {
42 | String txt = "ABCCDDAEFG";
43 | String pattern = "CDD";
44 | int q = 13;
45 | search(pattern, txt, q);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Algorithms/selectionsort.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | class SelectionSort {
4 | void selectionSort(int array[]) {
5 | int size = array.length;
6 |
7 | for (int i = 0; i < size - 1; i++) {
8 | int min_idx = i;
9 |
10 | for (int j = i + 1; j < size; j++) {
11 |
12 | if (array[j] < array[min_idx]) {
13 | min_idx = j;
14 | }
15 | }
16 |
17 | int tem = array[step];
18 | array[i] = array[min_idx];
19 | array[min_idx] = tem;
20 | }
21 | }
22 |
23 | public static void main(String args[]) {
24 | int[] data = { 20, 12, 10, 15, 2 };
25 | SelectionSort s = new SelectionSort();
26 | s.selectionSort(data);
27 | System.out.println("Sorted Array in Ascending Order: ");
28 | System.out.println(Arrays.toString(data));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/BinarySearch.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Example code of binary search
3 | */
4 | public class BinarySearch {
5 |
6 | public static void main(String[] args) {
7 | int[] numbersArray = {1, 2, 3, 4, 5, 6, 7};
8 | int key = 4;
9 | int left = 0;
10 | int right = numbersArray.length - 1;
11 |
12 | System.out.println("Found on index: " + binarySearch(numbersArray, key, left, right));
13 | }
14 |
15 | public static int binarySearch(int[] array, int elem, int left, int right) {
16 | while (left <= right) {
17 | int mid = left + (right - left) / 2;
18 |
19 | if (elem == array[mid]) {
20 | return mid;
21 | } else if (elem < array[mid]) {
22 | right = mid - 1;
23 | } else {
24 | left = mid + 1;
25 | }
26 | }
27 | return -1;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/dynamic_programing/FactorialDP.java:
--------------------------------------------------------------------------------
1 | public class FactorialDP {
2 | public int fact (int n){
3 | int [] memo = new int [n+1];
4 | memo [0] =1;
5 | for(int i=1; i<=n; i++){
6 | memo[i] = i* memo[i-1];
7 | }
8 | return memo[n];
9 | }
10 | }
--------------------------------------------------------------------------------
/Algorithms/src/main/java/dynamic_programing/FibonacciNumber.java:
--------------------------------------------------------------------------------
1 | package dynamic_programing;
2 |
3 | class Solution {
4 |
5 | public int fib(int n) {
6 | int f[] = new int[n+1];
7 | int i;
8 | f[0] = 0;
9 | f[1] = 1;
10 |
11 | for (i = 2; i <= n; i++) {
12 | f[i] = f[i-1] + f[i-2];
13 | }
14 |
15 | return f[n];
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/dynamic_programing/HouseRobber.java:
--------------------------------------------------------------------------------
1 | package dynamic_programing;
2 |
3 | import java.util.Arrays;
4 |
5 | public class HouseRobber {
6 |
7 | public String pad(String string, int num){
8 | StringBuilder temp = new StringBuilder(string);
9 |
10 | for(int i=0;i= 0){
30 | return memo[index];
31 | }
32 | else {
33 | int answer = Math.max(robRecursive(index - 2, nums, memo) + nums[index], robRecursive(index - 1, nums, memo));
34 | memo[index] = answer;
35 | return answer;
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/dynamic_programing/ReadMe.md:
--------------------------------------------------------------------------------
1 | # Codes for common DP problems
2 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/kadane.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | // Java program to print largest contiguous array sum
3 | import java.util.*;
4 |
5 | class Kadane
6 | {
7 | public static void main (String[] args)
8 | {
9 | int [] a = {-2, -3, 4, -1, -2, 1, 5, -3};
10 | System.out.println("Maximum contiguous sum is " +
11 | maxSubArraySum(a));
12 | }
13 |
14 | static int maxSubArraySum(int a[])
15 | {
16 | int size = a.length;
17 | int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;
18 |
19 | for (int i = 0; i < size; i++)
20 | {
21 | max_ending_here = max_ending_here + a[i];
22 | if (max_so_far < max_ending_here)
23 | max_so_far = max_ending_here;
24 | if (max_ending_here < 0)
25 | max_ending_here = 0;
26 | }
27 | return max_so_far;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/linearSearch.java:
--------------------------------------------------------------------------------
1 | public class linearSearch {
2 |
3 | void findElementArray(int a[],int key) {
4 | boolean found = false;
5 | for(int i=0;i0){
13 | int r = n%10;
14 | sum = (sum*10)+r;
15 | n=n/10;
16 | }
17 | if(temp == sum){
18 | System.out.println("The input is a palindrome number.");
19 | }else{
20 | System.out.println("The input is not a palindrome number.");
21 | }
22 |
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/numbers/add_fibonacci_numbers/BruteForce.java:
--------------------------------------------------------------------------------
1 | package numbers.add_fibonacci_numbers;
2 |
3 | import java.io.*;
4 |
5 | public class BruteForce {
6 |
7 |
8 | static int calculateSum(int n) {
9 | if (n <= 0)
10 | return 0;
11 |
12 | int fibo[]=new int[n+1];
13 | fibo[0] = 0; fibo[1] = 1;
14 |
15 | // Initialize result
16 | int sum = fibo[0] + fibo[1];
17 |
18 | // Add remaining terms
19 | for (int i=2; i<=n; i++)
20 | {
21 | fibo[i] = fibo[i-1]+fibo[i-2];
22 | sum += fibo[i];
23 | }
24 |
25 | return sum;
26 | }
27 |
28 | public static void main(String args[]) {
29 | int n = 4;
30 | System.out.println("Sum of Fibonacci" +
31 | " numbers is : "+ calculateSum(n));
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/numbers/add_fibonacci_numbers/DPEfficient.java:
--------------------------------------------------------------------------------
1 | package numbers.add_fibonacci_numbers;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class DPEfficient {
7 | Map map = new HashMap<>();
8 |
9 | public int fib(int n) {
10 | if(n <= 0) {
11 | return 0;
12 | } else if(n == 1 || n == 2) {
13 | return 1;
14 | } else if(map.containsKey(n)){
15 | return map.get(n);
16 | } else {
17 | int answer = fib(n - 1) + fib(n - 2);
18 | map.put(n, answer);
19 | return answer;
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/numbers/find_prime/FindPrime.java:
--------------------------------------------------------------------------------
1 | package numbers.find_prime;
2 |
3 | public class FindPrime{
4 | public static void main(String[] args) {
5 | int num = 0;
6 | try{
7 | num = Integer.parseInt(args[0]);
8 |
9 | }catch(NumberFormatException e){
10 | // The first argument isn't a valid integer. Print
11 | // an error message, then exit with an error code.
12 | System.out.println("The first argument must be an integer.");
13 | System.exit(1);
14 | }
15 | for(int i =2; i <=num; i++){
16 | int fact = 0;
17 | for(int j = 1;j <=i;j++){
18 | if(i%j==0){
19 | fact++;
20 | }
21 | }
22 | if(fact ==2){
23 | System.out.println(i);
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/numbers/find_prime/Solution.java:
--------------------------------------------------------------------------------
1 | package numbers.find_prime;
2 |
3 | import java.io.*;
4 | import java.math.*;
5 | import java.security.*;
6 | import java.text.*;
7 | import java.util.*;
8 | import java.util.concurrent.*;
9 | import java.util.regex.*;
10 |
11 | public class Solution {
12 |
13 |
14 |
15 | private static final Scanner scanner = new Scanner(System.in);
16 |
17 | public static void main(String[] args) {
18 | BigInteger n = scanner.nextBigInteger();
19 |
20 | scanner.close();
21 |
22 | if(n.isProbablePrime(100) == true){
23 | System.out.println("prime");
24 | }else{
25 | System.out.println("not prime");
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/sorting/RadixSort.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class RadixSort {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Scanner scan=new Scanner(System.in);
8 | int n=scan.nextInt();
9 | int max = Integer.MIN_VALUE;
10 | int a[]=new int[n];
11 | for(int i=0;i 0; exp *= 10) countSort(a, n, exp);
17 |
18 | for(int i=0;i= 0; i--) {
35 | output[count[(a[i] / exp) % 10] - 1] = a[i];
36 | count[(a[i] / exp) % 10]--;
37 | }
38 |
39 | for (i = 0; i < n; i++) a[i] = output[i];
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/sorting/SelectionSort.java:
--------------------------------------------------------------------------------
1 | package sorting;
2 |
3 | import java.util.Scanner;
4 |
5 | public class SelectionSort
6 | {
7 | public static void main(String args[])
8 | {
9 | int size, i, j, temp;
10 | int arr[] = new int[50];
11 | Scanner scan = new Scanner(System.in);
12 |
13 | System.out.print("Array Size : ");
14 | size = scan.nextInt();
15 |
16 | System.out.print("Array Elements separated by space : ");
17 | for(i=0; i arr[j])
27 | {
28 | temp = arr[i];
29 | arr[i] = arr[j];
30 | arr[j] = temp;
31 | }
32 | }
33 | }
34 |
35 | System.out.print("Sorted Array :\n");
36 | for(i=0; i count) {
21 | count = current_count;
22 | ch = str.charAt(i);
23 | }
24 | }
25 | return ch;
26 | }
27 |
28 | public static void main(String args[]) {
29 |
30 | Scanner sc = new Scanner(System.in);
31 | System.out.print("Enter a string: ");
32 | String str = sc.nextLine();
33 | System.out.println(findMaxRepeatingChar(str));
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/strings/PermutationOfString.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class PermutationOfString {
4 |
5 | public static void main(String[] args) {
6 | // Given an input string to return all possible permuations in a string Array
7 | String input="abc";
8 | String [] result=permutationOfString(input);
9 | print(result);
10 |
11 | }
12 | private static String[] permutationOfString(String input) {
13 | if (input.length() == 0) {
14 | String[] result = {""};
15 | return result;
16 | }
17 | String[] smallAns = permutationOfString(input.substring(1));
18 | String[] ans = new String[smallAns.length*(smallAns[0].length() + 1)];
19 |
20 | int k = 0;
21 | for (int i = 0; i < smallAns.length; i++) {
22 | String current = smallAns[i];
23 | for (int j = 0; j <= current.length(); j++) {
24 | ans[k] = current.substring(0, j) + input.charAt(0)
25 | + current.substring(j);
26 | k++;
27 | }
28 |
29 | }
30 | return ans;
31 | }
32 | private static void print(String[] result) {
33 | for(String s: result) {
34 | System.out.println(s);
35 | }
36 |
37 | }
38 |
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/strings/ReverseString.java:
--------------------------------------------------------------------------------
1 | public class ReverseString {
2 | public static void main(String[] args) {
3 |
4 | String input = "CodeNerve";
5 |
6 | // getBytes() method to convert string
7 | // into bytes[].
8 | byte[] strAsByteArray = input.getBytes();
9 |
10 | byte[] result = new byte[strAsByteArray.length];
11 |
12 | // Store result in reverse order into the
13 | // result byte[]
14 | for (int i = 0; i < strAsByteArray.length; i++)
15 | result[i] = strAsByteArray[strAsByteArray.length - i - 1];
16 |
17 | System.out.println(new String(result));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/trees/tree/Tree.java:
--------------------------------------------------------------------------------
1 | package trees.tree;
2 | public class Tree extends Node {
3 |
4 | public Tree(int data) {
5 | super(data);
6 | }
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/Algorithms/src/main/java/trees/tree/Tree_Set.java:
--------------------------------------------------------------------------------
1 | package trees.tree;
2 | // Java program to demonstrate insertions in TreeSet
3 | import java.util.*;
4 |
5 | class TreeSetDemo {
6 | public static void main(String[] args)
7 | {
8 | TreeSet ts1 = new TreeSet();
9 |
10 | // Elements are added using add() method
11 | ts1.add("A");
12 | ts1.add("B");
13 | ts1.add("C");
14 |
15 | // Duplicates will not get insert
16 | ts1.add("C");
17 |
18 | // Elements get stored in default natural
19 | // Sorting Order(Ascending)
20 | System.out.println(ts1);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/AlternatePrimeNumber/Apn.java:
--------------------------------------------------------------------------------
1 | public class AlternatePrimeNumber
2 |
3 | {
4 |
5 | public static void main(String[] args)
6 |
7 | {
8 |
9 | int num = 20;
10 |
11 | System.out.print("Alternate prime numbers up to " + num+" are: ");
12 |
13 | printAlternatePrimeNumber(num);
14 |
15 | }
16 |
17 | static int checkPrime(int num)
18 |
19 | {
20 |
21 | int i, flag = 0;
22 |
23 | for(i = 2; i<= num / 2; i++)
24 |
25 | {
26 |
27 | if(num % i == 0)
28 |
29 | {
30 |
31 | flag = 1;
32 |
33 | break;
34 |
35 | }
36 |
37 | }
38 |
39 | if(flag == 0)
40 |
41 | return 1;
42 |
43 | else
44 |
45 | return 0;
46 |
47 | }
48 |
49 | static void printAlternatePrimeNumber(int n)
50 |
51 | {
52 |
53 | int temp = 2;
54 |
55 | for(int num = 2; num <= n-1; num++)
56 |
57 | {
58 |
59 | if (checkPrime(num) == 1)
60 |
61 | {
62 |
63 | if (temp % 2 == 0)
64 |
65 | System.out.print(num + " ");
66 |
67 | temp ++;
68 |
69 | }
70 |
71 | }
72 |
73 | }
74 |
75 | }
--------------------------------------------------------------------------------
/Arrange_Buildings/Main.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | import java.util.*;
3 |
4 | public class Main{
5 |
6 | public static void main(String[] args) throws Exception {
7 | // write your code here
8 | Scanner scn = new Scanner(System.in);
9 | long n= scn.nextInt();
10 |
11 | long zeros = 1;
12 | long ones = 1;
13 |
14 | for(int i=2;i<=n;i++)
15 | {
16 | long new0 = ones;
17 | long new1 = ones + zeros;
18 |
19 | zeros = new0;
20 | ones = new1;
21 | }
22 | long total = zeros+ones;
23 |
24 | System.out.println(total*total);
25 | }
26 |
27 | }
--------------------------------------------------------------------------------
/Arrange_Buildings/Problem_Statement:
--------------------------------------------------------------------------------
1 | 1. You are given a number n, which represents the length of a road. The road has n plots on it's each side.
2 | 2. The road is to be so planned that there should not be consecutive buildings on either side of the road.
3 | 3. You are required to find and print the number of ways in which the buildings can be built on both side of roads.
--------------------------------------------------------------------------------
/BinarySearch/BinaryRecurse.java:
--------------------------------------------------------------------------------
1 | public class Main
2 | {
3 | public static void main (String[]args)
4 | {
5 | int[] arr = { 1, 3, 4, 6, 9 };
6 | int target = 6;
7 | System.out.println (BinarySearch (arr, target, 0, arr.length - 1));
8 | }
9 | static int BinarySearch (int[]arr, int target, int s, int e)
10 | {
11 | int mid = s + (e - s) / 2;
12 |
13 | if (arr[mid] == target)
14 | {
15 | return mid;
16 | }
17 | if (s <= e)
18 | {
19 | if (target < arr[mid])
20 | {
21 | return BinarySearch (arr, target, s, mid - 1);
22 | }
23 | else
24 | {
25 | return BinarySearch (arr, target, mid + 1, e);
26 | }
27 | }
28 |
29 |
30 |
31 | return -1;
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/BinarySearch/BinarySearch.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/BinarySearch/BinarySearch.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BinSearchDesc
6 | {
7 | public static void main(String[] args)
8 | {
9 | int[] arr = {6, 5, 4, 3, 2, 1};
10 | int target = 2;
11 |
12 | int res = binsearch(arr, target);
13 |
14 | System.out.println(res);
15 | }
16 |
17 | private static int binsearch(int[] arr, int target)
18 | {
19 | int start = 0;
20 | int end = arr.length-1;
21 |
22 | while(start <= end){
23 | int middle = start + (end - start) / 2;
24 |
25 | if(target < arr[middle])
26 | {
27 | end = middle - 1;
28 | }
29 | else if(target > arr[middle])
30 | {
31 | start = middle + 1;
32 | }
33 | else
34 | {
35 | return middle;
36 | }
37 |
38 | }
39 | return -1;
40 | }
41 |
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/BinarySearch/BinarySearch2D.java:
--------------------------------------------------------------------------------
1 |
2 | public class BinarySearch2D {
3 | public static boolean searchMatrix(int[][] matrix, int target) {
4 |
5 | int row = matrix.length;
6 | int col = matrix[0].length;
7 |
8 | int begin = 0;
9 | int last = row * col - 1;
10 | int middle = begin + (last - begin) / 2;
11 |
12 | while (begin <= last) {
13 | int element = matrix[middle / col][middle % col];
14 |
15 | if (element == target) {
16 | return true;
17 | }
18 |
19 | else if (element < target) {
20 | begin = middle + 1;
21 | }
22 |
23 | else {
24 | last = middle - 1;
25 | }
26 |
27 | middle = begin + (last - begin) / 2;
28 |
29 | }
30 | return false;
31 |
32 | }
33 |
34 | public static void main(String[] args) {
35 |
36 | int[][] arr = { { 1, 3, 5 }, { 10, 11, 16 }, { 24, 36, 37 } };
37 |
38 | System.out.println(searchMatrix(arr, 11));
39 | }
40 | }
--------------------------------------------------------------------------------
/BinarySearch/OrderAgnosticBinarySearch.java:
--------------------------------------------------------------------------------
1 | public class Main
2 | {
3 | public static void main(String[] args) {
4 | int[] arr={10,6,4,3,2,1};
5 | int target=3;
6 | System.out.println(OrderAgnosticBinarySearch(arr,target));
7 | }
8 | //function for Order Agnostic Binary Search
9 | static int OrderAgnosticBinarySearch(int[] arr,int target){
10 | int low,high,mid;
11 | low=0;
12 | high=arr.length-1;
13 |
14 | while(low<=high){
15 | mid=low+(high-low)/2;
16 | if(arr[mid]==target){
17 | return mid;
18 | }
19 | else if(arr[low]e){
16 | return -1;
17 | }
18 | int m=s+(e-s)/2;
19 | if(arr[m]==target){
20 | return m;
21 | }
22 | if(arr[s]<=arr[m]){
23 | if(target>=arr[s] && target<=arr[m]){
24 | return search(arr,target,s, m-1);
25 | }
26 | else{
27 | return search(arr,target,m+1,e);
28 | }
29 | }
30 | if(target>=arr[m] && target<=arr[e]){
31 | return search(arr,target,m+1,e);
32 | }
33 | return search(arr,target,s,m-1);
34 | }
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/Buy and Sell Stock/Main.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Main{
4 | static int maximumProfit(int []Arr){
5 | // Write your code here.
6 | int maxProfit = 0;
7 | int mini = Arr[0];
8 |
9 | for(int i=1;i
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | CodeQuotient2
13 |
14 |
15 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/Student.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | import java.util.Scanner;
4 | //Other imports go here
5 | //Do NOT change the class name
6 | class Student
7 | {
8 |
9 | public static void main(String arg[])
10 | {cq1(21);}
11 |
12 | private static void cq1(int y) {
13 | int x = y / 10;
14 | int z = (x + y);
15 | x = cq2(z, y);
16 | System.out.println("cq1: x = " + x + ", y = " + y + ", z = " + z);
17 | }
18 |
19 | private static int cq2(int x, int y) {
20 | int z = cq3(x + y, y);
21 | y /= z;
22 | System.out.println("cq2: x = " + x + ", y = " + y + ", z = " + z);
23 | return z;
24 | }
25 |
26 | private static int cq3(int x, int y) {
27 | y = x / (x % 10);
28 | System.out.println("cq3: x = " + x + ", y = " + y);
29 | return y;
30 | }
31 | }
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/add1toeachDigit.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.lang.Math;
3 | class add1toeachDigit
4 | {
5 | public static void main(String[] args)
6 | {
7 | // Write your code here
8 | Scanner input = new Scanner(System.in);
9 | int n = input.nextInt();
10 | int num=0,i=0;
11 | while(n!=0)
12 | {
13 | int r=n%10;
14 | if(r==9)
15 | r=0;
16 | else
17 | r+=1;
18 | num=num+r*(int)(Math.pow(10,i));
19 | i++;
20 | n=n/10;
21 |
22 | }
23 | System.out.println(num);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/aggMarks.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class aggMarks {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Scanner s = new Scanner(System.in);
8 | int marks1=s.nextInt();
9 | int marks2=s.nextInt();
10 | int marks3=s.nextInt();
11 | int marks4=s.nextInt();
12 | int marks5=s.nextInt();
13 | int totalMarks=marks1+marks2+marks3+marks4+marks5;
14 | double percent=totalMarks*100/500;
15 | System.out.println(totalMarks+" "+(int)percent);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/classRectangle.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 |
3 | class classRectangle
4 | {
5 |
6 | public void main(ArrayList v)
7 | {
8 | for (int i = 0; i < v.size(); i++)
9 | {
10 | int n = v.get(i);
11 | if (n % 10 == 0)
12 | {
13 | v.remove(i);
14 | v.add(n);
15 | }
16 | }
17 | System.out.println(v);
18 | }
19 | }
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/cmpareReverse.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class cmpareReverse {
4 |
5 |
6 | public static void main (String[] args) {
7 | //code
8 | Scanner sc=new Scanner(System.in);
9 | int T=sc.nextInt();
10 | while(T!=0)
11 | {
12 | long n=sc.nextLong(),rev,sum=0;
13 | long x=n;
14 | while(n!=0)
15 | {
16 | rev=n%10;
17 | sum=sum*10+rev;
18 | n/=10;
19 | }
20 | if(x==sum)
21 | System.out.println("Equal");
22 |
23 | else
24 | System.out.println("Not Equal");
25 | T--;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/evenodd.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class evenodd {
4 |
5 | public static void main(String[] args) {
6 | Scanner s=new Scanner(System.in);
7 | int a=s.nextInt();
8 | if(a%2==0) {
9 | System.out.println("Even");
10 | }
11 | else
12 | System.out.println("Odd");
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/factorial.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class factorial {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Scanner s=new Scanner(System.in);
8 | int x=s.nextInt();
9 | int fact=1;
10 | for(int i=1;i<=x;i++) {
11 | fact=fact*i;
12 | }System.out.println(fact);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/happy.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class happy
3 | {
4 | int sum;
5 | int n;
6 | happy()
7 | {
8 | n=0;
9 | }
10 | void getnum(int nn)
11 | {
12 | n=nn;
13 | }
14 | int sum_sqdigits(int n)
15 | {
16 | if(n==0)
17 | return 0;
18 | else
19 | {
20 | int t=n%10;
21 | return((t*t)+sum_sqdigits(n/10));
22 | }
23 | }
24 | void ishappy()
25 | {
26 | while(n>9)
27 | {
28 | n=sum_sqdigits(n);
29 | }
30 | if(n==1)
31 | System.out.println("the number is happy");
32 | else
33 | System.out.println("the number is not happy");
34 | }
35 | public static void main(String args[])
36 | {
37 | Scanner sc=new Scanner(System.in);
38 | System.out.println("enter any number:");
39 | int num=sc.nextInt();
40 | happy obj=new happy();
41 | obj.getnum(num);
42 | obj.ishappy();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/hra.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class hra {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Scanner s=new Scanner(System.in);
8 | int bs=s.nextInt();
9 | double hra=0.1*bs;
10 | double da=0.2*bs;
11 | double gs= bs+hra+da;
12 | System.out.println((int)gs);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/kaperker.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | //Other imports go here// Do NOT change the class name
3 | class kaperker
4 | {
5 | public static void main(String[] args)
6 | {
7 | // Write your code here
8 | int j;
9 | Scanner ob=new Scanner(System.in);
10 | int t = ob.nextInt();
11 |
12 | while(t!=0){
13 | // while(true) {
14 | int n=ob.nextInt();
15 |
16 | int N=n*n;
17 | int tn=n;int c=0;
18 | while(tn!=0)
19 | {
20 | tn=tn/10;c++;
21 | }
22 | int q=(int)(N/Math.pow(10,c));
23 | int r=(int)(N%Math.pow(10,c));
24 | j=q+r;
25 |
26 | if(j==n)
27 | System.out.println("1");
28 | else
29 | System.out.println("0");
30 | // }
31 | t--;
32 | }
33 | }
34 | }
35 |
36 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/leapYear.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class leapYear {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Scanner s=new Scanner(System.in);
8 | int year=s.nextInt();
9 | if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0))) {
10 | System.out.println("Leap Year");
11 | }else
12 | System.out.println("Not a Leap Year");
13 |
14 | }
15 | }
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/max.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class max{
4 | public static void main(String args[])
5 | {
6 | Scanner s= new Scanner(System.in);
7 | int a=s.nextInt();
8 | int b=s.nextInt();
9 | int c=s.nextInt();
10 | // prints the maximum of two numbers
11 | System.out.println(Math.max(Math.max(a, b),c));
12 | }
13 |
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/maxElement.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class maxElement {
4 |
5 | // Other imports go here
6 |
7 |
8 | public static void main(String[] args)
9 | {
10 | // Write your code here
11 | Scanner inp = new Scanner(System.in);
12 | int[] a = new int[10];
13 | for(int i=0;i=0;i--)
18 | b[j++]=a[i];
19 |
20 | for(int x:b)
21 | System.out.println(x);
22 |
23 | }
24 | }
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/multiplication.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class multiplication {
4 | public static void main(String[] args)
5 | {
6 | // Write your code here
7 | Scanner s=new Scanner(System.in);
8 | // int n= s.nextInt();
9 | int x= s.nextInt();
10 | int m=s.nextInt();
11 |
12 | int product=1;
13 | for(int i=1;i<=m;i++) {
14 | product=x*i;
15 | System.out.println(product);
16 | }
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/primenum.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class primenum {
4 | public static void main(String[] args)
5 | {
6 | // Write your code here
7 | Scanner inp = new Scanner(System.in);
8 | int n = inp.nextInt();
9 | int m = inp.nextInt();
10 | boolean isPrime = true;
11 | int c=0;
12 |
13 | for(int i=2;i<=n;i++)
14 | { isPrime=true;
15 | for(int j=2;j<=i/2;j++)
16 | {
17 | if(i%j==0)
18 | {
19 | isPrime = false;
20 | break;
21 | }
22 | }
23 | if(isPrime)
24 | {
25 | c++;
26 | if(c<=m)
27 | System.out.println(i);
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/sumArray.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class sumArray
4 | {
5 | String name;
6 | int age;
7 | String college;
8 | String course;
9 | String address;
10 | sumArray
11 | (String name, int age, String college, String course, String address)
12 | {
13 | this.name = name;
14 | this.age = age;
15 | this.college = college;
16 | this.course = course;
17 | this.address = address;
18 | }
19 | public String toString()
20 | {
21 | return name + " " + age + " " + college + " " + course + " " + address;
22 | }
23 | public static void main(String[] args)
24 | {
25 | sumArray b =
26 | new sumArray("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
27 | System.out.println(b);
28 | System.out.println(b.toString());
29 | }
30 | }
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/sumofn.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class sumofn {
4 | public static void main(String[] args)
5 | {
6 | // Write your code here
7 | Scanner s= new Scanner(System.in);
8 | int n=s.nextInt();
9 | int sum=0;
10 | for(int i=0;i<=n;i++) {
11 | sum+=i;
12 | }
13 | System.out.println(sum);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/CodeQuotient2/src/main/java/swap.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.Scanner;
3 |
4 | public class swap {
5 |
6 |
7 | public static void main(String[] args){
8 | //int[] xs=new int[10];
9 | Scanner inp = new Scanner(System.in);
10 | int[] xs = new int[10];
11 | int[] x = new int[10];
12 | System.out.print("Enter elements of array");
13 | for(int i=0;i end-start){
24 | start = temp;
25 | end = i;
26 | }
27 | }
28 | x= Arrays.copyOfRange(xs, start, end+1);
29 | System.out.println(x);
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/CodechefQuestions/README.md:
--------------------------------------------------------------------------------
1 | I have renamed some of the files during refactoring:
2 | - ArrSort(Codechef) to SortExample to adhere to java naming convention
3 | - Zomcav(Codechef) to Zomcav to adhere to java naming convention
4 |
--------------------------------------------------------------------------------
/CodechefQuestions/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | CodechefQuestions
15 | jar
16 |
17 | Codechef Questions
18 |
19 |
20 |
--------------------------------------------------------------------------------
/CodechefQuestions/src/main/java/Codechef.java:
--------------------------------------------------------------------------------
1 | /* codechef Problem: FANCY Contest: JAN19B*/
2 |
3 | import java.util.*;
4 | import java.lang.*;
5 | import java.io.*;
6 |
7 | /* Name of the class has to be "Main" only if the class is public. */
8 | public class Codechef
9 | {
10 | public static void main (String[] args) throws java.lang.Exception
11 | {
12 | Scanner sc=new Scanner(System.in);
13 | int t=sc.nextInt();
14 | for (int i=0;i0)
29 | System.out.println("Real Fancy");
30 | else
31 | System.out.println("regularly fancy");
32 | }}
33 | }
34 |
--------------------------------------------------------------------------------
/CodechefQuestions/src/main/java/ShufflingPatries.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class ShufflingPatries
3 | {
4 | public static void main (String[] args) throws java.lang.Exception
5 | {
6 | // your code goes here
7 |
8 | Scanner sc = new Scanner(System.in);
9 | int T = sc.nextInt();
10 | while(T--!=0)
11 | {
12 | int i,x,y;
13 | int n = sc.nextInt();
14 | int[] a=new int[n];
15 | for(i=0;i= array[k - 1] && array[i] > 0) {
15 | count++;
16 | }
17 | }
18 | System.out.println(count);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Codeforces-Solution/BuyingTorches.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class BuyingTorches {
4 | public static void main(String[] args)
5 | {
6 | Scanner scan = new Scanner(System.in);
7 | int test = scan.nextInt();
8 | while (test-- >0)
9 | {
10 | long x = scan.nextLong();
11 | long y = scan.nextLong();
12 | long k = scan.nextLong();
13 | long reqSticksForCrafting = y*k+k;
14 | long i=0;
15 | if((reqSticksForCrafting -1)%(x-1)==0)
16 | {
17 | i = (reqSticksForCrafting -1)/(x-1);
18 | }
19 | else {
20 | i = (reqSticksForCrafting -1)/(x-1) + 1;
21 | }
22 |
23 | System.out.println(i+k);
24 |
25 |
26 |
27 | }
28 | }
29 | }
--------------------------------------------------------------------------------
/Codeforces-Solution/DeadlyLaser.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class DeadlyLaser {
3 | public static int distance(int n,int m, int sx,int sy)
4 | {
5 | return (Math.abs(n-sx) + Math.abs(m-sy));
6 | }
7 | public static void main(String[] args){
8 | Scanner scan = new Scanner(System.in);
9 | int testcase = scan.nextInt();
10 | while (testcase>0)
11 | {
12 | int n = scan.nextInt(), m =scan.nextInt(), sx = scan.nextInt(), sy = scan.nextInt(),d = scan.nextInt();
13 | if(((distance(n,sy,sx,sy)>d) && (distance(sx,1,sx,sy)>d) ) || ((distance(1,sy,sx,sy)>d) && (distance(sx,m,sx,sy)>d)))
14 | {
15 | System.out.println(n+m-2);
16 | }else
17 | {
18 | System.out.println(-1);
19 | }
20 | testcase--;
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/Codeforces-Solution/NewBusRoute.java:
--------------------------------------------------------------------------------
1 | import java.lang.reflect.Array;
2 | import java.util.Arrays;
3 | import java.util.Scanner;
4 |
5 | public class NewBusRoute {
6 | public static void main(String[] args)
7 | {
8 | Scanner scan= new Scanner(System.in);
9 | int n = scan.nextInt();
10 | long[] cd = new long[n];
11 | for(int i=0;i0)
9 | {
10 | int n = scan.nextInt();
11 | long [] a= new long[n];
12 | for(int i=0;i=miiin)
22 | {
23 | if(a[i]%(miiin-1)==0)
24 | {
25 | steps += (a[i]/(miiin-1)-1);
26 | }else {
27 | steps +=(a[i]/(miiin-1));
28 | }
29 | }
30 | }
31 | System.out.println(steps);
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/Codeforces-Solution/WorkingWeek.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class WorkingWeek {
4 |
5 | static int min(int a, int b) {
6 | return (a <= b) ? a : b;
7 | }
8 |
9 | static int abs(int a) {
10 | if (a < 0) {
11 | return -1 * a;
12 | } else {
13 | return a;
14 | }
15 | }
16 |
17 | public static void main(String[] args) {
18 | Scanner scan = new Scanner(System.in);
19 | int t = scan.nextInt();
20 |
21 | int res[] = new int[t];
22 | int c1, c2, c3, l1, l2, l3, n, m;
23 | for (int i = 0; i < res.length; i++) {
24 | n = scan.nextInt();
25 | c3 = n;
26 | c1 = n / 3;
27 | c2 = c1 + 2;
28 | l1 = c1 - 1;
29 | l2 = 1;
30 | l3 = c3 - c2 - 1;
31 |
32 | m = min(abs(l1 - l2), abs(l2 - l3));
33 | m = min(m, abs(l1 - l3));
34 | res[i] = m;
35 | }
36 |
37 | for (int i = 0; i < res.length; i++) {
38 | System.out.println(res[i]);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Codeforces-Solution/YaroslavAndPermutations.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class YaroslavAndPermutations {
4 | public static void main(String[] args)
5 | {
6 | Scanner scan = new Scanner(System.in);
7 | int n = scan.nextInt();
8 | int [] a = new int[n];
9 | int [] index = new int[1000];
10 | for(int i=0;i(n+1)/2)
19 | {
20 | flag++;
21 | }
22 | }
23 | if(flag>0)
24 | {
25 | System.out.println("NO");
26 | }
27 | else {
28 | System.out.println("YES");
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/Constant_enum/src/main/java/Main.java:
--------------------------------------------------------------------------------
1 | package com.constant_enum;
2 |
3 | public class Main {
4 |
5 | //creating enum data type to store contants
6 | enum Color {
7 | RED("red"), GREEN("green"), BLUE("blue");
8 |
9 | private String Value;
10 |
11 | Color(String Value){
12 | this.Value = Value;
13 | }
14 | public String getValue(){
15 | return Value;
16 | }
17 | }
18 |
19 | public static void main(String[] args) {
20 | Color color = Color.BLUE;
21 |
22 | System.out.println("Enum name: "+ color.name());
23 | System.out.println("Enum value: "+ color.getValue());
24 |
25 | //for itterate all the enum:
26 | System.out.println("Itterate all the enum: ");
27 | for(Color color1: Color.values()){
28 | System.out.println("Enum Name: "+color1.name());
29 | System.out.println("Enum value: "+color1.getValue());
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Cool Map of India/map_of_india.java:
--------------------------------------------------------------------------------
1 | class Main
2 | {
3 | // Java program to print the map of India
4 | public static void main(String[] args)
5 | {
6 | int a, b, c;
7 | String S = "Greetings from Techie Delight!!"
8 | + "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQ"
9 | + "AIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r"
10 | + "\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
11 |
12 | for (b = c = 10; (b + 21 < S.length()) && (a = S.charAt(b+++21))!=0;)
13 | for (; a-- > 64;)
14 | System.out.print((char)(++c == 'Z' ? c = c / 9 : 33 ^ b & 1));
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Count_Binary_Strings/Main.java:
--------------------------------------------------------------------------------
1 | import java.io.*;
2 | import java.util.*;
3 |
4 | public class Main{
5 |
6 | public static void main(String[] args) throws Exception {
7 | // write your code here
8 | Scanner scn = new Scanner(System.in);
9 | int n= scn.nextInt();
10 |
11 | int zeros = 1;
12 | int ones = 1;
13 |
14 | for(int i=2;i<=n;i++)
15 | {
16 | int new0 = ones;
17 | int new1 = ones + zeros;
18 |
19 | zeros = new0;
20 | ones = new1;
21 | }
22 |
23 | System.out.println(zeros+ones);
24 | }
25 |
26 | }
--------------------------------------------------------------------------------
/Count_Binary_Strings/Problem_Statement:
--------------------------------------------------------------------------------
1 | 1. You are given a number n.
2 | 2. You are required to print the number of binary strings of length n with no consecutive 0's.
--------------------------------------------------------------------------------
/Fibonacciseries/FibonacciRecurse.java:
--------------------------------------------------------------------------------
1 | public class Main
2 | {
3 | public static void main(String[] args) {
4 | int n=7;
5 | System.out.println(fibonacci(n));
6 | }
7 | //function to get nth fibonacci number using recursion
8 | static int fibonacci(int n){
9 | if(n<=1){
10 | return n;
11 | }
12 | if(n==2){
13 | return 1;
14 | }
15 | return fibonacci(n-1)+fibonacci(n-2);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Fibonacciseries/Main.java:
--------------------------------------------------------------------------------
1 | class Main {
2 | public static void main(String[] args) {
3 |
4 | int n = 10, firstTerm = 0, secondTerm = 1;
5 | System.out.println("Fibonacci Series till " + n + " terms:");
6 |
7 | for (int i = 1; i <= n; ++i) {
8 | System.out.print(firstTerm + ", ");
9 |
10 | int nextTerm = firstTerm + secondTerm;
11 | firstTerm = secondTerm;
12 | secondTerm = nextTerm;
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/FindFirstIndexOfNumber/findfirstindexofnumber.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class Runner {
4 |
5 | static Scanner s = new Scanner(System.in);
6 |
7 | public static int[] takeInput(){
8 | int size = s.nextInt();
9 | int[] input = new int[size];
10 | for(int i = 0; i < size; i++){
11 | input[i] = s.nextInt();
12 | }
13 | return input;
14 | }
15 |
16 | public static void main(String[] args) {
17 | int[] input = takeInput();
18 | int x = s.nextInt();
19 | System.out.println(findIndex.firstIndex(input, x));
20 | }
21 | }
22 |
23 |
24 | class findIndex {
25 | public static int firstIndex(int input[], int x, int startIndex) {
26 | if(startIndex == input.length) {
27 | return -1;
28 | }
29 | if(input[startIndex] == x) {
30 | return startIndex;
31 | }
32 | return firstIndex(input, x, startIndex + 1);
33 | }
34 | public static int firstIndex(int input[], int x) {
35 | return firstIndex(input, x, 0);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/GUI/BasicChatAppInterface.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeNerve/Let-Us-Java/d8d14bc62aff52d882f98f942887e23caa524ec0/GUI/BasicChatAppInterface.class
--------------------------------------------------------------------------------
/Games/GuessTheMovieGame/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | Games
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | GuessTheMovieGame
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Games/GuessTheMovieGame/src/main/java/Game.java:
--------------------------------------------------------------------------------
1 |
2 | public class Game {
3 |
4 | public static void main(String[] args) {
5 | System.out.println("Welcome Player! \nLet's get started");
6 | GuessTheMovie game = new GuessTheMovie();
7 | while(!game.gameOver()) {
8 | game.play();
9 | }
10 |
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/Games/GuessTheMovieGame/src/main/java/Movie.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.io.*;
3 | public class Movie {
4 | public String name;
5 | public int length;
6 |
7 | public Movie(){
8 | File movieList = new File("src/main/movies.txt");
9 | try {
10 | Scanner s = new Scanner(movieList);
11 | int totalMovies = 0;
12 | int rnd = (int)(Math.random()*26);
13 | for (int i = 0; i < 26; i++) {
14 | String line = s.nextLine();
15 | if (i == rnd) {
16 | name = line;
17 | length = line.length();
18 | break;
19 | }
20 | }
21 | s.close();
22 | }
23 | catch( Exception e) {
24 |
25 | }
26 |
27 | }
28 |
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/Games/GuessTheMovieGame/src/main/resources/movies.txt:
--------------------------------------------------------------------------------
1 | the shawshank redemption
2 | the godfather
3 | the dark knight
4 | schindler's list
5 | pulp fiction
6 | the lord of the rings
7 | the good the bad and the ugly
8 | fight club
9 | the lord of the rings
10 | forrest gump
11 | star wars
12 | inception
13 | the lord of the rings
14 | the matrix
15 | samurai
16 | star wars
17 | city of god
18 | the silence of the lambs
19 | batman begins
20 | die hard
21 | chinatown
22 | room
23 | dunkirk
24 | fargo
25 | no country for old men
--------------------------------------------------------------------------------
/Games/GuessingNumberGame/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | Games
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | GuessingNumberGame
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Games/GuessingNumberGame/src/main/resources/Guessing_Game_Human_vs_CPU.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CodeNerve/Let-Us-Java/d8d14bc62aff52d882f98f942887e23caa524ec0/Games/GuessingNumberGame/src/main/resources/Guessing_Game_Human_vs_CPU.class
--------------------------------------------------------------------------------
/Games/GuessingNumberGame/src/main/resources/Guessing_Game_Human_vs_CPU.ctxt:
--------------------------------------------------------------------------------
1 | #BlueJ class context
2 | comment0.params=lower\ upper\ randnum\ count
3 | comment0.target=int\ computerGuess(int,\ int,\ int,\ int)
4 | comment1.params=args
5 | comment1.target=void\ main(java.lang.String[])
6 | numComments=2
7 |
--------------------------------------------------------------------------------
/Games/MiniTennis/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | Games
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | MiniTennis
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Games/MiniTennis/src/main/java/entities/Entity.java:
--------------------------------------------------------------------------------
1 | package entities;
2 |
3 | import java.awt.Graphics;
4 | import java.awt.Rectangle;
5 |
6 | public abstract class Entity {
7 | protected int width;
8 | protected int height;
9 | protected int x,y;
10 | protected float xMove,yMove;
11 | protected static float speed = 1f;
12 |
13 | public abstract void tick();
14 | public abstract void render(Graphics g);
15 |
16 | public void move() {
17 | x += xMove;
18 | y += yMove;
19 | }
20 |
21 | public Rectangle getBounds() {
22 | return new Rectangle(x,y,width,height);
23 | }
24 |
25 | public int getWidth() {
26 | return width;
27 | }
28 | public int getHeight() {
29 | return height;
30 | }
31 | public int getX() {
32 | return x;
33 | }
34 | public int getY() {
35 | return y;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Games/MiniTennis/src/main/java/entities/Raquet.java:
--------------------------------------------------------------------------------
1 | package entities;
2 |
3 | import java.awt.Color;
4 | import java.awt.Graphics;
5 |
6 | import game.Game;
7 |
8 | public class Raquet extends Entity{
9 |
10 | private Game game;
11 |
12 | public Raquet(Game game) {
13 | this.game = game;
14 | width = 60;
15 | height = 10;
16 | x = 0;
17 | y = 330;
18 | xMove = 0;
19 | yMove = 0;
20 | }
21 |
22 | @Override
23 | public void tick() {
24 | xMove = 0;
25 | getInput();
26 | if(x + xMove > 0 && x + xMove < game.getWidth() - 60) {
27 | move();
28 | }
29 |
30 | }
31 |
32 | private void getInput() {
33 | if(game.getKeyManager().left) {
34 | xMove = -speed;
35 | }
36 | if(game.getKeyManager().right) {
37 | xMove = speed;
38 | }
39 | }
40 |
41 | @Override
42 | public void render(Graphics g) {
43 | g.setColor(Color.black);
44 | g.drawRect(x, y, width, height);
45 | g.fillRect(x, y, width, height);
46 | }
47 |
48 |
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/Games/MiniTennis/src/main/java/game/KeyManager.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | import java.awt.event.KeyEvent;
4 | import java.awt.event.KeyListener;
5 |
6 | public class KeyManager implements KeyListener{
7 |
8 | public boolean left,right;
9 | private boolean Kleft,Kright;
10 |
11 | public void tick() {
12 | left = Kleft;
13 | right = Kright;
14 | }
15 |
16 | @Override
17 | public void keyPressed(KeyEvent e) {
18 |
19 | if(e.getExtendedKeyCode() == KeyEvent.VK_LEFT) {
20 | Kleft = true;
21 | }
22 | if(e.getExtendedKeyCode() == KeyEvent.VK_RIGHT) {
23 | Kright = true;
24 | }
25 |
26 | }
27 |
28 | @Override
29 | public void keyReleased(KeyEvent e) {
30 | if(e.getExtendedKeyCode() == KeyEvent.VK_LEFT) {
31 | Kleft = false;
32 | }
33 | if(e.getExtendedKeyCode() == KeyEvent.VK_RIGHT) {
34 | Kright = false;
35 | }
36 |
37 | }
38 |
39 | @Override
40 | public void keyTyped(KeyEvent arg0) {
41 | // TODO Auto-generated method stub
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/Games/MiniTennis/src/main/java/game/Launch.java:
--------------------------------------------------------------------------------
1 | package game;
2 |
3 | public class Launch {
4 |
5 | public static void main(String[] args) {
6 | // TODO Auto-generated method stub
7 | Game game = new Game("Mini Tennis",300,400);
8 | game.run();
9 |
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/Games/MiniTennis/src/main/resources/README.md:
--------------------------------------------------------------------------------
1 | # Mini-tennis
2 | It is an interactive game developed for entertainment purpose with basic graphical interface in Java. It is One-Man Game i.e. for indivual players.
3 |
4 | Skills used: Java, Object-Oriented programming, Game Development Basics (Spikes, Collision Detection), Threads
5 |
6 | It containts 2 entities: Ball and Raquet. The player uses raquet to bounce back the ball and once ball crosses raquet the game ends. To make the game interesting, the speed of the ball increases by some percent each time it bounces off raquet.
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Games/SnakeGameApplet/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | Games
10 | 1.0-SNAPSHOT
11 |
12 |
13 | SnakeGameApplet
14 | jar
15 |
16 | Snake Game Applet
17 |
18 |
19 |
--------------------------------------------------------------------------------
/Games/SnakeGameApplet/src/main/java/Direction.java:
--------------------------------------------------------------------------------
1 |
2 | public class Direction {
3 | public static final int NO_DIRECTION = 0;
4 | public static final int NORTH = 1;
5 | public static final int SOUTH = 2;
6 | public static final int WEST = 3;
7 | public static final int EAST = 4;
8 | }
9 |
--------------------------------------------------------------------------------
/Games/SnakeGameApplet/src/main/java/SnakeAppletCode.java:
--------------------------------------------------------------------------------
1 | import java.applet.Applet;
2 | import java.awt.*;
3 |
4 | public class SnakeAppletCode extends Applet {
5 | private SnakeCanvas c;
6 |
7 | public void init()
8 | {
9 | c = new SnakeCanvas();
10 |
11 | c.setPreferredSize(new Dimension(640, 480));
12 |
13 | c.setVisible(true);
14 |
15 | c.setFocusable(true);
16 |
17 | this.add(c);
18 |
19 | this.setVisible(true);
20 |
21 | this.setSize(new Dimension(640, 480));
22 |
23 | }
24 | public void paint(Graphics g)
25 | {
26 | this.setSize(new Dimension(640, 480));
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Games/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | pom
11 | 4.0.0
12 |
13 | Games
14 |
15 |
16 | GuessingNumberGame
17 | GuessTheMovieGame
18 | MiniTennis
19 | SnakeGameApplet
20 |
21 |
--------------------------------------------------------------------------------
/Goldmine/Problem_Statement:
--------------------------------------------------------------------------------
1 | 1. You are given a number n, representing the number of rows.
2 | 2. You are given a number m, representing the number of columns.
3 | 3. You are given n*m numbers, representing elements of 2d array a, which represents a gold mine.
4 | 4. You are standing in front of left wall and are supposed to dig to the right wall. You can start from
5 | any row in the left wall.
6 | 5. You are allowed to move 1 cell right-up (d1), 1 cell right (d2) or 1 cell right-down(d3).
--------------------------------------------------------------------------------
/GridUniquePath/Main.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Main{
4 | static int countWaysUtil(int i, int j, int[][] dp) {
5 | if(i==0 && j == 0)
6 | return 1;
7 | if(i<0 || j<0)
8 | return 0;
9 | if(dp[i][j]!=-1) return dp[i][j];
10 |
11 | int up = countWaysUtil(i-1,j,dp);
12 | int left = countWaysUtil(i,j-1,dp);
13 |
14 | return dp[i][j] = up+left;
15 |
16 | }
17 |
18 | static int countWays(int m, int n){
19 | int dp[][]=new int[m][n];
20 | for (int[] row : dp)
21 | Arrays.fill(row, -1);
22 | return countWaysUtil(m-1,n-1,dp);
23 |
24 | }
25 |
26 | public static void main(String args[]) {
27 |
28 | int m=3;
29 | int n=2;
30 |
31 | System.out.println(countWays(m,n));
32 | }
33 | }
--------------------------------------------------------------------------------
/GridUniquePath/ProblemStatement.txt:
--------------------------------------------------------------------------------
1 | Given two values M and N, which represent a matrix[M][N]. We need to find the total unique paths from the top-left cell (matrix[0][0]) to the rightmost cell (matrix[M-1][N-1]).
2 |
3 | At any cell we are allowed to move in only two directions:- bottom and right.
--------------------------------------------------------------------------------
/Hackerrank/Java_Date_and_Time.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.Calendar;
3 | import java.util.List;
4 | import java.util.Scanner;
5 |
6 | public class JavaDateAndTime {
7 | static List days = Arrays.asList("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY");
8 |
9 | public static String getDay(String day, String month, String year) {
10 |
11 | int y = Integer.parseInt(year);
12 | int m = Integer.parseInt(month);
13 | int d = Integer.parseInt(day);
14 |
15 | Calendar c = Calendar.getInstance();
16 | c.set(y, m-1, d);
17 |
18 | int p = c.get(Calendar.DAY_OF_WEEK);
19 | String s = days.get(p-1);
20 | return s;
21 | }
22 |
23 | public static void main(String[] args) {
24 | Scanner in = new Scanner(System.in);
25 | String month = in.next();
26 | String day = in.next();
27 | String year = in.next();
28 |
29 | System.out.println(getDay(day, month, year));
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Hackerrank/LeftRotation.java:
--------------------------------------------------------------------------------
1 | /*
2 | Question Link : https://www.hackerrank.com/challenges/array-left-rotation/problem
3 | */
4 | import java.util.*;
5 | import java.lang.*;
6 | import java.io.*;
7 | class LeftRotation{
8 | public static void main (String[] args){
9 | Scanner scn = new Scanner(System.in);
10 | int n = scn.nextInt();
11 | int d = scn.nextInt();
12 | List al = new ArrayList<>();
13 | for(int i=0;i rotateLeft(int d, List arr) {
19 | if(d>arr.size()){
20 | d = d%arr.size();
21 | }
22 | List al = new ArrayList<>();
23 | for(int j=d;j= prev) {
13 | prev = arr[i];
14 | prev_index = i;
15 |
16 | temp = 0;
17 | }
18 | else {
19 |
20 | water += prev - arr[i];
21 |
22 | temp += prev - arr[i];
23 | }
24 | }
25 |
26 | if (prev_index < size) {
27 |
28 | water -= temp;
29 |
30 | prev = arr[size];
31 |
32 | for (int i = size; i >= prev_index; i--) {
33 |
34 | if (arr[i] >= prev) {
35 | prev = arr[i];
36 | }
37 | else {
38 | water += prev - arr[i];
39 | }
40 | }
41 | }
42 | return water;
43 | }
44 |
45 | public static void main(String[] args)
46 | {
47 | int arr[] = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
48 | int N = arr.length;
49 | System.out.print(maxWater(arr, N));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Hackerrank/towerofhanoi.java:
--------------------------------------------------------------------------------
1 |
2 | // Java recursive program to solve tower of hanoi puzzle
3 | import java.util.*;
4 |
5 | class towerofhanoi {
6 | // Java recursive function to solve tower of hanoi puzzle
7 | static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
8 | if (n == 1) {
9 | System.out.println("Move disk 1 from rod " + from_rod + " to rod " + to_rod);
10 | return;
11 | }
12 | towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
13 | System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + to_rod);
14 | towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
15 | }
16 |
17 | // Driver method
18 | public static void main(String args[]) {
19 | Scanner sc = new Scanner(System.in);
20 | System.out.println("Enter number of disks: ");
21 | int n = sc.nextInt();// Number of disks
22 | towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
23 | sc.close();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/HashTables/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | HashTables
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/Interface/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | Interface
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Interface/src/main/java/Driver.java:
--------------------------------------------------------------------------------
1 | public class Driver {
2 |
3 | public static void main(String[] args) {
4 | // TODO Auto-generated method stub
5 | Wind w = new Wind();
6 | Percussion p = new Percussion();
7 | Stringed s = new Stringed();
8 | w.play();
9 | String str = w.what();
10 | System.out.println(str);
11 | w.adjust();
12 | p.play();
13 | str = p.what();
14 | System.out.println(str);
15 | p.adjust();
16 | s.play();
17 | str = s.what();
18 | System.out.println(str);
19 | s.adjust();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/Interface/src/main/java/Instrument.java:
--------------------------------------------------------------------------------
1 | public interface Instrument {
2 | void play();
3 | String what();
4 | void adjust();
5 | }
6 |
--------------------------------------------------------------------------------
/Interface/src/main/java/Percussion.java:
--------------------------------------------------------------------------------
1 | public class Percussion {
2 | public void play() {
3 | System.out.println("Playing Percussion");
4 | }
5 | public String what() {
6 | String s = "Cymbal, Bell, Drums, Tabla";
7 | return s;
8 | }
9 | public void adjust() {
10 | System.out.println("Membranes");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Interface/src/main/java/Stringed.java:
--------------------------------------------------------------------------------
1 | public class Stringed {
2 | public void play() {
3 | System.out.println("Playing Strings");
4 | }
5 | public String what() {
6 | String s = "Violin, Cello, Double Bass, Viola";
7 | return s;
8 | }
9 | public void adjust() {
10 | System.out.println("Tuning");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Interface/src/main/java/Wind.java:
--------------------------------------------------------------------------------
1 | public class Wind {
2 | public void play() {
3 | System.out.println("Playing Woodwind");
4 | }
5 | public String what() {
6 | String s = "Clarinet, Flute, Oboe, English Horn";
7 | return s;
8 | }
9 | public void adjust() {
10 | System.out.println("Adjusting Keys");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Intresting_games/Number_of_ways_in_a_Maze.java:
--------------------------------------------------------------------------------
1 | package Practice;
2 | public class Number_of_ways_in_a_Maze {
3 | public static int num_of_ways(int row, int col){
4 | if(row==1||col==1){
5 | return 1;
6 | }
7 | return num_of_ways(row-1,col)+num_of_ways(row,col-1);
8 | }
9 | public static void main(String []args){
10 | System.out.println(num_of_ways(3,3));
11 | }
12 | }
--------------------------------------------------------------------------------
/KickStart2019/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | KickStart2019
15 | jar
16 |
17 | KickStart 2019
18 |
19 |
20 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 CodeNerve
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Algorithm_Day2_SquaresofaSortedArray.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 |
3 | public class Algorithm_Day2_SquaresofaSortedArray {
4 | public int[] sortedSquares(int[] nums) {
5 | for(int i=0;i= nums.length) {
6 | k = k-nums.length;
7 | }
8 | int [] arr = new int[nums.length];
9 | for(int i=0;i< nums.length;i++) {
10 | if(itarget) {
17 | H=M;
18 | }
19 | else if(nums[M]= 0; i--) {
10 | for (int j = 0; j < n; j++) {
11 | dp[i][j] = Integer.MAX_VALUE;
12 | for (int k = 0; k < n; k++) {
13 | if (ring.charAt(k) == key.charAt(i)) {
14 | int diff = Math.abs(j - k);
15 | int step = Math.min(diff, n - diff);
16 | dp[i][j] = Math.min(dp[i][j], step + dp[i + 1][k]);
17 | }
18 | }
19 | }
20 | }
21 |
22 | return dp[0][0] + m;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Longest consecutive sequence.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 |
3 | public List> groupAnagrams(String[] strs) {
4 | List> res = new ArrayList<>();
5 | if (strs.length == 0) return res;
6 | HashMap> map = new HashMap<>();
7 | for (String s : strs) {
8 | char[] hash = new char[26];
9 | for (char c : s.toCharArray()) {
10 | hash[c - 'a']++;
11 | }
12 | String key = new String(hash);
13 | map.computeIfAbsent(key, k -> new ArrayList<>());
14 | map.get(key).add(s);
15 | }
16 | res.addAll(map.values());
17 | return res;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/LongestSubstringWithoutRepeatingCharacters.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class Algorithm_Day6_LongestSubstringWithoutRepeatingCharacters {
4 | public static int lengthOfLongestSubstring(String s) {
5 | int L=0;
6 | int R=0;
7 | int max=0;
8 | HashSet c = new HashSet<>();
9 |
10 | while(R= 0 && nums[i + 1] <= nums[i]) {
5 | i--;
6 | }
7 | if (i >= 0) {
8 | int j = nums.length - 1;
9 | while (nums[j] <= nums[i]) {
10 | j--;
11 | }
12 | swap(nums, i, j);
13 | }
14 | reverse(nums, i + 1);
15 | }
16 |
17 | private void reverse(int[] nums, int start) {
18 | int i = start, j = nums.length - 1;
19 | while (i < j) {
20 | swap(nums, i, j);
21 | i++;
22 | j--;
23 | }
24 | }
25 |
26 | private void swap(int[] nums, int i, int j) {
27 | int temp = nums[i];
28 | nums[i] = nums[j];
29 | nums[j] = temp;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/PalindromeNumber.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/palindrome-number/
2 |
3 | class Solution {
4 | public boolean isPalindrome(int x) {
5 | int i=0;
6 | int reverse[]= new int[15];
7 | int j=0;
8 | int original[]= new int[15];
9 | if(x<0)
10 | return false;
11 | while(x>0)
12 | {
13 | reverse[i++]=x%10;
14 | x/=10;
15 | }
16 | while(j= N
10 | // M: N - B1
11 | int M;
12 | Random r;
13 | Map map;
14 |
15 | public Solution(int N, int[] blacklist) {
16 | map = new HashMap();
17 | for (int b : blacklist) // O(B)
18 | map.put(b, -1);
19 | M = N - map.size();
20 |
21 | for (int b : blacklist) { // O(B)
22 | if (b < M) { // re-mapping
23 | while (map.containsKey(N - 1))
24 | N--;
25 | map.put(b, N - 1);
26 | N--;
27 | }
28 | }
29 |
30 | r = new Random();
31 | }
32 |
33 | public int pick() {
34 | int p = r.nextInt(M);
35 | if (map.containsKey(p))
36 | return map.get(p);
37 | return p;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/ReverseInteger.java:
--------------------------------------------------------------------------------
1 | class ReverseInteger {
2 | public int reverse(int x) {
3 | int rev = 0;
4 | while (x != 0) {
5 | int pop = x % 10;
6 | x /= 10;
7 | if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
8 | if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
9 | rev = rev * 10 + pop;
10 | }
11 | return rev;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/ReverseString.java:
--------------------------------------------------------------------------------
1 | public class Algorithm_Day4_ReverseString {
2 | public void reverseString(char[] s) {
3 | int L = 0;
4 | int R = s.length-1;
5 |
6 | while(L < R) {
7 | char temp = s[L];
8 | s[L] = s[R];
9 | s[R] = temp;
10 | L = L+1;
11 | R = R-1;
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/ReverseWordsinaStringiII.java:
--------------------------------------------------------------------------------
1 | public class Algorithm_Day4_ReverseWordsinaStringiII {
2 | public static String reverseWords(String s) {
3 | StringBuilder n = new StringBuilder("");
4 | int x = 0;
5 | for(int i=0; i=x;j--) {
8 | char c = s.charAt(j);
9 | n.append(c);
10 | }
11 | n.append(' ');
12 | x = i+1;
13 | }
14 | else {
15 | if(i==s.length()-1) {
16 | for (int j=i;j>=x;j--) {
17 | char c = s.charAt(j);
18 | n.append(c);
19 | }
20 | }
21 | }
22 | }
23 | return n.toString();
24 | }
25 |
26 | public static void main(String[] args) {
27 | System.out.println(reverseWords("lakshay roopchandani great"));
28 | System.out.println(Math.ceil((double) 7/2));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/SetMismatch.java:
--------------------------------------------------------------------------------
1 |
2 | // https://leetcode.com/problems/set-mismatch/
3 | public class SetMismatch {
4 |
5 | public static int[] findErrorNums(int[] arr) {
6 | int i = 0;
7 | while (i < arr.length) {
8 | int correct = arr[i] - 1;
9 | if (arr[i] != arr[correct]) {
10 | swap(arr, i , correct);
11 | } else {
12 | i++;
13 | }
14 | }
15 |
16 | // search for first missing number
17 | for (int index = 0; index < arr.length; index++) {
18 | if (arr[index] != index + 1) {
19 | return new int[] {arr[index], index+1};
20 | }
21 | }
22 | return new int[] {-1, -1};
23 | }
24 |
25 | static void swap(int[] arr, int first, int second) {
26 | int temp = arr[first];
27 | arr[first] = arr[second];
28 | arr[second] = temp;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Strange Printer .java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/strange-printer/
2 |
3 | class Solution {
4 | public int strangePrinter(String s) {
5 | if (s == null || s.length() == 0) {
6 | return 0;
7 | }
8 |
9 | int n = s.length();
10 | int[][] dp = new int[n][n];
11 | for (int i = 0; i < n; i++) {
12 | dp[i][i] = 1;
13 | if (i < n - 1) {
14 | dp[i][i + 1] = s.charAt(i) == s.charAt(i + 1) ? 1 : 2;
15 | }
16 | }
17 |
18 | for (int len = 2; len < n; len++) {
19 | for (int start = 0; start + len < n; start++) {
20 | dp[start][start + len] = len + 1;
21 | for (int k = 0; k < len; k++) {
22 | int temp = dp[start][start + k] + dp[start + k + 1][start + len];
23 | dp[start][start + len] = Math.min(
24 | dp[start][start + len],
25 | s.charAt(start + k) == s.charAt(start + len) ? temp - 1 : temp
26 | );
27 | }
28 | }
29 | }
30 |
31 | return dp[0][n - 1];
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Tallest Billboard.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/tallest-billboard/
2 |
3 |
4 | class Solution {
5 | public int tallestBillboard(int[] rods) {
6 | int n = rods.length;
7 | boolean[][] dp = new boolean[n + 1][10001];
8 | int[][] max = new int[n + 1][10001];
9 | dp[0][5000] = true;
10 | for (int i = 0; i < n; i++) {
11 | for (int j = 0; j <= 10000; j++) {
12 | if (j - rods[i] >= 0 && dp[i][j - rods[i]]) {
13 | dp[i + 1][j] = true;
14 | max[i + 1][j] = Math.max(max[i + 1][j], max[i][j - rods[i]] + rods[i]);
15 | }
16 | if (j + rods[i] <= 10000 && dp[i][j + rods[i]]) {
17 | dp[i + 1][j] = true;
18 | max[i + 1][j] = Math.max(max[i + 1][j], max[i][j + rods[i]]);
19 | }
20 | if (dp[i][j]) {
21 | dp[i + 1][j] = true;
22 | max[i + 1][j] = Math.max(max[i + 1][j], max[i][j]);
23 | }
24 | }
25 | }
26 | return max[n][5000];
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Trailing_zeroes.java:
--------------------------------------------------------------------------------
1 | public class Trailing_zeroes {
2 | // Approach 1:-O(N) time complexity.
3 | // public static int TrailingZeroes(int num){
4 | // int temp = 1;
5 | // for(int i=num;i>0;i-=1){
6 | // if(i==5||i==2||i%10==0){
7 | // temp*=i;
8 | // }
9 | // }
10 | // int count =0;
11 | // while(temp%10==0){
12 | // count+=1;
13 | // temp/=10;
14 | // }
15 | // return count;
16 | // }
17 | // Approach 2:- O(logN) time complexity
18 | public static int TrailingZeroes(int N){
19 | int ans = 0;
20 | for(int i = 5; i <= N; i *= 5) {
21 | ans += N/i;
22 | }
23 | return ans;
24 | }
25 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/Trapping_rain_water_problem.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int trap(int[] height) {
3 | int l = height.length;
4 |
5 | int left[] = new int[l];
6 | int right[] = new int [l];
7 | int largest1 = 0;
8 | int largest2 = 0;
9 | for(int i=0;ilargest1)
12 | {
13 | largest1 = height[i];
14 | }
15 | left[i] = largest1;
16 | }
17 |
18 | for(int i=l-1;i>=0;i--)
19 | {
20 | if(height[i]>largest2)
21 | {
22 | largest2 = height[i];
23 | }
24 | right[i] = largest2;
25 | }
26 |
27 | int sum = 0;
28 | for(int i=0;i stack = new Stack<>();
8 |
9 | for (int i = 0; i < s.length(); i++) {
10 |
11 | if (
12 |
13 | stack.isEmpty() &&
14 |
15 | (s.charAt(i) == ')' || s.charAt(i) == '}' || s.charAt(i) == ']')
16 |
17 | ) return false; else {
18 |
19 | if (!stack.isEmpty()) {
20 |
21 | if (
22 |
23 | stack.peek() == '(' && s.charAt(i) == ')'
24 |
25 | ) stack.pop(); else if (
26 |
27 | stack.peek() == '{' && s.charAt(i) == '}'
28 |
29 | ) stack.pop(); else if (
30 |
31 | stack.peek() == '[' && s.charAt(i) == ']'
32 |
33 | ) stack.pop(); else stack.add(s.charAt(i));
34 |
35 | } else stack.add(s.charAt(i));
36 |
37 | }
38 |
39 | }
40 |
41 | return stack.isEmpty();
42 |
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/Wildcard Matching.java:
--------------------------------------------------------------------------------
1 | // https://leetcode.com/problems/wildcard-matching/
2 |
3 |
4 | public class Solution {
5 | public boolean isMatch(String s, String p) {
6 | boolean[][] match=new boolean[s.length()+1][p.length()+1];
7 | match[s.length()][p.length()]=true;
8 | for(int i=p.length()-1;i>=0;i--){
9 | if(p.charAt(i)!='*')
10 | break;
11 | else
12 | match[s.length()][i]=true;
13 | }
14 | for(int i=s.length()-1;i>=0;i--){
15 | for(int j=p.length()-1;j>=0;j--){
16 | if(s.charAt(i)==p.charAt(j)||p.charAt(j)=='?')
17 | match[i][j]=match[i+1][j+1];
18 | else if(p.charAt(j)=='*')
19 | match[i][j]=match[i+1][j]||match[i][j+1];
20 | else
21 | match[i][j]=false;
22 | }
23 | }
24 | return match[0][0];
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/lengthOfLastWord.java:
--------------------------------------------------------------------------------
1 | class Solution {
2 | public int lengthOfLastWord(String s) {
3 | int length = 0;
4 | for (int i = s.length() - 1; i >= 0; i--) {
5 | if (s.charAt(i) != ' ') {
6 | length++;
7 | } else {
8 | if (length > 0) return length;
9 | }
10 | }
11 | return length;
12 | }
13 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | LeetCodeSolutions
15 | jar
16 |
17 | LeetCode Solutions
18 |
19 |
20 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/search-in-a-binary-search-tree.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for a binary tree node.
3 | * public class TreeNode {
4 | * int val;
5 | * TreeNode left;
6 | * TreeNode right;
7 | * TreeNode() {}
8 | * TreeNode(int val) { this.val = val; }
9 | * TreeNode(int val, TreeNode left, TreeNode right) {
10 | * this.val = val;
11 | * this.left = left;
12 | * this.right = right;
13 | * }
14 | * }
15 | */
16 | class Solution {
17 | public TreeNode searchBST(TreeNode root, int val) {
18 | if(root==null)
19 | return null;
20 | if(root.val==val)
21 | return root;
22 | else if(root.val 0; i++) {
31 | n -= i;
32 | if (n < 0) {
33 | break;
34 | }
35 | count++;
36 | }
37 | return count;
38 | }
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/FindPeakInMountArray.java:
--------------------------------------------------------------------------------
1 | public class FindPeakInMountArray {
2 | public static void main(String[] args) {
3 | // Leetcode default...............code
4 |
5 | }
6 |
7 |
8 | public int peakIndexInMountainArray(int[] arr) {
9 | int start= 0;
10 | int end =arr.length-1;
11 |
12 | while(startarr[mid+1]){
15 | end = mid;
16 | }else {
17 | start= mid+1;
18 | }
19 | }
20 | return start;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/HouseRobber.java:
--------------------------------------------------------------------------------
1 |
2 | //Problem - https://leetcode.com/problems/house-robber
3 |
4 | public class HouseRobber {
5 |
6 | public static void main(String[] args) {
7 | int[] nums = {1,2,3,1};
8 | System.out.println(rob(nums));
9 | }
10 | public static int rob(int[] nums) {
11 | int n = nums.length;
12 | int[] dp = new int[n];
13 | if(n == 1) {
14 | return nums[0];
15 | } else if(n == 2) {
16 | return Math.max(nums[0], nums[1]);
17 | }
18 | dp[0] = nums[0];
19 | dp[1] = Math.max(nums[0], nums[1]);
20 | for(int i = 2;i=0;i--)
19 | {
20 | if(nums[i]+i>=index) //if sum of nums[i]+i> index then this means it can reach index from the position i
21 | index=i; //updating index
22 | }
23 | return index==0; //if index turns out to be 0, we can reach the end from the 0th index i.e. the starting point. Hence, returns True else false.
24 | }
25 | }
26 |
27 | //Time Complexity: O(N) , where N is length of given array.
28 | //Space Complexity: O(1), no extra space is needed in this solution.
29 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/KMissingPositiveNumber.java:
--------------------------------------------------------------------------------
1 | public class KMissingPositiveNumber {
2 | public static void main(String[] args) {
3 | //Leetcode default code....
4 | }
5 |
6 | public int findKthPositive(int[] arr, int k) {
7 |
8 | int missing = 0;
9 | int j = 1;
10 | int i = 0;
11 | while(i queue = new PriorityQueue<>(Collections.reverseOrder());
15 | for (int i : inputArray)
16 | queue.add(i);
17 | while (k > 1) {
18 | queue.poll();
19 | k--;
20 | }
21 | return Objects.isNull(queue.peek()) ? null : queue.peek(); //returns kth largest element
22 | }
23 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/LongestSubstringWithoutRepeatingCharacters.java:
--------------------------------------------------------------------------------
1 | import java.util.Map;
2 | import java.util.HashMap;
3 |
4 | public class LongestSubstringWithoutRepeatingCharacters {
5 | public int lengthOfLongestSubstring(String s) {
6 | if (s.length() == 0) {
7 | return 0;
8 | }
9 |
10 | int maxLength = 1, last = 0;
11 | Map map = new HashMap<>();
12 |
13 | for (int i = 0; i < s.length(); i++) {
14 | char c = s.charAt(i);
15 | if (map.containsKey(c)) {
16 | last = Math.max(last, map.get(c) + 1);
17 | }
18 |
19 | map.put(c, i);
20 | maxLength = Math.max(maxLength, i - last + 1);
21 | }
22 |
23 | return maxLength;
24 | }
25 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/MaxConsecutiveOnes.java:
--------------------------------------------------------------------------------
1 | public class MaxConsecutiveOnes {
2 | public int findMaxConsecutiveOnes(int[] nums) {
3 | int consec=0;
4 | int result=0;
5 | for(int i = 0 ; i < nums.length;i++){
6 | if(nums[i]==1){
7 | consec++;
8 | if(resultarr[mid+1]){
11 | end = mid;
12 | }
13 | else{
14 | start=mid+1;
15 | }
16 | }
17 | return start;
18 |
19 | }
20 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/RemoveElement.java:
--------------------------------------------------------------------------------
1 | public class RemoveElement {
2 |
3 | void delElement(int[] arr, int key, int len){
4 | for(int i=key;i= 1; i--) {
13 | dpik0[i] = Math.max(dpik0[i], dpik1[i] + val);
14 | dpik1[i] = Math.max(dpik1[i], dpik0[i - 1] - val);
15 | }
16 | }
17 |
18 | return dpik0[k];
19 | }
20 | }
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/java/searchInMountainArray.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | public class searchInMountainArray {
4 |
5 | //find index of the target in mountain array
6 |
7 | public static void main(String[] args) {
8 | int[] arr = {1, 2, 5, 3, 2, 1};
9 | int target = 5;
10 | System.out.println(searachMountain(arr,target));
11 | }
12 | static int searachMountain(int[] arr, int target) {
13 | int start = 0;
14 | int end = arr.length - 1;
15 |
16 | while (start <= end) {
17 | int mid = start + (end - start) / 2;
18 | if (arr[mid] == target) {
19 | return mid;
20 | } else if (arr[mid] > target) {
21 | end = mid - 1;
22 | } else {
23 | if (arr[mid] > arr[mid + 1]) {
24 | end = mid - 1;
25 | } else {
26 | start = mid + 1;
27 | }
28 | }
29 | }
30 | return -1;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/src/main/main.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/LeetCodeSolutions/validAnagram.java:
--------------------------------------------------------------------------------
1 | public class Solution {
2 | public boolean isAnagram(String s, String t) {
3 | int[] alphabet = new int[26];
4 |
5 | for (int i = 0; i < s.length(); i++) {
6 | alphabet[s.charAt(i) - 'a']++;
7 | }
8 | for (int i = 0; i < t.length(); i++) {
9 | alphabet[t.charAt(i) - 'a']--;
10 | }
11 | for (int i : alphabet) {
12 | if (i != 0) {
13 | return false;
14 | }
15 | }
16 |
17 | return true;
18 | }
19 | }
--------------------------------------------------------------------------------
/LinkedList/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | LinkedList
13 |
14 |
15 |
--------------------------------------------------------------------------------
/LinkedLists/SinglyLinkedLists/Delete_linked_list.java:
--------------------------------------------------------------------------------
1 | // Java program to delete a linked list
2 | class LinkedList
3 | {
4 | Node head; // head of the list
5 |
6 | /* Linked List node */
7 | class Node
8 | {
9 | int data;
10 | Node next;
11 | Node(int d) { data = d; next = null; }
12 | }
13 |
14 | /* Function deletes the entire linked list */
15 | void deleteList()
16 | {
17 | head = null;
18 | }
19 |
20 | /* Inserts a new Node at front of the list. */
21 | public void push(int new_data)
22 | {
23 | /* 1 & 2: Allocate the Node &
24 | Put in the data*/
25 | Node new_node = new Node(new_data);
26 |
27 | /* 3. Make next of new Node as head */
28 | new_node.next = head;
29 |
30 | /* 4. Move the head to point to new Node */
31 | head = new_node;
32 | }
33 |
34 | public static void main(String [] args)
35 | {
36 | LinkedList llist = new LinkedList();
37 | /* Use push() to construct below list
38 | 1->12->1->4->1 */
39 |
40 | llist.push(1);
41 | llist.push(4);
42 | llist.push(1);
43 | llist.push(12);
44 | llist.push(1);
45 |
46 | System.out.println("Deleting the list");
47 | llist.deleteList();
48 |
49 | System.out.println("Linked list deleted");
50 | }
51 | }
52 |
53 |
--------------------------------------------------------------------------------
/Longest Palindromic Subsequence/ProblemStatement.txt:
--------------------------------------------------------------------------------
1 | A palindromic string is a string that is equal to its reverse. For example: “nitin” is a palindromic string. Now the question states to find the length of the longest palindromic subsequence of a string. It is that palindromic subsequence of the given string with the greatest length.
--------------------------------------------------------------------------------
/MatrixOperations/MatrixChainMultiplicationImplementation.java:
--------------------------------------------------------------------------------
1 |
2 | class MatrixChainMultiplication {
3 | // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
4 | static int MatrixChainOrder(int p[], int i, int j)
5 | {
6 | if (i == j)
7 | return 0;
8 |
9 | int min = Integer.MAX_VALUE;
10 |
11 | // place parenthesis at different places between first
12 | // and last matrix, recursively calculate count of
13 | // multiplications for each parenthesis placement and
14 | // return the minimum count
15 | for (int k = i; k < j; k++) {
16 | int count = MatrixChainOrder(p, i, k) +
17 | MatrixChainOrder(p, k + 1, j) +
18 | p[i - 1] * p[k] * p[j];
19 |
20 | if (count < min)
21 | min = count;
22 | }
23 |
24 | // Return minimum count
25 | return min;
26 | }
27 |
28 | // Driver program to test above function
29 | public static void main(String args[])
30 | {
31 | int arr[] = new int[] { 1, 2, 3, 4, 3 };
32 | int n = arr.length;
33 |
34 | System.out.println("Minimum number of multiplications is "
35 | + MatrixChainOrder(arr, 1, n - 1));
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/MatrixOperations/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | MatrixOperations
15 | jar
16 |
17 | matrix_operation.Matrix Operations
18 |
19 |
20 |
--------------------------------------------------------------------------------
/NoMatcher/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | NoMatcher
15 | jar
16 |
17 | No Matcher
18 |
19 |
20 |
--------------------------------------------------------------------------------
/NoMatcher/src/main/java/NoMatcher.java:
--------------------------------------------------------------------------------
1 | class NoMatchException extends Exception {
2 | public NoMatchException(String message){
3 | super(message);
4 | }
5 | }
6 |
7 | class IndiaAssertComparer {
8 |
9 | private String s;
10 |
11 | IndiaAssertComparer(String s) throws NoMatchException {
12 | this.s = s;
13 |
14 | if (s.equals("India")) {
15 | System.out.print("Matched!\n");
16 | } else {
17 | throw new NoMatchException("Not Matched!\n");
18 | }
19 | }
20 | }
21 |
22 | public class NoMatcher {
23 | public static void main(String[] a) throws NoMatchException {
24 | IndiaAssertComparer v = new IndiaAssertComparer("America");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Numbers Programs/Armstrong Number.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | public class Armstrong {
3 |
4 | public static void main (String[] args) {
5 | Scanner sc=new Scanner(System.in);
6 | int number=sc.nextInt();
7 | int originalNumber, remainder, result = 0, count=0;
8 |
9 | originalNumber = number;
10 |
11 | while (originalNumber != 0)
12 | {
13 | count++;
14 | originalNumber /= 10;
15 | }
16 | originalNumber = number;
17 | while (originalNumber != 0)
18 | {
19 | remainder = originalNumber % 10;
20 | result += Math.pow(remainder, count);
21 | originalNumber /= 10;
22 | }
23 | if(result == number)
24 | System.out.println(number + " is an Armstrong number.");
25 | else
26 | System.out.println(number + " is not an Armstrong number.");
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Numbers Programs/CatalanNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | Java program to print the Nth Catalan Number
3 | In combinatorial mathematics, the Catalan numbers are a sequence of natural numbers
4 | that occur in various counting problems,
5 | often involving recursively defined objects.
6 | */
7 | import java.util.*;
8 | public class CatalanNumber {
9 | static int Catalan(int n)
10 | {
11 | int res = 0;
12 |
13 | // Base case
14 | if (n <= 1) {
15 | return 1;
16 | }
17 | for (int i = 0; i < n; i++) {
18 | res += catalan(i) * catalan(n - i - 1);
19 | }
20 | return res;
21 | }
22 |
23 | public static void main(String[] args)
24 | {
25 | Scanner sc = new Scanner(System.in);
26 | System.out.println("Enter the value of n:");
27 | int n=sc.nextInt();
28 | System.out.println("Catalan Number is: "+Catalan(n));
29 | }
30 | }
--------------------------------------------------------------------------------
/Numbers Programs/HappyNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | Java program to check whether a number is a Happy Number or Not
3 | In number theory, a Happy Number is a number which eventually
4 | reaches 1 when replaced by the sum of the square of each digit.
5 | */
6 | import java.util.*;
7 | public class HappyNumber {
8 | static boolean isHappyNumber(int n)
9 | {
10 | Set unique_num = new HashSet();
11 | while (unique_num.add(n))
12 | {
13 | int value = 0;
14 | while (n > 0)
15 | {
16 | value += Math.pow(n % 10, 2);
17 | n /= 10;
18 | }
19 | n = value;
20 | }
21 | return n == 1;
22 | }
23 | public static void main(String[] args)
24 | {
25 | Scanner sc = new Scanner(System.in);
26 | System.out.println("Enter a number");
27 | int n=sc.nextInt();
28 | if(isHappyNumber(n))
29 | {
30 | System.out.println("Happy Number!");
31 | }
32 | else
33 | {
34 | System.out.println("Not a Happy Number!");
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/Numbers Programs/Palindrome Number.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | public class Main
3 | {
4 | public static String palindrome(int n)
5 | {
6 | int n1=n;
7 | int rev=0;
8 | int rem=0;
9 | while(n1>0)
10 | {
11 | rem=n1%10;
12 | rev=rev*10+rem;
13 | n1/=10;
14 | }
15 | if(rev==n)
16 | return "It is a palindrome number";
17 | else
18 | return "It is not a palindrome number";
19 | }
20 | public static void main(String[] args)
21 | {
22 | Scanner sc=new Scanner(System.in);
23 | System.out.println("Enter the number to be checked for palindrome");
24 | int num=sc.nextInt();
25 | String p= palindrome(num);
26 | System.out.println(p);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Numbers Programs/PerfectNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | Java program to check whether a number is a Perfect Number or Not
3 | A number whose sum of factors (excluding the number itself) is equal to the number
4 | is called a perfect number. In other words,
5 | if the sum of positive divisors (excluding the number itself) of a number equals the number itself is called a perfect number.
6 | */
7 | import java.util.*;
8 | public class PerfectNumber {
9 | static boolean isPerfectNumber(int n)
10 | {
11 | long k=n;
12 | long sum=0;
13 | for(int i=1; i <= n/2; i++)
14 | {
15 | if(n % i == 0)
16 | {
17 | sum=sum + i;
18 | }
19 | }
20 | return sum==k;
21 | }
22 | public static void main(String[] args)
23 | {
24 | Scanner sc = new Scanner(System.in);
25 | System.out.println("Enter a number");
26 | int n=sc.nextInt();
27 | if(isPerfectNumber(n))
28 | {
29 | System.out.println("Perfect Number!");
30 | }
31 | else
32 | {
33 | System.out.println("Not a Perfect Number!");
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Numbers Programs/all_subsets_of_first_n_natural_numbers.java:
--------------------------------------------------------------------------------
1 | import java.util.ArrayList;
2 | public class all_subsets_of_first_n_natural_numbers {
3 | public static void printSubsets(ArrayList subset) {
4 | for(int i=0; i subset) {
10 | if(n == 0) {
11 | printSubsets(subset);
12 | return;
13 | }
14 | findSubsets(n-1, subset);
15 | subset.add(n);
16 | findSubsets(n-1, subset);
17 | subset.remove(subset.size() - 1);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/OTPGenerator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | OTPGenerator
15 | jar
16 |
17 | OTP Generator
18 |
19 |
20 |
--------------------------------------------------------------------------------
/OTPGenerator/src/main/java/OtpGenerator.java:
--------------------------------------------------------------------------------
1 | import org.apache.commons.lang.RandomStringUtils;
2 | import java.util.Random;
3 |
4 | public class OtpGenerator {
5 |
6 | public static void main(String[] args) {
7 |
8 | String numbers = RandomStringUtils.randomNumeric(10);
9 |
10 | Random rand = new Random();
11 | int length = 6;
12 |
13 | char[] otp = new char[length];
14 |
15 | for (int i = 0; i < length; i++) {
16 |
17 | otp[i] = numbers.charAt(rand.nextInt(numbers.length()));
18 |
19 | }
20 |
21 | System.out.println(otp);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/PassGenerator/README.md:
--------------------------------------------------------------------------------
1 | Password Generator
2 |
3 | I do not own copyrights.
4 |
--------------------------------------------------------------------------------
/PassGenerator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | PassGenerator
15 | jar
16 |
17 | Password Generator
18 |
19 |
20 |
--------------------------------------------------------------------------------
/PasswordValidationInJava/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | PasswordValidationInJava
15 | jar
16 |
17 | Password Validation In Java
18 |
19 |
20 |
--------------------------------------------------------------------------------
/PrintOneToHundredWithoutLoop/PrintOneToHundredWithoutLoopAndRecursion.java:
--------------------------------------------------------------------------------
1 | class PrintOneToHundred
2 | {
3 | // Java program to print the map of India
4 | public static void main(String[] args)
5 | {
6 | String set = new BitSet() {{set(1,101);}}.toString();
7 | System.out.append(set,1,set.length());
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/RMI/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | RMI
13 |
14 |
15 |
--------------------------------------------------------------------------------
/RMI/src/main/java/Client.java:
--------------------------------------------------------------------------------
1 |
2 | import java.rmi.Naming;
3 | public class Client{
4 | public static void main(String[] argv)
5 | {
6 | try
7 | {
8 | HelloInt hello=(HelloInt)Naming.lookup("//127.0.0.1/Hello");
9 | System.out.println(hello.say());
10 | }
11 | catch(Exception e)
12 | {
13 | System.out.println("HelloClient exception:" +e);
14 | }
15 |
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/RMI/src/main/java/HelloImpl.java:
--------------------------------------------------------------------------------
1 |
2 | import java.rmi.*;
3 | import java.rmi.server.*;
4 | public class HelloImpl extends UnicastRemoteObject implements HelloInt
5 | {
6 | private String message;
7 | public HelloImpl(String msg) throws RemoteException
8 | {
9 | message= msg;
10 | }
11 | public String say() throws RemoteException
12 | {
13 | return message;
14 | }
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/RMI/src/main/java/HelloInt.java:
--------------------------------------------------------------------------------
1 |
2 | import java.rmi.*;
3 | public interface HelloInt extends Remote
4 | {
5 | public String say() throws RemoteException;
6 | }
7 |
8 |
--------------------------------------------------------------------------------
/RMI/src/main/java/Server.java:
--------------------------------------------------------------------------------
1 |
2 | import java.rmi.Naming;
3 | public class Server{
4 | public static void main(String[] argv){
5 | try
6 | {
7 | Naming.rebind("Hello", new HelloImpl ("Hello, this is a java RMI program"));
8 | System.out.println("server is connected");
9 | }
10 | catch(Exception e)
11 | {
12 | System.out.println("server is not connected"+e);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/RainWaterProblem/TrappingRainwaterProblem.java:
--------------------------------------------------------------------------------
1 | public class TrappingRainwaterProblem {
2 | public static void main(String[] args) {
3 | int[] arr = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
4 | int ans = rainWater(arr);
5 | System.out.println(ans);
6 | }
7 | static int rainWater(int arr[]) {
8 | int n = arr.length;
9 | int left[] = new int[n];
10 | int right[] = new int[n];
11 |
12 | left[0] = arr[0];
13 | for(int i=1; i= 0; i--) {
19 | right[i] = Math.max(right[i+1], arr[i]);
20 | }
21 |
22 | int ans = 0;
23 | for(int i=0; i=0;i--){
14 | rightmax[i]=Math.max(height[i],leftmax[i+1]);
15 | }
16 | int trappedwat=0;
17 |
18 | //loop
19 | for(int i=0;i= arr[start] && key <= arr[end]){
7 | if(start == end){
8 | if(arr[start] == key) return start ;
9 | return -1 ;
10 | }
11 | int pos = start + ((end - start) / (arr[end] - arr[start])*(key - arr[start]));
12 | if(arr[pos] == key)
13 | return pos ;
14 | if(arr[pos] < key)
15 | start = pos + 1 ;
16 | else
17 | end = pos - 1;
18 | }
19 | return -1 ;
20 | }
21 |
22 | public static void main(String ...s){
23 | int arr[]= {1,3,5,7,9,13,19,30};
24 | int key = 19;
25 | int index = interpolation_search(arr, key);
26 | System.out.println("-----INTERPOLATION SEARCH-----");
27 | System.out.print("The key " + key + " ");
28 | System.out.print((index == -1) ? "doesn't exist in the Array." : ("is present at index: " + index));
29 |
30 | }
31 | }
--------------------------------------------------------------------------------
/RandomInterviewQuestions/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | RandomInterviewQuestions
13 |
14 |
15 |
--------------------------------------------------------------------------------
/RandomInterviewQuestions/src/main/resources/ReadMe.md:
--------------------------------------------------------------------------------
1 | # Question description
2 |
3 | List the top k times frequently appearing numbers in an array. Example:
4 | [1,1,1,2,2,3] and k = 2 => [1,2]
--------------------------------------------------------------------------------
/Recursion/AllSubsets.java:
--------------------------------------------------------------------------------
1 |
2 | import java.io.IOException;
3 |
4 | class AllSubsets
5 | {
6 | static void printSubsets(char set[])
7 | {
8 | int n = set.length;
9 | for (int i = 0; i < (1< 0)
14 | System.out.print(set[j] + " ");
15 |
16 | System.out.println("}");
17 | }
18 | }
19 |
20 | // Driver code
21 | public static void main(String[] args)
22 | {
23 | char set[] = {'a', 'b', 'c', 'd'};
24 | printSubsets(set);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Recursion/BinarySearch.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | public class BinarySearch {
4 | public static void main(String[] args) {
5 | int[] a = {1,2,3,4,5,6};
6 | int n = a.length;
7 | int start = a[0];
8 | int end = a[n-1];
9 | int ans = binary(a,2,start,end);
10 | System.out.println(ans);
11 | }
12 | static int binary(int[] a,int target,int start, int end){
13 |
14 | int mid = start + (end-start)/2;
15 | if(a[mid] == target){
16 | return mid;
17 | }
18 | if(a[mid]>target){
19 | start = a[0];
20 | end = a[mid-1];
21 | return binary(a,target,start,end);
22 | }
23 | if(a[mid]arr[c+1]){
19 | int temp=arr[c];
20 | arr[c]=arr[c+1];
21 | arr[c+1]=temp;
22 | }
23 | bubble(arr,r,c+1);
24 | }
25 | else{
26 | bubble(arr,r-1,0);
27 | }
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Recursion/Count_zeros.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class Count_zeros {
3 | public static void main(String[] args) {
4 | Scanner sc=new Scanner(System.in);
5 | int n=sc.nextInt();
6 | System.out.println(count(n));
7 | }
8 | static int count(int n){
9 | if(n==0){
10 | return 0;
11 | }
12 | if(n%10==0){
13 | return 1+ count(n/10);
14 | }
15 | else{
16 | return count(n/10) ;
17 | }
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Recursion/FactorialOfNumber.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class FactorialOfNumber {
4 | public static void main(String[] args) {
5 | Scanner scanner = new Scanner(System.in);
6 | System.out.println("Enter a number for which you want to find a factorial: ");
7 | //taking input from the user
8 | int input = scanner.nextInt();
9 | System.out.println("Factorial of " + input + "! = "+getMyFactorialNumber(input));
10 | scanner.close();
11 | }
12 | public static long getMyFactorialNumber(int inputNumber) {
13 | if (inputNumber == 1)//base condition
14 | return 1;
15 | return inputNumber * getMyFactorialNumber(inputNumber - 1);//recursive call
16 | }
17 | }
--------------------------------------------------------------------------------
/Recursion/FibonacciSeries.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class FibonacciSeries{
4 | static int num1=0,num2=1,num3=0;
5 | static void fibonacci(int n){
6 | if(n>0){
7 | num3 = num1 + num2;
8 | num1 = num2;
9 | num2 = num3;
10 | System.out.print(" "+num3);
11 | fibonacci(n-1);
12 | }
13 | }
14 | public static void main(String args[]){
15 | Scanner scanner = new Scanner(System.in);
16 | System.out.println("Enter a number for which you want to find the Fibonacci Series: ");
17 | //taking input from the user
18 | int n = scanner.nextInt();
19 | System.out.print(num1+" "+num2);//printing constant first two digits 0 and 1
20 | fibonacci(n-2);//Since first two numbers are already done
21 | }
22 | }
--------------------------------------------------------------------------------
/Recursion/JosephusProblem.java:
--------------------------------------------------------------------------------
1 | public class Josephus {
2 | // "n" is the count of the numbers.
3 | // "k" is the fixed difference between the shots.
4 | public static int remaining(int n, int k){
5 | // The last person remaining is our winner.
6 | if(n==1){
7 | return 0;
8 | }
9 | // This is the recursive part.
10 | return (remaining(n-1,k)+k)%n;
11 | }
12 | // Let's understand this with the help of an example:-
13 | // Consider n = 5 and k = 3.
14 | // The recursive calls will keep taking place till "n" != 1, once "n" = 1 the call with parameters n = 1 will return 0.
15 | // Now call with parameter n = 2 will return (0+3) % 2 = 1.
16 | // Now call with parameter n = 3 will return (1+3) % 3 = 1.
17 | // Now call with parameter n = 4 will return (1+3) % 4 = 0.
18 | // Now call with parameter n = 5 will return (0+3) % 5 = 3 (which is our desired answer).
19 | }
20 |
--------------------------------------------------------------------------------
/Recursion/Power.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | class Power
3 | {
4 | static double power(int n, int P)
5 | {
6 | if (P == 0)
7 | return 1;
8 | else if(P>0)
9 | return n * power(n, P - 1);
10 | else
11 | return 1.0/n*power(n,P+1);
12 | }
13 |
14 | public static void main(String[] args)
15 | {
16 | Scanner sc=new Scanner(System.in);
17 | int n,P;
18 | System.out.println("Enter the Number: ");
19 | n=sc.nextInt();
20 | System.out.println("Enter the power: ");
21 | P=sc.nextInt();
22 | System.out.println("The power is: "+power(n, P));
23 | }
24 | }
--------------------------------------------------------------------------------
/Recursion/Prime.java:
--------------------------------------------------------------------------------
1 | package com.company;
2 |
3 | public class Prime {
4 | public static void main(String[] args) {
5 | int n = 9;
6 | int i=2;
7 | System.out.println(isPrime(n,i));
8 | }
9 | static boolean isPrime(int n, int i){
10 | //base case
11 | if(n<=2){
12 | return n==2 ? true:false;
13 | }
14 | if(n%i == 0){
15 | return false;
16 | }
17 | if(i*i > n){
18 | return true;
19 | }
20 | //if none of the cases are true then check for another divisor
21 | return isPrime(n,i+1);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Recursion/RecursiveSumOfAllNumbers.java:
--------------------------------------------------------------------------------
1 | public class RecursiveSumOfAllNumbers {
2 |
3 | public static void main(String[] args) {
4 | long sumOfAllNumbers = sumOfAllNumbers(9);
5 | System.out.println(sumOfAllNumbers);
6 | }
7 |
8 | /* A recursive Java example to sum all natural numbers up to a given long. */
9 | public static long sumOfAllNumbers(long number) {
10 | /* Stop the recursive Java program at zero */
11 | if (number != 0) {
12 | return number + sumOfAllNumbers(number - 1);
13 | } else {
14 | return number;
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Recursion/ReverseNumber.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class ReverseNumber {
3 | public static void main(String[] args) {
4 | Scanner sc=new Scanner(System.in);
5 | System.out.println("Enter the number");
6 | int n=sc.nextInt();
7 | System.out.println(recur(n));
8 | }
9 | static int recur(int n){
10 | int d=(int)(Math.log10(n))+1;
11 | return help(n,d);
12 | }
13 | static int help(int n,int d){
14 | if(n%10==n){
15 | return n;
16 | }
17 | int r=n%10;
18 | return r*(int)(Math.pow(10,d-1))+help(n/10,d-1);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Recursion/string.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | public class ReverseString
3 | {
4 | public String reverseString(String str)
5 | {
6 | if(str.isEmpty())
7 | {
8 | return str;
9 | }
10 | else
11 | {
12 | return reverseString(str.substring(1))+str.charAt(0);
13 | }
14 | }
15 |
16 | public static void main(String[] args)
17 | {
18 | ReverseString ob = new ReverseString();
19 | Scanner sc=new Scanner(System.in);
20 | System.out.println("Enter a string: ");
21 | String str=sc.nextLine();
22 | System.out.println(ob.reverseString(str));
23 | }
24 | }
--------------------------------------------------------------------------------
/ReverseOfTheString/ReverseOfTheString.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | public class ReverseOfTheString
3 | {
4 | public static void main(String[] args) {
5 | Scanner sc = new Scanner(System.in);
6 | System.out.println("Enter the string to be reversed");
7 | String s=sc.next();
8 | char ch;
9 | String s1="";
10 | for(int i=s.length()-1;i>=0;i--)
11 | {
12 | ch=s.charAt(i);
13 | s1=s1+ch;
14 | }
15 | System.out.println("Reversed String is "+s1);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/ReverseaNumber/main.java:
--------------------------------------------------------------------------------
1 | class Main {
2 | public static void main(String[] args) {
3 |
4 | int number = 23456, reversed = 0;
5 |
6 | System.out.println("Original Number: " + number);
7 |
8 | while(number != 0) {
9 | int digit = number % 10;
10 | reversed = reversed * 10 + digit;
11 | number /= 10;
12 | }
13 | System.out.println("");
14 | System.out.println("Reversed Number: " + reversed);
15 | }
16 | }
--------------------------------------------------------------------------------
/Small project/Multiplication.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class Multiplication {
4 | public static void main (String[] args) {
5 | int number, x=1, result;
6 |
7 | Scanner read = new Scanner(System.in);
8 |
9 | System.out.print("Choose a number to see the multiplication tables: ");
10 | number = read.nextInt();
11 |
12 | do {
13 | result = number*x;
14 | System.out.println(number+" x "+x+" = "+result);
15 | x++;
16 | }while (x<=10);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Small project/TemperatureConverter.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class TemperatureConverter {
4 | public static void main(String[] args) {
5 | double celsius, fahrenheit;
6 |
7 | Scanner read = new Scanner(System.in);
8 |
9 | System.out.println("Enter a temperature in Celsius to be converted to Fahrenheit: ");
10 | celsius = read.nextDouble(); //if you want to put a not integer number use virgula. ex: 18,3
11 |
12 | fahrenheit = (9*celsius+160)/5; // Fahrenheit formula
13 |
14 | System.out.println(celsius+" degrees Celsius = "+fahrenheit+" degrees Fahrenheit");
15 |
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Small project/Text-Binary Code Interconvertible utility/binary_string_converter.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.stream.Collectors;
3 |
4 | public class binary_string_converter {
5 |
6 | public static void main(String[] args) {
7 |
8 | String input = "01000101 01111000 01100001 01101101 01110000 01101100 01100101";
9 |
10 | String raw = Arrays.stream(input.split(" "))
11 | .map(binary -> Integer.parseInt(binary, 2))
12 | .map(Character::toString)
13 | .collect(Collectors.joining()); //cut the space
14 |
15 | System.out.println(raw);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Small project/ip_validator.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 |
3 | public class ip_validator {
4 | public static void main(String[] args) {
5 | System.out.print("Enter an IP address(IP-v4): ");
6 | Scanner sc = new Scanner(System.in);
7 |
8 | String ipAddress = sc.nextLine();
9 | boolean t = validateIpAddress(ipAddress);
10 | if (t == true)
11 | System.out.println("Valid");
12 | else
13 | System.out.println("Invalid");
14 | }
15 |
16 | public static boolean validateIpAddress(String ipAddress) {
17 | boolean b1 = false;
18 | StringTokenizer t = new StringTokenizer(ipAddress, ".");
19 | int a = Integer.parseInt(t.nextToken());
20 | int b = Integer.parseInt(t.nextToken());
21 | int c = Integer.parseInt(t.nextToken());
22 | int d = Integer.parseInt(t.nextToken());
23 | if ((a >= 0 && a <= 255) && (b >= 0 && b <= 255)
24 | && (c >= 0 && c <= 255) && (d >= 0 && d <= 255))
25 | b1 = true;
26 | return b1;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Snake Game with Various Features and GUI/GameFrame.java:
--------------------------------------------------------------------------------
1 | import javax.swing.*;
2 |
3 | //GAME FRAME CLASS EXTENDING JFRAME CLASS
4 | public class GameFrame extends JFrame {
5 |
6 | //CONSTRUCTOR
7 | GameFrame(){
8 |
9 | this.add(new GamePanel());
10 | this.setTitle("Snake");
11 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12 | this.setResizable(false);
13 | this.pack();
14 | this.setVisible(true);
15 | this.setLocationRelativeTo(null);
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Snake Game with Various Features and GUI/README.md:
--------------------------------------------------------------------------------
1 | # Snake-Game
2 |
3 | A simple Snake Game using JAVA . WIth three classes GameFrame GamePanel and SnakeGamer . Main class is in SnakeGamer . Features are Score Pause Resume Difficulty increases .
4 |
--------------------------------------------------------------------------------
/Snake Game with Various Features and GUI/SnakeGamer.java:
--------------------------------------------------------------------------------
1 | public class SnakeGamer {
2 | //MAIN
3 | public static void main(String[] args) {
4 | new GameFrame();
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/Sorting/InPlaceSort.java:
--------------------------------------------------------------------------------
1 | package Sorting;
2 |
3 | // A shared interface amongst sorting algorithms which
4 | public interface InPlaceSort {
5 | public void sort(int[] values);
6 | }
--------------------------------------------------------------------------------
/Sorting/Odd_even_sort.java:
--------------------------------------------------------------------------------
1 | // Java Program to implement
2 | // Odd-Even / Brick Sort
3 | import java.io.*;
4 |
5 | class GFG
6 | {
7 | public static void oddEvenSort(int arr[], int n)
8 | {
9 | boolean isSorted = false; // Initially array is unsorted
10 |
11 | while (!isSorted)
12 | {
13 | isSorted = true;
14 | int temp =0;
15 |
16 | // Perform Bubble sort on odd indexed element
17 | for (int i=1; i<=n-2; i=i+2)
18 | {
19 | if (arr[i] > arr[i+1])
20 | {
21 | temp = arr[i];
22 | arr[i] = arr[i+1];
23 | arr[i+1] = temp;
24 | isSorted = false;
25 | }
26 | }
27 |
28 | // Perform Bubble sort on even indexed element
29 | for (int i=0; i<=n-2; i=i+2)
30 | {
31 | if (arr[i] > arr[i+1])
32 | {
33 | temp = arr[i];
34 | arr[i] = arr[i+1];
35 | arr[i+1] = temp;
36 | isSorted = false;
37 | }
38 | }
39 | }
40 |
41 | return;
42 | }
43 | public static void main (String[] args)
44 | {
45 | int arr[] = {34, 2, 10, -9};
46 | int n = arr.length;
47 |
48 | oddEvenSort(arr, n);
49 | for (int i=0; i < n; i++)
50 | System.out.print(arr[i] + " ");
51 |
52 | System.out.println(" ");
53 | }
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/Sorting/Pigeonhole_sort.java:
--------------------------------------------------------------------------------
1 | /* Java program to implement Pigeonhole Sort */
2 |
3 | import java.lang.*;
4 | import java.util.*;
5 |
6 | public class GFG
7 | {
8 | public static void pigeonhole_sort(int arr[],
9 | int n)
10 | {
11 | int min = arr[0];
12 | int max = arr[0];
13 | int range, i, j, index;
14 |
15 | for(int a=0; a max)
18 | max = arr[a];
19 | if(arr[a] < min)
20 | min = arr[a];
21 | }
22 |
23 | range = max - min + 1;
24 | int[] phole = new int[range];
25 | Arrays.fill(phole, 0);
26 |
27 | for(i = 0; i0)
35 | arr[index++]=j+min;
36 |
37 | }
38 |
39 | public static void main(String[] args)
40 | {
41 | GFG sort = new GFG();
42 | int[] arr = {8, 3, 2, 7, 4, 6, 8};
43 |
44 | System.out.print("Sorted order is : ");
45 |
46 | sort.pigeonhole_sort(arr,arr.length);
47 |
48 | for(int i=0 ; i arr[i+1])
21 | {
22 | // swap arr[i], arr[i+1]
23 | int temp = arr[i];
24 | arr[i] = arr[i+1];
25 | arr[i+1] = temp;
26 | count = count+1;
27 | }
28 |
29 | // Check if any recursion happens or not
30 | // If any recursion is not happen then return
31 | if (count == 0)
32 | return;
33 |
34 | // Largest element is fixed,
35 | // recur for remaining array
36 | bubbleSort(arr, n-1);
37 | }
38 |
39 | // Driver Method
40 | public static void main(String[] args)
41 | {
42 | int arr[] = {64, 34, 25, 12, 22, 11, 90};
43 |
44 | bubbleSort(arr, arr.length);
45 |
46 | System.out.println("Sorted array : ");
47 | System.out.println(Arrays.toString(arr));
48 | }
49 | }
50 |
51 |
52 |
--------------------------------------------------------------------------------
/Sorting/Recursive_insertion_sort.java:
--------------------------------------------------------------------------------
1 | // Recursive Java program for insertion sort
2 |
3 | import java.util.Arrays;
4 |
5 | public class GFG
6 | {
7 | // Recursive function to sort an array using
8 | // insertion sort
9 | static void insertionSortRecursive(int arr[], int n)
10 | {
11 | // Base case
12 | if (n <= 1)
13 | return;
14 |
15 | // Sort first n-1 elements
16 | insertionSortRecursive( arr, n-1 );
17 |
18 | // Insert last element at its correct position
19 | // in sorted array.
20 | int last = arr[n-1];
21 | int j = n-2;
22 |
23 | /* Move elements of arr[0..i-1], that are
24 | greater than key, to one position ahead
25 | of their current position */
26 | while (j >= 0 && arr[j] > last)
27 | {
28 | arr[j+1] = arr[j];
29 | j--;
30 | }
31 | arr[j+1] = last;
32 | }
33 |
34 | // Driver Method
35 | public static void main(String[] args)
36 | {
37 | int arr[] = {12, 11, 13, 5, 6};
38 |
39 | insertionSortRecursive(arr, arr.length);
40 |
41 | System.out.println(Arrays.toString(arr));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Sorting/Stooge_sort.java:
--------------------------------------------------------------------------------
1 | // Java program to implement stooge sort
2 | import java.io.*;
3 |
4 | public class stooge {
5 | // Function to implement stooge sort
6 | static void stoogesort(int arr[], int l, int h)
7 | {
8 | if (l >= h)
9 | return;
10 |
11 | // If first element is smaller
12 | // than last, swap them
13 | if (arr[l] > arr[h]) {
14 | int t = arr[l];
15 | arr[l] = arr[h];
16 | arr[h] = t;
17 | }
18 |
19 | // If there are more than 2 elements in
20 | // the array
21 | if (h - l + 1 > 2) {
22 | int t = (h - l + 1) / 3;
23 |
24 | // Recursively sort first 2/3 elements
25 | stoogesort(arr, l, h - t);
26 |
27 | // Recursively sort last 2/3 elements
28 | stoogesort(arr, l + t, h);
29 |
30 | // Recursively sort first 2/3 elements
31 | // again to confirm
32 | stoogesort(arr, l, h - t);
33 | }
34 | }
35 |
36 | // Driver Code
37 | public static void main(String args[])
38 | {
39 | int arr[] = { 2, 4, 5, 3, 1 };
40 | int n = arr.length;
41 |
42 | stoogesort(arr, 0, n - 1);
43 |
44 | for (int i = 0; i < n; i++)
45 | System.out.print(arr[i] + " ");
46 | }
47 | }
48 |
49 |
--------------------------------------------------------------------------------
/Sorting/WaveSort.java:
--------------------------------------------------------------------------------
1 |
2 | public class WaveSort
3 | {
4 | void swap(int arr[], int a, int b)
5 | {
6 | int temp = arr[a];
7 | arr[a] = arr[b];
8 | arr[b] = temp;
9 | }
10 | void sortInWave(int arr[], int n)
11 | {
12 | for(int i = 0; i < n-1; i+=2){
13 | if(i > 0 && arr[i - 1] > arr[i])
14 | swap(arr, i, i-1);
15 | if(i < n-1 && arr[i + 1] > arr[i])
16 | swap(arr, i, i+1);
17 | }
18 | }
19 |
20 | // Driver program to test above function
21 | public static void main(String args[])
22 | {
23 | SortWave ob = new SortWave();
24 | int arr[] = {10, 90, 49, 2, 1, 5, 23};
25 | int n = arr.length;
26 | ob.sortInWave(arr, n);
27 | for (int i : arr)
28 | System.out.print(i+" ");
29 | }
30 | };
31 |
--------------------------------------------------------------------------------
/Stacks/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | Stacks
15 | jar
16 |
17 | Stacks
18 |
19 |
20 |
--------------------------------------------------------------------------------
/Steganography/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | Steganography
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Steganography/src/main/resources/README.md:
--------------------------------------------------------------------------------
1 | # LSB_Steganography
2 |
3 | ##### Precautions before execution (assuming that repository is already cloned. If not, then clone the repository first):
4 | 1. create 3 necessary files for encryption
5 | - any text file with filename message.txy
6 | - any sample image with name cover.png
7 | - stego image with file name steg.png
8 | 2. Go to each source file and rconfirm the path.
9 | 3. Compile and Execute the Program using **javac filename.java** and **java filename.class** resp.
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/String/shortestpath.java:
--------------------------------------------------------------------------------
1 | package String;
2 | public class shortestpath {
3 | public static float shortestPath(String str){
4 | int x=0,y=0;
5 | for(int i=0;i maxSum) {
14 | maxSum = curSum;
15 | }
16 | if(curSum < 0) {
17 | curSum = 0;
18 | }
19 | }
20 | return maxSum;
21 | }
22 | }
--------------------------------------------------------------------------------
/Timer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | LetUsJava
7 | com.let.us.java
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | Timer
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Timer/src/main/java/Main.java:
--------------------------------------------------------------------------------
1 | public class Main {
2 |
3 | public static void main(String[] args) {
4 | Timer timer = new Timer();
5 | timer.start();
6 | for (int i = 0; i < 1000; i++) {
7 | System.out.println(i);
8 | }
9 | System.out.println(timer.end());
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Timer/src/main/java/Timer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * cool timer
3 | */
4 | public class Timer {
5 |
6 | public long now;
7 |
8 | public void start() {
9 | now = System.currentTimeMillis();
10 | }
11 |
12 | public long end() {
13 | return System.currentTimeMillis() - now;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/TowersOfHanoi/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | TowersOfHanoi
15 | jar
16 |
17 | Towers Of Hanoi
18 |
19 |
20 |
--------------------------------------------------------------------------------
/TowersOfHanoi/src/main/java/TowersOfHanoi.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class TowersOfHanoi {
4 |
5 | public void solve(int n, String start, String auxiliary, String end) {
6 | if (n == 1) {
7 | System.out.println(start + " -> " + end);
8 | } else {
9 | solve(n - 1, start, end, auxiliary);
10 | System.out.println(start + " -> " + end);
11 | solve(n - 1, auxiliary, start, end);
12 | }
13 | }
14 |
15 | public static void main(String[] args) {
16 | TowersOfHanoi towersOfHanoi = new TowersOfHanoi();
17 | System.out.print("Enter number of discs: ");
18 | Scanner scanner = new Scanner(System.in);
19 | int discs = scanner.nextInt();
20 | towersOfHanoi.solve(discs, "A", "B", "C");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Wildcard Matching/ProblemStatement.txt:
--------------------------------------------------------------------------------
1 | #Problem Statement
2 |
3 | For every index of string S1, we have different options to match that index with string S2. Therefore, we can think in terms of string matching path as we have done already in previous questions.
4 |
5 | Either the characters match already.
6 | Or, if there is a ‘?’, we can explicitly match a single character.
7 | For a ‘*’, we can explicitly match a single character.
8 |
9 |
--------------------------------------------------------------------------------
/Zomcav/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 |
8 | com.let.us.java
9 | LetUsJava
10 | 1.0-SNAPSHOT
11 |
12 |
13 | com.let.us.java
14 | Zomcav
15 | jar
16 |
17 | Zomcav(Codechef)
18 |
19 |
20 |
--------------------------------------------------------------------------------
/armstrongNumberChecker/IsArmstrongNum.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.lang.Math;
3 |
4 | public class IsArmstrongNum {
5 | static boolean isArmstrongNum(int n){
6 | int digits=0, sum=0, last=0, temp;
7 | temp=n;
8 |
9 | while(temp>0){
10 | temp = temp/10;
11 | digits+=1;
12 | }
13 |
14 | temp=n;
15 | while(temp>0){
16 | last = temp %10;
17 | sum += (Math.pow(last,digits));
18 | temp = temp/10;
19 | }
20 |
21 | if (sum==n){
22 | return true;
23 | }
24 | else{
25 | return false;
26 | }
27 | }
28 | public static void main(String[] args) {
29 |
30 | System.out.println("///--------Program to check whether a number is an Armstrong Number or not---------///");
31 |
32 | Scanner sc = new Scanner(System.in);
33 | System.out.println("Enter the number to check : ");
34 | int num = sc.nextInt();
35 | if(isArmstrongNum(num)){
36 | System.out.println(num+" is a armstrong number");
37 | }
38 | else{
39 | System.out.println(num+" is not a armstrong number");
40 | }
41 | }
42 | }
--------------------------------------------------------------------------------
/arrays in java/array_bubble_char.java:
--------------------------------------------------------------------------------
1 |
2 | import java.util.Scanner;
3 | public class array_bubble_char
4 | {//ascending
5 | void bubblesort()
6 | {
7 | char c[],temp;
8 | int i,j,n;
9 | Scanner sc=new Scanner(System.in);
10 | System.out.println("Enter number of elements");
11 | n=sc.nextInt();
12 | c=new char[n];
13 | System.out.println("Enter "+n+" characters");
14 | for(i=0;ic[j+1])
23 | {
24 | temp=c[j];
25 | c[j]=c[j+1];
26 | c[j+1]=temp;
27 | }
28 | }
29 | }
30 | for(i=0;iarr[j+1])
23 | {
24 | temp=arr[j];
25 | arr[j]=arr[j+1];
26 | arr[j+1]=temp;
27 | }
28 | }
29 | }
30 | for(i=0;i0)
23 | {
24 | temp=c[j];
25 | c[j]=c[j+1];
26 | c[j+1]=temp;
27 | }
28 | }
29 | }
30 | for(i=0;i=0; --i) {
9 | reverseStr = reverseStr + str.charAt(i);
10 | }
11 |
12 | if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
13 | System.out.println(str + " is a palindrome");
14 | }
15 | else {
16 | System.out.println(str + " is not a palindrome");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/competitive programming/GFG.java:
--------------------------------------------------------------------------------
1 | //Java Program to generate hailstone numbers
2 | //Calculate steps required to reduce them to 1
3 | import java.util.*;
4 | class GFG{
5 | static int c;
6 | //Function to print hailstone numbers and to calculate the number of steps required
7 | static int HailstoneNumbers(int N)
8 | {
9 | System.out.println(N+" ");
10 | if(N==1 && c==0){
11 | //N is initially 1.
12 | return c;
13 | }
14 | else if(N==1 && c!=0){
15 | //N is reduced to 1.
16 | c++;
17 | return c;
18 | }
19 | else if(N%2==0){
20 | //If N is even.
21 | c++;
22 | HailstoneNumbers(N/2);
23 | }
24 | else if(N%2!=0){
25 | //Nis odd.
26 | c++;
27 | HailstoneNumbers(3*N+1);
28 | }
29 | return c;
30 | }
31 | //Driver code
32 | public static void main(String[] args)
33 | {
34 | int N=7;
35 | int x;
36 | //Function to generate Hailstone Numbers
37 | x=HailstoneNumbers(N);
38 | //Output: Number of steps
39 | System.out.println();
40 | System.out.println("Number of steps:" + x);
41 | }
42 | }
--------------------------------------------------------------------------------
/competitive programming/README.md:
--------------------------------------------------------------------------------
1 | ## AIM
2 |
3 | Generate Hailstone Numbers in Java
4 |
5 | -------------------------------------------------------------
6 |
7 | A Hailstone series is defined as follows:
8 | * * Start with any integer value greater than 0, say x.
9 | * If x is even, then the next value in the series is x/2.
10 | * If x is odd, then the next value in the series is 3x + 1.
11 | * Now apply the same rules to create the next value in the series.
12 |
13 |
14 | ## Output
15 |
16 | * 7
17 | * 22
18 | * 11
19 | * 34
20 | * 17
21 | * 52
22 | * 26
23 | * 13
24 | * 40
25 | * 20
26 | * 10
27 | * 5
28 | * 16
29 |
30 | **Number of steps : 17**
31 |
32 | Generating Hailstone Numbers is one of the competive programming questions in Java.
33 |
--------------------------------------------------------------------------------
/copy_array/Genrate_random_number/random.java:
--------------------------------------------------------------------------------
1 | import java.util.Random;
2 |
3 | public class RandomNumber
4 |
5 | {
6 |
7 | public static void main(String[] args)
8 |
9 | {
10 |
11 | randomInts(5);
12 |
13 | randomInts(9, 50, 90);
14 |
15 | }
16 |
17 | public static void randomInts(int num)
18 |
19 | {
20 |
21 | Random random = new Random();
22 |
23 | random.ints(num).forEach(System.out::println);
24 |
25 | }
26 |
27 | public static void randomInts(int num, int origin, int bound)
28 |
29 | {
30 |
31 | Random random1 = new Random();
32 |
33 | random1.ints(num, origin, bound).forEach(System.out::println);
34 |
35 | }
36 |
37 | }
--------------------------------------------------------------------------------
/copy_array/array.java:
--------------------------------------------------------------------------------
1 | public class array {
2 | public static void main(String[] args) {
3 |
4 | int [] arr1 = new int [] {1, 2, 3, 4, 5};
5 |
6 | int arr2[] = new int[arr1.length];
7 |
8 | for (int i = 0; i < arr1.length; i++) {
9 | arr2[i] = arr1[i];
10 | }
11 |
12 | System.out.println("Elements of original array: ");
13 | for (int i = 0; i < arr1.length; i++) {
14 | System.out.print(arr1[i] + " ");
15 | }
16 |
17 | System.out.println();
18 |
19 |
20 | System.out.println("Elements of new array: ");
21 | for (int i = 0; i < arr2.length; i++) {
22 | System.out.print(arr2[i] + " ");
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/interviewQuestions/LeapingFrog.java:
--------------------------------------------------------------------------------
1 | import java.util.Arrays;
2 | import java.util.Random;
3 |
4 | public class LeapingFrog {
5 | // leaping frog code, calculating the number of jumps needed for the frog to cross a pond.
6 | // pond has 20 stones, each with a random number between 1 and 5.
7 | // frog starts out on first stone and jumps as much as indicated on stone that it is on.
8 | public static void main(String[] args) {
9 |
10 | int[] stones = arrayBuilder();
11 | int total=0;
12 | int jump=0;
13 |
14 | for (int i=0; i< stones.length;){
15 | total += stones[i];
16 | jump++;
17 | i += stones[i];
18 | if (total > stones.length){
19 | break;
20 | }
21 | }
22 | System.out.println(Arrays.toString(stones));
23 | System.out.println(jump);
24 | }
25 |
26 | public static int[] arrayBuilder(){
27 | int[] array = new int[20];
28 | Random random = new Random();
29 | int min = 1, max = 5;
30 | for (int i=0; i= 1){
24 | System.out.print(value);
25 | value = value - 1;
26 | }
27 | System.out.println();
28 | row = row + 1;
29 | numStars = numStars + 2;
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------