├── LeetCode ├── Algorithms │ ├── Hard │ │ └── a.txt │ ├── Easy │ │ ├── Sum Multiples.java │ │ ├── Calculate Delayed Arrival Time.java │ │ ├── AddTwoIntegers.java │ │ ├── ReverseString.java │ │ ├── Maximum Sum With Exactly K Elements.java │ │ ├── Convert the Temperature.java │ │ ├── LengthofLastWord.java │ │ ├── SmallestEvenMultiple.java │ │ ├── Difference Between Element Sum and Digit Sum of an Array.java │ │ ├── reverseWordsInAString3.java │ │ ├── KeepMultiplyingFoundValuesbyTwo.java │ │ ├── Minimum Bit Flips to Convert Number.java │ │ ├── RunningSumof1dArray.java │ │ ├── NumberofCommonFactors.java │ │ ├── NumberofGoodPairs.java │ │ ├── containsDuplicate.java │ │ ├── DefanginganIPAddress.java │ │ ├── FindGreatestCommonDivisorofArray.java │ │ ├── XOROperationinanArray.java │ │ ├── SubtracttheProductandSumofDigitsofanInteger.java │ │ ├── Count the Digits That Divide a Number.java │ │ ├── Count Items Matching a Rule.java │ │ ├── PalindromeNumber.java │ │ ├── CountNegativeNumbersinaSortedMatrix.java │ │ ├── SmalletIndexWithEqualValue.java │ │ ├── MissingNumber.java │ │ ├── PercentageofLetterinString.java │ │ ├── SearchInsertPosition.java │ │ ├── AddDigits.java │ │ ├── concatenationOfArrays.java │ │ ├── CountingWordsWithaGivenPrefix.java │ │ ├── CountingBits.java │ │ ├── MaximumProductofTwoElementsinanArray.java │ │ ├── ShuffleString.java │ │ ├── JewelsandStones.java │ │ ├── buildArrayFromPermutation.java │ │ ├── FizzBuzz.java │ │ ├── ShuffletheArray.java │ │ ├── ToLowerCase.java │ │ ├── IntersectionofTwoArrays.java │ │ ├── NumberofPairsWithAbsoluteDifferenceK.java │ │ ├── ThousandSeparator.java │ │ ├── validAnagram.java │ │ ├── GenerateaStringWithCharactersThatHaveOddCounts.java │ │ ├── NumberofStepstoReduceaNumbertoZero.java │ │ ├── BinarySearch.java │ │ ├── RootEqualsSumofChildren.java │ │ ├── SumofAllOddLengthSubarrays.java │ │ ├── FindNumbersWithEvenNumberofDigits.java │ │ ├── fibonacciNumber.java │ │ ├── MakeArrayZerobySubtractingEqualAmounts.java │ │ ├── CountEqualandDivisiblePairsinanArray.java │ │ ├── panagram.java │ │ ├── SplitaStringinBalancedStrings.java │ │ ├── SumofUniqueElements.java │ │ ├── DecodeXORedArray.java │ │ ├── Sign oftheProductofanArray.java │ │ ├── RichestCustomerWealth.java │ │ ├── GoalParserInterpretation.java │ │ ├── MergeStringsAlternately.java │ │ ├── countAsterisks.java │ │ ├── TruncateSentence.java │ │ ├── numberOfArithmeticTriplets.java │ │ ├── twoSum.java │ │ ├── ReplaceAllDigitswithCharacters.java │ │ ├── TwoSum.java │ │ ├── DetermineColorofaChessboardSquare.java │ │ ├── CheckifAllA'sAppearsBeforeAllB's.java │ │ ├── MatrixDiagonalSum.java │ │ ├── MaximumNumberofWordsFoundinSentences.java │ │ ├── CheckIfTwoStringArraysareEquivalent.java │ │ ├── FindtheDistanceValueBetweenTwoArrays.java │ │ ├── UniqueNumberofOccurrences.java │ │ ├── ReversePrefixofWord.java │ │ ├── CheckifAllCharactersHaveEqualNumberofOccurrences.java │ │ ├── FinalValueofVariableAfterPerformingOperations.java │ │ ├── DecompressRun-LengthEncodedList.java │ │ ├── Left and Right Sum Differences.java │ │ ├── CountGoodTriplets.java │ │ ├── HowManyNumbersAreSmallerThantheCurrentNumber.java │ │ ├── FirstLettertoAppearTwice.java │ │ ├── CounttheNumberofConsistentStrings.java │ │ ├── MaximumNestingDepthoftheParentheses.java │ │ ├── MinimumSumofFourDigitNumberAfterSplittingDigits.java │ │ ├── FlippinganImage.java │ │ ├── Sort the People.java │ │ ├── Maximum69Number.java │ │ ├── KthDistinctStringinanArray.java │ │ ├── CountofMatchesinTournament.java │ │ ├── CountOperationstoObtainZero.java │ │ ├── CountPrefixesofaGivenString.java │ │ ├── MaximumProductDifferenceBetweenTwoPairs.java │ │ └── cellsInRangeOnAnExcelSheet.java │ └── Medium │ │ ├── maxSumFromSubarrayKADANES.java │ │ ├── ReverseWordsinaString.java │ │ ├── QueriesonNumberofPointsInsideaCircle.java │ │ └── kthLargestElementInAnArray.java └── Database │ ├── Easy │ └── a.txt │ └── Medium │ └── a.txt ├── HackerRank ├── Java │ ├── Strings │ │ └── intro.java │ ├── Introduction │ │ ├── welcome.java │ │ ├── stdInAndOut.java │ │ ├── loops1.java │ │ ├── stdInAndStdOut2.java │ │ ├── staticInitializer.java │ │ ├── EOF.java │ │ ├── ifElse.java │ │ └── intToString.java │ └── Data Structures │ │ ├── subarrayNegativeSumCount.java │ │ ├── 2D-array-hour-glass.java │ │ ├── hashSet.java │ │ ├── map.java │ │ └── comparator.java ├── README.md ├── Max sum sub array.java ├── Reverse array without using new array.c ├── LCM.c ├── Peak_Element.c ├── peakElement.c ├── Min max value in array.c ├── Reverse String using Stack.c ├── Jumping on Cloud.c ├── GCD or HCF.c ├── Fizz Buzz.cpp ├── Delete Array Value.c ├── kPanagrams.java ├── isomorphic strings using HashMap.java ├── Sub-array with largest sum.c └── Doubly Linked List to check string palindrome.c ├── Output Screenshots ├── patterns.jpg ├── Fizz Buzz.jpg ├── DeleteArrayElement.jpg ├── Lexicographical substrings.jpg ├── Reverse string using stack.jpg ├── Sub-array with largest sum.jpg └── Doubly Linked List to check string palindrome.png.jpg ├── Company Specific ├── TCS IPA │ └── Basics │ │ ├── characterAtOddPlaces.java │ │ └── averageBetweenLimits.java ├── TCS NQT │ ├── zeroOneBikerPairs.java │ ├── airportRiskSort.java │ ├── hashAndStar.java │ ├── makeDistinctLeastSum.java │ ├── leapyear.java │ ├── piggyBank max difference.java │ ├── superMarketProductPricing.java │ ├── cutCarpet.java │ ├── starAndHash.java │ ├── eventManagementTicket.java │ ├── numberSeriesTwist1.java │ ├── primeTwisted.java │ ├── decimalToBinary Toggle bits.java │ ├── parkingLot.java │ ├── paintingProperty.java │ ├── candyJar.java │ ├── goldWires BRUTEFORCE.java │ ├── greaterThanPrior.java │ ├── caesarCipher.java │ └── doctorClinic.java ├── ProGrad │ └── BinaryToDecimalTo0MinOperations.java ├── Veritas Prime Labs │ ├── elementAfterRotation O(1).java │ ├── BruteForce longestSubstringWithoutRepeatingChars.java │ ├── elementAfterRotation O(n^2).java │ └── elementAfterRotation O(n).java ├── SOTI │ ├── longestValidParentheses BRUTEFORCE.java │ └── validParenthesis.java ├── Verifone │ └── countPairsWithTheGivenSum.java ├── Dark Horse Digital │ ├── weddingBingoGame.java │ └── bodyBuildingsandRanking.java ├── Lithumus7 │ └── numberOfStockOutStalls.java └── Nest Digital │ └── minimumNumberOfToggles.java ├── README.md ├── DSA Sheet - Love Babbar ├── reverseTheArray.java ├── maxAndMinValueOfArray.java ├── dutchNationalFlag By couting.java ├── MaxSubArrayKadanesAlgo.java └── dutchNationalFlag3pointersAlgo.java ├── DSA Sheet - Raj Vikramaditya └── Binary Search │ ├── Search Insert Position.java │ └── Binary Search.java ├── DSA Sheet - Aditya Verma └── zeroOneKnapSack Recursive.java ├── LeetCode Sheet - Neetcode.io ├── Arrays&Hashing │ ├── containsDuplicate HashMap.java │ ├── anagram array-charCount.java │ └── Two Sum.java └── Two Pointers │ └── bestTimeBuySellStock BRUTEFORCE.java ├── LICENSE └── Pattern Problems └── Hpattern.java /LeetCode/Algorithms/Hard/a.txt: -------------------------------------------------------------------------------- 1 | a 2 | -------------------------------------------------------------------------------- /LeetCode/Database/Easy/a.txt: -------------------------------------------------------------------------------- 1 | a 2 | -------------------------------------------------------------------------------- /LeetCode/Database/Medium/a.txt: -------------------------------------------------------------------------------- 1 | a 2 | -------------------------------------------------------------------------------- /HackerRank/Java/Strings/intro.java: -------------------------------------------------------------------------------- 1 | a.t 2 | -------------------------------------------------------------------------------- /Output Screenshots/patterns.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/patterns.jpg -------------------------------------------------------------------------------- /Output Screenshots/Fizz Buzz.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/Fizz Buzz.jpg -------------------------------------------------------------------------------- /Output Screenshots/DeleteArrayElement.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/DeleteArrayElement.jpg -------------------------------------------------------------------------------- /Output Screenshots/Lexicographical substrings.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/Lexicographical substrings.jpg -------------------------------------------------------------------------------- /Output Screenshots/Reverse string using stack.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/Reverse string using stack.jpg -------------------------------------------------------------------------------- /Output Screenshots/Sub-array with largest sum.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/Sub-array with largest sum.jpg -------------------------------------------------------------------------------- /Output Screenshots/Doubly Linked List to check string palindrome.png.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ranjul-Arumadi/Coding-Problems/HEAD/Output Screenshots/Doubly Linked List to check string palindrome.png.jpg -------------------------------------------------------------------------------- /Company Specific/TCS IPA/Basics/characterAtOddPlaces.java: -------------------------------------------------------------------------------- 1 | class characterAtOddPlaces{ 2 | public static void main(String[] args) { 3 | String input = "Hello"; 4 | for(int i=0;i=24){ 9 | return Math.abs(24-finalTime); 10 | } 11 | return finalTime; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Company Specific/TCS NQT/zeroOneBikerPairs.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class zeroOneBikerPairs{ 3 | public static void main(String [] args){ 4 | int arr[] = {0,1,0,1,1}; 5 | 6 | int l = arr.length; 7 | 8 | int count=0; 9 | 10 | for(int i=0;ia[j+1]){ 11 | t = a[j]; 12 | a[j] = a[j+1]; 13 | a[j+1] = t; 14 | } 15 | } 16 | } 17 | 18 | for(int i=0;imax){ 10 | max = x; 11 | } 12 | } 13 | int sum=max; 14 | for(int i=1;i= target) 13 | end = mid; 14 | else 15 | start = mid + 1; 16 | } 17 | return start; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/LengthofLastWord.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string s consisting of words and spaces, return the length of the last word in the string. 3 | 4 | A word is a maximal 5 | substring 6 | consisting of non-space characters only. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: s = "Hello World" 13 | Output: 5 14 | Explanation: The last word is "World" with length 5. 15 | */ 16 | 17 | class Solution { 18 | public int lengthOfLastWord(String s) { 19 | String a[] = s.split(" "); 20 | int l = a.length; 21 | int ans = a[l-1].length(); 22 | 23 | return ans; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/SmallestEvenMultiple.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/smallest-even-multiple/ 3 | 4 | A guaranteed way to find a multiple of 2 and n is to multiply them together. When is this the answer, and when is there a smaller answer? 5 | 6 | There is a smaller answer when n is even. 7 | */ 8 | class Solution { 9 | public int smallestEvenMultiple(int n) { 10 | if(n*2 > n){ 11 | if(n%2!=0){ 12 | return n*2; 13 | } 14 | else{ 15 | return n; 16 | } 17 | 18 | } 19 | return n*2; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/Difference Between Element Sum and Digit Sum of an Array.java: -------------------------------------------------------------------------------- 1 | /* 2 | Link: https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/submissions/982923105/ 3 | */ 4 | class Solution { 5 | public int differenceOfSum(int[] nums) { 6 | int arraySum=0, digitSum=0; 7 | for(int i=0;i0){ 11 | digitSum+=val%10; 12 | val/=10; 13 | } 14 | } 15 | 16 | int ans = Math.abs(arraySum-digitSum); 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DSA Sheet - Love Babbar/maxAndMinValueOfArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class maxAndMinValueOfArray{ 3 | public static void main(String [] args){ 4 | Scanner sc = new Scanner(System.in); 5 | int arr[] = {1,2,3,0,-999,-786,121212}; 6 | 7 | int len = arr.length; 8 | 9 | int max=Integer.MIN_VALUE; 10 | int min = Integer.MAX_VALUE; 11 | 12 | int t1 =0; 13 | 14 | for(int i=0;imax){ 20 | max = t1; 21 | } 22 | } 23 | System.out.println("Largest: "+max); 24 | System.out.println("Smallest: "+min); 25 | } 26 | } -------------------------------------------------------------------------------- /HackerRank/Java/Introduction/stdInAndOut.java: -------------------------------------------------------------------------------- 1 | /* 2 | Question: https://www.hackerrank.com/challenges/java-stdin-and-stdout-1/problem?isFullScreen=true 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class Solution { 8 | 9 | public static void main(String[] args) { 10 | Scanner scan = new Scanner(System.in); 11 | int a = scan.nextInt(); 12 | int b = scan.nextInt(); 13 | int c = scan.nextInt(); 14 | // Complete this line 15 | // Complete this line 16 | 17 | System.out.println(a); 18 | System.out.println(b); 19 | System.out.println(c); 20 | // Complete this line 21 | // Complete this line 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/reverseWordsInAString3.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/reverse-words-in-a-string-iii/ 3 | */ 4 | 5 | import java.util.*; 6 | class reverseWordsInAString3{ 7 | public static void main(String [] args){ 8 | String s = "Let's take LeetCode contest"; 9 | 10 | String a[] = s.split(" "); 11 | int l = a.length; 12 | String ans = ""; 13 | String word = ""; 14 | int wl = 0; 15 | 16 | for(int i=0;i=0;j--){ 21 | ans+=word.charAt(j); 22 | } 23 | ans+=" "; 24 | } 25 | 26 | ans.trim(); 27 | System.out.println(ans); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Company Specific/TCS IPA/Basics/averageBetweenLimits.java: -------------------------------------------------------------------------------- 1 | /* Find average of a number array between two limit numbers (except the two numbers) 2 | * Input 3 | * --------------- 4 | * 5 5 | 1 6 | 2 7 | 3 8 | 4 9 | 5 10 | * 1 11 | * 4 12 | * 13 | * Output 14 | * --------------- 15 | * 2 16 | */ 17 | class averageBetweenLimits{ 18 | public static void main(String[] args) { 19 | int arr[] = {1,2,3,4,5,6,7,8}; 20 | int l = 1; 21 | int h = 5; 22 | 23 | int sum=0; 24 | int n=0; 25 | for(int i=l+1;imax){ 16 | max = sum; 17 | s=start; 18 | e=end; 19 | } 20 | sum=0; 21 | } 22 | 23 | } 24 | 25 | System.out.println("Max sub array sum: "+ max); 26 | for(int i=s;i<=e;i++){ 27 | System.out.print(arr[i]+" "); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/KeepMultiplyingFoundValuesbyTwo.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/keep-multiplying-found-values-by-two/ 3 | 4 | trick is to use do while loop in a clever way. 5 | */ 6 | class Solution { 7 | public int findFinalValue(int[] nums, int original) { 8 | int l = nums.length; 9 | int f = 1; 10 | int t = original; 11 | 12 | do{ 13 | f=1; 14 | for(int i=0;i0 || goal>0){ 10 | int startBit = start%2; 11 | start/=2; 12 | int goalBit = goal%2; 13 | goal/=2; 14 | int xorResult = startBit ^ goalBit; 15 | if(xorResult==1){ 16 | count++; 17 | } 18 | } 19 | 20 | return count; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /HackerRank/Java/Data Structures/2D-array-hour-glass.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://www.hackerrank.com/challenges/java-2d-array/problem?isFullScreen=true 3 | */ 4 | class a{ 5 | public static void main(String [] args){ 6 | 7 | int a[][] = { 8 | {1,1,1,0,0,0}, 9 | {0,1,0,0,0,0}, 10 | {1,1,1,0,0,0}, 11 | {0,0,2,4,4,0}, 12 | {0,0,0,2,0,0}, 13 | {0,0,1,2,4,0} 14 | }; 15 | 16 | int sum = Integer.MIN_VALUE; 17 | int tempSum = 0; 18 | 19 | for(int r=0;r<=3;r++){ 20 | for(int c=0;c<=3;c++){ 21 | tempSum+= 22 | a[r][c] + a[r][c+1] + a[r][c+2] 23 | + a[r+1][c+1] + 24 | a[r+2][c]+ a[r+2][c+1] + a[r+2][c+2]; 25 | 26 | sum = Math.max(sum, tempSum); 27 | tempSum=0; 28 | } 29 | } 30 | System.out.println(sum); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/RunningSumof1dArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/running-sum-of-1d-array/ 3 | Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). 4 | 5 | Return the running sum of nums. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,2,3,4] 12 | Output: [1,3,6,10] 13 | Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. 14 | */ 15 | class Solution { 16 | public int[] runningSum(int[] nums) { 17 | int sum=0; 18 | int l = nums.length; 19 | int ans[] = new int[l]; 20 | for(int i=0;i 0) { 7 | 8 | int N; 9 | scanf("%d", & N); 10 | int A[N]; 11 | for (int i = 0; i < N; i++) { 12 | scanf("%d", & A[i]); 13 | } 14 | 15 | //reversing 16 | int j = N - 1; 17 | int temp; 18 | for (int i = 0; i < N / 2; i++) { 19 | temp = A[i]; 20 | A[i] = A[j]; 21 | A[j] = temp; 22 | j--; 23 | } 24 | /* 25 | Using a tewmp varibale do swap of extreme values untill N/2. Exceeding N/2 or upto N will give back original array.While loop can also be used as i hm = new HashMap(); 18 | 19 | for(int i=0;imax){ 13 | max = nums[i]; 14 | } 15 | if(nums[i] 14 | int main() 15 | { 16 | int a,b,gcd=0; 17 | printf("\nEnter two numbers: "); 18 | scanf("%d %d",&a,&b); 19 | 20 | int x=a,y=b; 21 | 22 | while(a!=b){ 23 | 24 | if(a>b){ 25 | a-=b; 26 | } 27 | 28 | else{ 29 | b-=a; 30 | } 31 | } 32 | 33 | printf("\nLCM of is :%d ",(x*y/a)); //a stores the value of the gcd 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/SubtracttheProductandSumofDigitsofanInteger.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ 3 | 4 | Given an integer number n, return the difference between the product of its digits and the sum of its digits. 5 | 6 | 7 | Example 1: 8 | 9 | Input: n = 234 10 | Output: 15 11 | Explanation: 12 | Product of digits = 2 * 3 * 4 = 24 13 | Sum of digits = 2 + 3 + 4 = 9 14 | Result = 24 - 9 = 15 15 | */ 16 | 17 | import java.util.*; 18 | class SubtracttheProductandSumofDigitsofanInteger{ 19 | public static void main(String [] args){ 20 | int n = 234; 21 | int sum=0, prod=1; 22 | int digit = 0; 23 | while(n>0){ 24 | digit = n%10; 25 | sum+=digit; 26 | prod*=digit; 27 | n/=10; 28 | } 29 | 30 | System.out.println(prod-sum); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Company Specific/ProGrad/BinaryToDecimalTo0MinOperations.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | 4 | public class binaryToDecimal { 5 | public static void main(String args[]){ 6 | String input = "1011"; 7 | int l = input.length(); 8 | 9 | double decimal=0; 10 | int digit =0; 11 | int p=0; 12 | for(int i=input.length()-1;i>=0;i--){ 13 | digit = input.charAt(i) - '0'; 14 | decimal=decimal+ (digit*Math.pow(2,p++)); 15 | } 16 | 17 | 18 | //odd - -1 even - /2 19 | int d = (int)decimal; 20 | int c=0; 21 | while(d>0){ 22 | if(d%2!=0){ 23 | d-=1; 24 | } 25 | else{ 26 | d/=2; 27 | } 28 | c++; 29 | } 30 | System.out.println(c); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/Count the Digits That Divide a Number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Link: https://leetcode.com/problems/count-the-digits-that-divide-a-number/description/ 3 | 4 | Notes: 5 | 1. Integer.toString() inorder to get the length of the number as string 6 | 2. If unque digits were needed then we can use hashset instead of array to remove duplicated 7 | */ 8 | 9 | class Solution { 10 | public int countDigits(int num) { 11 | int numCopy = num; 12 | int count=0; 13 | String numString = Integer.toString(num); 14 | int len = numString.length(); 15 | int a[] = new int[len]; 16 | int i=0; 17 | while(numCopy>0){ 18 | a[i++] = numCopy%10; 19 | numCopy/=10; 20 | } 21 | 22 | for(int x: a){ 23 | if(num%x==0){ 24 | count++; 25 | } 26 | } 27 | 28 | return count; 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/Count Items Matching a Rule.java: -------------------------------------------------------------------------------- 1 | /* 2 | Basics: Refer https://github.com/Ranjul-Arumadi/Java-Basics/blob/main/Array%20List%20of%20Array%20List.java 3 | Link: https://leetcode.com/problems/count-items-matching-a-rule/description/ 4 | */ 5 | class Solution { 6 | public int countMatches(List> items, String ruleKey, String ruleValue) { 7 | int count=0; 8 | 9 | for(int i=0;i0){ 24 | digit = x%10; 25 | ans = (ans*10) + digit; 26 | x=x/10; 27 | } 28 | if(ans==copy){ 29 | return true; 30 | } 31 | else{ 32 | return false; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/CountNegativeNumbersinaSortedMatrix.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/ 3 | Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] 10 | Output: 8 11 | Explanation: There are 8 negatives number in the matrix. 12 | */ 13 | class Solution { 14 | public int countNegatives(int[][] grid) { 15 | int r = grid.length; 16 | int c = grid[0].length; 17 | int count=0; 18 | for(int i=0;i hs = new HashSet<>(); 26 | for(int i=0;icapacity){ 11 | return knapsack(weight, value, capacity, n-1); 12 | } 13 | else if(weight[n-1]<=capacity){ 14 | return Math.max(knapsack(weight, value, capacity, n-1), (value[n-1]+ knapsack(weight, value, capacity - weight[n-1], n-1))); 15 | } 16 | return 0; 17 | } 18 | public static void main(String [] args){ 19 | Scanner sc = new Scanner(System.in); 20 | 21 | int value[] = {60,100,120}; 22 | int weight[] = {10,20,30}; 23 | 24 | int capacity = 50; 25 | 26 | int size = weight.length; 27 | 28 | int maxProfit = knapsack(weight, value, capacity, size); 29 | System.out.println("Max prfit: "+maxProfit); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | /* 2 | https://leetcode.com/problems/search-insert-position/ 3 | Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 4 | 5 | You must write an algorithm with O(log n) runtime complexity. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: nums = [1,3,5,6], target = 5 12 | Output: 2 13 | */ 14 | 15 | class Solution { 16 | public int searchInsert(int[] nums, int target) { 17 | int start = 0, end = nums.length, mid; 18 | while (start < end) { 19 | mid = (start + end) / 2; 20 | if (nums[mid]==target){ 21 | return mid; 22 | } 23 | else if (nums[mid] >= target) 24 | end = mid; 25 | else 26 | start = mid + 1; 27 | } 28 | return start; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/AddDigits.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | link: https://leetcode.com/problems/add-digits/ 4 | Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: num = 38 11 | Output: 2 12 | Explanation: The process is 13 | 38 --> 3 + 8 --> 11 14 | 11 --> 1 + 1 --> 2 15 | Since 2 has only one digit, return it. 16 | 17 | equivalent to taking the remainder upon division by 9 (except when the digital root is 9, where the remainder upon division by 9 will be 0), which allows it to be used as a divisibility rule. 18 | 19 | */ 20 | 21 | class Solution { 22 | public int addDigits(int num) { 23 | if(num==0){ 24 | return 0; 25 | } 26 | 27 | int rem = num%9; 28 | 29 | if(rem==0){ 30 | return 9; 31 | } 32 | 33 | return rem; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Company Specific/TCS NQT/piggyBank max difference.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A family is about to break their piggy bank to use the money for different purposes. The piggy bank here 4 | represents an array (arr[]) consisting of N coins. The family has to split the coins of piggy bank into smaller 5 | stack (sub-array) of coins such that the sum of the difference between the maximum value and the minimum 6 | value of the coins for all the stacks (sub-arrays) is maximum. 7 | Note: Each value of the array can be used only once that is only in one subarray 8 | */ 9 | 10 | import java.util.*; 11 | class piggyBank{ 12 | public static void main(String [] args){ 13 | int arr[] = {8,1,7,9,2}; 14 | 15 | int l = arr.length; 16 | 17 | // for max difference we can diff min and max values of sorted array 18 | Arrays.sort(arr); 19 | int sum=0; 20 | for(int i=0, j=l-1 ;i=l && words[i].substring(0,l).equals(pref)){ 25 | count++; 26 | } 27 | } 28 | 29 | return count; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /HackerRank/Java/Data Structures/map.java: -------------------------------------------------------------------------------- 1 | /* 2 | LINK: https://www.hackerrank.com/challenges/phone-book/problem?isFullScreen=true 3 | */ 4 | 5 | 6 | //Complete this code or write your own from scratch 7 | import java.util.*; 8 | import java.io.*; 9 | 10 | class Solution{ 11 | public static void main(String []argh) 12 | { 13 | Scanner in = new Scanner(System.in); 14 | int n=in.nextInt(); 15 | in.nextLine(); 16 | 17 | HashMap hm = new HashMap<>(); 18 | 19 | for(int i=0;iValue of N 9 | Output: 10 | 160 -->Price 11 | Explanation: 12 | From the input above: 13 | Product of the digits: 5,1,4,4 14 | 5*2*4*4 = 160 15 | Hence Output is 160 16 | */ 17 | import java.util.*; 18 | class superMarketProductPricing{ 19 | public static void main(String [] args){ 20 | int price=5244; 21 | int digit = 0; 22 | int val=1; 23 | while(price>0){ 24 | digit = price%10; 25 | price/=10; 26 | val*=digit; 27 | } 28 | 29 | System.out.println("Price: "+val); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/CountingBits.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/counting-bits/ 3 | 4 | Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: n = 2 11 | Output: [0,1,1] 12 | Explanation: 13 | 0 --> 0 14 | 1 --> 1 15 | 2 --> 10 16 | */ 17 | 18 | class Solution { 19 | public int[] countBits(int n) { 20 | int a[] = new int[n+1]; 21 | int t = 0; 22 | int digit = 0; 23 | int c=0; 24 | int pos=0; 25 | 26 | for(int i=0;i<=n;i++){ 27 | t = i; 28 | while(t>0){ 29 | digit = t%2; 30 | if(digit==1){ 31 | c++; 32 | } 33 | t/=2; 34 | } 35 | a[pos++] = c; 36 | c=0; 37 | } 38 | 39 | return a; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/MaximumProductofTwoElementsinanArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/ 3 | 4 | Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1). 5 | 6 | 7 | Example 1: 8 | 9 | Input: nums = [3,4,5,2] 10 | Output: 12 11 | Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12. 12 | */ 13 | class Solution { 14 | public int maxProduct(int[] nums) { 15 | int max = Integer.MIN_VALUE; 16 | int l = nums.length; 17 | 18 | for(int i=0;i max){ 21 | max = (nums[i]-1) * (nums[j]-1); 22 | } 23 | } 24 | } 25 | 26 | return max; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Company Specific/TCS NQT/cutCarpet.java: -------------------------------------------------------------------------------- 1 | /* 2 | A carpet manufacturing industry has newly ventured into the carpet installation business. All the carpets 3 | manufactured are large squares in shape. To install, each carpet has to be cut into shapes of squares or 4 | rectangles. The number of slits to be made is given as N. 5 | The task is to find the maximum number of equal squares or rectangles that can be achieved using N slits. 6 | Note: 7 | The square carpet can be cut only using horizontal or vertical slits. 8 | Cuttings are done on a single carpet which should be rolled out completely i.e. no folding or stacking is 9 | allowed. 10 | Squares or rectangles cut should be equal size. 11 | Example 1: 12 | Input: 13 | 4 → Value of N(No. of cuts) 14 | Output: 15 | 9 → maximum number of equal squares or rectangle 16 | */ 17 | import java.util.*; 18 | class cutCarpet{ 19 | public static void main(String [] args){ 20 | int n=4; 21 | int x = n/2; 22 | System.out.println((x+1)*(n-x+1)); 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /HackerRank/Java/Introduction/stdInAndStdOut2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Link: https://www.hackerrank.com/challenges/java-stdin-stdout/problem?isFullScreen=true 3 | */ 4 | 5 | /* 6 | Issue with nextLine() after nextInt(): 7 | 8 | after the nextInt(), the cursor will stay after the number and wont read the string value. 9 | 10 | Solve by putting a dummy nextLine() after the nextInt() before doing the actual nextLine(). 11 | 12 | 13 | */ 14 | 15 | import java.util.Scanner; 16 | 17 | public class Solution { 18 | 19 | public static void main(String[] args) { 20 | Scanner scan = new Scanner(System.in); 21 | 22 | // <> 23 | int i = scan.nextInt(); scan.nextLine(); 24 | 25 | // <> 26 | Double d = scan.nextDouble(); scan.nextLine(); 27 | 28 | String s = scan.nextLine(); 29 | 30 | System.out.println("String: " + s); 31 | System.out.println("Double: " + d); 32 | System.out.println("Int: " + i); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /HackerRank/Java/Introduction/staticInitializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | Link: https://www.hackerrank.com/challenges/java-static-initializer-block/problem?isFullScreen=true 3 | */ 4 | 5 | import java.io.*; 6 | import java.util.*; 7 | import java.text.*; 8 | import java.math.*; 9 | import java.util.regex.*; 10 | 11 | public class Solution { 12 | 13 | 14 | static Scanner s = new Scanner(System.in); 15 | 16 | static int B = s.nextInt(); 17 | static int H = s.nextInt(); 18 | static boolean flag = true; 19 | 20 | static{ 21 | try{ 22 | if(B<0 || H<0){ 23 | flag = false; 24 | s.close(); 25 | throw new Exception("Breadth and height must be positive"); 26 | 27 | } 28 | } 29 | catch(Exception e){ 30 | System.out.println(e); 31 | } 32 | 33 | s.close(); 34 | } 35 | 36 | 37 | 38 | public static void main(String[] args){ 39 | if(flag){ 40 | int area=B*H; 41 | System.out.print(area); 42 | } 43 | 44 | }//end of main 45 | 46 | }//end of class 47 | 48 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/ShuffleString.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/shuffle-string/ 3 | 4 | You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. 5 | 6 | Return the shuffled string. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | 13 | Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] 14 | Output: "leetcode" 15 | Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. 16 | */ 17 | 18 | class Solution { 19 | public String restoreString(String s, int[] indices) { 20 | int l = s.length(); 21 | 22 | char a[] = new char[l]; 23 | 24 | a = s.toCharArray(); 25 | 26 | char b[] = new char[l]; 27 | 28 | for(int i=0;i l = new ArrayList<>(); 18 | while(s.hasNext()){ 19 | l.add(s.nextLine()); 20 | } 21 | 22 | int i=1; 23 | for(String x:l){ 24 | System.out.println(i+" "+x); 25 | i++; 26 | } 27 | } 28 | } 29 | 30 | OR 31 | 32 | 33 | BETTER-METHOD 34 | 35 | int number = 0; 36 | while (sc.hasNext()){ 37 | String file = sc.nextLine(); 38 | number ++; 39 | System.out.println(number + " " + file); 40 | } 41 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/fizz-buzz/ 3 | Given an integer n, return a string array answer (1-indexed) where: 4 | 5 | answer[i] == "FizzBuzz" if i is divisible by 3 and 5. 6 | answer[i] == "Fizz" if i is divisible by 3. 7 | answer[i] == "Buzz" if i is divisible by 5. 8 | answer[i] == i (as a string) if none of the above conditions are true. 9 | 10 | 11 | Example 1: 12 | 13 | Input: n = 3 14 | Output: ["1","2","Fizz"] 15 | */ 16 | class Solution { 17 | public List fizzBuzz(int n) { 18 | List l = new ArrayList(); 19 | 20 | for(int i=1;i<=n;i++){ 21 | if(i%3==0 && i%5==0){ 22 | l.add("FizzBuzz"); 23 | } 24 | else if(i%3==0){ 25 | l.add("Fizz"); 26 | } 27 | else if(i%5==0){ 28 | l.add("Buzz"); 29 | } 30 | else{ 31 | l.add(Integer.toString(i)); 32 | } 33 | } 34 | 35 | return l; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/ShuffletheArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/shuffle-the-array/ 3 | 4 | Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. 5 | 6 | Return the array in the form [x1,y1,x2,y2,...,xn,yn]. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: nums = [2,5,1,3,4,7], n = 3 13 | Output: [2,3,5,4,1,7] 14 | Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7]. 15 | */ 16 | 17 | class Solution { 18 | public int[] shuffle(int[] nums, int n) { 19 | int l = nums.length; 20 | int ans[] = new int[l]; 21 | /*int x=0; 22 | for(int i=0;i=65 && alpha<=90){ 33 | alpha+=32; 34 | x = (char)alpha; 35 | ans+=x; 36 | } 37 | else{ 38 | ans+=x; 39 | } 40 | } 41 | 42 | return ans; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /HackerRank/Java/Introduction/ifElse.java: -------------------------------------------------------------------------------- 1 | /* 2 | Question link: https://www.hackerrank.com/challenges/java-if-else/problem?isFullScreen=true 3 | */ 4 | 5 | import java.io.*; 6 | import java.math.*; 7 | import java.security.*; 8 | import java.text.*; 9 | import java.util.*; 10 | import java.util.concurrent.*; 11 | import java.util.regex.*; 12 | 13 | public class Solution { 14 | 15 | 16 | 17 | private static final Scanner scanner = new Scanner(System.in); 18 | 19 | public static void main(String[] args) { 20 | int N = scanner.nextInt(); 21 | scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 22 | 23 | if(N%2!=0){ 24 | System.out.println("Weird"); 25 | } 26 | else{ 27 | if(N>=2 && N<=5){ 28 | System.out.println("Not Weird"); 29 | } 30 | else if(N>=6 && N<=20){ 31 | System.out.println("Weird"); 32 | } 33 | else if(N>20){ 34 | System.out.println("Not Weird"); 35 | } 36 | } 37 | 38 | scanner.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/IntersectionofTwoArrays.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/intersection-of-two-arrays/ 3 | 4 | Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order. 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: nums1 = [1,2,2,1], nums2 = [2,2] 11 | Output: [2] 12 | */ 13 | class Solution { 14 | public int[] intersection(int[] nums1, int[] nums2) { 15 | 16 | int l1 = nums1.length; 17 | int l2 = nums2.length; 18 | 19 | HashSet s = new HashSet(); 20 | 21 | for(int i=0;imax replace max with sum value 19 | 4. max will have the answer 20 | */ 21 | 22 | int max = nums[0]; 23 | int sum = 0; 24 | /*if(nums.length==1){ 25 | return max; 26 | }*/ 27 | for(int i=0;imax){ 30 | max = sum; 31 | } 32 | if(sum<0){ 33 | sum = 0; 34 | } 35 | } 36 | 37 | return max; 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/NumberofPairsWithAbsoluteDifferenceK.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/submissions/ 3 | 4 | Just bruteforce comparison O(n^2) 5 | Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k. 6 | 7 | The value of |x| is defined as: 8 | 9 | x if x >= 0. 10 | -x if x < 0. 11 | 12 | 13 | Example 1: 14 | 15 | Input: nums = [1,2,2,1], k = 1 16 | Output: 4 17 | Explanation: The pairs with an absolute difference of 1 are: 18 | - [1,2,2,1] 19 | - [1,2,2,1] 20 | - [1,2,2,1] 21 | - [1,2,2,1] 22 | 23 | */ 24 | 25 | class Solution { 26 | public int countKDifference(int[] nums, int k) { 27 | int l = nums.length; 28 | int c=0; 29 | for(int i=0;i=0;i--){ 29 | c++; 30 | if(c%3==0 && c!=0){ 31 | ans+="."; 32 | } 33 | ans+=b[i]; 34 | } 35 | 36 | String ans1=""; 37 | char m[] = ans.toCharArray(); 38 | for(int i=m.length-1;i>=0;i--){ 39 | ans1+=m[i]; 40 | } 41 | 42 | return ans1; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Company Specific/Veritas Prime Labs/elementAfterRotation O(1).java: -------------------------------------------------------------------------------- 1 | /* 2 | 2. Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. 3 | Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 4 | Output: 5 5 | Input: arr[] = {1, 2, 3, 4, 5}, K = 3, M = 2 6 | Output: 4 7 | */ 8 | 9 | import java.util.*; 10 | class elementAfterRotation{ 11 | public static void main(String [] args){ 12 | Scanner sc = new Scanner(System.in); 13 | 14 | int arr[] = {1, 2, 3, 4, 5}; 15 | int rot = 3; 16 | int index = 2; 17 | 18 | int size = arr.length; 19 | /* 20 | r0: 1 2 3 4 5 21 | r1: 5 1 2 3 4 22 | r2: 4 5 1 2 3 23 | r3: 3 4 5 1 2 24 | r4: 2 3 4 5 1 25 | r5: r0 26 | 27 | So after 4-1[size of array-1] rotations it goes back to initial state 28 | 29 | */ 30 | 31 | /* O(1) approach 32 | Uses mod operator 33 | */ 34 | if(rot==size-1){ 35 | System.out.println("value is: "+ arr[index]); 36 | } 37 | else{ 38 | System.out.println("value is: "+ arr[size-(rot%index)]); 39 | } 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LeetCode Sheet - Neetcode.io/Arrays&Hashing/containsDuplicate HashMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. 3 | */ 4 | 5 | import java.util.*; 6 | class containsDuplicate{ 7 | public static void main(String [] args){ 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int nums[] = {1,1,1,3,3,4,3,2,4,2}; 11 | Map hm = new HashMap(); 12 | 13 | int val=0; 14 | 15 | for(int i=0;i1){ 31 | System.out.println("Has duplis"); 32 | return; 33 | } 34 | System.out.println("key: "+ i + "Value: "+ x); 35 | } 36 | System.out.println("No duplis"); 37 | 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/validAnagram.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/valid-anagram/ 3 | 4 | Given two strings s and t, return true if t is an anagram of s, and false otherwise. 5 | 6 | An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: s = "anagram", t = "nagaram" 13 | Output: true 14 | */ 15 | 16 | class Solution { 17 | public boolean isAnagram(String s, String t) { 18 | int l1 = s.length(); 19 | int l2 = t.length(); 20 | 21 | int hash1[] = new int[26]; 22 | int hash2[] = new int[26]; 23 | 24 | if(l1!=l2){ 25 | return false; 26 | } 27 | 28 | for(int i=0;i#): positive integer 9 | (#>*): negative integer 10 | (#=*): 0 11 | Example 1: 12 | Input 1: 13 | 14 | ###*** -> Value of S 15 | Output : 16 | 17 | 0 → number of * and # are equal 18 | */ 19 | import java.util.*; 20 | class starAndHash{ 21 | public static void main(String[] args) { 22 | Scanner sc = new Scanner(System.in); 23 | String s; 24 | s = sc.next(); 25 | int star=0, hash=0; 26 | for (int i=0;i0){ 29 | ans+='a'; 30 | c--; 31 | } 32 | 33 | 34 | 35 | return ans; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Ranjul_Arumadi 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 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/NumberofStepstoReduceaNumbertoZero.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 3 | 4 | Given an integer num, return the number of steps to reduce it to zero. 5 | 6 | In one step, if the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it. 7 | 8 | 9 | 10 | Example 1: 11 | 12 | Input: num = 14 13 | Output: 6 14 | Explanation: 15 | Step 1) 14 is even; divide by 2 and obtain 7. 16 | Step 2) 7 is odd; subtract 1 and obtain 6. 17 | Step 3) 6 is even; divide by 2 and obtain 3. 18 | Step 4) 3 is odd; subtract 1 and obtain 2. 19 | Step 5) 2 is even; divide by 2 and obtain 1. 20 | Step 6) 1 is odd; subtract 1 and obtain 0. 21 | 22 | */ 23 | 24 | import java.util.*; 25 | class NumberofStepstoReduceaNumbertoZero{ 26 | public static void main(String [] args){ 27 | int num = 8; 28 | int count=0; 29 | 30 | while(num>0){ 31 | if(num%2==0){ 32 | num/=2; 33 | count++; 34 | } 35 | else{ 36 | num--; 37 | count++; 38 | } 39 | } 40 | 41 | System.out.println(count); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Company Specific/TCS NQT/eventManagementTicket.java: -------------------------------------------------------------------------------- 1 | /* 2 | An event management company has come up with a unique idea of printing their event tickets. Based 3 | on the ticket number combination (str1), the visitor is directed towards a particular class of audience. The 4 | task is to create a program/application to fetch the ticket number based on the following conditions: 5 | 6 | Any occurrences of digits EF, 56 and G, & should be deleted 7 | The characters EF should be in the same format. 8 | Example 1: 9 | Input: 10 | 4523EF58G -> Value of STR1 11 | Output: 12 | 452358 -> After removal of characters 13 | ‘EF’ and ‘G’ 14 | */ 15 | import java.util.*; 16 | class eventManagementTicket{ 17 | public static void main(String [] args){ 18 | String s = "E12F35G58"; //"4523EF58G"; //E12F35G58 19 | int l = s.length(); 20 | String ans = ""; 21 | 22 | for(int i=0;i 1. 8 | Given n, calculate F(n). 9 | 10 | 11 | 12 | Example 1: 13 | 14 | Input: n = 2 15 | Output: 1 16 | Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. 17 | */ 18 | 19 | class Solution { 20 | static int fibo(int n){ 21 | if(n==0){ 22 | return 0; 23 | } 24 | else if(n==1){ 25 | return 1; 26 | } 27 | else{ 28 | return fibo(n-1) + fibo(n-2); 29 | } 30 | } 31 | public int fib(int n) { 32 | int ans = fibo(n); 33 | return ans; 34 | } 35 | } 36 | 37 | /* 38 | recursive solution takes O(2^n) so iterative approach is better. 39 | 40 | if n==0 => return 1 41 | a=0; 42 | b=1; 43 | sum=0; 44 | while(n>1){ 45 | sum=a+b; 46 | a = b; 47 | b = sum; 48 | } 49 | 50 | return sum; 51 | */ 52 | -------------------------------------------------------------------------------- /Company Specific/Veritas Prime Labs/BruteForce longestSubstringWithoutRepeatingChars.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class longestSubstringWithoutRepeatingChars{ 3 | static int checkDup(String x){ 4 | char temp = ' '; 5 | char temp1 = ' '; 6 | int len = x.length(); 7 | 8 | for(int i=0;imaxlen){ 40 | maxlen = l; 41 | } 42 | } 43 | temp=""; 44 | } 45 | } 46 | System.out.println("max len w/o repeating substring: "+maxlen); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/MakeArrayZerobySubtractingEqualAmounts.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/ 3 | 4 | Number of unique non-zero elements is the answer. 5 | 6 | You are given a non-negative integer array nums. In one operation, you must: 7 | 8 | Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums. 9 | Subtract x from every positive element in nums. 10 | Return the minimum number of operations to make every element in nums equal to 0. 11 | 12 | 13 | 14 | Example 1: 15 | 16 | Input: nums = [1,5,0,3,5] 17 | Output: 3 18 | Explanation: 19 | In the first operation, choose x = 1. Now, nums = [0,4,0,2,4]. 20 | In the second operation, choose x = 2. Now, nums = [0,2,0,0,2]. 21 | In the third operation, choose x = 2. Now, nums = [0,0,0,0,0]. 22 | */ 23 | 24 | class Solution { 25 | public int minimumOperations(int[] nums) { 26 | HashSet hs = new HashSet(); 27 | 28 | for(Integer x: nums){ 29 | if(x!=0){ 30 | hs.add(x); 31 | } 32 | } 33 | 34 | int c = hs.size(); 35 | return c; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /HackerRank/Peak_Element.c: -------------------------------------------------------------------------------- 1 | /* 2 | An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists). 3 | 4 | This means that for any element A we consider with index = x then value at index-1 and value at index+1 should not be greater than the elment A. 5 | 6 | Simple approaches: 7 | Simple sorting gives us the answer at the end indexes. 8 | 9 | O(log n) approach: 10 | This makes use of bainry search algo. 11 | 12 | 13 | */ 14 | // 1 2 |3| 4 5 15 | int peakElement(int arr[], int n) 16 | { 17 | // Your code here 18 | int start = 0, end = n-1; 19 | int mid = (start+end)/2; 20 | 21 | while(start<=end){ 22 | mid = (start+end)/2; 23 | 24 | // 1 2 |1| 4 5 25 | // then mid value wont be ans, so start from mid+1 26 | if(arr[mid] < arr[mid+1]) { 27 | start = mid+1; 28 | } 29 | 30 | // 1 4|1| 4 5 31 | // then mid value wont be ans, so start from mid-1 32 | else if(arr[mid] < arr[mid-1]){ 33 | end = mid-1; 34 | } 35 | else{ 36 | return mid; 37 | } 38 | } 39 | // final return value 40 | return mid; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /HackerRank/peakElement.c: -------------------------------------------------------------------------------- 1 | /* 2 | An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists). 3 | 4 | This means that for any element A we consider with index = x then value at index-1 and value at index+1 should not be greater than the elment A. 5 | 6 | Simple approaches: 7 | Simple sorting gives us the answer at the end indexes. 8 | 9 | O(log n) approach: 10 | This makes use of bainry search algo. 11 | 12 | 13 | */ 14 | // 1 2 |3| 4 5 15 | int peakElement(int arr[], int n) 16 | { 17 | // Your code here 18 | int start = 0, end = n-1; 19 | int mid = (start+end)/2; 20 | 21 | while(start<=end){ 22 | mid = (start+end)/2; 23 | 24 | // 1 2 |1| 4 5 25 | // then mid value wont be ans, so start from mid+1 26 | if(arr[mid] < arr[mid+1]) { 27 | start = mid+1; 28 | } 29 | 30 | // 1 4|1| 4 5 31 | // then mid value wont be ans, so start from mid-1 32 | else if(arr[mid] < arr[mid-1]){ 33 | end = mid-1; 34 | } 35 | else{ 36 | return mid; 37 | } 38 | } 39 | // final return value 40 | return mid; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /LeetCode/Algorithms/Easy/CountEqualandDivisiblePairsinanArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | link: https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/ 3 | 4 | Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k. 5 | 6 | 7 | Example 1: 8 | 9 | Input: nums = [3,1,2,2,2,1,3], k = 2 10 | Output: 4 11 | Explanation: 12 | There are 4 pairs that meet all the requirements: 13 | - nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2. 14 | - nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2. 15 | - nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2. 16 | - nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2. 17 | */ 18 | class Solution { 19 | public int countPairs(int[] nums, int k) { 20 | int l = nums.length; 21 | int c=0; 22 | for(int i=0;i zero count 27 | zerocount -> zerocount+onecount 28 | zerocount+onecount -> zerocount+onecount+twocount 29 | */ 30 | //fill 0 31 | for(int i=0;i hm = new HashMap(); 21 | int l = nums.length; 22 | 23 | for(int i=0;i