├── Array ├── Basic │ ├── Alternates in an Array.java │ ├── Array Search.java │ ├── Array Subset of another array.java │ ├── Binary Search.java │ ├── Count Odd Even.java │ ├── Find the median.java │ ├── Max Min.java │ ├── Palindromic Array.java │ ├── Searching a number.java │ ├── Searching an element in a sorted array.java │ ├── Union of two arrays.java │ └── Value equal to index value.java ├── Easy │ ├── Bitonic Point.java │ ├── Bubble Sort (20-08-24).java │ ├── Bubble Sort.java │ ├── Chocolate Distribution Problem.java │ ├── Common elements.java │ ├── Convert array into Zig-Zag fashion.java │ ├── Count pairs with given sum.java │ ├── Count the Zeros.java │ ├── Find all pairs with a given sum.java │ ├── Floor in a Sorted Array.java │ ├── Implement two stacks in an array.java │ ├── Index of an Extra Element.java │ ├── Insertion Sort.java │ ├── Key Pair.java │ ├── Maximize sum(arr[i]*i) of an Array.java │ ├── Maximum no of 1's row.java │ ├── Move all negative elements to end.java │ ├── Product array puzzle.java │ ├── Rotate by 90 degree.java │ ├── Rotation.java │ ├── Search in a matrix.java │ ├── Selection Sort.java │ ├── Unique Number of Occurrences.java │ └── Unique rows in boolean matrix.java ├── Hard │ └── Merge Without Extra Space.java ├── Immediate Smaller Element.java ├── Largest Element in Array.java └── Medium │ ├── Find Indexes of a subarray with given sum.java │ ├── Find Missing And Repeating.java │ ├── First and last occurrences of x.java │ ├── K-th element of two Arrays.java │ ├── Kadane's Algorithm.java │ ├── Kth smallest element.java │ ├── Largest subarray with 0 sum.java │ ├── Maximum of all subarrays of size k.java │ ├── Rearrange Array Alternately.java │ ├── Row with max 1s.java │ ├── Stock buy and sell.java │ ├── Subarray with 0 sum.java │ └── Sum of Middle Elements of two sorted arrays.java ├── GreedyAlgo ├── Easy │ └── N meetings in one room.java └── Medium │ ├── Fractional Knapsack.java │ ├── Job Sequencing Problem.java │ ├── Minimum Platforms.java │ └── Number of Coins.java ├── JavaBasic ├── Basic │ ├── Java Inheritance.java │ ├── Learn to Comment - Java.java │ └── Reverse a String.java ├── Easy │ ├── Common Elements.java │ ├── Data Types - Java.java │ ├── Deque Implementations.java │ ├── Java Arrays | Set 1.java │ ├── Java Collection | Set 1 (ArrayList) Part-2.java │ ├── Java Collection | Set 2 (Set).java │ ├── Java Collections | Set 7 (LinkedList).java │ ├── Java Exception Handling.java │ ├── Operations on PriorityQueue.java │ ├── Precise Format - Java.java │ ├── Taking input - Java.java │ ├── The New Line - Java.java │ ├── logical Operators - Java.java │ └── nextLine and next.java └── School │ └── Java Arrays | Set 1.java ├── LinkedList ├── Basic │ ├── Count nodes of linked list.java │ ├── Implement Stack using Linked List.java │ ├── Linked List Insertion.java │ └── Print Linked List elements.java ├── Easy │ ├── Check If Circular Linked List.java │ ├── Delete Alternate Nodes.java │ ├── Delete Middle of Linked List.java │ ├── Delete N nodes after M nodes of a linked list.java │ ├── Delete a Node in Single Linked List.java │ ├── Delete nodes having greater value on right.java │ ├── Delete without head pointer.java │ ├── Detect Loop in linked list.java │ ├── Find length of Loop.java │ ├── Finding middle element in a linked list.java │ ├── Given a linked list of 0s, 1s and 2s, sort it.java │ ├── Insert in a Sorted List.java │ ├── Intersection of Two Linked Lists.java │ ├── Intersection of two sorted Linked lists.java │ ├── Nth node from end of linked list.java │ ├── Pairwise swap elements of a linked list.java │ ├── Remove duplicate element from sorted Linked List.java │ ├── Remove duplicates from an unsorted linked list.java │ └── Reverse a linked list.java ├── Hard │ └── Clone a linked list with next and random pointer.java └── Medium │ ├── Check if Linked List is Palindrome.java │ ├── Intersection Point in Y Shaped Linked Lists.java │ ├── Merge two sorted linked lists.java │ ├── Remove loop in Linked List.java │ ├── Reverse a Linked List in groups of given size.java │ ├── Rotate a Linked List.java │ ├── Segregate even and odd nodes in a Linked List.java │ └── Union of Two Linked Lists.java ├── Math ├── Basic │ ├── Count Squares.java │ ├── Factorial.java │ ├── Power of 2.java │ ├── Prime Number.java │ └── Print first n Fibonacci Numbers.java ├── Easy │ ├── Arithmetic Number.java │ ├── Count Digits.java │ ├── Find first set bit.java │ ├── For Loop- primeCheck.java │ ├── Is Binary Number Multiple of 3.java │ ├── Nth Fibonacci Number.java │ ├── Number of 1 Bits.java │ ├── Perfect Numbers.java │ └── Summed Matrix.java ├── Medium │ └── Power Of Numbers.java └── School │ └── Sum of Series.java ├── Queue ├── Basic │ └── Implement Queue using array.java └── Easy │ └── Queue using two Stacks.java ├── Stack ├── Basic │ └── Implement stack using array.java ├── Easy │ └── Stack using two queues.java └── Medium │ └── Next Greater Element.java ├── String ├── Basic │ ├── Implement strstr.java │ └── Replace all 0's with 5.java ├── Easy │ ├── Longest Distinct characters in string.java │ ├── Minimum indexed character.java │ ├── Parenthesis Checker.java │ ├── Roman Number to Integer.java │ ├── String Rotated by 2 Places.java │ └── Winner of an election.java └── Medium │ ├── Implement Atoi.java │ └── Validate an IP Address.java └── potd ├── Basic └── Identical Linked Lists.java ├── Easy ├── Armstrong Numbers.java ├── Array to BST.java ├── Binary representation of next number.java ├── Check for BST.java ├── Compare two fractions.java ├── Convert array into Zig-Zag fashion.java ├── Count Pairs whose sum is equal to X.java ├── Count numbers containing 4.java ├── Count pairs Sum in matrices.java ├── Count the elements.java ├── Count ways to N'th Stair.java ├── Coverage of all Zeros in a Binary Matrix.java ├── Delete Middle of Linked List.java ├── Delete node in Doubly Linked List.java ├── Delete without head pointer.java ├── Divisor Game.java ├── Extract the Number from the String.java ├── Fibonacci series up to Nth term.java ├── Find Pair Given Difference.java ├── Find length of Loop.java ├── Find maximum volume of a cuboid.java ├── Find the Highest number.java ├── Find the closest number.java ├── Index of an Extra Element.java ├── Insert an Element at the Bottom of a Stack.java ├── Juggler Sequence.java ├── K-Pangrams.java ├── Left View of Binary Tree.java ├── Level order traversal.java ├── Linked list of strings forms a palindrome.java ├── Longest Common Prefix of Strings.java ├── Maximize Array Value After Rearrangement.java ├── N meetings in one room.java ├── Odd Even Problem.java ├── Padovan Sequence.java ├── Parenthesis Checker.java ├── Party of Couples.java ├── Print Bracket Number.java ├── Product array puzzle.java ├── Remaining String.java ├── Remove Duplicates.java ├── Remove Half Nodes.java ├── Remove all duplicates from a given string.java ├── Remove every kth node.java ├── Reverse Bits.java ├── Root to leaf path sum.java ├── Row with minimum number of 1's.java ├── Segregate 0s and 1s.java ├── Sort 0s, 1s and 2s.java ├── Split an array into two equal Sum subarrays.java ├── Square root of a number.java ├── Summed Matrix.java ├── Swap two nibbles in a byte.java ├── Three way partitioning.java ├── Trail of ones.java ├── Xoring and Clearing.java └── You and your books.java ├── Hard ├── Count Smaller elements.java └── The Palindrome Pattern.java └── Medium ├── 0 - 1 Knapsack Problem.java ├── Check If two Line segments Intersect.java ├── Combination Sum II.java ├── Construct list using given q XOR queries.java ├── Find missing in second array.java ├── Find the N-th character.java ├── Gray to Binary equivalent.java ├── Integral Points Inside Triangle.java ├── Job Sequencing Problem.java ├── K-th element of two Arrays.java ├── Kth Smallest.java ├── Left Rotate Matrix K times.java ├── Linked List that is Sorted Alternatingly.java ├── Longest alternating subsequence.java ├── Longest subsequence-1.java ├── Max sum in the configuration.java ├── Maximum Tip Calculator.java ├── Maximum occured integer.java ├── Minimum steps to destination.java ├── Mobile numeric keypad.java ├── Modular Exponentiation for large numbers.java ├── Nth Natural Number.java ├── Number of Rectangles in a Circle.java ├── Nuts and Bolts Problem.java ├── Prime Pair with Target Sum.java ├── Remove all occurences of duplicates in a linked list.java ├── Rotate a Linked List.java ├── Row with max 1s.java ├── Search in Rotated Sorted Array.java ├── Smallest number.java ├── Sum Tree.java ├── Sum of Middle elements of two sorted arrays.java ├── Swapping pairs make sum equal.java ├── The Celebrity Problem.java ├── Toeplitz matrix.java ├── Two Repeated Elements.java ├── Union of Two Sorted Arrays.java └── Validate an IP Address.java /Array/Basic/Alternates in an Array.java: -------------------------------------------------------------------------------- 1 | 2 | // arr[] is the array 3 | // n is the number of elements in array. 4 | class Solution { 5 | public static void print(int arr[]) { 6 | // your code here 7 | for(int i=0; ik){ 12 | high=mid-1; 13 | } 14 | else 15 | low=mid+1; 16 | } 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Array/Basic/Count Odd Even.java: -------------------------------------------------------------------------------- 1 | //User function Template for Java 2 | 3 | class Solution 4 | { 5 | public int[] countOddEven(int[] arr, int n) 6 | { 7 | // Code here 8 | int[] oddEven =new int[2]; 9 | for(int num : arr){ 10 | if(num%2==0) 11 | oddEven[1]++; 12 | else 13 | oddEven[0]++; 14 | } 15 | return oddEven; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Array/Basic/Find the median.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public int find_median(int[] v) 4 | { 5 | // Code here 6 | Arrays.sort(v); 7 | if(v.length%2==0){ 8 | return (v[v.length/2 -1]+v[v.length/2])/2; 9 | }else{ 10 | return v[v.length/2]; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Array/Basic/Max Min.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public static int findSum(int A[],int N) 4 | { 5 | //code here 6 | int min=Integer.MAX_VALUE; 7 | int max=Integer.MIN_VALUE; 8 | 9 | for(int num : A){ 10 | min=Math.min(min,num); 11 | max=Math.max(max,num); 12 | } 13 | return max+min; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Array/Basic/Palindromic Array.java: -------------------------------------------------------------------------------- 1 | /*Complete the Function below*/ 2 | class Solution { 3 | public static boolean palinArray(int[] arr) { 4 | // add code here. 5 | for(int num : arr){ 6 | if(!isPalindrome(num)){ 7 | return false; 8 | } 9 | } 10 | return true; 11 | } 12 | private static boolean isPalindrome(int num){ 13 | int temp = num; 14 | int reverse=0; 15 | while(temp>0){ 16 | reverse=reverse*10+(temp%10); 17 | temp/=10; 18 | } 19 | return reverse==num; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Array/Basic/Searching a number.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | class Solution { 3 | public static int search(int n, int k, int[] arr) { 4 | 5 | // code here 6 | for(int i=0; i set=new HashSet<>(); 8 | for(int i : a){ 9 | if(!set.contains(i)) 10 | set.add(i); 11 | } 12 | for(int i: b){ 13 | if(!set.contains(i)) 14 | set.add(i); 15 | } 16 | return set.size(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Array/Basic/Value equal to index value.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List valueEqualToIndex(List nums) { 3 | // code here 4 | ArrayList answer=new ArrayList<>(); 5 | for(int i=0; iarr[i+1]) 6 | return arr[i]; 7 | } 8 | return arr[n-1]; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Array/Easy/Bubble Sort (20-08-24).java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to sort the array using bubble sort algorithm. 4 | public static void bubbleSort(int arr[], int n) 5 | { 6 | //code here 7 | for (int i = 0; i < n - 1; i++) { 8 | boolean swapped = false; 9 | for (int j = 0; j < n - i - 1; j++) { 10 | if (arr[j] > arr[j + 1]) { 11 | int temp = arr[j]; 12 | arr[j] = arr[j + 1]; 13 | arr[j + 1] = temp; 14 | swapped = true; 15 | } 16 | } 17 | if (!swapped) break; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Array/Easy/Bubble Sort.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to sort the array using bubble sort algorithm. 4 | public static void bubbleSort(int arr[], int n) 5 | { 6 | //code here 7 | for(int i=0;iarr[j]){ 10 | int temp=arr[i]; 11 | arr[i]=arr[j]; 12 | arr[j]=temp; 13 | } 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Array/Easy/Chocolate Distribution Problem.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public long findMinDiff (ArrayList a, int n, int m) 4 | { 5 | // your code here 6 | Collections.sort(a); 7 | int result=Integer.MAX_VALUE; 8 | for(int i=0; i<=n-m; i++){ 9 | int answer=a.get(i+m-1)-a.get(i); 10 | result=Math.min(answer,result); 11 | } 12 | return result; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Array/Easy/Common elements.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | ArrayList commonElements(int A[], int B[], int C[], int n1, int n2, int n3) 4 | { 5 | // code here 6 | ArrayList result=new ArrayList<>(); 7 | int i=0,j=0,k=0; 8 | while(ia[i+1]){ 7 | int temp=a[i]; 8 | a[i]=a[i+1]; 9 | a[i+1]=temp; 10 | } 11 | } 12 | else{ 13 | if(a[i] map=new HashMap<>(); 5 | int count=0; 6 | for(int i : arr){ 7 | int sum=k-i; 8 | if(map.containsKey(sum)){ 9 | count+=map.get(sum); 10 | } 11 | map.put(i,map.getOrDefault(i,0)+1); 12 | } 13 | return count; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Array/Easy/Count the Zeros.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int countZeroes(int[] arr) { 4 | // code here 5 | int count=0; 6 | for(int i=arr.length-1 ; i>=0; i--){ 7 | if(arr[i]==0) 8 | count++; 9 | else{ 10 | break; 11 | } 12 | } 13 | return count; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Array/Easy/Find all pairs with a given sum.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | class pair { 4 | long first, second; 5 | public pair(long first, long second) 6 | { 7 | this.first = first; 8 | this.second = second; 9 | } 10 | } 11 | */ 12 | 13 | class Solution { 14 | public pair[] allPairs( long A[], long B[], long N, long M, long X) { 15 | // Your code goes here 16 | Arrays.sort(A); 17 | HashSet set=new HashSet<>(); 18 | ArrayList list=new ArrayList<>(); 19 | for(long i : B){ 20 | set.add(i); 21 | } 22 | 23 | for(long i : A){ 24 | if(set.contains(X-i)){ 25 | list.add(new pair(i,X-i)); 26 | } 27 | } 28 | pair[] result=new pair[list.size()]; 29 | for(int i=0;ix){ 13 | return i-1; 14 | } 15 | if(arr[i]==x){ 16 | return i; 17 | } 18 | } 19 | return n-1; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Array/Easy/Implement two stacks in an array.java: -------------------------------------------------------------------------------- 1 | class twoStacks 2 | { 3 | int arr[]=new int[100]; 4 | int size=100; 5 | int top1,top2; 6 | twoStacks() 7 | { 8 | for(int i=0;i<100;i++){ 9 | arr[i]=-1; 10 | } 11 | top1=-1; 12 | top2=100; 13 | } 14 | //Function to push an integer into the stack1. 15 | void push1(int x) 16 | { 17 | arr[++top1]=x; 18 | } 19 | //Function to push an integer into the stack2. 20 | void push2(int x) 21 | { 22 | arr[--top2]=x; 23 | } 24 | //Function to remove an element from top of the stack1. 25 | int pop1() 26 | { 27 | if(top1==-1) 28 | return -1; 29 | int x=arr[top1]; 30 | arr[top1]=-1; 31 | top1--; 32 | return x; 33 | } 34 | //Function to remove an element from top of the stack2. 35 | int pop2() 36 | { 37 | if(top2==size) 38 | return -1; 39 | int x=arr[top2]; 40 | arr[top2]=-1; 41 | top2++; 42 | return x; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Array/Easy/Index of an Extra Element.java: -------------------------------------------------------------------------------- 1 | /*Complete the function below*/ 2 | class Solution { 3 | public int findExtra(int a[], int b[], int n) { 4 | // add code here. 5 | int low=0; 6 | int high=n-1; 7 | while(low0 && arr[j-1]>arr[j]){ 9 | int temp=arr[j-1]; 10 | arr[j-1]=arr[j]; 11 | arr[j]=temp; 12 | j--; 13 | } 14 | } 15 | //Function to sort the array using insertion sort algorithm. 16 | public void insertionSort(int arr[], int n) 17 | { 18 | //code here 19 | for(int i=0; i set=new HashSet<>(); 5 | for (int i=0;ires[index]){ 18 | index=i; 19 | } 20 | } 21 | return index; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Array/Easy/Move all negative elements to end.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public void segregateElements(int arr[], int n) 4 | { 5 | // Your code goes here 6 | int[] ans=new int[n]; 7 | int index=0; 8 | for(int i=0;i=0){ 10 | ans[index++]=arr[i]; 11 | } 12 | } 13 | for(int i=0;i=0;i--){ 13 | result[i]*=x; 14 | x*=nums[i]; 15 | } 16 | return result; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Array/Easy/Rotate by 90 degree.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to rotate matrix anticlockwise by 90 degrees. 4 | static void rotateby90(int matrix[][], int n) 5 | { 6 | // code here 7 | 8 | for(int i=0; i= 0) { 10 | if (mat[i][j] == X) { 11 | return 1; 12 | } else if (mat[i][j] > X) { 13 | j--; 14 | } else { 15 | i++; 16 | } 17 | } 18 | 19 | return 0; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Array/Easy/Selection Sort.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | int select(int arr[], int i) 4 | { 5 | // code here such that selectionSort() sorts arr[] 6 | int min=i; 7 | for(int j=i; j map=new HashMap<>(); 5 | 6 | for(int num : arr){ 7 | map.put(num, map.getOrDefault(num,0)+1); 8 | } 9 | 10 | HashSet set=new HashSet<>(); 11 | 12 | for(int num :map.values()){ 13 | if(!set.add(num)) 14 | return false; 15 | } 16 | return true; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Array/Easy/Unique rows in boolean matrix.java: -------------------------------------------------------------------------------- 1 | /*Complete the given function*/ 2 | class GfG 3 | { 4 | public static ArrayList> uniqueRow(int a[][],int r, int c) 5 | { 6 | //add code here. 7 | ArrayList> result=new ArrayList>(); 8 | for(int i=0;i row=new ArrayList<>(); 10 | for(int j=0;j=0 && j= arr2[j]){ 12 | long temp=arr1[i]; 13 | arr1[i]=arr2[j]; 14 | arr2[j]=temp; 15 | i--; 16 | j++; 17 | } 18 | else if(arr1[i] < arr2[j]){ 19 | break; 20 | } 21 | } 22 | Arrays.sort(arr1); 23 | Arrays.sort(arr2); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array/Immediate Smaller Element.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | void immediateSmaller(int arr[], int n) { 3 | // code here 4 | // int[] res=new int[n]; 5 | for(int i=0;imax){ 8 | max=arr[i]; 9 | } 10 | } 11 | return max; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Array/Medium/Find Indexes of a subarray with given sum.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to find a continuous sub-array which adds up to a given number. 4 | static ArrayList subarraySum(int[] arr, int n, int s) 5 | { 6 | // Your code here 7 | ArrayList answer=new ArrayList<>(); 8 | int i,begin=0,sum=arr[0]; 9 | 10 | for(i=1; i<=n ; i++){ 11 | 12 | while(sum>s && begin map=new HashMap<>(); 5 | int[] result=new int[2]; 6 | for(int num : arr){ 7 | if(map.containsKey(num)){ 8 | result[0]=num; 9 | break; 10 | } 11 | else{ 12 | map.put(num,1); 13 | } 14 | } 15 | result[1]=1; 16 | Arrays.sort(arr); 17 | 18 | for(int i = 0; i < n; i++) { 19 | if(arr[i]==result[1]){ 20 | result[1]++; 21 | } 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array/Medium/First and last occurrences of x.java: -------------------------------------------------------------------------------- 1 | class GFG 2 | { 3 | ArrayList find(int arr[], int n, int x) 4 | { 5 | // code here 6 | ArrayList answer=new ArrayList<>(); 7 | answer.add(-1); 8 | answer.add(-1); 9 | int apperence=0; 10 | int index=-1; 11 | for(int i=0;i m + n) 4 | return 0; 5 | 6 | int i = 0, j = 0; 7 | 8 | while (i < n && j < m) { 9 | if (arr1[i] <= arr2[j]) { 10 | k--; 11 | if (k == 0) 12 | return arr1[i]; 13 | i++; 14 | } else { 15 | k--; 16 | if (k == 0) 17 | return arr2[j]; 18 | j++; 19 | } 20 | } 21 | 22 | while (i < n) { 23 | k--; 24 | if (k == 0) 25 | return arr1[i]; 26 | i++; 27 | } 28 | 29 | while (j < m) { 30 | k--; 31 | if (k == 0) 32 | return arr2[j]; 33 | j++; 34 | } 35 | 36 | return -1; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Array/Medium/Kadane's Algorithm.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | // arr: input array 4 | // n: size of array 5 | //Function to find the sum of contiguous subarray with maximum sum. 6 | long maxSubarraySum(int arr[], int n){ 7 | 8 | // Your code here 9 | long answer=Long.MIN_VALUE; 10 | long sum=0; 11 | for(int i:arr){ 12 | sum+=i; 13 | if(answer map=new HashMap<>(); 7 | map.put(0,-1); 8 | int answer=0; 9 | int sum=0; 10 | 11 | for(int i=0; i max_of_subarrays(int arr[], int n, int k) 5 | { 6 | // Your code here 7 | ArrayList result=new ArrayList<>(); 8 | Deque queue=new LinkedList<>(); 9 | for(int i=0;i=arr[queue.peekLast()]){ 11 | queue.pollLast(); 12 | } 13 | queue.offerLast(i); 14 | } 15 | result.add(arr[queue.peekFirst()]); 16 | for(int i=k;i=arr[queue.peekLast()]){ 21 | queue.pollLast(); 22 | } 23 | queue.offerLast(i); 24 | result.add(arr[queue.peekFirst()]); 25 | } 26 | return result; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Array/Medium/Rearrange Array Alternately.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | // temp: input array 4 | // n: size of array 5 | //Function to rearrange the array elements alternately. 6 | public static void rearrange(long arr[], int n){ 7 | 8 | // Your code here 9 | long arr2[]=Arrays.copyOf(arr,n); 10 | 11 | int i=0, j=n-1; 12 | int k=0; 13 | while(i<=j){ 14 | if(k%2==0){ 15 | arr[k++]=arr2[j--]; 16 | } 17 | else{ 18 | arr[k++]=arr2[i++]; 19 | } 20 | } 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Array/Medium/Row with max 1s.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int rowWithMax1s(int arr[][], int n, int m) { 3 | // code here 4 | HashMap map=new HashMap<>(); 5 | for(int i=0;i entry : map.entrySet()){ 16 | int count=entry.getValue(); 17 | int row=entry.getKey(); 18 | if(max > stockBuySell(int A[], int n) { 4 | // code here 5 | ArrayList> answer=new ArrayList<>(); 6 | 7 | for(int i=1; iA[i-1]){ 9 | ArrayList arr=new ArrayList<>(); 10 | arr.add(i-1); 11 | while(iA[i-1]){ 12 | i++; 13 | } 14 | arr.add(i-1); 15 | answer.add(arr); 16 | } 17 | } 18 | return answer; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Array/Medium/Subarray with 0 sum.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | //Function to check whether there is a subarray present with 0-sum or not. 3 | static boolean findsum(int arr[],int n) 4 | { 5 | //Your code here 6 | HashMap map=new HashMap<>(); 7 | int sum=0; 8 | for(int i=0; i{ 14 | public int compare(Meeting o1 , Meeting o2){ 15 | if(o1.end < o2.end) 16 | return -1; 17 | else if(o1.end > o2.end) 18 | return 1; 19 | else if(o1.pos < o2.pos) 20 | return -1; 21 | return 1; 22 | } 23 | } 24 | 25 | class Solution 26 | { 27 | //Function to find the maximum number of meetings that can 28 | //be performed in a meeting room. 29 | public static int maxMeetings(int start[], int end[], int n) 30 | { 31 | // add your code here 32 | int count=1; 33 | ArrayList meetings=new ArrayList<>(); 34 | 35 | for(int i=0 ; i endLimit){ 45 | count++; 46 | endLimit=meetings.get(i).end; 47 | } 48 | } 49 | return count; 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /GreedyAlgo/Medium/Fractional Knapsack.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Item { 3 | int value, weight; 4 | Item(int x, int y){ 5 | this.value = x; 6 | this.weight = y; 7 | } 8 | } 9 | */ 10 | class itemComparator implements Comparator{ 11 | public int compare(Item a, Item b){ 12 | double r1=(double) a.value/(double)a.weight; 13 | double r2=(double) b.value/(double)b.weight; 14 | if(r1 < r2) 15 | return 1; 16 | else if(r1 > r2) 17 | return -1; 18 | else 19 | return 0; 20 | } 21 | } 22 | 23 | class Solution { 24 | // Function to get the maximum total value in the knapsack. 25 | double fractionalKnapsack(int w, Item arr[], int n) { 26 | // Your code here 27 | itemComparator im=new itemComparator(); 28 | Arrays.sort(arr,im); 29 | int currentWeight=0; 30 | double totalValue=0.0; 31 | 32 | for(int i=0; i (b.profit - a.profit)); 8 | 9 | int maxi=0; 10 | int jobCount=0; 11 | int jobProfit=0; 12 | 13 | for(int i=0; imaxi) 16 | maxi=arr[i].deadline; 17 | } 18 | int[] result=new int[maxi+1]; 19 | Arrays.fill(result, -1); 20 | 21 | for(int i=0; i0; j--){ 23 | 24 | if(result[j] == -1){ 25 | result[j]=i; 26 | jobCount++; 27 | jobProfit+=arr[i].profit; 28 | break; 29 | } 30 | } 31 | } 32 | return new int[] {jobCount,jobProfit}; 33 | 34 | } 35 | } 36 | 37 | /* 38 | class Job { 39 | int id, profit, deadline; 40 | Job(int x, int y, int z){ 41 | this.id = x; 42 | this.deadline = y; 43 | this.profit = z; 44 | } 45 | } 46 | */ 47 | -------------------------------------------------------------------------------- /GreedyAlgo/Medium/Minimum Platforms.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to find the minimum number of platforms required at the 4 | //railway station such that no train waits. 5 | static int findPlatform(int arr[], int dep[], int n) 6 | { 7 | // add your code here 8 | Arrays.sort(arr); 9 | Arrays.sort(dep); 10 | 11 | int platNeeded=1; 12 | int answer=1; 13 | int i=1,j=0; 14 | 15 | while(i < n && j < n){ 16 | if(arr[i] <= dep[j]){ 17 | platNeeded++; 18 | i++; 19 | } 20 | else if(arr[i] > dep[j]){ 21 | platNeeded--; 22 | j++; 23 | } 24 | answer=Math.max(answer,platNeeded); 25 | } 26 | return answer; 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /GreedyAlgo/Medium/Number of Coins.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | public int minCoins(int coins[], int M, int V) 4 | { 5 | // Your code goes here 6 | int dp[] = new int[V + 1]; 7 | Arrays.fill(dp,Integer.MAX_VALUE); 8 | dp[0]=0; 9 | for(int i=1; i<=V; i++){ 10 | for(int j=0;j=0; i--){ 6 | sb.append(s.charAt(i)); 7 | } 8 | return sb.toString(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Common Elements.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public static ArrayList common_element(int v1[], int v2[]) 3 | { 4 | //Your code here 5 | ArrayList answer=new ArrayList<>(); 6 | HashMap map=new HashMap<>(); 7 | 8 | for(int num : v1){ 9 | map.put(num, map.getOrDefault(num, 0) + 1); 10 | } 11 | for(int num : v2){ 12 | if(map.containsKey(num) && map.get(num) > 0) { 13 | answer.add(num); 14 | map.put(num, map.get(num) - 1); 15 | } 16 | } 17 | Collections.sort(answer); 18 | return answer; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Data Types - Java.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | 3 | // Function to do operations with different data types 4 | static void dataTypes(int a, float b, double c, long l, byte d){ 5 | 6 | double p = c/b;//c/b 7 | double q = b/a;//b/a 8 | double r = c/a;//c/a 9 | double m = r+l;//r+l 10 | int s = a/d;//a/d 11 | 12 | //Printing all the results 13 | System.out.println(p + " " + q + " " + r + " " + m + " " + s); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Deque Implementations.java: -------------------------------------------------------------------------------- 1 | 2 | // dq : deque in which element is to be pushed 3 | // x : element to be pushed 4 | 5 | // Function to push element x to the back of the deque. 6 | public static void push_back_pb(ArrayDeque dq, int x) { 7 | // Your code here 8 | dq.addLast(x); 9 | } 10 | 11 | // Function to pop element from back of the deque. 12 | public static void pop_back_ppb(ArrayDeque dq) { 13 | // Your code here 14 | if(!dq.isEmpty()) 15 | dq.removeLast(); 16 | } 17 | 18 | // Function to return element from front of the deque. 19 | public static int front_dq(ArrayDeque dq) { 20 | // Your code here 21 | if(!dq.isEmpty()) 22 | return dq.getFirst(); 23 | return -1; 24 | } 25 | 26 | // Function to push element x to the front of the deque. 27 | public static void push_front_pf(ArrayDeque dq, int x) { 28 | // Your code here 29 | dq.addFirst(x); 30 | } 31 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Java Arrays | Set 1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String average(int arr[]) { 3 | // code here 4 | int sum=0; 5 | for(int num : arr){ 6 | sum+=num; 7 | } 8 | double avg= (double)sum/arr.length; 9 | return String.format("%.2f",avg); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Java Collection | Set 1 (ArrayList) Part-2.java: -------------------------------------------------------------------------------- 1 | /*You are required to complete below methods*/ 2 | 3 | class GfG 4 | { 5 | /*inserts an element x at 6 | the back of the ArrayList A */ 7 | void add_to_ArrayList(ArrayList A, int x) 8 | { 9 | // Your code here 10 | A.add(x); 11 | } 12 | 13 | /*sort the ArrayList A in ascending order*/ 14 | void sort_ArrayList_Asc(ArrayList A) 15 | { 16 | // Your code here 17 | Collections.sort(A); 18 | } 19 | 20 | /*reverses the ArrayList A*/ 21 | void reverse_ArrayList(ArrayList A) 22 | { 23 | // Your code here 24 | Collections.reverse(A); 25 | } 26 | 27 | /*returns the size of the ArrayList A */ 28 | int size_Of_ArrayList(ArrayList A) 29 | { 30 | // Your code here 31 | return A.size(); 32 | } 33 | 34 | /*sorts the ArrayList A in descending order*/ 35 | void sort_ArrayList_Desc(ArrayList A) 36 | { 37 | // Your code here 38 | Collections.sort(A,Collections.reverseOrder()); 39 | } 40 | 41 | /*prints space separated 42 | elements of ArrayList A*/ 43 | void print_ArrayList(ArrayList A) 44 | { 45 | // Your code here 46 | for(int num : A){ 47 | System.out.print(num+" "); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Java Collection | Set 2 (Set).java: -------------------------------------------------------------------------------- 1 | /* You are required to complete below methods */ 2 | class GfG 3 | { 4 | /*inserts an element x to the set s */ 5 | void insert(Set s, int x) 6 | { 7 | // Your code here 8 | s.add(x); 9 | } 10 | 11 | /*prints the contents of the set s in ascending order */ 12 | void print_contents(Set s) 13 | { 14 | // Your code here 15 | TreeSet sortedSet = new TreeSet<>(s); 16 | Iterator it = sortedSet.iterator(); 17 | while(it.hasNext()){ 18 | System.out.print(it.next() +" "); 19 | } 20 | } 21 | 22 | /*erases an element x from the set s */ 23 | void erase(Set s, int x) 24 | { 25 | // Your code here 26 | s.remove(x); 27 | } 28 | 29 | /*returns the size of the set s */ 30 | int size(Set s) 31 | { 32 | // Your code here 33 | return s.size(); 34 | } 35 | 36 | /*returns 1 if the element x is 37 | present in set s else returns -1 */ 38 | int find(Set s, int x) 39 | { 40 | // Your code here 41 | if(s.contains(x)) 42 | return 1; 43 | return -1; 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Java Collections | Set 7 (LinkedList).java: -------------------------------------------------------------------------------- 1 | class GfG 2 | { 3 | public void deleteNode(Node node) { 4 | //Add your code here 5 | if(node.next==null) 6 | return; 7 | node.data=node.next.data; 8 | node.next=node.next.next; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Java Exception Handling.java: -------------------------------------------------------------------------------- 1 | class TestClass 2 | { 3 | // Add your code here. Define private variables, Constructors to initialize 4 | // variables with the given values and a method findMin() which prints the 5 | // Minimum value of a$b. 6 | private int a; 7 | private int b; 8 | 9 | TestClass (int a, int b) { 10 | this.a = a; 11 | this.b = b; 12 | } 13 | 14 | public void findMin() { 15 | int min = Integer.MAX_VALUE; 16 | try { 17 | min = Math.min(min, a+b); 18 | min = Math.min(min, a-b); 19 | min = Math.min(min, a*b); 20 | min = Math.min(min, a/b); 21 | } catch (Exception e) { 22 | 23 | } 24 | System.out.println(min); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Operations on PriorityQueue.java: -------------------------------------------------------------------------------- 1 | // Helper class Geeks to implement 2 | // insert() and findFrequency() 3 | class Geeks{ 4 | 5 | // Function to insert element into the queue 6 | static void insert(PriorityQueue q, int k){ 7 | 8 | // Your code here 9 | //Just insert k in q and don't return anything 10 | q.add(k); 11 | } 12 | 13 | // Function to find an element k 14 | static boolean find(PriorityQueue q, int k){ 15 | 16 | // Your code here 17 | // If k is in q return true else return false 18 | for(int i:q) { 19 | if(i==k) return true; 20 | } 21 | return false; 22 | 23 | } 24 | 25 | // Function to delete the max element from queue 26 | static int delete(PriorityQueue q){ 27 | 28 | // Your code here 29 | //Delete the max element from q. The priority queue property might be useful here 30 | int maxi=0; 31 | for(int element: q) { 32 | if(element>maxi) { 33 | maxi = element; 34 | } 35 | } 36 | if(q.size()!=0) { 37 | q.remove(maxi); 38 | } 39 | return maxi; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Precise Format - Java.java: -------------------------------------------------------------------------------- 1 | 2 | class Geeks{ 3 | 4 | static void printInFormat(float a, float b){ 5 | float result = a/b; 6 | 7 | System.out.print(result+" "); 8 | System.out.format("%.3f",result); 9 | // Your code here to print upto 3 decimal places 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /JavaBasic/Easy/Taking input - Java.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | 3 | // Function to take input using Scanner class 4 | static void IOFunction(){ 5 | Scanner sc = new Scanner(System.in); 6 | int t = sc.nextInt(); 7 | 8 | while(t-- > 0){ 9 | 10 | // Your code here 11 | int a=sc.nextInt(); 12 | float b=sc.nextFloat(); 13 | long c=sc.nextLong(); 14 | byte d=sc.nextByte(); 15 | sc.nextLine(); 16 | String s=sc.nextLine(); 17 | System.out.println(a+"\n"+b+"\n"+c+"\n"+d+"\n"+s); 18 | } 19 | 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /JavaBasic/Easy/The New Line - Java.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | 3 | // Function to print each word in single line 4 | static void printIndividualLine(){ 5 | 6 | // Your code here 7 | System.out.println("Geeks"); 8 | System.out.println("for"); 9 | System.out.println("Geeks"); 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /JavaBasic/Easy/logical Operators - Java.java: -------------------------------------------------------------------------------- 1 | class Geeks{ 2 | 3 | static void logicOp(boolean a, boolean b){ 4 | /*output (a&&b), (a||b), and ((!a)&&(!b))separated by spaces*/ 5 | boolean c=a&&b; 6 | boolean d=a||b; 7 | boolean e=((!a)&&(!b)); 8 | System.out.print(c+" "+d+" "+e); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /JavaBasic/Easy/nextLine and next.java: -------------------------------------------------------------------------------- 1 | //Complete the function 2 | //Take input for a and s 3 | class Geeks { 4 | static void getInput () { 5 | 6 | Scanner sc = new Scanner(System.in); 7 | int t=sc.nextInt(); //Taking the number of testcases 8 | while(t-->0) 9 | { 10 | int a = sc.nextInt(); 11 | String s =sc.nextLine(); 12 | s+=sc.nextLine(); 13 | 14 | //Your code here 15 | 16 | System.out.println(a); 17 | System.out.println(s); 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /JavaBasic/School/Java Arrays | Set 1.java: -------------------------------------------------------------------------------- 1 | class Compute 2 | { 3 | String average(int A[], int N) 4 | { 5 | float sum=0.00f; 6 | for(int n : A){ 7 | sum+=n; 8 | } 9 | float a=sum/N; 10 | String avg=String.format("%.2f",a); 11 | 12 | return avg; 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LinkedList/Basic/Count nodes of linked list.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public static Node addOne(Node head) 4 | { 5 | //code here. 6 | String str=""; 7 | Node temp=head; 8 | while(temp!=null){ 9 | str+=str(temp.data); 10 | } 11 | int sum=(int)str+1; 12 | str=(String)sum; 13 | temp=head; 14 | int j=str.length(),i=0; 15 | while(temp!=null && i=key){ 14 | newNode.next=head1; 15 | return newNode; 16 | } 17 | Node current=head1; 18 | Node prev=null; 19 | while(current!=null && current.data set=new HashSet(); 28 | while(head2!=null){ 29 | set.add(head2.data); 30 | head2=head2.next; 31 | } 32 | while(head1!=null){ 33 | if(set.contains(head1.data)){ 34 | temp.next=new Node(head1.data); 35 | temp=temp.next; 36 | } 37 | head1=head1.next; 38 | } 39 | return answer.next; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LinkedList/Easy/Intersection of two sorted Linked lists.java: -------------------------------------------------------------------------------- 1 | /* Node of a linked list 2 | class Node { 3 | int data; 4 | Node next; 5 | Node(int d) { data = d; next = null; } 6 | } 7 | */ 8 | 9 | class Solution 10 | { 11 | public static Node findIntersection(Node head1, Node head2) 12 | { 13 | // code here. 14 | 15 | Node newList=new Node(0); 16 | Node current=newList; 17 | while(head1!=null && head2!=null){ 18 | if(head1.data==head2.data){ 19 | current.next=new Node(head1.data); 20 | current=current.next; 21 | head1=head1.next; 22 | head2=head2.next; 23 | } 24 | else if(head1.datalen){ 24 | return -1; 25 | } 26 | ptr=head; 27 | for(int i=1;i<=len-n;i++){ 28 | ptr=ptr.next; 29 | } 30 | return ptr.data; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LinkedList/Easy/Pairwise swap elements of a linked list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to pairwise swap elements of a linked list. 3 | // It should returns head of the modified list 4 | public Node pairwiseSwap(Node head) 5 | { 6 | // code here 7 | Node newNode = new Node(0); 8 | newNode.next = head; 9 | Node current = head; 10 | Node prev = newNode; 11 | Node nextNode = current.next; 12 | 13 | while(current != null && current.next != null){ 14 | prev.next = nextNode; 15 | current.next=nextNode.next; 16 | nextNode.next = current; 17 | if(current.next==null){ 18 | break; 19 | } 20 | prev = current; 21 | current = current.next; 22 | nextNode=current.next; 23 | } 24 | return newNode.next; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LinkedList/Easy/Remove duplicate element from sorted Linked List.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int d) {data = d; next = null; } 7 | } 8 | */ 9 | 10 | class GfG 11 | { 12 | //Function to remove duplicates from sorted linked list. 13 | Node removeDuplicates(Node head) 14 | { 15 | // Your code here 16 | HashSet set=new HashSet<>(); 17 | Node prev=null; 18 | Node current=head; 19 | Node nextNode=null; 20 | while(current!=null){ 21 | nextNode=current.next; 22 | if(set.contains(current.data)){ 23 | prev.next=nextNode; 24 | } 25 | else{ 26 | set.add(current.data); 27 | prev=current; 28 | 29 | } 30 | current=nextNode; 31 | } 32 | return head; 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LinkedList/Easy/Remove duplicates from an unsorted linked list.java: -------------------------------------------------------------------------------- 1 | /* The structure of linked list is the following 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int d) { 7 | data = d; 8 | next = null; 9 | } 10 | } 11 | */ 12 | 13 | class Solution 14 | { 15 | //Function to remove duplicates from unsorted linked list. 16 | public Node removeDuplicates(Node head) 17 | { 18 | // Your code here 19 | HashSet set=new HashSet<>(); 20 | Node prev=null; 21 | Node current=head; 22 | while(current!=null){ 23 | if(set.contains(current.data)){ 24 | prev.next=current.next; 25 | } 26 | else{ 27 | set.add(current.data); 28 | prev=current; 29 | } 30 | current=current.next; 31 | } 32 | return head; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LinkedList/Easy/Reverse a linked list.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to reverse a linked list. 4 | Node reverseList(Node head) 5 | { 6 | // code here 7 | Node prev=null; 8 | Node ptr=head; 9 | Node next=null; 10 | while(ptr!=null){ 11 | next=ptr.next; 12 | ptr.next=prev; 13 | prev=ptr; 14 | ptr=next; 15 | } 16 | head=prev; 17 | return head; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LinkedList/Hard/Clone a linked list with next and random pointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | int data; 4 | Node next, arb; 5 | 6 | Node(int d) 7 | { 8 | data = d; 9 | next = arb = null; 10 | 11 | } 12 | }*/ 13 | 14 | class Clone { 15 | //Function to clone a linked list with next and random pointer. 16 | Node copyList(Node head) { 17 | // your code here 18 | if(head==null) 19 | return null; 20 | 21 | Node temp=head; 22 | while(temp!=null){ 23 | Node newNode=new Node(temp.data); 24 | newNode.next=temp.next; 25 | temp.next=newNode; 26 | temp=newNode.next; 27 | } 28 | 29 | temp=head; 30 | while(temp!=null){ 31 | Node copy=temp.next; 32 | copy.arb=temp.arb==null ? null : temp.arb.next; 33 | temp=copy.next; 34 | } 35 | 36 | temp=head; 37 | Node dummy=new Node(-1); 38 | Node copy=dummy; 39 | while(temp != null){ 40 | copy.next=temp.next; 41 | temp.next=temp.next.next; 42 | copy=copy.next; 43 | temp=temp.next; 44 | } 45 | return dummy.next; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /LinkedList/Medium/Check if Linked List is Palindrome.java: -------------------------------------------------------------------------------- 1 | /* Structure of class Node is 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | 7 | Node(int d) 8 | { 9 | data = d; 10 | next = null; 11 | } 12 | }*/ 13 | 14 | class Solution 15 | { 16 | //Function to check whether the list is palindrome. 17 | boolean isPalindrome(Node head) 18 | { 19 | //Your code here 20 | Node tail=head; 21 | int len=0; 22 | while(tail.next!=null){ 23 | tail=tail.next; 24 | len++; 25 | } 26 | tail=head; 27 | for(int i=0;i<(int)len/2;i++){ 28 | tail=tail.next; 29 | } 30 | Node prev=null; 31 | Node current=tail; 32 | Node nextNode; 33 | while(current!=null){ 34 | nextNode=current.next; 35 | current.next=prev; 36 | prev=current; 37 | current=nextNode; 38 | } 39 | tail=prev; 40 | current=head; 41 | while(current!=null && tail!=null){ 42 | if(current.data!=tail.data){ 43 | return false; 44 | } 45 | current=current.next; 46 | tail=tail.next; 47 | } 48 | return true; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /LinkedList/Medium/Intersection Point in Y Shaped Linked Lists.java: -------------------------------------------------------------------------------- 1 | /* Node of a linked list 2 | class Node { 3 | int data; 4 | Node next; 5 | Node(int d) { data = d; next = null; } 6 | } 7 | Linked List class 8 | class LinkedList 9 | { 10 | Node head; // head of list 11 | }*/ 12 | 13 | class Intersect 14 | { 15 | //Function to find intersection point in Y shaped Linked Lists. 16 | int intersectPoint(Node head1, Node head2) 17 | { 18 | // code here 19 | Node ptr1 = head1; 20 | Node ptr2 = head2; 21 | 22 | while(ptr1 != ptr2){ 23 | ptr1 = ptr1==null ? head2 : ptr1.next; 24 | ptr2 = ptr2==null ? head1 : ptr2.next; 25 | } 26 | return ptr1==null ? -1 : ptr1.data; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LinkedList/Medium/Merge two sorted linked lists.java: -------------------------------------------------------------------------------- 1 | /* 2 | Merge two linked lists 3 | head pointer input could be NULL as well for empty list 4 | Node is defined as 5 | class Node 6 | { 7 | int data; 8 | Node next; 9 | Node(int d) {data = d; next = null; } 10 | } 11 | */ 12 | 13 | class LinkedList 14 | { 15 | //Function to merge two sorted linked list. 16 | Node sortedMerge(Node head1, Node head2) { 17 | // This is a "method-only" submission. 18 | // You only need to complete this method 19 | if(head1==null) 20 | return head2; 21 | if(head2==null) 22 | return head1; 23 | 24 | if(head1.data set=new HashSet<>(); 19 | Node prev=null; 20 | Node current=head; 21 | while(current.next!=null){ 22 | if(set.contains(current)){ 23 | prev.next=null; 24 | break; 25 | } 26 | else{ 27 | set.add(current); 28 | } 29 | prev=current; 30 | current=current.next; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LinkedList/Medium/Reverse a Linked List in groups of given size.java: -------------------------------------------------------------------------------- 1 | /*node class of the linked list 2 | 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int key) 8 | { 9 | data = key; 10 | next = null; 11 | } 12 | } 13 | 14 | */ 15 | 16 | class Solution 17 | { 18 | public static Node reverse(Node node, int k) 19 | { 20 | //Your code here 21 | Node temp=node; 22 | for(int i=0;i odd=new ArrayList<>(); 20 | ArrayList even=new ArrayList<>(); 21 | while(temp!=null){ 22 | if(temp.data%2==0) 23 | even.add(temp.data); 24 | else 25 | odd.add(temp.data); 26 | temp=temp.next; 27 | } 28 | temp=head; 29 | for(int e:even){ 30 | temp.data=e; 31 | temp=temp.next; 32 | } 33 | for(int o:odd){ 34 | temp.data=o; 35 | temp=temp.next; 36 | } 37 | 38 | return head; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LinkedList/Medium/Union of Two Linked Lists.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | Node(int d) {data = d; next = null; } 7 | } 8 | */ 9 | 10 | class Sol 11 | { 12 | public static Node findUnion(Node head1,Node head2) 13 | { 14 | //Add your code here. 15 | TreeSet set=new TreeSet<>(); 16 | while(head1!=null){ 17 | set.add(head1.data); 18 | head1=head1.next; 19 | } 20 | while(head2!=null){ 21 | set.add(head2.data); 22 | head2=head2.next; 23 | } 24 | Node newList=new Node(0); 25 | Node head=newList; 26 | for(int i:set){ 27 | head.next=new Node(i); 28 | head=head.next; 29 | } 30 | return newList.next; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Math/Basic/Count Squares.java: -------------------------------------------------------------------------------- 1 | //User function Template for Java 2 | 3 | class Solution { 4 | static int countSquares(int N) { 5 | // code here 6 | double x=Math.sqrt(N); 7 | if((int)x * (int)x == N){ 8 | return (int)x - 1; 9 | } 10 | return (int)x; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Math/Basic/Factorial.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static long factorial(int N){ 3 | // code here 4 | long answer=1; 5 | for(int i=1; i<=N; i++){ 6 | answer*=i; 7 | } 8 | return answer; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Math/Basic/Power of 2.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | 3 | // Function to check if given number n is a power of two. 4 | public static boolean isPowerofTwo(long n){ 5 | 6 | // Your code here 7 | if(n==1){ 8 | return true; 9 | } 10 | if(n==0){ 11 | return false; 12 | } 13 | if(n%2!=0){ 14 | return false; 15 | } 16 | return isPowerofTwo(n/2); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Math/Basic/Prime Number.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int isPrime(int N){ 3 | // code here 4 | if(N==1){ 5 | return 0; 6 | } 7 | for(int i=2;i<=Math.sqrt(N);i++){ 8 | if(N%i==0){ 9 | return 0; 10 | } 11 | } 12 | return 1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Math/Basic/Print first n Fibonacci Numbers.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to return list containing first n fibonacci numbers. 4 | public static long[] printFibb(int n) 5 | { 6 | //Your code here 7 | long[] answer=new long[n]; 8 | if(n>=1) 9 | answer[0]=1; 10 | if(n>=2) 11 | answer[1]=1; 12 | for (int i = 2; i < n; i++) { 13 | answer[i] = answer[i - 1] + answer[i - 2]; 14 | } 15 | return answer; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Math/Easy/Arithmetic Number.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int inSequence(int A, int B, int C){ 3 | // code here 4 | if(C==0) 5 | return (A==B) ? 1:0; 6 | else{ 7 | if((B-A)%C==0 && (B-A)/C>=0) 8 | return 1; 9 | } 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Math/Easy/Count Digits.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int evenlyDivides(int N){ 3 | // code here 4 | int temp=N; 5 | int count=0; 6 | while(temp!=0){ 7 | int digit=temp%10; 8 | if(digit!=0 && N%digit==0){ 9 | count++; 10 | } 11 | temp/=10; 12 | } 13 | return count; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Math/Easy/Find first set bit.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to find position of first set bit in the given number. 4 | public static int getFirstSetBit(int n){ 5 | 6 | // Your code here 7 | int index=1; 8 | String bit=Integer.toBinaryString(n); 9 | for(int i=bit.length()-1;i>=0;i--){ 10 | if(bit.charAt(i)=='1') 11 | return index; 12 | index++; 13 | } 14 | return 0; 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Math/Easy/For Loop- primeCheck.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static String isPrime(int n) { 3 | 4 | 5 | // Code here 6 | if(n==1) 7 | return "No"; 8 | for (int i = 2; i <= Math.sqrt(n) ; i++) { 9 | // Write your logic here 10 | // Return "Yes" if n is prime, else return "No" 11 | /*n is prime only if it is not divisible by any i. 12 | We will not reach till n and 1 is already discarded*/ 13 | // return Yes if n is a prime number ,else return No 14 | if(n%i==0) 15 | return "No"; 16 | 17 | } 18 | return "Yes"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Math/Easy/Is Binary Number Multiple of 3.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int isDivisible(String s) { 3 | // code here 4 | int answer=0; 5 | for(char n:s.toCharArray()){ 6 | answer=(answer*2+(n-'0'))%3; 7 | } 8 | return (answer==0)?1:0; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Math/Easy/Nth Fibonacci Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int nthFibonacci(int n){ 3 | // code here 4 | if(n<=1) 5 | return n; 6 | if(n==2) 7 | return 1; 8 | int a=1,b=1; 9 | int mod=(int)1e9+7; 10 | for(int i=3;i<=n;i++){ 11 | int c=(a+b)%mod; 12 | a=b; 13 | b=c; 14 | } 15 | return b; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Math/Easy/Number of 1 Bits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int setBits(int N) { 3 | // code here 4 | int count=0; 5 | String binary=Integer.toBinaryString(N); 6 | for(char ch : binary.toCharArray()){ 7 | if(ch=='1') 8 | count++; 9 | } 10 | return count; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Math/Easy/Perfect Numbers.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int isPerfectNumber(long N) { 3 | // code here 4 | if(N==1) 5 | return 0; 6 | long sum=1; 7 | for(int i=2;i<=Math.sqrt(N);i++){ 8 | if(N%i==0){ 9 | sum+=i; 10 | if(i!=(N/i)) 11 | sum+=(N/i); 12 | } 13 | } 14 | return sum==N?1:0; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Math/Easy/Summed Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static long sumMatrix(long n, long q) { 3 | // code here 4 | if(q > 2*n) 5 | return 0; 6 | 7 | return n-Math.abs(n+1 -q); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Math/Medium/Power Of Numbers.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | 4 | long power(int N,int R) 5 | { 6 | //Your code here 7 | if(R == 0) 8 | return 1; 9 | if(R == 1) 10 | return N; 11 | 12 | int mod = 1000000007; 13 | 14 | long answer= power(N, R/2)%mod; 15 | 16 | if(R%2 == 0){ 17 | return (answer*answer)%mod; 18 | } 19 | else{ 20 | return ((answer*answer)%mod*N)%mod; 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Math/School/Sum of Series.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static long seriesSum(int n) { 3 | // code here 4 | return ((long)n*(long)(n+1))/2; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Queue/Basic/Implement Queue using array.java: -------------------------------------------------------------------------------- 1 | class MyQueue { 2 | 3 | int front, rear; 4 | int arr[] = new int[100005]; 5 | 6 | MyQueue() 7 | { 8 | front=0; 9 | rear=0; 10 | } 11 | 12 | //Function to push an element x in a queue. 13 | void push(int x) 14 | { 15 | // Your code here 16 | arr[rear++]=x; 17 | } 18 | 19 | //Function to pop an element from queue and return that element. 20 | int pop() 21 | { 22 | // Your code here 23 | if(front == rear) 24 | return -1; 25 | return arr[front++]; 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Queue/Easy/Queue using two Stacks.java: -------------------------------------------------------------------------------- 1 | class StackQueue 2 | { 3 | Stack s1 = new Stack(); 4 | Stack s2 = new Stack(); 5 | 6 | //Function to push an element in queue by using 2 stacks. 7 | void Push(int x) 8 | { 9 | // Your code here 10 | s1.add(x); 11 | } 12 | 13 | 14 | //Function to pop an element from queue by using 2 stacks. 15 | int Pop() 16 | { 17 | // Your code here 18 | if(s1.isEmpty()) 19 | return -1; 20 | while(s1.size()>1){ 21 | s2.push(s1.pop()); 22 | } 23 | int val=s1.pop(); 24 | while(!s2.isEmpty()){ 25 | s1.push(s2.pop()); 26 | } 27 | return val; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Stack/Basic/Implement stack using array.java: -------------------------------------------------------------------------------- 1 | class MyStack 2 | { 3 | int top; 4 | int arr[] = new int[1000]; 5 | 6 | MyStack() 7 | { 8 | top = -1; 9 | } 10 | 11 | //Function to push an integer into the stack. 12 | void push(int a) 13 | { 14 | // Your code here 15 | arr[++top]=a; 16 | } 17 | 18 | //Function to remove an item from top of the stack. 19 | int pop() 20 | { 21 | // Your code here 22 | if(top == -1) 23 | return -1; 24 | return arr[top--]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Stack/Easy/Stack using two queues.java: -------------------------------------------------------------------------------- 1 | class Queues 2 | { 3 | Queue q1 = new LinkedList(); 4 | Queue q2 = new LinkedList(); 5 | 6 | //Function to push an element into stack using two queues. 7 | void push(int a) 8 | { 9 | // Your code here 10 | q1.add(a); 11 | } 12 | 13 | //Function to pop an element from stack using two queues. 14 | int pop() 15 | { 16 | // Your code here 17 | if(q1.isEmpty()) 18 | return -1; 19 | else{ 20 | while(q1.size() >1){ 21 | q2.add(q1.remove()); 22 | } 23 | int val=q1.remove(); 24 | while(!q2.isEmpty()) 25 | q1.add(q2.remove()); 26 | return val; 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Stack/Medium/Next Greater Element.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to find the next greater element for each element of the array. 4 | public static long[] nextLargerElement(long[] arr, int n) 5 | { 6 | // Your code here 7 | Stack stack=new Stack<>(); 8 | long[] result=new long[n]; 9 | Arrays.fill(result,-1); 10 | for(int i=0;iarr[stack.peek()]){ 12 | result[stack.peek()]=arr[i]; 13 | stack.pop(); 14 | } 15 | stack.push(i); 16 | } 17 | return result; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /String/Basic/Implement strstr.java: -------------------------------------------------------------------------------- 1 | class GfG 2 | { 3 | //Function to locate the occurrence of the string x in the string s. 4 | int strstr(String s, String x) 5 | { 6 | // Your code here 7 | if(s.contains(x)){ 8 | return s.indexOf(x); 9 | } 10 | return -1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /String/Basic/Replace all 0's with 5.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int convertfive(int num) { 3 | // Your code here 4 | String n= String.valueOf(num); 5 | n=n.replaceAll(String.valueOf('0'),String.valueOf('5')); 6 | num=Integer.valueOf(n); 7 | return num; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /String/Easy/Longest Distinct characters in string.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int longestSubstrDistinctChars(String S){ 3 | // code here 4 | HashSet set=new HashSet<>(); 5 | int count=0; 6 | int answer=0; 7 | int start=0; 8 | 9 | for (char ch : S.toCharArray()) { 10 | if (!set.contains(ch)) { 11 | set.add(ch); 12 | count++; 13 | answer = Math.max(answer, count); 14 | } else { 15 | while (set.contains(ch)) { 16 | set.remove(S.charAt(start)); 17 | start++; 18 | count--; 19 | } 20 | set.add(ch); 21 | count++; 22 | } 23 | } 24 | return answer; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /String/Easy/Minimum indexed character.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to find the minimum indexed character. 4 | public static int minIndexChar(String str, String patt) 5 | { 6 | // Your code here 7 | HashMap map=new HashMap<>(); 8 | for(int i=0;i stack=new Stack<>(); 10 | for(int i=0;i map = new HashMap<>(); 6 | map.put('I',1); 7 | map.put('V',5); 8 | map.put('X',10); 9 | map.put('L',50); 10 | map.put('C',100); 11 | map.put('D',500); 12 | map.put('M',1000); 13 | 14 | int result=map.get(str.charAt(0)); 15 | for(int i=1;i map.get(str.charAt(i-1))){ 17 | result+=map.get(str.charAt(i))-2*map.get(str.charAt(i-1)); 18 | } 19 | else{ 20 | result+=map.get(str.charAt(i)); 21 | } 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /String/Easy/String Rotated by 2 Places.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to check if a string can be obtained by rotating 4 | //another string by exactly 2 places. 5 | public static boolean isRotated(String str1, String str2) 6 | { 7 | // Your code here 8 | if (str1.length() != str2.length() || (str1.length() < 2 && !str1.equals(str2))) { 9 | return false; 10 | } 11 | 12 | String leftRotate = str1.substring(2) + str1.substring(0, 2); 13 | 14 | String rightRotate = str1.substring(str1.length() - 2) + str1.substring(0, str1.length() - 2); 15 | 16 | return leftRotate.equals(str2) || rightRotate.equals(str2); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /String/Easy/Winner of an election.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to return the name of candidate that received maximum votes. 4 | public static String[] winner(String arr[], int n) 5 | { 6 | // add your code 7 | HashMap map=new HashMap<>(); 8 | for(String str:arr){ 9 | if(map.containsKey(str)){ 10 | map.put(str,map.get(str)+1); 11 | } 12 | else{ 13 | map.put(str,1); 14 | } 15 | } 16 | int max=-1; 17 | String winner=""; 18 | for(Map.Entry entry : map.entrySet()){ 19 | String person=entry.getKey(); 20 | int val=entry.getValue(); 21 | if(max0; i--){ 10 | if(s.charAt(i)>='0' && s.charAt(i)<='9'){ 11 | answer+=(s.charAt(i)-'0')*mul; 12 | mul*=10; 13 | } 14 | else{ 15 | return -1; 16 | } 17 | } 18 | if(s.charAt(0)=='-'){ 19 | answer*=-1; 20 | }else if(s.charAt(0)>='0' && s.charAt(0)<='9'){ 21 | answer+=(s.charAt(0)-'0')*mul; 22 | }else{ 23 | return -1; 24 | } 25 | return answer; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /String/Medium/Validate an IP Address.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | public boolean isValid(String s) { 4 | // Write your code here 5 | if(s.startsWith(".")||s.endsWith(".")) 6 | return false; 7 | String[] list=s.split("\\."); 8 | if(list.length !=4) 9 | return false; 10 | for (String part : list) { 11 | if (part.isEmpty()) { 12 | return false; 13 | } 14 | 15 | if (part.length()>3||(part.length() > 1 && part.startsWith("0"))) { 16 | return false; 17 | } 18 | try { 19 | int num = Integer.parseInt(part); 20 | 21 | if (num < 0 || num > 255) { 22 | return false; 23 | } 24 | } catch (NumberFormatException e) { 25 | return false; 26 | } 27 | } 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /potd/Basic/Identical Linked Lists.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node { 3 | int data; 4 | Node next; 5 | 6 | public Node(int data){ 7 | this.data = data; 8 | this.next = null; 9 | } 10 | }*/ 11 | 12 | class Solution { 13 | // Function to check whether two linked lists are identical or not. 14 | public boolean areIdentical(Node head1, Node head2) { 15 | // write your code here 16 | Node ptr1=head1; 17 | Node ptr2=head2; 18 | 19 | while(ptr1!=null && ptr2!=null){ 20 | if(ptr1.data!=ptr2.data){ 21 | return false; 22 | } 23 | ptr1=ptr1.next; 24 | ptr2=ptr2.next; 25 | } 26 | if(ptr1==null && ptr2!=null) 27 | return false; 28 | if(ptr2==null && ptr1!=null) 29 | return false; 30 | return true; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /potd/Easy/Armstrong Numbers.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | class Solution { 3 | static String armstrongNumber(int n) { 4 | // code here 5 | String answer=String.valueOf(n); 6 | int result=0; 7 | for(char ch : answer.toCharArray()){ 8 | int num= ch -'0'; 9 | result+=((int)Math.pow(num,3)); 10 | } 11 | return result == n ? "true" : "false"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /potd/Easy/Array to BST.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | Node arrtobst (int[] nums, int low, int high) { 3 | if (low > high) 4 | return null; 5 | 6 | int mid = (low + high)/2; 7 | Node root = new Node(nums[mid]); 8 | root.left = arrtobst(nums, low, mid-1); 9 | root.right = arrtobst(nums, mid+1, high); 10 | return root; 11 | } 12 | public Node sortedArrayToBST(int[] nums) { 13 | // Code here 14 | return arrtobst(nums, 0, nums.length-1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /potd/Easy/Binary representation of next number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | String binaryNextNumber(String s) { 3 | // code here. 4 | char[] arr=s.toCharArray(); 5 | boolean noZeroes=true; 6 | for(int i=arr.length-1; i>=0; i--){ 7 | if(arr[i]=='0'){ 8 | arr[i]='1'; 9 | noZeroes=false; 10 | break; 11 | } 12 | else{ 13 | arr[i]='0'; 14 | } 15 | 16 | } 17 | if(noZeroes){ 18 | s="1"+new String(arr); 19 | } 20 | else{ 21 | s=new String(arr); 22 | } 23 | while(s.charAt(0)=='0' && s.length()>1 ){ 24 | s = s.substring(1); 25 | } 26 | return s; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /potd/Easy/Check for BST.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to check whether a Binary Tree is BST or not. 3 | boolean isBST(Node root) { 4 | // code here. 5 | return check(root,Long.MIN_VALUE,Long.MAX_VALUE); 6 | } 7 | 8 | boolean check(Node root,long MINVAL,long MAXVAL){ 9 | if(root==null) { 10 | return true; 11 | } 12 | 13 | if(root.data>=MAXVAL || root.data<=MINVAL) { 14 | return false; 15 | } 16 | 17 | return check(root.left,MINVAL,root.data) && check(root.right,root.data,MAXVAL); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /potd/Easy/Compare two fractions.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | String compareFrac(String str) { 4 | // Code here 5 | String parts[] = str.split(", "); 6 | String val1[] = parts[0].split("/"); 7 | String val2[] = parts[1].split("/"); 8 | int a=Integer.parseInt(val1[0]); 9 | int b=Integer.parseInt(val1[1]); 10 | int c=Integer.parseInt(val2[0]); 11 | int d=Integer.parseInt(val2[1]); 12 | 13 | double ab = (double)a/b; 14 | double cd = (double)c/d; 15 | 16 | if(ab == cd){ 17 | return "equal"; 18 | }else if(ab > cd){ 19 | return parts[0]; 20 | }else{ 21 | return parts[1]; 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /potd/Easy/Convert array into Zig-Zag fashion.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static void zigZag(int n, int[] arr) { 3 | // code here 4 | for(int i=0; i arr[i+1] && i%2==0) || (arr[i] < arr[i+1] && i%2!=0)){ 6 | int temp=arr[i]; 7 | arr[i]=arr[i+1]; 8 | arr[i+1]=temp; 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /potd/Easy/Count Pairs whose sum is equal to X.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | { 4 | int data; 5 | Node next; 6 | 7 | Node(int key) 8 | { 9 | data = key; 10 | next = null; 11 | } 12 | } 13 | */ 14 | 15 | class Solution { 16 | 17 | public static int countPairs(LinkedList head1, LinkedList head2, 18 | int x) { 19 | // add your code here 20 | HashSet set=new HashSet<>(); 21 | int count=0; 22 | for(int element:head1){ 23 | set.add(element); 24 | } 25 | for(int element : head2){ 26 | if(set.contains(x-element)){ 27 | count++; 28 | } 29 | } 30 | return count; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /potd/Easy/Count numbers containing 4.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int countNumberswith4(int n) { 3 | // code here 4 | if(n<4) 5 | return 0; 6 | if(n<14) 7 | return 1; 8 | int count=0; 9 | for(int i=1; i<=n; i++){ 10 | if(contains(i)) 11 | count++; 12 | } 13 | return count; 14 | } 15 | private static boolean contains(int n){ 16 | while(n >0){ 17 | if(n%10 == 4) 18 | return true; 19 | n/=10; 20 | } 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /potd/Easy/Count pairs Sum in matrices.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | int countPairs(int mat1[][], int mat2[][], int n, int x) { 4 | // code here 5 | HashSet set=new HashSet<>(); 6 | for(int i=0;i=j){ 12 | count++; 13 | } 14 | } 15 | answer[i]=count; 16 | } 17 | return answer; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /potd/Easy/Count ways to N'th Stair.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to count number of ways to reach the nth stair 3 | // when order does not matter. 4 | Long countWays(int n) { 5 | // your code here 6 | return (long)(n/2)+1; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /potd/Easy/Coverage of all Zeros in a Binary Matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findCoverage(int[][] matrix) { 3 | // code here 4 | int count=0; 5 | int[] X={0,0,-1,1}; 6 | int[] Y={-1,1,0,0}; 7 | 8 | for(int i=0; i=0 && newX=0 && newY set=new HashSet<>(); 5 | for(int num : arr){ 6 | if(set.contains(num-x) || set.contains(x+num) ) 7 | return 1; 8 | set.add(num); 9 | } 10 | return -1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /potd/Easy/Find length of Loop.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node 4 | { 5 | int data; 6 | Node next; 7 | Node(int d) {data = d; next = null; } 8 | } 9 | 10 | */ 11 | 12 | // Function should return the length of the loop in LL. 13 | 14 | class Solution { 15 | // Function to find the length of a loop in the linked list. 16 | public int countNodesinLoop(Node head) { 17 | // Add your code here. 18 | Node slow=head; 19 | Node fast=head; 20 | while(fast!=null && fast.next!=null && fast.next.next!=null){ 21 | fast=fast.next.next; 22 | slow=slow.next; 23 | if(slow==fast){ 24 | int count=1; 25 | slow=slow.next; 26 | while(slow!=fast){ 27 | slow=slow.next; 28 | count++; 29 | } 30 | return count; 31 | } 32 | } 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /potd/Easy/Find maximum volume of a cuboid.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | 3 | class Solution { 4 | 5 | double maxVolume(double perimeter, double area) { 6 | // code here 7 | double length=(perimeter - Math.sqrt(perimeter*perimeter- 24*area))/12; 8 | double height=(perimeter/4)-2*length; 9 | return length*length*height; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /potd/Easy/Find the Highest number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPeakElement(List a) { 3 | // Code here 4 | for(int i=0; ia.get(i+1)) 6 | return a.get(i); 7 | } 8 | return -1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /potd/Easy/Find the closest number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int findClosest(int n, int k, int[] arr) { 3 | // code here 4 | int answer=Integer.MAX_VALUE; 5 | int closestNum=0; 6 | for(int num : arr){ 7 | int difference=Math.abs(num-k); 8 | if (difference <= answer) { 9 | answer = difference; 10 | if(num>closestNum) 11 | closestNum = num; 12 | } 13 | } 14 | return closestNum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /potd/Easy/Index of an Extra Element.java: -------------------------------------------------------------------------------- 1 | /*Complete the function below*/ 2 | class Solution { 3 | public int findExtra(int n, int arr1[], int arr2[]) { 4 | // add code here. 5 | int i=0; 6 | int j=0; 7 | 8 | while(i insertAtBottom(Stack st, int x) { 3 | Stack newStack=new Stack(); 4 | while(!st.empty()){ 5 | newStack.push(st.pop()); 6 | } 7 | st.push(x); 8 | while(!newStack.empty()){ 9 | st.push(newStack.pop()); 10 | } 11 | return st; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /potd/Easy/Juggler Sequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static List jugglerSequence(long n) { 3 | // code here 4 | List answer=new ArrayList<>(); 5 | sequence(n,answer); 6 | return answer; 7 | } 8 | private static void sequence(long n , List answer){ 9 | if(n==1){ 10 | answer.add(n); 11 | return; 12 | } 13 | answer.add(n); 14 | if(n%2==0){ 15 | sequence((long)Math.pow(n,0.5),answer); 16 | } 17 | else{ 18 | sequence((long)Math.pow(n,1.5),answer); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /potd/Easy/K-Pangrams.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | boolean kPangram(String str, int k) { 3 | // code here 4 | String s=""; 5 | for(int i=0;i set=new HashSet<>(); 14 | 15 | for(int i=0;i=26){ 19 | return true; 20 | } 21 | if(set.size()>=26){ 22 | return true; 23 | } 24 | return false; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /potd/Easy/Left View of Binary Tree.java: -------------------------------------------------------------------------------- 1 | /* A Binary Tree node 2 | class Node 3 | { 4 | int data; 5 | Node left, right; 6 | 7 | Node(int item) 8 | { 9 | data = item; 10 | left = right = null; 11 | } 12 | }*/ 13 | class Tree 14 | { 15 | //Function to return list containing elements of left view of binary tree. 16 | static int h; 17 | 18 | ArrayList leftView(Node root) 19 | { 20 | // Your code here 21 | h = 0; 22 | ArrayList list = new ArrayList<>(); 23 | solve(list,root); 24 | return list; 25 | } 26 | public static void solve(ArrayList list,Node root){ 27 | if(root == null){ 28 | return; 29 | } 30 | if(h>= list.size()){ 31 | list.add(root.data); 32 | } 33 | h++; 34 | solve(list,root.left); 35 | solve(list,root.right); 36 | h--; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /potd/Easy/Level order traversal.java: -------------------------------------------------------------------------------- 1 | /* 2 | class Node 3 | { 4 | int data; 5 | Node left, right; 6 | 7 | Node(int item) 8 | { 9 | data = item; 10 | left = right = null; 11 | } 12 | } 13 | */ 14 | class Solution 15 | { 16 | //Function to return the level order traversal of a tree. 17 | static ArrayList levelOrder(Node root) 18 | { 19 | // Your code here 20 | ArrayList result=new ArrayList<>(); 21 | Queue que=new LinkedList<>(); 22 | que.add(root); 23 | while(!que.isEmpty()){ 24 | Node removed=que.remove(); 25 | result.add(removed.data); 26 | if(removed.left!=null){ 27 | que.add(removed.left); 28 | } 29 | if(removed.right!=null){ 30 | que.add(removed.right); 31 | } 32 | } 33 | return result; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /potd/Easy/Linked list of strings forms a palindrome.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean compute(Node root) { 3 | 4 | Node current=root; 5 | StringBuilder s=new StringBuilder(); 6 | while(current!=null){ 7 | s.append(current.data); 8 | current=current.next; 9 | } 10 | StringBuilder sb=new StringBuilder(s).reverse(); 11 | return s.toString().equals(sb.toString()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /potd/Easy/Longest Common Prefix of Strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String arr[]) { 3 | // code here 4 | String result=arr[0]; 5 | 6 | for(int i=1; i{ 14 | public int compare(Meeting o1, Meeting o2){ 15 | if(o2.end < o1.end){ 16 | return 1; 17 | }else if(o1.end < o2.end){ 18 | return -1; 19 | }else if(o1.pos < o2.pos){ 20 | return -1; 21 | } 22 | return 1; 23 | } 24 | } 25 | class Solution { 26 | // Function to find the maximum number of meetings that can 27 | // be performed in a meeting room. 28 | 29 | public int maxMeetings(int n, int start[], int end[]) { 30 | // add your code here 31 | int count=1; 32 | ArrayList meets=new ArrayList<>(); 33 | 34 | for(int i=0; i endLimit){ 44 | count++; 45 | endLimit=meets.get(i).end; 46 | } 47 | } 48 | return count; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /potd/Easy/Odd Even Problem.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static String oddEven(String s) { 3 | // code here 4 | HashMap map=new HashMap<>(); 5 | for(char ch : s.toCharArray()){ 6 | map.put(ch, map.getOrDefault(ch,0)+1); 7 | } 8 | int x=0; 9 | int y=0; 10 | for (Map.Entry entry : map.entrySet()) { 11 | if(entry.getKey()%2==0 && entry.getValue()%2==0){ 12 | x++; 13 | } 14 | else if(entry.getKey()%2==1 && entry.getValue()%2==1){ 15 | y++; 16 | } 17 | } 18 | return (x+y)%2 == 0? "EVEN" : "ODD"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /potd/Easy/Padovan Sequence.java: -------------------------------------------------------------------------------- 1 | //User function Template for Java 2 | 3 | class Solution 4 | { 5 | public int padovanSequence(int n) 6 | { 7 | //code here. 8 | if(n==0 || n==1 ||n==2) 9 | return 1; 10 | int a=1,b=1,c=1; 11 | int answer=1; 12 | for(int i=3; i<=n; i++){ 13 | answer=(b+c)%(int)(1e9+7); 14 | c=b; 15 | b=a; 16 | a=answer; 17 | } 18 | return answer; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /potd/Easy/Parenthesis Checker.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | //Function to check if brackets are balanced or not. 4 | static boolean ispar(String x) 5 | { 6 | // add your code here 7 | if(x.length()==0 || x.length()%2==1 || x.length()==1){ 8 | return false; 9 | } 10 | 11 | Stack stack=new Stack<>(); 12 | 13 | for(char ch : x.toCharArray()){ 14 | if(ch=='('|| ch=='{'||ch=='['){ 15 | stack.push(ch); 16 | }else{ 17 | if(stack.isEmpty()){ 18 | return false; 19 | } 20 | char top=stack.pop(); 21 | if(top=='{'&&ch!='}'||top=='('&&ch!=')'||top=='['&&ch!=']'){ 22 | return false; 23 | } 24 | } 25 | } 26 | return stack.isEmpty(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /potd/Easy/Party of Couples.java: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | static int findSingle(int n, int arr[]){ 3 | // code here 4 | HashSet set =new HashSet<>(); 5 | for(int num:arr){ 6 | if(set.contains(num)) 7 | set.remove(num); 8 | else 9 | set.add(num); 10 | } 11 | for(int num:set) 12 | return num; 13 | return -1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /potd/Easy/Print Bracket Number.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | ArrayList bracketNumbers(String str) { 3 | // code here 4 | Stack stack=new Stack<>(); 5 | ArrayList answer=new ArrayList<>(); 6 | int brackets=0; 7 | 8 | for(char ch : str.toCharArray()){ 9 | if(ch == '('){ 10 | brackets++; 11 | answer.add(brackets); 12 | stack.push(brackets); 13 | } 14 | else if(ch == ')'){ 15 | answer.add(stack.peek()); 16 | stack.pop(); 17 | } 18 | } 19 | return answer; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /potd/Easy/Product array puzzle.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | 3 | class Solution { 4 | public static long[] productExceptSelf(int nums[]) { 5 | // code here 6 | int n=nums.length; 7 | long[] result=new long[n]; 8 | result[0]=1; 9 | for(int i=1; i=0; i--){ 14 | result[i]*=x; 15 | x*=nums[i]; 16 | } 17 | return result; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /potd/Easy/Remaining String.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String printString(String s, char ch, int count) { 3 | // code here 4 | int i=0; 5 | String str=new String(""); 6 | while(i set=new HashSet<>(); 5 | StringBuilder sb=new StringBuilder(); 6 | 7 | for(char ch: str.toCharArray()){ 8 | if (set.add(ch)) { 9 | sb.append(ch); 10 | } 11 | } 12 | return sb.toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /potd/Easy/Remove Half Nodes.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | class Node{ 4 | int data; 5 | Node left,right; 6 | Node(int d){ 7 | data=d; 8 | left=right=null; 9 | } 10 | } 11 | 12 | */ 13 | 14 | class Solution { 15 | // Return the root of the modified tree after removing all the half nodes. 16 | public Node help(Node node){ 17 | if(node==null)return null; 18 | 19 | node.left=help(node.left); 20 | node.right=help(node.right); 21 | 22 | if(node.left==null && node.right==null) return node; 23 | else if(node.right==null) return node=node.left; 24 | else if(node.left==null) return node=node.right; 25 | 26 | return node; 27 | } 28 | 29 | public Node RemoveHalfNodes(Node root) { 30 | Node ans=help(root); 31 | return ans; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /potd/Easy/Remove all duplicates from a given string.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | String removeDuplicates(String str) { 5 | HashSet h=new HashSet<>(); 6 | StringBuilder strNew=new StringBuilder(); 7 | for(int i=0; i arr) { 4 | // code here 5 | int[] count=new int[3]; 6 | for(int num : arr){ 7 | count[num]++; 8 | } 9 | int index=0; 10 | for(int i=0; i<3; i++){ 11 | for(int j=0; jb){ 22 | int temp=array[mid]; 23 | array[mid]=array[high]; 24 | array[high]=temp; 25 | high--; 26 | } 27 | else{ 28 | mid++; 29 | } 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /potd/Easy/Trail of ones.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int numberOfConsecutiveOnes(int n) { 3 | // code here 4 | int mod= 1000000007; 5 | int dp[]=new int[n+1]; 6 | dp[0]=0; 7 | dp[1]=0; 8 | dp[2]=1; 9 | 10 | int a=0; 11 | int b=1; 12 | for(int i=3; i<=n ;i++){ 13 | int c=(a+b)%mod; 14 | dp[i]=(dp[i-1]*2)%mod +c%mod; 15 | dp[i]=dp[i]%mod; 16 | a=b; 17 | b=c; 18 | } 19 | return dp[n]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /potd/Easy/Xoring and Clearing.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | class Solution { 3 | public void printArr(int n, int arr[]) { 4 | // code here 5 | for(int i:arr){ 6 | System.out.print(i+" "); 7 | } 8 | System.out.println(); 9 | } 10 | 11 | public void setToZero(int n, int arr[]) { 12 | // code here 13 | for(int i=0;i list=new ArrayList<>(); 7 | int ls=0; 8 | for(int i=n-1; i>=0; i--){ 9 | int id= add(list,ls,arr[i]); 10 | result[i]=id; 11 | ls++; 12 | } 13 | return result; 14 | } 15 | 16 | int add(List list, int n, int x){ 17 | int low=0; 18 | int high=n-1; 19 | while(low<=high){ 20 | int mid=(low+high)/2; 21 | if(list.get(mid)==x) 22 | high=mid-1; 23 | else if(list.get(mid)>x) 24 | high=mid-1; 25 | else 26 | low=mid+1; 27 | } 28 | if(low==n){ 29 | list.add(x); 30 | return low; 31 | } 32 | list.add(low,x); 33 | return low; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /potd/Hard/The Palindrome Pattern.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String pattern(int[][] arr) { 3 | // Code here 4 | for(int i=0; i 0)? 1:2; 8 | } 9 | 10 | private static boolean onSegment(int[] p, int[] q, int[] r){ 11 | return (q[0] <= Math.max(p[0], r[0]) && q[0] >= Math.min(p[0], r[0]) && 12 | q[1] <= Math.max(p[1], r[1]) && q[1] >= Math.min(p[1], r[1])); 13 | } 14 | 15 | String doIntersect(int p1[], int q1[], int p2[], int q2[]) { 16 | // code here 17 | int o1=orientation(p1,q1,p2); 18 | int o2=orientation(p1,q1,q2); 19 | int o3=orientation(p2,q2,p1); 20 | int o4=orientation(p2,q2,q1); 21 | 22 | if(o1 != o2 && o3 != o4) 23 | return "true"; 24 | 25 | if (o1 == 0 && onSegment(p1, p2, q1)) return "true"; 26 | 27 | if (o2 == 0 && onSegment(p1, q2, q1)) return "true"; 28 | 29 | if (o3 == 0 && onSegment(p2, p1, q2)) return "true"; 30 | 31 | if (o4 == 0 && onSegment(p2, q1, q2)) return "true"; 32 | 33 | return "false"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /potd/Medium/Combination Sum II.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> CombinationSum2(int arr[], int n, int k) { 3 | // Code Here. 4 | List> answer=new ArrayList<>(); 5 | Arrays.sort(arr); 6 | 7 | findCombination(0,arr,k,answer,new ArrayList<>()); 8 | return answer; 9 | } 10 | 11 | private static void findCombination(int index , int[] arr , int target , List> answer , List ds){ 12 | 13 | if(target==0){ 14 | answer.add(new ArrayList<>(ds)); 15 | return; 16 | } 17 | 18 | for(int i=index ; iindex && arr[i]==arr[i-1]) 20 | continue; 21 | if(arr[i]>target) 22 | break; 23 | 24 | ds.add(arr[i]); 25 | findCombination(i+1 , arr , target-arr[i] , answer , ds); 26 | ds.remove(ds.size()-1); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /potd/Medium/Construct list using given q XOR queries.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static ArrayList constructList(int q, int[][] queries) { 3 | // code here 4 | ArrayList answer=new ArrayList<>(); 5 | int xor=0; 6 | for(int i=q-1 ; i>=0; i--){ 7 | if(queries[i][0]==1){ 8 | xor^=queries[i][1]; 9 | } 10 | else{ 11 | answer.add(queries[i][1]^xor); 12 | } 13 | } 14 | answer.add(xor); 15 | Collections.sort(answer); 16 | return answer; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /potd/Medium/Find missing in second array.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | ArrayList findMissing(int a[], int b[], int n, int m) 4 | { 5 | 6 | ArrayList missing=new ArrayList<>(); 7 | HashSet set=new HashSet<>(); 8 | 9 | for(int i=0;i (b.profit - a.profit)); 8 | 9 | int maxi=0; 10 | int jobCount=0; 11 | int jobProfit=0; 12 | 13 | for(int i=0; imaxi) 16 | maxi=arr[i].deadline; 17 | } 18 | int[] result=new int[maxi+1]; 19 | Arrays.fill(result, -1); 20 | 21 | for(int i=0; i0; j--){ 23 | 24 | if(result[j] == -1){ 25 | result[j]=i; 26 | jobCount++; 27 | jobProfit+=arr[i].profit; 28 | break; 29 | } 30 | } 31 | } 32 | return new int[] {jobCount,jobProfit}; 33 | 34 | } 35 | } 36 | 37 | 38 | /* 39 | class Job { 40 | int id, profit, deadline; 41 | Job(int x, int y, int z){ 42 | this.id = x; 43 | this.deadline = y; 44 | this.profit = z; 45 | } 46 | } 47 | */ 48 | -------------------------------------------------------------------------------- /potd/Medium/K-th element of two Arrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long kthElement(int k, int arr1[], int arr2[]) { 3 | // code here 4 | int i=0; 5 | int j=0; 6 | while(i queue=new PriorityQueue<>(Collections.reverseOrder()); 5 | for(int num :arr){ 6 | queue.add(num); 7 | if(queue.size()>k) 8 | queue.poll(); 9 | } 10 | return queue.poll(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /potd/Medium/Left Rotate Matrix K times.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int[][] rotateMatrix(int k, int mat[][]) { 3 | // code here 4 | int[][] answer=new int[mat.length][mat[0].length]; 5 | for(int i=0; inextNode.data){ 22 | int temp=current.data; 23 | current.data=nextNode.data; 24 | nextNode.data=temp; 25 | } 26 | nextNode=nextNode.next; 27 | } 28 | current=current.next; 29 | } 30 | return head; 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /potd/Medium/Longest alternating subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int alternatingMaxLength(int[] arr) { 3 | // code here 4 | int i=1; 5 | int j=1; 6 | for(int k=1; karr[k-1]) 9 | i = j+1; 10 | else if(arr[k] b[0] - a[0]); 12 | int i=0; 13 | int j=0; 14 | for(int k=0; k=bTip){ 19 | if( i arr[ans]) { 20 | ans = i; 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /potd/Medium/Minimum steps to destination.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | static int minSteps(int d) { 3 | // code here 4 | for(int i=0; i< 2*d ; i++){ 5 | if((i*(i+1)/2 +d)%2==0 && (i*(i+1)/2)>=d) 6 | return i; 7 | } 8 | return -1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /potd/Medium/Mobile numeric keypad.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long getCount(int n) { 3 | // Your code goes here 4 | if(n==1){ 5 | return 10; 6 | } 7 | 8 | int arr[][] = { 9 | {0, 8}, 10 | {1, 2, 4}, 11 | {1, 2, 3, 5}, 12 | {2, 3, 6}, 13 | {1, 4, 5, 7}, 14 | {2, 4, 5, 6, 8}, 15 | {3, 5, 6, 9}, 16 | {4, 7, 8}, 17 | {0, 5, 7, 8, 9}, 18 | {6, 8, 9} 19 | }; 20 | 21 | long dp[][]=new long[n+1][10]; 22 | 23 | for(int i=0; i<10; i++){ 24 | dp[1][i]=1; 25 | } 26 | 27 | for(int i=2; i0){ 9 | result+=mul*(n%9); 10 | n/=9; 11 | mul*=10; 12 | } 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /potd/Medium/Number of Rectangles in a Circle.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int rectanglesInCircle(int r) { 3 | // code here 4 | int count=0; 5 | int max=4*r*r; 6 | for(int i=1; i<2*r; i++){ 7 | for(int j=1; j<2*r; j++){ 8 | if(i*i + j*j <=max){ 9 | count++; 10 | } 11 | } 12 | } 13 | return count; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /potd/Medium/Nuts and Bolts Problem.java: -------------------------------------------------------------------------------- 1 | // User function Template for Java 2 | 3 | class Solution { 4 | void matchPairs(int n, char nuts[], char bolts[]) { 5 | // code here 6 | int[] arr=new int[n]; 7 | for(int i=0; i getPrimes(int n) { 10 | // code here 11 | ArrayList arr=new ArrayList<>(); 12 | 13 | for(int i=2; i<=n/2; i++){ 14 | if(isPrime(i) && isPrime(n-i)){ 15 | arr.add(i); 16 | arr.add(n-i); 17 | break; 18 | } 19 | } 20 | if(arr.isEmpty()){ 21 | arr.add(-1); 22 | arr.add(-1); 23 | } 24 | return arr; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /potd/Medium/Remove all occurences of duplicates in a linked list.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public Node removeAllDuplicates(Node head) { 3 | // code here 4 | if(head==null) 5 | return null; 6 | 7 | Node dummy=new Node(0); 8 | dummy.next=head; 9 | 10 | Node prev=dummy; 11 | Node current=head; 12 | 13 | while(current!=null){ 14 | while(current.next!=null && current.data==current.next.data){ 15 | current=current.next; 16 | } 17 | if(prev.next==current){ 18 | prev=prev.next; 19 | } 20 | else{ 21 | prev.next=current.next; 22 | } 23 | current=current.next; 24 | } 25 | 26 | return dummy.next; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /potd/Medium/Rotate a Linked List.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to rotate a linked list. 3 | public Node rotate(Node head, int k) { 4 | // add code here 5 | if(head==null || head.next==null || k==0) 6 | return head; 7 | 8 | Node current = head; 9 | int length = 1; 10 | 11 | while (current.next != null) { 12 | current = current.next; 13 | length++; 14 | } 15 | 16 | k = k % length; 17 | if (k == 0) return head; 18 | 19 | current.next=head; 20 | 21 | for (int i = 0; i < length - k; i++) { 22 | current = current.next; 23 | } 24 | 25 | head = current.next; 26 | current.next = null; 27 | return head; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /potd/Medium/Row with max 1s.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rowWithMax1s(int arr[][]) { 3 | // code here 4 | int max=0; 5 | int index=-1; 6 | for(int i=0; i 9*d){ 5 | return "-1"; 6 | } 7 | int[] digits=new int[d]; 8 | s--; 9 | 10 | for(int i=d-1; i>0; i--){ 11 | if (s > 9) { 12 | digits[i] = 9; 13 | s -= 9; 14 | } else { 15 | digits[i] = s; 16 | s = 0; 17 | } 18 | } 19 | digits[0] = s + 1; 20 | StringBuilder result = new StringBuilder(); 21 | for (int digit : digits) { 22 | result.append(digit); 23 | } 24 | 25 | return result.toString(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /potd/Medium/Sum Tree.java: -------------------------------------------------------------------------------- 1 | /* A Binary Tree node 2 | class Node 3 | { 4 | int data; 5 | Node left, right; 6 | 7 | Node(int item) 8 | { 9 | data = item; 10 | left = right = null; 11 | } 12 | } 13 | */ 14 | class Solution { 15 | boolean flag=true; 16 | boolean isSumTree(Node root) { 17 | // Your code here 18 | solve(root); 19 | return flag; 20 | } 21 | int solve(Node root){ 22 | if(root==null){ 23 | return 0; 24 | } 25 | if(root.left ==null && root.right==null){ 26 | return root.data; 27 | } 28 | int left=solve(root.left); 29 | int right=solve(root.right); 30 | if(left+right!=root.data){ 31 | flag=false; 32 | } 33 | return left+right+root.data; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /potd/Medium/Sum of Middle elements of two sorted arrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int SumofMiddleElements(int[] arr1, int[] arr2) { 3 | // code here 4 | int n=arr1.length; 5 | int m=arr2.length; 6 | int[] arr= new int[n+m]; 7 | 8 | int i=0; 9 | int j=0; 10 | int index=0; 11 | 12 | while(i set=new HashSet<>(); 5 | long aSum=0,bSum=0; 6 | for(long num : a){ 7 | aSum+=num; 8 | } 9 | 10 | for(long num : b){ 11 | bSum+=num; 12 | } 13 | long difference=Math.abs(aSum-bSum); 14 | if(difference ==0){ 15 | return 1; 16 | } 17 | if((aSum+bSum)%2 == 1){ 18 | return -1; 19 | } 20 | 21 | for(long num : a){ 22 | set.add(num); 23 | } 24 | 25 | for(long num : b){ 26 | if(set.contains(difference - num)) 27 | return 1; 28 | } 29 | return -1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /potd/Medium/The Celebrity Problem.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Function to find if there is a celebrity in the party or not. 3 | public int celebrity(int mat[][]) { 4 | // code here 5 | HashSet set=new HashSet<>(); 6 | 7 | for (int i = 0; i < mat.length; i++) { 8 | boolean knowsSomeone = false; 9 | for (int j = 0; j < mat[i].length; j++) { 10 | if (mat[i][j] == 1) { 11 | knowsSomeone = true; 12 | set.add(j); 13 | } 14 | } 15 | if (!knowsSomeone) { 16 | set.add(i); 17 | } 18 | } 19 | 20 | for (int num : set) { 21 | boolean isCelebrity = true; 22 | for (int j = 0; j < mat.length; j++) { 23 | if (j != num) { 24 | if (mat[num][j] == 1 || mat[j][num] == 0) { 25 | isCelebrity = false; 26 | break; 27 | } 28 | } 29 | } 30 | if (isCelebrity) { 31 | return num; 32 | } 33 | } 34 | return -1; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /potd/Medium/Toeplitz matrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /*You are required to complete this method*/ 3 | boolean isToeplitz(int mat[][]) { 4 | // Your code here 5 | int row=mat.length; 6 | int col=mat[0].length; 7 | 8 | 9 | for(int i=0; i findUnion(int arr1[], int arr2[], int n, int m) 5 | { 6 | // add your code here 7 | ArrayList union =new ArrayList<>(); 8 | int lastAdded=Integer.MIN_VALUE; 9 | int i=0,j=0; 10 | 11 | while(iarr2[j]){ 26 | 27 | if(arr2[j] != lastAdded){ 28 | 29 | union.add(arr2[j]); 30 | lastAdded=arr2[j]; 31 | 32 | } 33 | 34 | j++; 35 | } 36 | 37 | else{ 38 | 39 | if(arr1[i] != lastAdded){ 40 | union.add(arr1[i]); 41 | lastAdded=arr1[i]; 42 | } 43 | 44 | i++; 45 | } 46 | } 47 | 48 | while(i1 && part.charAt(0)=='0')){ 12 | return false; 13 | } 14 | 15 | try{ 16 | int num=Integer.parseInt(part); 17 | if(num<0 || num>255){ 18 | return false; 19 | } 20 | }catch(NumberFormatException nfe){ 21 | return false; 22 | } 23 | } 24 | 25 | return true; 26 | } 27 | } 28 | --------------------------------------------------------------------------------