├── .vscode └── settings.json ├── Codechef ├── CampusChapter1 │ ├── funrun.cpp │ └── gamenum.cpp ├── Codepentry │ ├── aureole.cpp │ ├── grday1.cpp │ └── nosem.cpp ├── codezinga │ ├── beeances.cpp │ ├── lgst.cpp │ └── makeor.cpp ├── codiction │ └── cc000.cpp ├── cookoff │ ├── cyclesum.cpp │ ├── pshot.cpp │ └── slab.cpp ├── longApril20 │ ├── carsell.cpp │ ├── covidlq.cpp │ ├── strno.cpp │ ├── strno1.cpp │ └── unitgcd.cpp ├── longAugust20 │ ├── chefwars.cpp │ └── linchess.cpp ├── longFeb20 │ ├── cash.cpp │ ├── longcook.cpp │ ├── longcook1.cpp │ ├── nochange.cpp │ ├── snug_fit.cpp │ └── theatre.cpp ├── longJuly20 │ ├── adaking.cpp │ ├── chefstr1.cpp │ ├── chfnswps.cpp │ ├── crdgame.cpp │ ├── drchef.cpp │ └── ptmssng.cpp ├── longJune20 │ ├── chficrm.cpp │ ├── eoeo.cpp │ ├── pricecon.cpp │ └── xystr.cpp ├── longMarch20 │ ├── adashop2.cpp │ ├── chpintu │ ├── chpintu.cpp │ ├── engxor.cpp │ └── lazer.cpp ├── longMay20 │ ├── chandf.cpp │ ├── corus.cpp │ ├── covid19.cpp │ └── trplsrt.cpp ├── lunchJune20 │ ├── chfmot18.cpp │ ├── golmine.cpp │ ├── incrdec.cpp │ └── maxand18.cpp └── practice │ └── beginner │ ├── ffl.cpp │ └── h1.cpp ├── GeeksForGeeks ├── Arrays │ ├── 1-SubarrayWithGivenSum(SlidingWindow).cpp │ ├── 1-SubarrayWithGivenSum(trivial).cpp │ ├── 10-EqulibriumPoint.cpp │ ├── 11-LeadersInAnArray.cpp │ ├── 12-MinimumPlatforms.cpp │ ├── 13-ReverseArrayInGroups.cpp │ ├── 13.ReverseArraysInGroups(usingStack).cpp │ ├── 14-KthSmallestElement.cpp │ ├── 15.TrappingRainWater.cpp │ ├── 16.PhythagoreanTriplet.cpp │ ├── 17.ChocolateDistributionProblem.cpp │ ├── 18.StockBuyAndSell.cpp │ ├── 19.ElementWithLeftSmallerRightGreater.cpp │ ├── 2-CountTheTriplets.cpp │ ├── 20.ConvertArrayIntoZigZag.cpp │ ├── 21.LastIndexOf1.cpp │ ├── 22.SpirallyTraversingAMatrix.cpp │ ├── 23.LargestNumberFormedByArray.cpp │ ├── 3-Kadane'sAlgorithm.cpp │ ├── 4-MissingNumberInArray.cpp │ ├── 5-MergeTwoSortedArray(Using gap).cpp │ ├── 5-MergeTwoSortedArray(trivial).cpp │ ├── 6-ReaarangeArrayAlternatively.cpp │ ├── 7-NumberOfPairs.cpp │ ├── 8-InversionOfArray.cpp │ └── 9-SortAnArrayOf012.cpp ├── Backtracking │ ├── 1.NQueenProblem.cpp │ ├── 2.SolveTheSudoku.cpp │ ├── 3.RatInAMazeProblem1.cpp │ └── 4.WordBoggle.cpp ├── BitMagic │ ├── 1.FindFirstSetBit.cpp │ ├── 10.CountTotalSetBits.cpp │ ├── 11.LongestConsecutive1.cpp │ ├── 12.NumberIsSparseOrNot.cpp │ ├── 14.MaximumSubsetXor.cpp │ ├── 2.RightmostDifferentBit.cpp │ ├── 3.CheckWhethetKthBitIsSet.cpp │ ├── 4.ToggleBitsGivenRange.cpp │ ├── 5.SetKthBit.cpp │ ├── 6.PowerOf2.cpp │ ├── 7.BitDifference.cpp │ ├── 8.RotateBits.cpp │ └── 9.SwapAllOddAndEvenBits.cpp ├── DivideAndConquer │ ├── 1.FindElementThatAppearsOnceInSortedArray.cpp │ ├── 2.SearchInARotatedArray.cpp │ ├── 3.BinarySearch.cpp │ ├── 4.SumOfMiddleElementOf2SortedArrays.cpp │ ├── 5.QuickSort.cpp │ ├── 6.MergeSort.cpp │ └── 7.KthElementOf2SortedArrays.cpp ├── DynamicProgramming │ ├── 1.MinimumOperations.cpp │ ├── 10.EditDistance.cpp │ ├── 11.CoinChange.cpp │ ├── 12.SubsetSumProblem.cpp │ ├── 13.BoxStacking.cpp │ ├── 14.RodCutting.cpp │ ├── 15.PathInMatrix.cpp │ ├── 16.MinimumSumPartition.cpp │ ├── 17.CountNumberOfHops.cpp │ ├── 18.EggDroppingPuzzle.cpp │ ├── 19.OptimalStrategyForAGame.cpp │ ├── 2.MaxLengthChain.cpp │ ├── 20.ShortestCommonSuperSequence.cpp │ ├── 3.MinimumNumberOfCoins.cpp │ ├── 4.LongestCommonSubstring.cpp │ ├── 5.LongestIncreasingSubsequence.cpp │ ├── 6.LongestCommonSubsequence.cpp │ ├── 7.0_1Knapsack.cpp │ ├── 8.MaximumSumIncreasingSubsequence.cpp │ └── 9.MinimumNumberOfJumps.cpp ├── General │ ├── ArrayToBST.cpp │ ├── BleakNumbers.cpp │ ├── ChangeAllEvenBitsInNumberTo0.cpp │ ├── CircularLinkedListTraversal.cpp │ ├── CountPairsWithGivenSum.cpp │ ├── CountingSortForString.cpp │ ├── DoesArrayRepresentHeap.cpp │ ├── EulerTotientFunction.cpp │ ├── LevelOrderTraversalOfTree.cpp │ ├── LowestCommonAncestorInBinaryTree.cpp │ ├── MirrorTree.cpp │ ├── NegativeWeightCycle.cpp │ ├── PossiblePathsFromTopLeftToBottomRight.cpp │ ├── ReverseBits.cpp │ ├── ReverseLevelOrderTraversal.cpp │ ├── SearchInAMatrix.cpp │ ├── SetBits.cpp │ ├── ShortestPathFrom1ToN.cpp │ ├── SquaresInNXNChessboard.cpp │ ├── SticklerThief.cpp │ ├── Swap2NibblesInByte.cpp │ ├── SwapEvery2BitsInBytes.cpp │ └── ZigZagTreeTraversal.cpp ├── Graph │ ├── 1.DFS.cpp │ ├── 10.ShortestSourceToDestinationPath.cpp │ ├── 11.FindWhetherPathExist.cpp │ ├── 12.MinimumCostPath.cpp │ ├── 13.CircleOfStrings.cpp │ ├── 14.FloydWarshall.cpp │ ├── 15.AlienDictionary.cpp │ ├── 16.SnakeAndLadderProblem.cpp │ ├── 2.BFS.cpp │ ├── 3.DetectCycleInAnUndirectedGraph.cpp │ ├── 4.DetectCycleInADirectedGraph.cpp │ ├── 5.TopologicalSort.cpp │ ├── 6.FindTheNumberOfIslands.cpp │ ├── 7.ImplementingDijkstra.cpp │ ├── 8.MinimumSwapsToSort.cpp │ └── 9.StronglyConnectedComponents.cpp ├── Greedy │ ├── 1.ActivitySelection.cpp │ ├── 10.MinimumSpanningTree.cpp │ ├── 11.ShopInCandyStore.cpp │ ├── 12.GeekCollectsTheBall.cpp │ ├── 2.NMeetingsInOneRoom.cpp │ ├── 3.CoinPiles.cpp │ ├── 4.MaximizeToys.cpp │ ├── 5.PageFaultsInLRU.cpp │ ├── 6.LargestNumberPossible.cpp │ ├── 7.MinimizeTheHeights.cpp │ ├── 8.MinimizeTheSumOfProduct.cpp │ └── 9.HuffmanDecoding.cpp ├── Hashing │ ├── 1.RelativeSorting.cpp │ ├── 10.ArraySubsetOfAnotherArray.cpp │ ├── 11.FindAllPairsWithGivenSum.cpp │ ├── 12.FindFirstRepeatedCharacter.cpp │ ├── 13.ZeroSumSubarray.cpp │ ├── 14.MinimumIndexedCharacter.cpp │ ├── 15.CheckIf2ArraysAreEqual.cpp │ ├── 16.UncommonCharacters.cpp │ ├── 17.SmallestWindowInAStringContainingAllCharacterOfOtherString.cpp │ ├── 18.FirstElementToOccurKTimes.cpp │ ├── 19.CheckIfFrequenciesCanBeEqual.cpp │ ├── 2.SortingArrayElementsByFrequency.cpp │ ├── 3.LargestSubarraywith0Sum.cpp │ ├── 4.CommonElements.cpp │ ├── 5.FindAllFourSumNumbers.cpp │ ├── 6.SwappingPairsMakeSumEqual.cpp │ ├── 7.CountDistinctElementsInEveryWindow.cpp │ ├── 8.ArrayPairSumDivisibiltyProblem.cpp │ └── 9.LongestConsecutiveSubsequence.cpp ├── Heap │ ├── 1.FindMedianInAStream.cpp │ ├── 2.HeapSort.cpp │ ├── 3.BinaryHeapOperations.cpp │ ├── 4.RearrangeCharacters.cpp │ ├── 5.KthLargestElementInAStream.cpp │ └── 6.MergeKSortedLinkedList.cpp ├── Linked_List │ ├── 1.FindingMiddleElementInLinkedList.cpp │ ├── 10.Merge2SortedLinkedList.cpp │ ├── 12.PairwiseSwapInLinkedList.cpp │ ├── 13.Add2NumbersRepresentedByLinkedList.cpp │ ├── 14.CheckIfLinkedListIsPalindrome.cpp │ ├── 15.ImplementQueueUsingLinkedList.cpp │ ├── 16.ImplementStackUsingLinkedList.cpp │ ├── 17.LinkedListOf012.cpp │ ├── 18.DeleteWithoutHeadPointer.cpp │ ├── 2.ReverseALinkedList.cpp │ ├── 3.RotateALinkedList.cpp │ ├── 4.ReverseLinkedListInGroupOfGivenSize.cpp │ ├── 5.IntersectionPointInYShapedLinkedList.cpp │ ├── 6.DetectLoopInLinkedList.cpp │ ├── 7.RemoveLoopInLinkedList.cpp │ ├── 8.NthNodeFromEndOfLinkedList.cpp │ └── 9.FlateningALinkedList.cpp ├── Recursion │ ├── 1.FloodFillAlgorithm.cpp │ ├── 2.NumberOfPaths.cpp │ ├── 3.CombinationSumPart2.cpp │ ├── 4.SpecialKeyboard.cpp │ └── 5.JosephusProblem.cpp ├── StackAndQueue │ ├── 1.ParenthesisChecker.cpp │ ├── 10.MaximumOfAllSubarraysOfSizeK │ ├── 10.MaximumOfAllSubarraysOfSizeK.cpp │ ├── 2.NextLargerElement.cpp │ ├── 3.QueueUsing2Stacks.cpp │ ├── 4.StackUsing2Queues.cpp │ ├── 5.GetMinimumElementFromStack.cpp │ ├── 6.LRUCache.cpp │ ├── 7.CircularTour.cpp │ ├── 8.FirstNonRepeatingCharacterInStream.cpp │ └── 9.RottenOranges.cpp ├── String │ ├── 1-ReverseWordsInString.cpp │ ├── 10.LongestDistinctCharactersInString.cpp │ ├── 11.ImplementAtoi.cpp │ ├── 12.ImplementStrstr.cpp │ ├── 13.LongestCommonPrefix.cpp │ ├── 2-PermutationsOfString.cpp │ ├── 3-LongestPalindromeInString.cpp │ ├── 4-RecursivelyRemoveAdjacentDuplicates.cpp │ ├── 5-CheckIfStringRotatedBy2Places.cpp │ ├── 6-RomanNumberToInteger.cpp │ ├── 7-Anagram.cpp │ ├── 8-RemoveDuplicates.cpp │ └── 9-FormAPalindrome.cpp └── Tree │ ├── 1.LeftViewOfBinaryTree.cpp │ ├── 10.SymmetricTree.cpp │ ├── 11.HeightOfBinaryTree.cpp │ ├── 12.MaximumPathSum.cpp │ ├── 13.DiameterOfBinaryTree.cpp │ ├── 14.NumberOfLeafNodes.cpp │ ├── 15.CheckForBalancedTree.cpp │ ├── 16.SerializeAndDeserializeABinaryTree.cpp │ ├── 2.CheckForBST.cpp │ ├── 3.BottomViewOfBinaryTree.cpp │ ├── 4.VerticalTraversalOfBinaryTree.cpp │ ├── 5.LevelOrderTraversalInSpiralForm.cpp │ ├── 6.ConnectNodesAtSameLevel.cpp │ ├── 7.LowestCommonAncestorInBST.cpp │ ├── 8.BinaryTreeToDLL.cpp │ └── 9.DetermineIfTwoTreesAreIdentical.cpp ├── Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf ├── HackerRank ├── CountingValleys.cpp ├── InterviewPreparationKit │ └── Arrays │ │ └── 2DArray.cpp ├── JumpingOnTheClouds.cpp ├── RepeatedString.cpp └── SockMerchant.cpp ├── LeetCode ├── 1.TwoSum.cpp ├── 11.ContainerWithMostWater.cpp ├── 12.IntegerToRoman.cpp ├── 13.RomanToInteger.cpp ├── 14.LongestCommonPrefix.cpp ├── 15.3Sum.cpp ├── 16.3SumClosest.cpp ├── 17.LetterCombinationsOfAPhoneNumber.cpp ├── 2.AddTwoNumbers.cpp ├── 20.ValidParentheses.cpp ├── 21.MergeTwoSortedLists.cpp ├── 26.RemoveDuplicatesFromSortedArray.cpp ├── 27.RemoveElement.cpp ├── 28.Implement_strStr.cpp ├── 3.LongestSubstringWithoutRepeatingCharacters.cpp ├── 4.MedianOfTwoSortedArray.cpp ├── 5.LongestPalindromicSubstring.cpp ├── 6.ZigzaogConversion.cpp ├── 7.ReverseInteger.cpp ├── 8.StringToInteger.cpp └── 9.PalindromeNumber.cpp ├── README.md ├── cheatsheet.cpp └── codes ├── BST.cpp ├── DP ├── 1.01Knapsack.cpp ├── 2.01KnapsackTopDown.cpp ├── 3.SubsetSum.cpp ├── 4.EqualSumPartition.cpp ├── 5.CountOfSubsetSum.cpp ├── 6.MinimumSubsetSumDifference.cpp ├── 7.CountNumberOfSubsetsWithGivenDifference.cpp ├── 8.TargetSum.cpp └── 9.UnboundedKnapsack.cpp ├── LinkedListOperations.cpp ├── RandomizedQuickSort.cpp ├── Sorting ├── BubbleSort.cpp ├── CountingSort.cpp ├── HeapSort ├── HeapSort.cpp ├── InsertionSort.cpp ├── MergeSort.cpp ├── QuickSort.cpp └── SelectionSort.cpp └── Trees └── MaxPathSum.cpp /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "utility": "cpp", 4 | "type_traits": "cpp", 5 | "iostream": "cpp", 6 | "iosfwd": "cpp", 7 | "map": "cpp", 8 | "istream": "cpp", 9 | "algorithm": "cpp", 10 | "vector": "cpp", 11 | "queue": "cpp", 12 | "deque": "cpp", 13 | "ratio": "cpp", 14 | "stack": "cpp", 15 | "__split_buffer": "cpp", 16 | "array": "cpp", 17 | "string_view": "cpp", 18 | "iterator": "cpp", 19 | "set": "cpp", 20 | "string": "cpp", 21 | "unordered_map": "cpp", 22 | "unordered_set": "cpp", 23 | "hashtable": "cpp", 24 | "__functional_03": "cpp", 25 | "__mutex_base": "cpp", 26 | "memory": "cpp", 27 | "list": "cpp", 28 | "__locale": "cpp", 29 | "__string": "cpp", 30 | "__bit_reference": "cpp", 31 | "__hash_table": "cpp", 32 | "__tree": "cpp", 33 | "initializer_list": "cpp", 34 | "ios": "cpp", 35 | "functional": "cpp", 36 | "ostream": "cpp", 37 | "sstream": "cpp", 38 | "streambuf": "cpp", 39 | "tuple": "cpp", 40 | "*.tcc": "cpp" 41 | } 42 | } -------------------------------------------------------------------------------- /Codechef/CampusChapter1/funrun.cpp: -------------------------------------------------------------------------------- 1 | // Walter and Jesse 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N; 11 | cin>>N; 12 | long int A[N],B[N]; 13 | for(int i=0;i>A[i]; 15 | for(int i=0;i>B[i]; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Codechef/CampusChapter1/gamenum.cpp: -------------------------------------------------------------------------------- 1 | // Another game of numbers 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | long long int A,B,C,max=0,maxVal=0; 12 | int bitA,bitB; 13 | cin>>A>>B; 14 | bitA = log2(A) + 1; 15 | bitB = log2(B) + 1; 16 | if(bitA > bitB){ 17 | for(int i=0;i> 1)|(lsbs << (bitA - 1)); 20 | C = A^B; 21 | if(C > maxVal){ 22 | maxVal = C; 23 | max = i+1; 24 | } 25 | } 26 | } 27 | else{ 28 | for(int i=0;i> 1)|(lsbs << (bitB - 1)); 31 | C = A^B; 32 | if(C > maxVal){ 33 | maxVal = C; 34 | max = i+1; 35 | } 36 | } 37 | } 38 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | string s1,s2; 12 | cin>>s1; 13 | int len = s1.length(); 14 | int count = 1,intcount=0,charcount=0,len1,len2,count1 = 0, count2=0,out; 15 | for(int i=0;i= '0' && s1[i] <= '9'){ 23 | intcount++; 24 | count1 += count; 25 | } 26 | if(s1[i] >= 'a' && s1[i] <= 'z'){ 27 | charcount++; 28 | count2 += count; 29 | } 30 | count = 1; 31 | } 32 | } 33 | len1 = (count1 * 32) + (count2 * 8); 34 | len2 = (intcount * 64) + (charcount * 40); 35 | out = len1 - len2; 36 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,X,P; 11 | char dir,lan,L; 12 | cin>>N>>X; 13 | cin>>dir>>lan; 14 | if(dir == 'L') 15 | P = X; 16 | else if(dir == 'R') 17 | P = (N - X) + 1; 18 | 19 | if(P%2 == 1) 20 | L = lan; 21 | else{ 22 | if(lan == 'H') 23 | L = 'E'; 24 | else if(lan == 'E') 25 | L = 'H'; 26 | } 27 | cout< 4 | #include 5 | using namespace std; 6 | 7 | bool IsPowerOfTwo(int x) 8 | { 9 | return (x != 0) && ((x & (x - 1)) == 0); 10 | } 11 | 12 | int main() { 13 | int T; 14 | cin>>T; 15 | while(T--){ 16 | int X,cur=1,flag=0; 17 | cin>>X; 18 | if(X == 1 || X == 2){ 19 | cout<<"Yes\n"; 20 | continue; 21 | } 22 | if(IsPowerOfTwo(X) || IsPowerOfTwo((X*3)+1)) 23 | cout<<"Yes\n"; 24 | else 25 | cout<<"No\n"; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Codechef/codezinga/beeances.cpp: -------------------------------------------------------------------------------- 1 | // No Change required 2 | 3 | #include 4 | using namespace std; 5 | 6 | long int bee(long long int n,char g){ 7 | if((n==1 || n==2 || n==3) && g=='m') 8 | return n; 9 | if((n==1 || n==2) && g=='f') 10 | return n+1; 11 | if(g =='m') 12 | return (2*n)-3; 13 | else 14 | return (2*n)-1; 15 | } 16 | 17 | int main() { 18 | int T; 19 | cin>>T; 20 | while(T--){ 21 | long long int N,out; 22 | char G; 23 | cin>>G>>N; 24 | 25 | out = bee(N,G); 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int N,Q,flag =0; 7 | cin>>N>>Q; 8 | 9 | long int d[N],S; 10 | for(int i=0;i>d[i]; 12 | 13 | sort(d,d+N); 14 | 15 | for(int i=0;i>S; 17 | int j; 18 | for(j=N-1;j>=0;j--){ 19 | if(d[j] <= S){ 20 | flag = 1; 21 | break; 22 | } 23 | } 24 | if(flag == 0) 25 | cout<<-1<<"\n"; 26 | else 27 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int N,K,X; 6 | cin>>N>>K; 7 | cout<<"\n"; 8 | int A[N],B[N]; 9 | for(int i=0;i>A[i]; 11 | 12 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin>>T; 8 | while(T--){ 9 | float x1,x2,y1,y2,pos; 10 | cin>>x1>>y1; 11 | cin>>x2>>y2; 12 | 13 | if(y1 == y2){ 14 | pos = (x1 + x2)/2; 15 | } 16 | else if(y1 < y2){ 17 | float lim = (x1 + x2)/2; 18 | for(float i=x1;ilim;i=i-0.1){ 31 | float a1,a2; 32 | a1 = y1/(sqrt((y1*y1) + ((i-x1)*(i-x1)))); 33 | a2 = y2/(sqrt((y2*y2) + ((x2-i)*(x2-i)))); 34 | if(abs(a1 - a2) < 0.01){ 35 | pos = i; 36 | break; 37 | } 38 | } 39 | } 40 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N; 9 | cin>>N; 10 | long int arr[N],sum[N]; 11 | for(int i=0;i>arr[i]; 13 | 14 | for(int j=0;j (arr[i] + lmax)) 25 | lmax = arr[i]; 26 | else 27 | lmax = arr[i] + lmax; 28 | 29 | if(lmax > gmax) 30 | gmax = lmax; 31 | if(i==lim) 32 | break; 33 | i = (i+1) % N; 34 | count++; 35 | } 36 | 37 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int N,count1=0,count2=0,rem,s,flag=0; 9 | cin>>N; 10 | char S[2*N]; 11 | rem = N; 12 | s = 2*N; 13 | for(int i=0;i<2*N;i++){ 14 | cin>>S[i]; 15 | if(i%2 == 0){ 16 | if((count1 - count2) > rem || (count2 -count1) > rem){ 17 | s = i; 18 | break; 19 | } 20 | if(S[i] == '1') 21 | count1++; 22 | rem--; 23 | } 24 | else if(S[i] == '1') 25 | count2++; 26 | } 27 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | long int N,tax=0,bal,amt,net; 9 | cin>>N; 10 | 11 | bal = N; 12 | 13 | if(bal > 1500000){ 14 | amt = bal - 1500000; 15 | tax = tax + (amt*3)/10; 16 | bal = 1500000; 17 | } 18 | if(bal > 1250000){ 19 | amt = bal - 1250000; 20 | tax = tax + (amt*25)/100; 21 | bal = 1250000; 22 | } 23 | if(bal > 1000000){ 24 | amt = bal - 1000000; 25 | tax = tax + (amt*2)/10; 26 | bal = 1000000; 27 | } 28 | if(bal > 750000){ 29 | amt = bal - 750000; 30 | tax = tax + (amt*15)/100; 31 | bal = 750000; 32 | } 33 | if(bal > 500000){ 34 | amt = bal - 500000; 35 | tax = tax + (amt*1)/10; 36 | bal = 500000; 37 | } 38 | if(bal > 250000){ 39 | amt = bal - 250000; 40 | tax = tax + (amt*5)/100; 41 | bal = 250000; 42 | } 43 | 44 | net = N - tax; 45 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,max; 12 | long int profit = 0; 13 | cin>>N; 14 | long int P[N]; 15 | for(int i=0;i>P[i]; 17 | 18 | sort(P, P+N, greater()); 19 | 20 | for(int i=0;i 0) 23 | profit = profit + (max - i); 24 | } 25 | 26 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,start=0,end=0,flag=0,first=0; 11 | cin>>N; 12 | int A[N]; 13 | for(int i=0;i>A[i]; 15 | for(int i=0;i 4 | #include 5 | using namespace std; 6 | 7 | bool isPrime(int n) 8 | { 9 | if (n <= 1) 10 | return false; 11 | if (n <= 3) 12 | return true; 13 | 14 | if (n % 2 == 0 || n % 3 == 0) 15 | return false; 16 | 17 | for (int i = 5; i <= sqrt(n); i = i + 6) 18 | if (n % i == 0 || n % (i + 2) == 0) 19 | return false; 20 | 21 | return true; 22 | } 23 | 24 | int strangeNum(int X, int K){ 25 | if(K == 1) 26 | return 1; 27 | int flag = 0,res; 28 | for(int i=2;i<=sqrt(X);i++){ 29 | if(X % i == 0){ 30 | X = X / i; 31 | flag = 1; 32 | if(isPrime(i)) 33 | K--; 34 | } 35 | } 36 | if(flag == 1) 37 | res = strangeNum(X, K); 38 | else 39 | return 0; 40 | return res; 41 | } 42 | 43 | int main() { 44 | int T; 45 | cin>>T; 46 | while(T--){ 47 | int X,K,out; 48 | cin>>X>>K; 49 | out = strangeNum(X,K); 50 | cout< 4 | #include 5 | using namespace std; 6 | 7 | bool isPrime(int n) 8 | { 9 | if (n <= 1) 10 | return false; 11 | if (n <= 3) 12 | return true; 13 | 14 | if (n % 2 == 0 || n % 3 == 0) 15 | return false; 16 | 17 | for (int i = 5; i <= sqrt(n); i = i + 6) 18 | if (n % i == 0 || n % (i + 2) == 0) 19 | return false; 20 | 21 | return true; 22 | } 23 | 24 | int main() { 25 | int T; 26 | cin>>T; 27 | while(T--){ 28 | int X,K,div,pri,flag=0; 29 | cin>>X>>K; 30 | for(int j=X;j 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int gcd(int a, int b){ 9 | if (b == 0) 10 | return a; 11 | return gcd(b, a % b); 12 | } 13 | 14 | int main() { 15 | int T; 16 | cin>>T; 17 | while(T--){ 18 | int N,count=1; 19 | cin>>N; 20 | map > mp; 21 | vector v; 22 | vector :: iterator it; 23 | v.push_back(1); 24 | mp[1] = v; 25 | for(int i=2;i v1; 27 | v1.push_back(0); 28 | mp[i] = v1; 29 | } 30 | for(int i=2;i<=N;i++){ 31 | for(int j=0;j v2 = mp[j]; 33 | int len = v2.size(); 34 | int flag = 0; 35 | for(it = v2.begin();it != v2.end(); it++){ 36 | if(gcd(i, *it) != 1){ 37 | flag = 1; 38 | break; 39 | } 40 | } 41 | if(flag == 1 && j==count-1){ 42 | cout< v3; 44 | v3.push_back(i); 45 | count++; 46 | mp[count] = v3; 47 | break; 48 | } 49 | else if(flag == 0){ 50 | cout< v3; 52 | v3 = mp[j+1]; 53 | v3.push_back(i); 54 | // mp[j].push_back(i); 55 | mp[j] = v3; 56 | break; 57 | } 58 | } 59 | } 60 | for(int i=1;i<=count;i++){ 61 | vector v3 = mp[i]; 62 | int len = v3.size(); 63 | for(int j=0;j 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int H,P; 12 | cin>>H>>P; 13 | while(H > 0 && P > 0){ 14 | H = H - P; 15 | P = P/2; 16 | } 17 | if(H > 0) 18 | cout<<0<<"\n"; 19 | else if(P > 0) 20 | cout<<1<<"\n"; 21 | else 22 | cout<<1<<"\n"; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Codechef/longAugust20/linchess.cpp: -------------------------------------------------------------------------------- 1 | // Chef and Linear Chess 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,min=INT_MAX,out; 12 | long int K; 13 | cin>>N>>K; 14 | long int P[N]; 15 | for(int i=0;i>P[i]; 17 | if(P[i] < K && (K % P[i]) == 0){ 18 | int temp = K / P[i]; 19 | if(temp < min){ 20 | min = temp; 21 | out = P[i]; 22 | } 23 | } 24 | } 25 | if(min == INT_MAX) 26 | cout<<-1<<"\n"; 27 | else 28 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | long int N,K; 11 | cin>>N>>K; 12 | long int A[N]; 13 | for(int i=0;i>A[i]; 15 | 16 | long int i=0,j,sum=0,want; 17 | j = N-1; 18 | want = K - (A[j] % K); 19 | while(i<=j){ 20 | if(sum >= want){ 21 | sum = sum - want; 22 | j--; 23 | want = K - (A[j] % K); 24 | } 25 | else{ 26 | sum = sum + (A[i] % K); 27 | i++; 28 | } 29 | } 30 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | long int N,P,flag=0,q,r,d,pos; 11 | cin>>N>>P; 12 | long int D[N]; 13 | int C[N]; 14 | for(int i=0;i>D[i]; 16 | if((P % D[i]) != 0) 17 | flag = 1; 18 | } 19 | 20 | if(flag == 1){ 21 | for(int i=0;i0;i--){ 42 | if((D[i] % D[i-1]) != 0){ 43 | flag2 = 1; 44 | sum = sum + ((P/D[i])-1)*D[i]; 45 | C[i] = (P/D[i])-1; 46 | C[i-1] = 0; 47 | while(sum < P){ 48 | sum = sum + D[i-1]; 49 | C[i-1] = C[i-1] + 1; 50 | } 51 | pos = i-1; 52 | break; 53 | } 54 | C[i] = 0; 55 | } 56 | if(flag2 == 0) 57 | cout<<"NO"< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | for(int j=0;j>N; 13 | long int A[N],B[N]; 14 | for(int i=0;i>A[i]; 16 | for(int i=0;i>B[i]; 18 | 19 | sort(A,A+N); 20 | sort(B,B+N); 21 | 22 | for(int i=0;i 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T,totalsum=0; 10 | cin>>T; 11 | while(T--){ 12 | int N,max=0,max1=-99999999,max2=0,cash=100; 13 | cin>>N; 14 | char m[N]; 15 | int t[N]; 16 | int mat[4][4] = {0}; 17 | int a[4] = {0,1,2,3},b[4],c[13]; 18 | c[12] = 0;c[3] = 1;c[6] = 2;c[9] = 3; 19 | 20 | for(int i=0;i>m[i]>>t[i]; 22 | switch (m[i]) 23 | { 24 | case 'A': (mat[0][c[t[i]]])++; 25 | break; 26 | 27 | case 'B': (mat[1][c[t[i]]])++; 28 | break; 29 | 30 | case 'C': (mat[2][c[t[i]]])++; 31 | break; 32 | 33 | case 'D': (mat[3][c[t[i]]])++; 34 | break; 35 | 36 | default: break; 37 | } 38 | } 39 | 40 | do { 41 | int sum = 0,price = 100; 42 | b[0] = mat[0][a[0]]; 43 | b[1] = mat[1][a[1]]; 44 | b[2] = mat[2][a[2]]; 45 | b[3] = mat[3][a[3]]; 46 | sort(b, b + 4); 47 | for(int i=3;i>=0;i--){ 48 | if(b[i] != 0) 49 | sum = sum + (b[i]*price); 50 | else 51 | sum = sum - 100; 52 | price = price - 25; 53 | } 54 | if(sum <= 0){ 55 | if(sum >= max1) 56 | max1 = sum; 57 | 58 | }else{ 59 | if(sum > max2) 60 | max2 = sum; 61 | } 62 | 63 | } while(next_permutation(a, a + 4)); 64 | 65 | if(max2 > 0) 66 | max = max2; 67 | else 68 | max = max1; 69 | 70 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int K; 12 | cin>>K; 13 | 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Codechef/longJuly20/chefstr1.cpp: -------------------------------------------------------------------------------- 1 | // Chef and Strings 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,skip=0; 12 | cin>>N; 13 | int A[N]; 14 | for(int i=0;i>A[i]; 16 | if(i != 0) 17 | skip += abs(A[i] - A[i-1]) - 1; 18 | } 19 | cout< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | int A[N],B[N]; 15 | set s1,s2; 16 | for(int i=0; i>A[i]; 18 | for(int i=0; i>B[i]; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Codechef/longJuly20/crdgame.cpp: -------------------------------------------------------------------------------- 1 | // Chef and Card Game 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N; 12 | long long int chefp=0,mortyp=0,pow1,pow2,num; 13 | cin>>N; 14 | long int A[N],B[N]; 15 | for(int i=0;i>A[i]>>B[i]; 17 | if(A[i] > 9){ 18 | num = A[i]; 19 | pow1 = 0; 20 | while(num > 0){ 21 | pow1 += num % 10; 22 | num = num / 10; 23 | } 24 | } 25 | else 26 | pow1 = A[i]; 27 | 28 | if(B[i] > 9){ 29 | num = B[i]; 30 | pow2 = 0; 31 | while(num > 0){ 32 | pow2 += num % 10; 33 | num = num / 10; 34 | } 35 | } 36 | else 37 | pow2 = B[i]; 38 | 39 | if(pow1 > pow2) 40 | chefp++; 41 | else if(pow2 > pow1) 42 | mortyp++; 43 | else{ 44 | chefp++; 45 | mortyp++; 46 | } 47 | } 48 | if(chefp > mortyp) 49 | cout<<0<<" "< chefp) 51 | cout<<1<<" "< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N,x; 13 | cin>>N>>x; 14 | int A[N]; 15 | set s1,s2; 16 | for(int i=0; i>A[i]; 18 | 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codechef/longJuly20/ptmssng.cpp: -------------------------------------------------------------------------------- 1 | // Missing a Point 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | int X[4*N],Y[4*N]; 15 | set s1,s2; 16 | for(int i=0; i<(4*N)-1; i++){ 17 | cin>>X[i]>>Y[i]; 18 | if(s1.find(X[i]) != s1.end()) 19 | s1.erase(X[i]); 20 | else 21 | s1.insert(X[i]); 22 | 23 | if(s2.find(Y[i]) != s2.end()) 24 | s2.erase(Y[i]); 25 | else 26 | s2.insert(Y[i]); 27 | } 28 | if(!s1.empty() && !s2.empty()) 29 | cout<<*s1.begin()<<" "<<*s2.begin()< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,flag=0; 11 | cin>>N; 12 | int a[N]; 13 | for(int i=0;i>a[i]; 15 | int c5=0,c10=0,c15=0; 16 | for(int i=0;i 0){ 21 | c5--; 22 | c10++; 23 | } 24 | else{ 25 | flag = 1; 26 | break; 27 | } 28 | } 29 | else if(a[i] == 15){ 30 | if(c10 > 0){ 31 | c10--; 32 | c15++; 33 | } 34 | else if(c5 > 1){ 35 | c5 -= 2; 36 | c15++; 37 | } 38 | else{ 39 | flag = 1; 40 | break; 41 | } 42 | } 43 | } 44 | if(flag == 1) 45 | cout<<"NO\n"; 46 | else 47 | cout<<"YES\n"; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Codechef/longJune20/eoeo.cpp: -------------------------------------------------------------------------------- 1 | // The Tom and Jerry Game! 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | long long int TS,JS; 12 | int count = 0; 13 | cin>>TS; 14 | for(long long int i=1;i 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,K,lost=0; 11 | cin>>N>>K; 12 | int P[N]; 13 | for(int i=0;i>P[i]; 15 | if(P[i] > K) 16 | lost += (P[i] - K); 17 | } 18 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | string s; 12 | cin>>s; 13 | int len = s.length(),pair=0; 14 | for(int i=0;i 4 | using namespace std; 5 | 6 | int main() { 7 | ios_base::sync_with_stdio(false); 8 | cin.tie(NULL); 9 | int T; 10 | int chess[9][9]; 11 | for(int i=1;i<9;i++){ 12 | for(int j=1;j<9;j++){ 13 | if((i%2 == 0 && j%2 == 0) || ( i%2 == 1 && j%2 == 1)) 14 | chess[i][j] = 1; 15 | else 16 | chess[i][j] = 0; 17 | } 18 | } 19 | cin>>T; 20 | while(T--){ 21 | int r,c,temp,r1,c1,count=0,i=0,j=0; 22 | int R[64],C[64]; 23 | cin>>r>>c; 24 | 25 | temp = (r+c)/2; 26 | R[i++] = temp; C[j++] = temp; 27 | R[i++] = 1; C[j++] = 1; 28 | R[i++] = 2; C[j++] = 2; 29 | count += 3; 30 | r = 2; c = 2; 31 | while(r != 8 || c != 8){ 32 | r1 = r; c1 = c; 33 | while(chess[r1][c1] == 1 && r1<=8 && c1>=1){ 34 | r1++; 35 | c1--; 36 | } 37 | r1--; c1++; 38 | R[i++] = r1; C[j++] = c1; 39 | 40 | r1 = r; c1 = c; 41 | while(chess[r1][c1] == 1 && r1>=1 && c1<=8){ 42 | r1--; 43 | c1++; 44 | } 45 | r1++; c1--; 46 | R[i++] = r1; C[j++] = c1; 47 | R[i++] = r; C[j++] = c; 48 | r++; c++; 49 | R[i++] = r; C[j++] = c; 50 | count += 4; 51 | } 52 | 53 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | // int main() { 9 | // ios_base::sync_with_stdio(false); 10 | // cin.tie(NULL); 11 | // int T; 12 | // cin>>T; 13 | // while(T--){ 14 | // int N,M,min = 1000; 15 | // cin>>N>>M; 16 | // int F[N],P[N]; 17 | // map mp; 18 | // map::iterator itr; 19 | // for(int i=0;i>F[i]; 21 | 22 | // for(int i=0;i>P[i]; 24 | // if (mp.find(F[i]) != mp.end()) 25 | // mp[F[i]] = mp[F[i]] + P[i]; 26 | // else 27 | // mp.insert(pair(F[i], P[i])); 28 | // } 29 | 30 | // for (itr = mp.begin(); itr != mp.end(); ++itr) { 31 | // if(itr->second < min) 32 | // min = itr->second; 33 | // } 34 | // cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | 12 | int T; 13 | scanf("%d",&T); 14 | while(T--){ 15 | int N,Q,even,odd; 16 | scanf("%d%d",&N,&Q); 17 | int A[N],B[N],P; 18 | for(int i=0;i 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | long int X,Y,Z=0,L,R,F=0,max=0; 13 | cin>>X>>Y>>L>>R; 14 | 15 | for(long int i=L;i<=R;i++){ 16 | F = (X & i) * (Y & i); 17 | cout<<"i: "< max){ 19 | max = F; 20 | Z = i; 21 | } 22 | } 23 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N,Q,C; 13 | cin>>N>>Q; 14 | string S; 15 | cin>>S; 16 | int ch[26] = {0}; 17 | for(int i=0;i>C; 22 | int minSize = 0; 23 | for(int j=0;j<26;j++){ 24 | if(ch[j] > C) 25 | minSize = minSize + (ch[j] - C); 26 | } 27 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,min,max; 12 | cin>>N; 13 | int X[N]; 14 | for(int i=0;i>X[i]; 16 | 17 | min = N; 18 | max = 0; 19 | int count = 1; 20 | for(int i=1;i max) 28 | max = count; 29 | count = 1; 30 | } 31 | } 32 | if(count < min) 33 | min = count; 34 | if(count > max) 35 | max = count; 36 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N,K; 13 | cin>>N>>K; 14 | int P[N]; 15 | for(int i=0;i>P[i]; 17 | 18 | 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Codechef/lunchJune20/chfmot18.cpp: -------------------------------------------------------------------------------- 1 | // Chef and Demonetisation 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int S,N,count=0,coin; 13 | cin>>S>>N; 14 | coin = N; 15 | while(S > 0){ 16 | if(S == 1){ 17 | count++; 18 | break; 19 | } 20 | while(coin > S) 21 | coin = coin-2; 22 | count = count + (S/coin); 23 | S = S % coin; 24 | } 25 | cout< 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int T; 10 | cin>>T; 11 | while(T--){ 12 | int N; 13 | cin>>N; 14 | int A[N]; 15 | for(int i=0;i>A[i]; 17 | 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Codechef/lunchJune20/maxand18.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krishna-gavas/Competitive-Coding/2b2dcc75013a4f31f7ed5ae32bb02b7f58e43083/Codechef/lunchJune20/maxand18.cpp -------------------------------------------------------------------------------- /Codechef/practice/beginner/ffl.cpp: -------------------------------------------------------------------------------- 1 | // Chef in Fantasy League 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,S,dmin=999999,fmin=999999; 11 | cin>>N>>S; 12 | int P[N],R[N]; 13 | int sum = 100 - S; 14 | for(int i=0;i>P[i]; 16 | for(int i=0;i>R[i]; 18 | if(R[i] == 0 && P[i] < dmin) 19 | dmin = P[i]; 20 | else if(R[i] == 1 && P[i] < fmin) 21 | fmin = P[i]; 22 | } 23 | if(dmin + fmin <= sum) 24 | cout<<"yes\n"; 25 | else 26 | cout<<"no\n"; 27 | 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Codechef/practice/beginner/h1.cpp: -------------------------------------------------------------------------------- 1 | // A puzzle game 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int A[3][3]; 11 | for(int i=0;i<3;i++) 12 | for(int j=0;j<3;j++) 13 | cin>>A[i][j]; 14 | 15 | 16 | 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Arrays/1-SubarrayWithGivenSum(SlidingWindow).cpp: -------------------------------------------------------------------------------- 1 | // Subarray with given sum(Sliding window technique) 2 | 3 | // Question: 4 | // Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds 5 | // to a given number S. 6 | 7 | // Input: 8 | // The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. 9 | // Each test case consists of two lines. The first line of each test case is N and S, where N is the size of 10 | // array and S is the sum. The second line of each test case contains N space separated integers denoting the 11 | // array elements. 12 | 13 | // Output: 14 | // For each testcase, in a new line, print the starting and ending positions(1 indexing) of first such 15 | // occuring subarray from the left if sum equals to subarray, else print -1. 16 | 17 | // Example: 18 | // Input: 19 | // 2 20 | // 5 12 21 | // 1 2 3 7 5 22 | // 10 15 23 | // 1 2 3 4 5 6 7 8 9 10 24 | // Output: 25 | // 2 4 26 | // 1 5 27 | 28 | // Solution: 29 | // Initialize flag,head and sum as 0. 30 | // for(i=0 to N): sum = sum + arr[i]. 31 | // while( sum > S): sum = sum - arr[head] and increment head. 32 | // if(sum == S) then print head+1 to i+1, make flag = 1 and break. 33 | // if(flag == 0) then print -1; 34 | 35 | #include 36 | using namespace std; 37 | 38 | int main() { 39 | int T; 40 | cin>>T; 41 | while(T--){ 42 | long int N,S; 43 | long int flag=0, start=0, sum=0; 44 | cin>>N>>S; 45 | long int A[N]; 46 | for(int i=0;i>A[i]; 48 | 49 | for(int i=0;i S){ 52 | sum = sum - A[start]; 53 | start++; 54 | } 55 | if(sum == S){ 56 | cout< 14 | using namespace std; 15 | 16 | int main() { 17 | int T; 18 | cin>>T; 19 | while(T--){ 20 | long int N,S,flag=0,start=0,stop=0; 21 | cin>>N>>S; 22 | long int arr[N]; 23 | for(int i=0;i>arr[i]; 25 | 26 | for(int i=0;i 36 | #include 37 | using namespace std; 38 | 39 | int main() { 40 | long int T; 41 | cin>>T; 42 | while(T--){ 43 | long int N; 44 | cin>>N; 45 | int arr[N],i=1,j=N-1,pos=-1,leftsum=0,rightsum=0; 46 | 47 | for(long int i=0;i>arr[i]; 49 | rightsum = rightsum + arr[i]; 50 | } 51 | 52 | for(long int i=0;i 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | long int N,K; 40 | cin>>N>>K; 41 | long long int arr[N]; 42 | stack s; 43 | 44 | for(long int i=0;i>arr[i]; 46 | 47 | for(long int i=0;i 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Solution{ 9 | public: 10 | bool checkTriplet(int A[], int N) { 11 | sort(A,A+N); 12 | int L,R,num,sum; 13 | for(int i=N-1; i>1; i--){ 14 | L = 0; 15 | R = i-1; 16 | num = A[i] * A[i]; 17 | while(L < R){ 18 | sum = (A[L]*A[L]) + (A[R]*A[R]); 19 | if(sum == num) 20 | return true; 21 | else if(sum > num) 22 | R--; 23 | else 24 | L++; 25 | } 26 | } 27 | return false; 28 | } 29 | }; 30 | 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | int n, i; 36 | cin >> n; 37 | int arr[n]; 38 | for (i = 0; i < n; i++) { 39 | cin >> arr[i]; 40 | } 41 | Solution ob; 42 | auto ans = ob.checkTriplet(arr, n); 43 | if (ans) { 44 | cout << "Yes\n"; 45 | } else { 46 | cout << "No\n"; 47 | } 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Arrays/20.ConvertArrayIntoZigZag.cpp: -------------------------------------------------------------------------------- 1 | // Convert array into Zig-Zag fashion 2 | 3 | // Question: 4 | // Given an array Arr (distinct elements) of size N. Rearrange the elements of array in zig-zag fashion. The 5 | // converted array should be in form a < b > c < d > e < f. The relative order of elements is same in the output 6 | // i.e you have to iterate on the original array only. 7 | 8 | // Input: 9 | // N = 7 10 | // Arr[] = {4, 3, 7, 8, 6, 2, 1} 11 | // Output: 3 7 4 8 2 6 1 12 | 13 | // Your Task: 14 | // You don't need to read input or print anything. Your task is to complete the function zigZag() which takes the 15 | // array of integers arr and n as parameters and returns void. You need to modify the array itself. 16 | 17 | // Solution: 18 | // zigZag(): 19 | // for(i=0 to n-1): 20 | // if(i is even and arr[i] > arr[i+1]) then swap(arr[i],arr[i+1]) 21 | // else if(i is odd and arr[i] < arr[i+1]) then swap(arr[i],arr[i+1]) 22 | // return 23 | 24 | #include 25 | #include 26 | using namespace std; 27 | 28 | class Solution{ 29 | public: 30 | void zigZag(int arr[], int n) { 31 | for(int i=0;i arr[i+1]) 33 | swap(arr[i],arr[i+1]); 34 | else if(i%2 == 1 && arr[i] < arr[i+1]) 35 | swap(arr[i],arr[i+1]); 36 | } 37 | return; 38 | } 39 | }; 40 | 41 | int main() { 42 | int t; 43 | cin >> t; 44 | while (t--) { 45 | int n; 46 | cin >> n; 47 | int arr[n]; 48 | for (int i = 0; i < n; i++) { 49 | cin >> arr[i]; 50 | } 51 | Solution ob; 52 | ob.zigZag(arr, n); 53 | for (int i = 0; i < n; i++) { 54 | cout << arr[i] << " "; 55 | } 56 | cout << "\n"; 57 | } 58 | return 0; 59 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Arrays/21.LastIndexOf1.cpp: -------------------------------------------------------------------------------- 1 | // Last index of One 2 | 3 | // Question: 4 | // Given a string S consisting only '0's and '1's, print the last index of the '1' present in it. 5 | 6 | // Input: 7 | // First line of the input contains the number of test cases T, T lines follow each containing a stream of characters. 8 | 9 | // Output: 10 | // Corresponding to every test case, output the last index of 1. If 1 is not present, print "-1" (without quotes). 11 | 12 | // Example: 13 | // Input: 14 | // 2 15 | // 00001 16 | // 0 17 | // Output: 18 | // 4 19 | // -1 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | int main() { 26 | int T; 27 | cin>>T; 28 | cin.ignore(); 29 | while(T--){ 30 | int pos = -1; 31 | string str; 32 | getline(cin,str); 33 | int len = str.length(); 34 | 35 | for(int i=(len-1);i>=0;i--){ 36 | if(str[i] == '1'){ 37 | pos = i; 38 | break; 39 | } 40 | } 41 | cout< A[i] + localmax) then localmax = A[i] else localmax = A[i] + localmax. 31 | // if(localmax > globalmax) then globalmax = localmax. 32 | // print globalmax 33 | 34 | #include 35 | using namespace std; 36 | 37 | int main() { 38 | int T; 39 | cin>>T; 40 | while(T--){ 41 | int N; 42 | cin>>N; 43 | int A[N]; 44 | for(int i=0;i>A[i]; 46 | long int lmax = A[0], gmax = A[0]; 47 | for(int i=1;i (A[i] + lmax)) 49 | lmax = A[i]; 50 | else 51 | lmax = lmax + A[i]; 52 | gmax = max(gmax, lmax); 53 | } 54 | cout< 32 | using namespace std; 33 | 34 | int main() { 35 | int T; 36 | cin>>T; 37 | while(T--){ 38 | int N; 39 | long int sum = 0, total; 40 | cin>>N; 41 | int A[N-1]; 42 | total = (N * (N+1))/2; 43 | for(int i=0;i>A[i]; 45 | sum = sum + A[i]; 46 | } 47 | cout< 19 | using namespace std; 20 | 21 | int main() { 22 | int T; 23 | cin>>T; 24 | while(T--){ 25 | long int X,Y; 26 | cin>>X>>Y; 27 | long int P[X],Q[Y]; 28 | 29 | for(long int i=0;i>P[i]; 31 | 32 | for(long int j=0;j>Q[j]; 34 | 35 | for(long int i=0;i Q[j]) && (j != Y-1)) 38 | continue; 39 | else if((P[i] > Q[j]) && (j == Y-1)){ 40 | int temp = P[i]; 41 | P[i] = Q[0]; 42 | for(int k=0;k 36 | using namespace std; 37 | 38 | int main() { 39 | int T; 40 | cin>>T; 41 | while(T--){ 42 | long int N; 43 | cin>>N; 44 | long int arr[N]; 45 | for(int i=0;i>arr[i]; 47 | 48 | long int min=0,max=N-1,M=arr[N-1]+1; // M = Largest element + 1 49 | 50 | for(int i=0;i 26 | using namespace std; 27 | 28 | int main() { 29 | int T; 30 | cin>>T; 31 | while(T--){ 32 | int N,pos=0; 33 | cin>>N; 34 | while(N > 0){ 35 | pos++; 36 | if(N & 1) 37 | break; 38 | N = N >> 1; 39 | } 40 | cout< 23 | using namespace std; 24 | 25 | int main() { 26 | int T; 27 | cin>>T; 28 | while(T--){ 29 | int N,count,sum=0; 30 | cin>>N; 31 | for(int i=1;i<=N;i++){ 32 | int x = i; 33 | count = 0; 34 | while(x > 0){ 35 | if(x & 1) 36 | count++; 37 | x = x >> 1; 38 | } 39 | sum = sum + count; 40 | } 41 | cout< 30 | using namespace std; 31 | 32 | int main() { 33 | int T; 34 | cin>>T; 35 | while(T--){ 36 | int N,count=0; 37 | cin>>N; 38 | 39 | while(N != 0){ 40 | N = N & (N << 1); 41 | count++; 42 | } 43 | cout< 30 | using namespace std; 31 | 32 | int main() { 33 | int T; 34 | cin>>T; 35 | while(T--){ 36 | int N,sparse=0; 37 | cin>>N; 38 | 39 | N = N & (N << 1); 40 | if(N == 0) 41 | sparse = 1; 42 | cout< 37 | using namespace std; 38 | 39 | #define INT_BITS 32 40 | int maxSubarrayXOR(int [], int n); 41 | int main() 42 | { 43 | 44 | int t; 45 | cin>>t; 46 | while(t--) 47 | { 48 | int n; 49 | cin>>n; 50 | int a[n]; 51 | for(int i=0;i>a[i]; 53 | 54 | cout< 2 | using namespace std; 3 | 4 | int main() { 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int M,N,pos=1; 9 | cin>>M>>N; 10 | if(M == N){ 11 | cout<<-1< 33 | #include 34 | using namespace std; 35 | 36 | int main() { 37 | int T; 38 | cin>>T; 39 | while(T--){ 40 | int N,K,oldN,newN; 41 | cin>>N>>K; 42 | oldN = N; 43 | for(int i=0;i> 1; 45 | } 46 | if(N & 1){ 47 | cout<<"Yes"<<"\n"; 48 | } 49 | else{ 50 | cout<<"No"<<"\n"; 51 | } 52 | 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /GeeksForGeeks/BitMagic/4.ToggleBitsGivenRange.cpp: -------------------------------------------------------------------------------- 1 | // Toggle bits given range 2 | 3 | // Question 4 | // Given a non-negative number N and two values L and R. The problem is to toggle the bits in the range L to R in 5 | // the binary representation of N, i.e, to toggle bits from the rightmost Lth bit to the rightmost Rth bit. A toggle 6 | // operation flips a bit 0 to 1 and a bit 1 to 0. 7 | 8 | // Input: 9 | // First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. 10 | // First line of each test case contains three space separated integers N, L and R. 11 | 12 | // Output: 13 | // For each test case , print the number obtained by toggling bits from the rightmost Lth bit to the rightmost Rth 14 | // bit in binary representation of N. 15 | 16 | // Example: 17 | // Input: 18 | // 2 19 | // 17 2 3 20 | // 50 2 5 21 | // Output: 22 | // 23 23 | // 44 24 | 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | int T; 31 | cin>>T; 32 | while(T--){ 33 | int N,L,R; 34 | cin>>N>>L>>R; 35 | for(int i=L-1; i 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | int T; 31 | cin>>T; 32 | while(T--){ 33 | int N,K,oldN,newN; 34 | cin>>N>>K; 35 | oldN = N; 36 | for(int i=0;i> 1; 38 | } 39 | if(N & 1){ 40 | newN = oldN; 41 | } 42 | else{ 43 | newN = oldN + pow(2,K); 44 | } 45 | 46 | cout< 27 | #include 28 | using namespace std; 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | long int N,count=0; 35 | cin>>N; 36 | while(N > 0){ 37 | if(N & 1) 38 | count++; 39 | N = N >> 1; 40 | } 41 | if(count == 1) 42 | cout<<"YES\n"; 43 | else 44 | cout<<"NO\n"; 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /GeeksForGeeks/BitMagic/7.BitDifference.cpp: -------------------------------------------------------------------------------- 1 | // Bit Difference 2 | 3 | // Question 4 | // You are given two numbers A and B. Write a program to count number of bits needed to be 5 | // flipped to convert A to B. 6 | 7 | // Input: 8 | // The first line of input contains an integer T denoting the number of test cases. 9 | // T testcases follow. The first line of each test case is A and B separated by a space. 10 | 11 | // Output: 12 | // For each testcase, in a new line, print the number of bits needed to be flipped. 13 | 14 | // Example: 15 | // Input: 16 | // 1 17 | // 10 20 18 | // Output: 19 | // 4 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | int main() { 26 | int T; 27 | cin>>T; 28 | while(T--){ 29 | int A,B,N,count=0; 30 | cin>>A>>B; 31 | N = A ^ B; 32 | while(N > 0){ 33 | if(N & 1) 34 | count++; 35 | N = N >> 1; 36 | } 37 | cout< 31 | #include 32 | using namespace std; 33 | 34 | int main() 35 | { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N,D; 40 | cin >> N >> D; 41 | bitset <16> b(N); 42 | 43 | D = D%16; 44 | 45 | cout << ((b<>(16-D))).to_ullong()<>D)|(b<<(16-D))).to_ullong()< 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | int X,Y; 35 | cin>>X; 36 | bitset<8> bset(X); 37 | for(int i=0;i<8;i=i+2){ 38 | bool temp = bset[i]; 39 | bset[i] = bset[i+1]; 40 | bset[i+1] = temp; 41 | } 42 | cout< 24 | using namespace std; 25 | 26 | int main(){ 27 | int T; 28 | cin>>T; 29 | while(T--){ 30 | int N; 31 | cin>>N; 32 | long int A[N]; 33 | for(int i=0;i>A[i]; 35 | for(int i=0;i pivot) then decrement i and swap A[i] with A[j]. 6 | // finally swap A[i-1] with A[l](i.e swap last element smaller than pivot with pivot) 7 | // return (i-1) (i.e position of pivot) 8 | 9 | #include 10 | using namespace std; 11 | 12 | int partition(int A[], int l, int r) //Using first element as pivot 13 | { 14 | int pivot =A[l]; 15 | int i = (r + 1); 16 | 17 | for (int j = r; j > l; j--){ 18 | if (A[j] > pivot){ 19 | i--; 20 | swap(A[i], A[j]); 21 | } 22 | } 23 | swap(A[i - 1], A[l]); 24 | return (i - 1); 25 | } 26 | 27 | void quickSort(int A[],int low, int high){ 28 | if (low < high) { 29 | int pi = partition(A, low, high); 30 | quickSort(A, low, pi - 1); 31 | quickSort(A, pi + 1, high); 32 | } 33 | } 34 | 35 | int main(){ 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N; 40 | cin>>N; 41 | int A[N]; 42 | for(int i=0;i>A[i]; 44 | quickSort(A,0,N-1); 45 | for(int i=0;i 10 | using namespace std; 11 | 12 | void merge(int A[],int l,int mid,int r){ 13 | int B[r+1]; 14 | int i=l,j=mid+1,k=l; 15 | while(i <= mid && j <= r){ 16 | if(A[i] <=A[j]) 17 | B[k] = A[i++]; 18 | else{ 19 | B[k] = A[j++]; 20 | } 21 | k = k + 1; 22 | } 23 | while(i <= mid) 24 | B[k++] = A[i++]; 25 | while(j <= r) 26 | B[k++] = A[j++]; 27 | 28 | for(int j=l;j<=r;j++) 29 | A[j] = B[j]; 30 | } 31 | 32 | void mergeSort(int arr[], int l, int r) { 33 | if (l < r) { 34 | int m = l+(r-l)/2; 35 | mergeSort(arr, l, m); 36 | mergeSort(arr, m+1, r); 37 | merge(arr, l, m, r); 38 | } 39 | } 40 | 41 | int main(){ 42 | int T; 43 | cin>>T; 44 | while(T--){ 45 | int N; 46 | cin>>N; 47 | int A[N]; 48 | for(int i=0;i>A[i]; 50 | mergeSort(A,0,N-1); 51 | for(int i=0;i 33 | using namespace std; 34 | 35 | void Merge(int A[],int B[],int C[],int n,int m){ 36 | int i=0,j=0,k=0; 37 | while(i < n && j < m){ 38 | if(A[i] <=B[j]) 39 | C[k] = A[i++]; 40 | else{ 41 | C[k] = B[j++]; 42 | } 43 | k = k + 1; 44 | } 45 | while(i < n) 46 | C[k++] = A[i++]; 47 | while(j < m) 48 | C[k++] = B[j++]; 49 | } 50 | 51 | int main(){ 52 | int T; 53 | cin>>T; 54 | while(T--){ 55 | int N,M,K; 56 | cin>>N>>M>>K; 57 | int A[N],B[M],C[N+M]; 58 | for(int i=0;i>A[i]; 60 | for(int i=0;i>B[i]; 62 | Merge(A,B,C,N,M); 63 | cout< 0): 12 | // if(N is 1) then increment count and break from the loop 13 | // if(N is odd) then increment count 14 | // divide N by 2 and increment count 15 | 16 | #include 17 | using namespace std; 18 | 19 | int main() { 20 | int T; 21 | cin>>T; 22 | while(T--){ 23 | int N,count=0; 24 | cin>>N; 25 | while(N > 0){ 26 | if(N == 1){ 27 | count++; 28 | break; 29 | } 30 | if(N%2 == 1) 31 | count++; 32 | N = N/2; 33 | count++; 34 | } 35 | cout< 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | int EditDistance(string s1, string s2, int m, int n){ 31 | int DP[m+1][n+1]; 32 | for(int i=0;i<=m;i++){ 33 | for(int j=0;j<=n;j++){ 34 | if(i==0) 35 | DP[i][j] = j; 36 | else if(j==0) 37 | DP[i][j] = i; 38 | else if(s1[i-1] == s2[j-1]) 39 | DP[i][j] = DP[i-1][j-1]; 40 | else 41 | DP[i][j] = min(DP[i-1][j-1], min(DP[i][j-1],DP[i-1][j])) + 1; 42 | } 43 | } 44 | return DP[m][n]; 45 | } 46 | 47 | int main() { 48 | int T; 49 | cin>>T; 50 | while(T--){ 51 | int N,M; 52 | cin>>N>>M; 53 | string str1,str2; 54 | cin>>str1>>str2; 55 | cout< 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int SubsetSum(int A[],int sum, int N){ 18 | bool DP[N+1][sum+1]; 19 | for(int i=0;i<=N;i++){ 20 | for(int j=0;j<=sum;j++){ 21 | if(i==0 && j!= 0) 22 | DP[i][j] = false; 23 | else if(j == 0) 24 | DP[i][j] = true; 25 | else if(A[i-1] <= j) 26 | DP[i][j] = (DP[i-1][j - A[i-1]]) || DP[i-1][j]; 27 | else 28 | DP[i][j] = DP[i-1][j]; 29 | } 30 | } 31 | return DP[N][sum]; 32 | } 33 | 34 | int main() { 35 | int T; 36 | cin>>T; 37 | while(T--){ 38 | int N,sum=0; 39 | cin>>N; 40 | int A[N]; 41 | for(int i=0;i>A[i]; 43 | sum += A[i]; 44 | } 45 | if(sum%2 == 1) 46 | cout<<"NO\n"; 47 | else{ 48 | if(SubsetSum(A,sum/2,N) == true) 49 | cout<<"YES\n"; 50 | else 51 | cout<<"NO\n"; 52 | } 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /GeeksForGeeks/DynamicProgramming/14.RodCutting.cpp: -------------------------------------------------------------------------------- 1 | // Maximize The Cut Segments 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int RodCutting(int sum,int x,int y,int z){ 10 | int DP[N+1][sum+1]; 11 | for(int i=0;i<=N;i++){ 12 | for(int j=0;j<=sum;j++){ 13 | if(i==0 && j!= 0) 14 | DP[i][j] = 0; 15 | else if(j == 0) 16 | DP[i][j] = 1; 17 | else if(coin[i-1] <= j) 18 | DP[i][j] = (DP[i][j - coin[i-1]]) + DP[i-1][j]; 19 | else 20 | DP[i][j] = DP[i-1][j]; 21 | cout<>T; 31 | while(T--){ 32 | int N; 33 | cin>>N; 34 | int x,y,z; 35 | cin>>x>>y>>z; 36 | cout< 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int static N; 10 | 11 | int findLongestFromACell(int i, int j, int A[N][N], int DP[N][N]) 12 | { 13 | if (i < 0 || i >= n || j < 0 || j >= n) 14 | return 0; 15 | 16 | if (DP[i][j] != -1) 17 | return DP[i][j]; 18 | 19 | // To store the path lengths in all the four directions 20 | int x = INT_MIN, y = INT_MIN, z = INT_MIN, w = INT_MIN; 21 | 22 | // Since all numbers are unique and in range from 1 to n*n, 23 | // there is atmost one possible direction from any cell 24 | if (j < n - 1 && ((A[i][j] + 1) == A[i][j + 1])) 25 | x = 1 + findLongestFromACell(i, j + 1, A, DP); 26 | 27 | if (j > 0 && (A[i][j] + 1 == A[i][j - 1])) 28 | y = 1 + findLongestFromACell(i, j - 1, A, DP); 29 | 30 | if (i > 0 && (A[i][j] + 1 == A[i - 1][j])) 31 | z = 1 + findLongestFromACell(i - 1, j, A, DP); 32 | 33 | if (i < n - 1 && (A[i][j] + 1 == A[i + 1][j])) 34 | w = 1 + findLongestFromACell(i + 1, j, A, DP); 35 | 36 | return DP[i][j] = max(x, max(y, max(z, max(w, 1)))); 37 | } 38 | 39 | int PathInMatrix(int A[N][N], int N){ 40 | int sum = 0; 41 | int DP[N][N]; 42 | memset(DP,-1,sizeof(DP)); 43 | for(int i=0;i>T; 56 | while(T--){ 57 | N = 0; 58 | cin>>N; 59 | int A[N][N] 60 | for(int i=0;i>A[i][j]; 63 | cout< 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | int NumberOfHops(int n){ 30 | int DP[n+1]; 31 | DP[0] = 0; 32 | DP[1] = 1; 33 | DP[2] = 2; 34 | DP[3] = 4; 35 | if(n < 4) 36 | return DP[n]; 37 | else{ 38 | for(int i=4;i<=n;i++) 39 | DP[i] = DP[i-1] + DP[i-2] + DP[i-3]; 40 | return DP[n]; 41 | } 42 | } 43 | 44 | int main() { 45 | int T; 46 | cin>>T; 47 | while(T--){ 48 | int N,sum=0; 49 | cin>>N; 50 | cout< 26 | #include 27 | using namespace std; 28 | 29 | struct val{ 30 | int first; 31 | int second; 32 | }; 33 | 34 | bool compareVal(val i1, val i2) 35 | { 36 | return (i1.first > i2.first); 37 | } 38 | 39 | int maxChainLen(struct val P[],int N) 40 | { 41 | int count=1,ele; 42 | sort(P,P+N,compareVal); 43 | ele = P[0].first; 44 | for(int i=1;i>T; 56 | while(T--){ 57 | int N; 58 | cin>>N; 59 | val P[N]; 60 | for(int i=0;i>P[i].first>>P[i].second; 62 | cout< 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int LCS(string X, string Y, int m, int n){ 18 | int DP[m+1][n+1]; 19 | int len=0; 20 | for(int i=0;i<=m;i++){ 21 | for(int j=0;j<=n;j++){ 22 | if(i==0 || j==0) 23 | DP[i][j] = 0; 24 | else if(X[i-1] == Y[j-1]) 25 | DP[i][j] = DP[i-1][j-1] + 1; 26 | else 27 | DP[i][j] = max(DP[i-1][j], DP[i][j-1]); 28 | } 29 | } 30 | return DP[m][n]; 31 | } 32 | 33 | int main() { 34 | int T; 35 | cin>>T; 36 | while(T--){ 37 | int n,m; 38 | string X,Y; 39 | cin>>X>>Y; 40 | m = X.length(); 41 | n = Y.length(); 42 | int lenOfLCS = LCS(X,Y,m,n); 43 | 44 | int lenOfSCS = (m + n) - lenOfLCS; 45 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | int T; 8 | cin>>T; 9 | while(T--){ 10 | int N,count=0; 11 | cin>>N; 12 | while(N > 2000){ 13 | cout<<2000<<" "; 14 | N = N - 2000; 15 | } 16 | while(N > 500){ 17 | cout<<500<<" "; 18 | N = N - 500; 19 | } 20 | while(N > 200){ 21 | cout<<200<<" "; 22 | N = N - 200; 23 | } 24 | while(N > 100){ 25 | cout<<100<<" "; 26 | N = N - 100; 27 | } 28 | while(N > 50){ 29 | cout<<50<<" "; 30 | N = N - 50; 31 | } 32 | while(N > 20){ 33 | cout<<20<<" "; 34 | N = N - 20; 35 | } 36 | while(N > 10){ 37 | cout<<10<<" "; 38 | N = N - 10; 39 | } 40 | while(N > 5){ 41 | cout<<5<<" "; 42 | N = N - 5; 43 | } 44 | while(N > 2){ 45 | cout<<2000<<" "; 46 | N = N - 2; 47 | } 48 | while(N > 1){ 49 | cout<<1<<" "; 50 | N = N - 1; 51 | } 52 | cout<<"\n"; 53 | } 54 | return 0; 55 | } -------------------------------------------------------------------------------- /GeeksForGeeks/DynamicProgramming/6.LongestCommonSubsequence.cpp: -------------------------------------------------------------------------------- 1 | // Longest Common Subsequence 2 | 3 | // Question: 4 | // Given two sequences, find the length of longest subsequence present in both of them. Both the strings are of 5 | // uppercase. 6 | 7 | // Input: 8 | // First line of the input contains no of test cases T,the T test cases follow. 9 | // Each test case consist of 2 space separated integers A and B denoting the size of string str1 and str2 respectively 10 | // The next two lines contains the 2 string str1 and str2 . 11 | 12 | // Output: 13 | // For each test case print the length of longest common subsequence of the two strings . 14 | 15 | // Example: 16 | // Input: 17 | // 2 18 | // 6 6 19 | // ABCDGH 20 | // AEDFHR 21 | // 3 2 22 | // ABC 23 | // AC 24 | 25 | // Output: 26 | // 3 27 | // 2 28 | 29 | // Solution: 30 | // LCS: 31 | // Create a DP table and initialize the length of longest substring(len) as 0 32 | // for(i=0 to m+1) 33 | // for(j=0 to n+1) 34 | // if(it is first row or first column) then DP[i][j] = 0 35 | // else if(left and top values are equal) then DP[i][j] = DP[i-1][j-1] + 1 36 | // else then DP[i][j] = max(DP[i-1][j], DP[i][j-1]) 37 | // return DP[m][n] 38 | 39 | #include 40 | #include 41 | #include 42 | using namespace std; 43 | 44 | int LCS(string X, string Y, int m, int n){ 45 | int DP[m+1][n+1]; 46 | int len=0; 47 | for(int i=0;i<=m;i++){ 48 | for(int j=0;j<=n;j++){ 49 | if(i==0 || j==0) 50 | DP[i][j] = 0; 51 | else if(X[i-1] == Y[j-1]) 52 | DP[i][j] = DP[i-1][j-1] + 1; 53 | else 54 | DP[i][j] = max(DP[i-1][j], DP[i][j-1]); 55 | } 56 | } 57 | return DP[m][n]; 58 | } 59 | 60 | int main() { 61 | int T; 62 | cin>>T; 63 | while(T--){ 64 | int N,M; 65 | cin>>N>>M; 66 | string X,Y; 67 | cin>>X>>Y; 68 | cout< 13 | #include //For memset function 14 | #include 15 | using namespace std; 16 | 17 | int DP[1001][1001]; 18 | 19 | int Knapsack(int val[],int wt[],int W,int N){ 20 | if(N==0 || W==0) 21 | return 0; 22 | if(DP[N][W] != -1) 23 | return DP[N][W]; 24 | if(wt[N-1] <= W){ 25 | DP[N][W] = max(val[N-1]+Knapsack(val,wt,W-wt[N-1],N-1), Knapsack(val,wt,W,N-1)); 26 | return DP[N][W]; 27 | } 28 | else{ 29 | DP[N][W] = Knapsack(val,wt,W,N-1); 30 | return DP[N][W]; 31 | } 32 | } 33 | 34 | int main() { 35 | int T; 36 | cin>>T; 37 | while(T--){ 38 | int N,W; 39 | cin>>N>>W; 40 | int val[N],wt[N]; 41 | for(int i=0;i>val[i]; 43 | for(int i=0;i>wt[i]; 45 | memset(DP,-1,sizeof(DP)); 46 | cout< len) then len = DP[i] 17 | // return len 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | int MSIS(int A[],int DP[], int n){ 26 | int len = A[0]; 27 | for(int i=1;i len) 35 | len = DP[i]; 36 | } 37 | return len; 38 | } 39 | 40 | int main() { 41 | int T; 42 | cin>>T; 43 | while(T--){ 44 | int N; 45 | cin>>N; 46 | int A[N],DP[N]; 47 | for(int i=0;i>A[i]; 49 | DP[i] = A[i]; 50 | } 51 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | struct Node 8 | { 9 | int data; 10 | Node* left; 11 | Node* right; 12 | }; 13 | 14 | Node* newNode(int val) 15 | { 16 | Node* temp = new Node; 17 | temp->data = val; 18 | temp->left = NULL; 19 | temp->right = NULL; 20 | 21 | return temp; 22 | } 23 | 24 | void PreOrder(Node* root){ 25 | if(root == NULL) 26 | return; 27 | cout<data<<" "; 28 | PreOrder(root->left); 29 | PreOrder(root->right); 30 | } 31 | 32 | Node* ArrayToBST(int A[], int L, int R){ 33 | if(L > R) 34 | return NULL; 35 | int mid = (L+R)/2; 36 | Node* root = newNode(A[mid]); 37 | cout<data<<" "; 38 | root->left = ArrayToBST(A,L,mid-1); 39 | root->left = ArrayToBST(A,mid+1,R); 40 | return root; 41 | } 42 | 43 | int main() { 44 | int t; 45 | cin>>t; 46 | while(t--) 47 | { 48 | int N; 49 | cin>>N; 50 | int A[N]; 51 | for(int i=0;i>A[i]; 53 | Node* root = ArrayToBST(A,0,N-1); 54 | // PreOrder(root); 55 | cout<<"\n"; 56 | } 57 | return 0; 58 | } -------------------------------------------------------------------------------- /GeeksForGeeks/General/BleakNumbers.cpp: -------------------------------------------------------------------------------- 1 | // Bleak Numbers 2 | 3 | // Question: 4 | // Given an integer, check whether it is Bleak or not. 5 | // A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, 6 | // i.e., x + countSetBits(x) is not equal to n for any non-negative number x. 7 | 8 | // Example 1: 9 | // Input: 4 10 | // Output: 1 11 | 12 | // Example 2: 13 | // Input: 3 14 | // Output: 0 15 | 16 | // Your Task: 17 | // You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input 18 | // parameter and returns 1 if n is not a Bleak number otherwise returns 0. 19 | 20 | #include 21 | #include 22 | using namespace std; 23 | 24 | class Solution 25 | { 26 | public: 27 | int is_bleak(int n) 28 | { 29 | int bleak = 1; 30 | for(int i=1; i num(i); 32 | if(i + num.count() == n){ 33 | bleak = 0; 34 | break; 35 | } 36 | } 37 | return bleak; 38 | } 39 | }; 40 | 41 | int main(){ 42 | int T; 43 | cin >> T; 44 | while(T--) 45 | { 46 | int n; 47 | cin >> n; 48 | Solution ob; 49 | int ans = ob.is_bleak(n); 50 | cout << ans << "\n"; 51 | } 52 | return 0; 53 | } -------------------------------------------------------------------------------- /GeeksForGeeks/General/ChangeAllEvenBitsInNumberTo0.cpp: -------------------------------------------------------------------------------- 1 | // Change all even bits in a number to 0 2 | 3 | // Question 4 | // Given a number, change all bits at even positions to 0. 5 | 6 | // Output: 7 | // Single line output, print the modified number. 8 | 9 | // Example: 10 | // Input: 11 | // 2 12 | // 30 13 | // 10 14 | // Output: 15 | // 10 16 | // 10 17 | 18 | #include 19 | #include 20 | using namespace std; 21 | 22 | int main() { 23 | int T; 24 | cin>>T; 25 | while(T--){ 26 | 27 | 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /GeeksForGeeks/General/CircularLinkedListTraversal.cpp: -------------------------------------------------------------------------------- 1 | // Circular Linked List Traversal 2 | 3 | #include 4 | #include 5 | 6 | struct Node 7 | { 8 | int data; 9 | struct Node *next; 10 | }; 11 | 12 | void push(struct Node **head_ref, int data) 13 | { 14 | struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node)); 15 | struct Node *temp = *head_ref; 16 | ptr1->data = data; 17 | ptr1->next = *head_ref; 18 | 19 | if (*head_ref != NULL) 20 | { 21 | while (temp->next != *head_ref) 22 | temp = temp->next; 23 | temp->next = ptr1; 24 | } 25 | else 26 | ptr1->next = ptr1; 27 | 28 | *head_ref = ptr1; 29 | } 30 | 31 | void printList(struct Node*); 32 | 33 | int main() 34 | { 35 | int i,n,t,val; 36 | scanf("%d",&t); 37 | 38 | while(t--) 39 | { 40 | struct Node *head = NULL; 41 | scanf("%d",&n); 42 | for(i=0;idata); 61 | struct Node* temp = head->next; 62 | while(temp != head ){ 63 | printf("%d ",temp->data); 64 | temp = temp->next; 65 | } 66 | return; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /GeeksForGeeks/General/CountPairsWithGivenSum.cpp: -------------------------------------------------------------------------------- 1 | // Count pairs with given sum 2 | 3 | // Question: 4 | // Given an array of integers, and an integer ‘K’ , find the count of pairs of elements in the array whose sum is 5 | // equal to 'K'. 6 | 7 | // Input: 8 | // First line of the input contains an integer T, denoting the number of test cases. Then T test cases follow. 9 | // Each test case consists of two lines. First line of each test case contains 2 space separated integers N and K 10 | // denoting the size of array and the sum respectively. Second line of each test case contains N space separated 11 | // integers denoting the elements of the array. 12 | 13 | // Output: 14 | // Print the count of pairs of elements in the array whose sum is equal to the K. 15 | 16 | // Example: 17 | // Input 18 | // 2 19 | // 4 6 20 | // 1 5 7 1 21 | // 4 2 22 | // 1 1 1 1 23 | // Output 24 | // 2 25 | // 6 26 | 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | int main() { 33 | int T; 34 | cin>>T; 35 | while(T--){ 36 | int N,K,count=0; 37 | cin>>N>>K; 38 | int A[N]; 39 | multiset s; 40 | for(int i=0;i>A[i]; 42 | } 43 | for(int i=0;i 3 | #include 4 | #define RANGE 255 5 | 6 | // The main function that sort the given string arr[] in 7 | // alphabatical order 8 | char* countSort(char arr[]); 9 | 10 | // Driver program to test above function 11 | int main() 12 | { 13 | long int t; 14 | scanf("%ld",&t); 15 | while(t--) 16 | { 17 | long int n; 18 | scanf("%ld",&n); 19 | char arr[n+1]; 20 | scanf("%s",arr); 21 | printf("%s\n", countSort(arr)); 22 | } 23 | return 0; 24 | } 25 | 26 | // The main function that sort the given string arr[] in 27 | // alphabatical order 28 | // return the sorted char array 29 | char* countSort(char A[]) 30 | { 31 | int count[26]={0}; 32 | int i,j,k=0,len=0; 33 | for(i=0;A[i]!='\0';i++){ 34 | len++; 35 | count[A[i] - 'a']++; 36 | } 37 | for(i=1;i<26;i++){ 38 | count[i] = count[i] + count[i-1]; 39 | } 40 | char* out = new char[len+1]; 41 | for(i=0;A[i]!='\0';i++){ 42 | count[A[i] - 'a']--; 43 | out[count[A[i] - 'a']] = char(A[i]); 44 | } 45 | return out; 46 | } -------------------------------------------------------------------------------- /GeeksForGeeks/General/DoesArrayRepresentHeap.cpp: -------------------------------------------------------------------------------- 1 | // Does Array Represent Heap 2 | 3 | // Question: 4 | // Given an array A of size N, the task is to check if the given array represents a Binary Max Heap. 5 | 6 | // Input: 7 | // The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. 8 | // Each test case contains two lines. The first line of input contains an integer N denoting the size of the array. 9 | // Then in the next line are N space separated array elements. 10 | 11 | // Output: 12 | // If array represents a Binary Max Heap, print "1", else print "0" (without quotes). 13 | 14 | // Example: 15 | // Input: 16 | // 2 17 | // 6 18 | // 90 15 10 7 12 2 19 | // 6 20 | // 9 15 10 7 12 11 21 | 22 | // Output: 23 | // 1 24 | // 0 25 | 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | int N, flag=1; 35 | cin>>N; 36 | int A[N]; 37 | for(int i=1;i<=N;i++) 38 | cin>>A[i]; 39 | for(int i=(N/2); i>=1; i--){ 40 | int j = 2*i; 41 | if(j < N && A[j] < A[j+1]) 42 | j = j+1; 43 | if(A[i] < A[j]){ 44 | flag = 0; 45 | break; 46 | } 47 | } 48 | if(flag == 1) 49 | cout<<1<<"\n"; 50 | else 51 | cout<<0<<"\n"; 52 | } 53 | return 0; 54 | } -------------------------------------------------------------------------------- /GeeksForGeeks/General/EulerTotientFunction.cpp: -------------------------------------------------------------------------------- 1 | // Bit Difference 2 | 3 | // Question 4 | // You are given two numbers A and B. Write a program to count number of bits needed to be 5 | // Euler Totient Function 6 | 7 | // Question: 8 | // Euler Totient Function (ETF) Φ(n) for an input n is count of numbers in {1, 2, 3, …, n} that are relatively 9 | // prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. 10 | 11 | // Input: 12 | // The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. 13 | // Each test case contains an integer n. 14 | 15 | // Output: 16 | // Print the Euler Totient Function Φ(n) for an input n. 17 | 18 | // Example: 19 | // Input: 20 | // 2 21 | // 11 22 | // 16 23 | 24 | // Output: 25 | // 10 26 | // 8 27 | 28 | // Solution: 29 | // Φ(n) = n-1 if n is prime 30 | // if n = p1^a1 * p2^a2 * ... pk^ak where p1,p2,... are prime factors of n then 31 | // Φ(n) = n * (p1-1)/p1 * (p2-1)/p2 * .... (pk-1)/pk 32 | 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | int main() { 38 | int T; 39 | cin>>T; 40 | while(T--){ 41 | int N,totient; 42 | cin>>N; 43 | totient = N; 44 | for(int i=2;i*i <= N; i++){ 45 | if(N % i == 0){ 46 | totient = totient/i; 47 | totient = totient * (i-1); 48 | 49 | while(N % i == 0) // to completely remove p1 which is a1 times 50 | N = N / i; 51 | } 52 | } 53 | if(N > 1){ 54 | totient = totient/N; 55 | totient = totient * (N-1); 56 | } 57 | cout< 23 | #include 24 | using namespace std; 25 | 26 | int main() { 27 | int T; 28 | cin>>T; 29 | while(T--){ 30 | int N,M; 31 | cin>>N>>M; 32 | long int DP[N][M]; 33 | for(int i=0;i 23 | #include 24 | using namespace std; 25 | 26 | int main() { 27 | int T; 28 | cin>>T; 29 | while(T--){ 30 | long int N,bit,out=0; 31 | cin>>N; 32 | for(int i=0;i<32;i++){ 33 | if(N & 1) 34 | bit = 1; 35 | else 36 | bit = 0; 37 | N = N >> 1; 38 | out = out << 1; 39 | out = out | bit; 40 | } 41 | 42 | cout< 23 | using namespace std; 24 | 25 | int main() { 26 | int T; 27 | cin>>T; 28 | while(T--){ 29 | int N,count=0; 30 | cin>>N; 31 | while(N > 0){ 32 | if(N & 1) 33 | count++; 34 | N = N >> 1; 35 | } 36 | 37 | cout< 27 | #include 28 | using namespace std; 29 | 30 | int main() { 31 | int T; 32 | cin>>T; 33 | while(T--){ 34 | int N; 35 | cin>>N; 36 | int edges=0; 37 | while(N > 1){ 38 | if(N % 3 == 0) 39 | N = N/3; 40 | else 41 | N = N -1; 42 | edges++; 43 | } 44 | cout< 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int T; 9 | cin>>T; 10 | while(T--){ 11 | int N,squares; 12 | cin>>N; 13 | squares = (N * (N+1) * (2*N + 1))/6; 14 | cout< 3 | using namespace std; 4 | 5 | int main() { 6 | int T; 7 | cin >> T; 8 | while(T--){ 9 | int N; 10 | cin >> N; 11 | int A[N]; 12 | for(int i = 0 ; i < N ; i++) 13 | cin >> A[i]; 14 | int DP[N]; 15 | DP[0] = A[0]; 16 | DP[1] = max(A[0],A[1]); 17 | for(int i=2;i 24 | #include 25 | using namespace std; 26 | 27 | int main() { 28 | int T; 29 | cin>>T; 30 | while(T--){ 31 | int X,Y,leftN,rightN; 32 | cin>>X; 33 | leftN = X; 34 | rightN = X; 35 | 36 | leftN = leftN & 240; 37 | rightN = rightN & 15; 38 | leftN = leftN >> 4; 39 | rightN = rightN << 4; 40 | 41 | 42 | Y = leftN | rightN; 43 | 44 | cout< 24 | #include 25 | #include 26 | using namespace std; 27 | 28 | int main() { 29 | int T; 30 | cin>>T; 31 | while(T--){ 32 | int X,Y; 33 | cin>>X; 34 | bitset<8> bset(X); 35 | for(int i=0;i<8;i=i+2){ 36 | bool temp = bset[i]; 37 | bset[i] = bset[i+1]; 38 | bset[i+1] = temp; 39 | } 40 | cout< K) then break out of the loop. 21 | // increase cost as cost + A[i] and increment count. 22 | // print count. 23 | 24 | #include 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | int main() { 30 | int T; 31 | cin>>T; 32 | while(T--){ 33 | int N,K,cost=0,count=0; 34 | cin>>N>>K; 35 | int A[N]; 36 | for(int i=0;i>A[i]; 38 | sort(A,A+N); 39 | 40 | for(int i=0;i K) 42 | break; 43 | cost = cost + A[i]; 44 | count++; 45 | } 46 | cout< N*9 or (S is 0 and N > 1)) then print -1. 21 | // else 22 | // while(S >= 0 && N > 0): if(S/10 > 0) then print 9 and subtract 9 from S. 23 | // else print (S % 10) and subtract (S % 10) from S. 24 | // decrement N; 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | int main() { 33 | int T; 34 | cin>>T; 35 | while(T--){ 36 | int N,S; 37 | cin>>N>>S; 38 | if(S > N*9 || (S == 0 && N > 1)) 39 | cout<<-1; 40 | else{ 41 | while(S >= 0 && N > 0){ 42 | if(S/10 > 0){ 43 | cout<<9; 44 | S = S - 9; 45 | } 46 | else{ 47 | cout< 33 | #include 34 | #include 35 | using namespace std; 36 | 37 | int main() { 38 | int T; 39 | cin>>T; 40 | while(T--){ 41 | int N,K; 42 | cin>>K; 43 | cin>>N; 44 | int A[N]; 45 | for(int i=0;i>A[i]; 47 | sort(A,A+N); 48 | if(A[N-1] - A[0] < K) 49 | cout< avg) 56 | A[i] = A[i] - K; 57 | } 58 | sort(A,A+N); 59 | cout< 31 | #include 32 | #include 33 | using namespace std; 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N; 40 | long long int sum=0; 41 | cin>>N; 42 | int A[N],B[N]; 43 | for(int i=0;i>A[i]; 45 | for(int i=0;i>B[i]; 47 | sort(A,A+N); 48 | sort(B,B+N); 49 | for(int i=0,j=N-1;ifirst to A3. 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | int N,M,k=0; 18 | cin>>N>>M; 19 | int A1[N], A2[M], A3[N]; 20 | map count; 21 | map :: iterator it; 22 | for(int i=0;i>A1[i]; 24 | if(count.find(A1[i]) != count.end()) 25 | count[A1[i]] = count[A1[i]] + 1; 26 | else 27 | count.insert(make_pair(A1[i], 1)); 28 | } 29 | for(int i=0;i>A2[i]; 31 | if(count.find(A2[i]) != count.end()){ 32 | int len = count[A2[i]]; 33 | for(int j=0;jsecond > 1){ 40 | for(int j=0;jsecond;j++) 41 | A3[k++] = it->first; 42 | } 43 | else 44 | A3[k++] = it->first; 45 | } 46 | 47 | for(int i=0;i 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | int M,N,flag = 0; 17 | map mp; 18 | cin>>M>>N; 19 | int A[M],B[N]; 20 | for(int i=0;i>A[i]; 22 | mp.insert(make_pair(A[i], 1)); 23 | } 24 | for(int i=0;i>B[i]; 26 | 27 | for(int i=0;i 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main(){ 12 | int t; 13 | cin>>t; 14 | while(t--){ 15 | int N,M,X,flag=0; 16 | map mp; 17 | cin>>N>>M>>X; 18 | int A[N],B[M]; 19 | for(int i=0;i>A[i]; 21 | 22 | for(int i=0;i>B[i]; 24 | mp.insert(make_pair(B[i], 1)); 25 | } 26 | sort(A,A+N); 27 | for(int i=0;i 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | string str; 17 | map mp; 18 | cin>>str; 19 | int len = str.length(), flag = 0; 20 | for(int i=0;i 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | string str,patt; 17 | map mp; 18 | cin>>str>>patt; 19 | int len1 = str.length(), len2 = patt.length(),flag = 0; 20 | for(int i=0;i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | int t; 11 | cin>>t; 12 | while(t--){ 13 | int N,X; 14 | cin>>N; 15 | vector A,B; 16 | for(int i=0;i>X; 18 | A.push_back(X); 19 | } 20 | for(int i=0;i>X; 22 | B.push_back(X); 23 | } 24 | sort(A.begin(),A.end()); 25 | sort(B.begin(),B.end()); 26 | if(A == B) 27 | cout<<1<<"\n"; 28 | else 29 | cout<<0<<"\n"; 30 | } 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /GeeksForGeeks/Hashing/16.UncommonCharacters.cpp: -------------------------------------------------------------------------------- 1 | // Uncommon characters 2 | 3 | // Create a map mp2 to store common characters of both the strings. 4 | // Now create another map to store the uncommon character from both strings which are not present in mp2. 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | while(t--){ 16 | string str1,str2; 17 | map mp1,mp2,mp3; 18 | map :: iterator it; 19 | cin>>str1>>str2; 20 | int len1 = str1.length(), len2 = str2.length(),flag = 0; 21 | for(int i=0;ifirst) == mp2.end()) 32 | mp3.insert(make_pair(it->first, 1)); 33 | } 34 | 35 | if(!mp3.empty()){ 36 | for(it = mp3.begin(); it != mp3.end(); it++) 37 | cout<first; 38 | cout<<"\n"; 39 | } 40 | } 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /GeeksForGeeks/Hashing/18.FirstElementToOccurKTimes.cpp: -------------------------------------------------------------------------------- 1 | // First element to occur k times 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main(){ 10 | int t; 11 | cin>>t; 12 | while(t--){ 13 | int N,K,flag=0; 14 | map mp; 15 | cin>>N>>K; 16 | int A[N]; 17 | for(int i=0;i>A[i]; 19 | 20 | for(int i=0;i1)) then print 0. 10 | // else print 1. 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | int main(){ 20 | int t; 21 | cin>>t; 22 | while(t--){ 23 | string str; 24 | int ch[150] = {0}; 25 | cin>>str; 26 | int len = str.length(),min = INT_MAX, max = 0,flag=0; 27 | for(int i=0;i vec; 31 | int a,b; 32 | for(int i=97;i<123;i++){ 33 | if(ch[i] != 0){ 34 | vec.push_back(ch[i]); 35 | } 36 | sort(vec.begin(), vec.end()); 37 | int n = vec.size(),start = vec[0],val =vec[0]; 38 | for(int i=1;i1)) 52 | cout<<0< 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | int N,k=0; 18 | cin>>N; 19 | int A1[N], A2[N]; 20 | map count; 21 | multimap > mp; 22 | map :: iterator it; 23 | multimap :: iterator it1; 24 | for(int i=0;i>A1[i]; 26 | if(count.find(A1[i]) != count.end()) 27 | count[A1[i]] = count[A1[i]] + 1; 28 | else 29 | count.insert(make_pair(A1[i], 1)); 30 | } 31 | 32 | for(it = count.begin();it != count.end(); it++){ 33 | mp.insert(make_pair(it->second,it->first)); 34 | } 35 | for(it1 = mp.begin();it1 != mp.end(); it1++){ 36 | for(int i=0;ifirst;i++) 37 | A2[k++] = it1->second; 38 | } 39 | 40 | for(int i=0;i count ) then count = len. 9 | // if(sum == 0) then return n. 10 | // finally return count. 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int maxLen(int A[], int n) { 19 | map mp; 20 | int sum=0,count=0,len=0; 21 | for(int i=0;i count) 33 | count = len; 34 | } 35 | if(sum == 0) 36 | return n; 37 | return count; 38 | } 39 | 40 | 41 | int main() { 42 | int T; 43 | cin>>T; 44 | while(T--){ 45 | int N; 46 | cin>>N; 47 | int A[N]; 48 | for(int i=0;i>A[i]; 50 | cout< 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | int T; 16 | cin>>T; 17 | while(T--){ 18 | map mp1,mp2,mp3; 19 | map :: iterator it; 20 | int N1,N2,N3; 21 | cin>>N1>>N2>>N3; 22 | int A[N1],B[N2],C[N3]; 23 | for(int i=0;i>A[i]; 25 | if(mp1.find(A[i]) == mp1.end()) 26 | mp1.insert(make_pair(A[i],1)); 27 | } 28 | for(int i=0;i>B[i]; 30 | if(mp1.find(B[i]) != mp1.end()) 31 | mp2.insert(make_pair(B[i],1)); 32 | } 33 | for(int i=0;i>C[i]; 35 | if(mp2.find(C[i]) != mp2.end() && mp3.find(C[i]) == mp3.end()) 36 | mp3.insert(make_pair(C[i],1)); 37 | } 38 | if(mp3.size()==0) 39 | cout<<-1; 40 | else{ 41 | for(it=mp3.begin(); it!=mp3.end(); it++) 42 | cout<first<<" "; 43 | } 44 | cout< 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main(){ 15 | int t; 16 | cin>>t; 17 | while(t--){ 18 | int N, K, c=0; 19 | cin>>N>>K; 20 | int a[N]; 21 | for(int i=0;i>a[i]; 22 | sort(a, a+N); 23 | map m; 24 | string str=""; 25 | for(int i=0;iK) 40 | r--; 41 | else 42 | l++; 43 | } 44 | } 45 | } 46 | if(c==0) cout<<"-1"; 47 | cout< 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | vector countDistinct(int A[], int n, int k){ 18 | map mp; 19 | map :: iterator it; 20 | vector result; 21 | int head = 0,size; 22 | for(int i=0;i>t; 43 | while(t--){ 44 | int N, K; 45 | cin>>N>>K; 46 | int A[N]; 47 | for(int i=0;i>A[i]; 49 | vector result = countDistinct(A,N,K); 50 | for(int i : result) 51 | cout< 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main(){ 14 | int t; 15 | cin>>t; 16 | while(t--){ 17 | int N, K,sum=0; 18 | cin>>N; 19 | int A[N]; 20 | for(int i=0;i>A[i]; 22 | sum += A[i]; 23 | } 24 | cin>>K; 25 | if(N % 2 == 1) 26 | cout<<"False"< max) then max = count. 7 | // print max. 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | int main(){ 15 | int t; 16 | cin>>t; 17 | while(t--){ 18 | int N,count=1,max=1; 19 | cin>>N; 20 | int A[N]; 21 | for(int i=0;i>A[i]; 23 | sort(A,A+N); 24 | for(int i=1;i max) 32 | max = count; 33 | } 34 | cout< arr[i]) then largest = l. 10 | // if (r < n && arr[r] > arr[largest]) then largest = r. 11 | // if(largest != i) then swap(arr[i], arr[largest]) and call heapify(arr, n, largest). 12 | 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | void heapify(int arr[], int n, int i) { 18 | int l = 2*i + 1; 19 | int r = 2*i + 2; 20 | int largest = i; 21 | if (l < n && arr[l] > arr[i]) 22 | largest = l; 23 | if (r < n && arr[r] > arr[largest]) 24 | largest = r; 25 | if (largest != i) { 26 | swap(arr[i], arr[largest]); 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | 31 | void buildHeap(int arr[], int n) { 32 | for(int i=((n/2)-1);i>=0;i--) 33 | heapify(arr,n,i); 34 | } 35 | 36 | void heapSort(int arr[], int n) { 37 | buildHeap(arr, n); 38 | for (int i=n-1; i>=0; i--) { 39 | swap(arr[0], arr[i]); 40 | heapify(arr, i, 0); 41 | } 42 | } 43 | 44 | void printArray(int arr[], int size){ 45 | for(int i=0;i>T; 53 | while(T--){ 54 | cin>>n; 55 | for(int i=0;i>arr[i]; 57 | heapSort(arr, n); 58 | printArray(arr, n); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /GeeksForGeeks/Heap/4.RearrangeCharacters.cpp: -------------------------------------------------------------------------------- 1 | // Rearrange characters 2 | 3 | // create a count array to store frequency count of characters( size is 125 because it only contains lower case characters). 4 | // input string and store string length in len and initialize max as 0. 5 | // for( i=0 to len): incremrnt count[str[i]] 6 | // if(count[str[i]] > max) then max = count[str[i]] 7 | // if( (len is even and max <= len/2) or (len is odd and max <= (len+1)/2)) 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main() { 14 | int T; 15 | cin>>T; 16 | while(T--){ 17 | char count[125] = {0}; 18 | string str; 19 | cin>>str; 20 | int len = str.length(),max=0; 21 | for(int i=0;i max) 24 | max = count[str[i]]; 25 | } 26 | if((len%2 == 0 && max <= len/2) || (len%2 == 1 && max <= (len+1)/2)) 27 | cout<<1<2->3->4->5->6 9 | // Output: 6 5 4 3 2 1 10 | // Explanation: After reversing the list, 11 | // elements are 6->5->4->3->2->1. 12 | 13 | // Your Task: 14 | // The task is to complete the function reverseList() with head reference as the only argument and should 15 | // return new head after reversing the list. 16 | 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | struct Node{ 22 | int data; 23 | struct Node* next; 24 | 25 | Node(int x){ 26 | data = x; 27 | next = NULL; 28 | } 29 | }; 30 | 31 | void Print(struct Node** head){ 32 | struct Node* temp = *head; 33 | while(temp != NULL){ 34 | cout<data<<" "; 35 | temp = temp->next; 36 | } 37 | cout<next != NULL) 47 | temp = temp->next; 48 | temp->next = newNode; 49 | } 50 | } 51 | 52 | struct Node* reverseList(struct Node* head){ 53 | struct Node *cur, *prev, *next; 54 | cur = head; 55 | prev = NULL; 56 | while(cur != NULL){ 57 | next = cur->next; 58 | cur->next = prev; 59 | prev = cur; 60 | cur = next; 61 | } 62 | head = prev; 63 | return head; 64 | } 65 | 66 | int main() { 67 | int T; 68 | cin>>T; 69 | while(T--){ 70 | struct Node *head = NULL; 71 | int N,l; 72 | 73 | cin>>N; 74 | for(int i=0;i>l; 76 | Insert(&head,l); 77 | } 78 | head = reverseList(head); 79 | Print(&head); 80 | 81 | } 82 | return 0; 83 | 84 | } -------------------------------------------------------------------------------- /GeeksForGeeks/Recursion/2.NumberOfPaths.cpp: -------------------------------------------------------------------------------- 1 | // Number of paths(using DP) 2 | 3 | // Question: 4 | // The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints 5 | // that from each cell you can either move to right or down. 6 | 7 | // Input: 8 | // The first line of input contains an integer T, denoting the number of test cases. The first line of each test case 9 | // is M and N, M is number of rows and N is number of columns. 10 | 11 | // Output: 12 | // For each test case, print the number of paths. 13 | 14 | // Example: 15 | // Input 16 | // 2 17 | // 3 3 18 | // 2 8 19 | 20 | // Output 21 | // 6 22 | // 8 23 | 24 | #include 25 | using namespace std; 26 | 27 | int numberOfPaths(int m, int n){ 28 | if(m==1 || n==1) 29 | return 1; 30 | return numberOfPaths(m-1,n) + numberOfPaths(m,n-1); 31 | } 32 | 33 | int main() { 34 | int T; 35 | cin>>T; 36 | while(T--){ 37 | int M,N,out; 38 | cin>>M>>N; 39 | cout< 26 | #include 27 | using namespace std; 28 | 29 | int keyboard(int n){ 30 | if(n < 5) 31 | return n; 32 | return max(4*keyboard(n-5), 3*keyboard(n-4)); 33 | } 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N; 40 | cin>>N; 41 | cout< 27 | #include 28 | #include 29 | using namespace std; 30 | 31 | int josephus(int n, int k) 32 | { 33 | if(n == 1) 34 | return n; 35 | return (josephus(n-1,k) + (k-1))%n +1; 36 | } 37 | 38 | int main() { 39 | int T; 40 | cin>>T; 41 | while(T--){ 42 | int N,K,l; 43 | cin>>N>>K; 44 | cout< 28 | #include 29 | using namespace std; 30 | 31 | string reverseWords(string s); 32 | int main() 33 | { 34 | int t; 35 | cin >> t; 36 | while (t--) 37 | { 38 | string s; 39 | cin >> s; 40 | cout<=0;i--){ 51 | if(S1[i] == '.'){ 52 | r = len - i; 53 | len = i -1; 54 | S2.append(S1.substr(i+1,r)); 55 | S2.append("."); 56 | } 57 | } 58 | S2.append(S1.substr(0,len+1)); 59 | return S2; 60 | } -------------------------------------------------------------------------------- /GeeksForGeeks/String/11.ImplementAtoi.cpp: -------------------------------------------------------------------------------- 1 | // Implement Atoi 2 | 3 | // Question: 4 | // Your task is to implement the function atoi. The function takes a string(str) as argument and converts it to 5 | // an integer and returns it. 6 | 7 | // Example 1: 8 | // Input: 9 | // str = 123 10 | // Output: 123 11 | 12 | // Example 2: 13 | // Input: 14 | // str = 21a 15 | // Output: -1 16 | 17 | // Your Task: 18 | // Complete the function atoi() which takes a string as input parameter and returns integer value of it. if the 19 | // input string is not a numerical string then returns 1.. 20 | 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | class Solution{ 26 | public: 27 | int atoi(string S) 28 | { 29 | int sign=0,len = S.length(); 30 | long int out; 31 | for(int i=0;i= '0' && S[i] <= '9'){ 37 | if(i==0) 38 | out = S[i] - 48; 39 | else{ 40 | out = out*10; 41 | out = out + (S[i] - 48); 42 | } 43 | } 44 | else{ 45 | return -1; 46 | } 47 | } 48 | if(sign == 1) 49 | out = 0 - out; 50 | return out; 51 | } 52 | }; 53 | 54 | // { Driver Code Starts. 55 | int main() 56 | { 57 | int t; 58 | cin>>t; 59 | while(t--) 60 | { 61 | string s; 62 | cin>>s; 63 | Solution ob; 64 | cout< 24 | #include 25 | using namespace std; 26 | 27 | int strstr(string ,string); 28 | 29 | int main() 30 | { 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | string a; 36 | string b; 37 | 38 | cin>>a; 39 | cin>>b; 40 | 41 | cout< 27 | #include 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | vector vec; 33 | 34 | void PermuteString(string str,int i,int len){ 35 | if(i>=len) 36 | return; 37 | for(int j=i;j>T; 50 | while(T--){ 51 | string str1; 52 | 53 | vec.clear(); 54 | cin>>str1; 55 | int len = str1.length(); 56 | 57 | PermuteString(str1,0,len); 58 | 59 | sort(vec.begin(),vec.end()); 60 | for(int i=0;i 31 | #include 32 | using namespace std; 33 | 34 | string RemoveAdjacent(string S){ 35 | string S2; 36 | int len = S.length(),l=0,r,flag,last=0; 37 | 38 | for(int i=0;i>T; 67 | while(T--){ 68 | string S,S2; 69 | cin>>S; 70 | 71 | S2 = RemoveAdjacent(S); 72 | 73 | cout< 28 | #include 29 | using namespace std; 30 | 31 | bool isRotated(string S1, string S2) 32 | { 33 | string temp1,temp2; 34 | int len1 = S1.length(),len2 = S2.length(); 35 | if(len1 < 2 || len2 < 2) 36 | if(S1 == S2) 37 | return true; 38 | else 39 | return false; 40 | temp1.append(S1.substr(2,len1-2)); 41 | temp1.append(S1.substr(0,2)); 42 | 43 | temp2.append(S1.substr(len1-2,2)); 44 | temp2.append(S1.substr(0,len1-2)); 45 | if(temp1 == S2 || temp2 == S2) 46 | return true; 47 | else 48 | return false; 49 | } 50 | 51 | 52 | 53 | // { Driver Code Starts. 54 | 55 | int main() { 56 | 57 | int t; 58 | cin>>t; 59 | while(t--) 60 | { 61 | string s; 62 | string b; 63 | cin>>s>>b; 64 | cout< 26 | #include 27 | #include 28 | using namespace std; 29 | 30 | bool isAnagram(string S1, string S2){ 31 | int A[26] = {0}; 32 | int B[26] = {0}; 33 | int len1 = S1.length(); 34 | int len2 = S2.length(); 35 | int flag=0; 36 | if(len1 != len2) 37 | return false; 38 | for(int i=0;i> t; 62 | 63 | while(t--){ 64 | string c, d; 65 | 66 | cin >> c >> d; 67 | 68 | if(isAnagram(c, d)) cout << "YES" << endl; 69 | else cout << "NO" << endl; 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /GeeksForGeeks/String/8-RemoveDuplicates.cpp: -------------------------------------------------------------------------------- 1 | // Remove Duplicates 2 | 3 | // Question: 4 | // Given a string, the task is to remove duplicates from it. Expected time complexity O(n) where n is length of 5 | // input string and extra space O(1) under the assumption that there are total 256 possible characters in a string. 6 | // Note: that original order of characters must be kept same. 7 | 8 | // Input: 9 | // First line of the input is the number of test cases T. And first line of test case contains a string. 10 | 11 | // Output: 12 | // Modified string without duplicates and same order of characters. 13 | 14 | // Example: 15 | // Input: 16 | // 2 17 | // geeksforgeeks 18 | // geeks for geeks 19 | 20 | // Output: 21 | // geksfor 22 | // geks for 23 | 24 | // Solution: 25 | // create a char array as count[256] ={0}, input string and store string length in len. 26 | // for( i=0 to len): if count[character] is zero increase count and print that character. 27 | 28 | #include 29 | #include 30 | using namespace std; 31 | 32 | class Solution{ 33 | public: 34 | 35 | string removeDups(string S) 36 | { 37 | string out = ""; 38 | int count[26] = {0}; 39 | int len = S.length(); 40 | for(int i=0;i= 'a' && S[i] <= 'z') 42 | if(count[S[i] - 'a']++ == 0) 43 | out = out + S[i]; 44 | else if(S[i] >= 'A' && S[i] <= 'Z') 45 | if(count[S[i] - 'A']++ == 0) 46 | out = out + S[i]; 47 | } 48 | return out; 49 | } 50 | }; 51 | 52 | 53 | // { Driver Code Starts. 54 | 55 | 56 | int main() 57 | { 58 | 59 | 60 | ios_base::sync_with_stdio(0); 61 | cin.tie(NULL); 62 | cout.tie(NULL); 63 | 64 | int t; 65 | cin >> t; 66 | while(t--) 67 | { 68 | string s; 69 | cin >> s; 70 | 71 | 72 | Solution ob; 73 | cout << ob.removeDups(s) << "\n"; 74 | 75 | } 76 | 77 | return 0; 78 | } -------------------------------------------------------------------------------- /GeeksForGeeks/String/9-FormAPalindrome.cpp: -------------------------------------------------------------------------------- 1 | // Form a palindrome 2 | 3 | // Question: 4 | // Given a string, find the minimum number of characters to be inserted to convert it to palindrome. 5 | // For Example: 6 | // ab: Number of insertions required is 1. bab or aba 7 | // aa: Number of insertions required is 0. aa 8 | // abcd: Number of insertions required is 3. dcbabcd 9 | 10 | // Input: 11 | // The first line of input contains an integer T denoting the number of test cases. 12 | // The first line of each test case is S. 13 | 14 | // Output: 15 | // Print the minimum number of characters. 16 | 17 | // Example: 18 | 19 | // Input: 20 | // 3 21 | // abcd 22 | // aba 23 | // geeks 24 | 25 | // Output: 26 | // 3 27 | // 0 28 | // 3 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | using namespace std; 35 | 36 | int countMin(string str); 37 | 38 | int main(){ 39 | int t; 40 | cin >> t; 41 | while(t--){ 42 | string str; 43 | cin >> str; 44 | cout << countMin(str) << endl; 45 | } 46 | return 0; 47 | } 48 | 49 | // } Driver Code Ends 50 | 51 | int LCS(string S1, string S2, int n){ 52 | int DP[n+1][n+1]; 53 | for(int i=0;i<=n;i++){ 54 | for(int j=0;j<=n;j++){ 55 | if(i==0 || j==0) 56 | DP[i][j] = 0; 57 | else if(S1[i-1] == S2[j-1]) 58 | DP[i][j] = 1 + DP[i-1][j-1]; 59 | else 60 | DP[i][j] = max(DP[i-1][j],DP[i][j-1]); 61 | } 62 | } 63 | return DP[n][n]; 64 | } 65 | 66 | int countMin(string S1){ 67 | string S2 = S1; 68 | int n = S1.length(); 69 | reverse(S2.begin(), S2.end()); 70 | int len = LCS(S1,S2,n); 71 | int insertions = n - len; 72 | return insertions; 73 | } -------------------------------------------------------------------------------- /Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krishna-gavas/Competitive-Coding/2b2dcc75013a4f31f7ed5ae32bb02b7f58e43083/Guide-to-Competitive-Programming-Learning-and-improving-Algorithms-through-Contests.pdf -------------------------------------------------------------------------------- /HackerRank/CountingValleys.cpp: -------------------------------------------------------------------------------- 1 | int countingValleys(int n, string s) { 2 | int val = 0; 3 | stack stk; 4 | for(int i=0;i c) { 2 | int count=0; 3 | for(int i=0;i ar) { 2 | set mySet; 3 | int count =0; 4 | for(int i=0;i twoSum(vector& nums, int target) { 4 | vector ret; 5 | vector :: iterator it; 6 | for(int i=0; i< nums.size(); i++){ 7 | int ele = target - nums[i]; 8 | it = find(nums.begin(), nums.end(), ele); 9 | if(it != nums.end()){ // found 10 | if((it - nums.begin()) == i) 11 | continue; 12 | ret.push_back(i); 13 | ret.push_back(it - nums.begin()); 14 | return ret; 15 | } 16 | } 17 | return ret; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/11.ContainerWithMostWater.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int water = 0, left = 0, right = height.size() - 1; 5 | while( left < right){ 6 | int area; 7 | if(height[left] < height[right]){ 8 | area = height[left] * (right - left); 9 | left++; 10 | } 11 | else{ 12 | area = height[right] * (right - left); 13 | right--; 14 | } 15 | if(area > water) 16 | water = area; 17 | } 18 | return water; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/12.IntegerToRoman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | string roman = ""; 5 | while(num >= 1000){ 6 | roman = roman + "M"; 7 | num = num - 1000; 8 | } 9 | if(num >= 900){ 10 | roman = roman + "CM"; 11 | num = num - 900; 12 | } 13 | 14 | while(num >= 500){ 15 | roman = roman + "D"; 16 | num = num - 500; 17 | } 18 | if(num >= 400){ 19 | roman = roman + "CD"; 20 | num = num - 400; 21 | } 22 | 23 | while(num >= 100){ 24 | roman = roman + "C"; 25 | num = num - 100; 26 | } 27 | if(num >= 90){ 28 | roman = roman + "XC"; 29 | num = num - 90; 30 | } 31 | 32 | while(num >= 50){ 33 | roman = roman + "L"; 34 | num = num - 50; 35 | } 36 | if(num >= 40){ 37 | roman = roman + "XL"; 38 | num = num - 40; 39 | } 40 | 41 | while(num >= 10){ 42 | roman = roman + "X"; 43 | num = num - 10; 44 | } 45 | if(num == 9){ 46 | roman = roman + "IX"; 47 | num = num - 9; 48 | } 49 | 50 | while(num >= 5){ 51 | roman = roman + "V"; 52 | num = num - 5; 53 | } 54 | if(num == 4){ 55 | roman = roman + "IV"; 56 | num = num - 4; 57 | } 58 | 59 | while(num >= 1){ 60 | roman = roman + "I"; 61 | num = num - 1; 62 | } 63 | 64 | return roman; 65 | } 66 | }; -------------------------------------------------------------------------------- /LeetCode/13.RomanToInteger.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string str1) { 4 | map roman; 5 | roman.insert(pair('I', 1)); 6 | roman.insert(pair('V', 5)); 7 | roman.insert(pair('X', 10)); 8 | roman.insert(pair('L', 50)); 9 | roman.insert(pair('C', 100)); 10 | roman.insert(pair('D', 500)); 11 | roman.insert(pair('M', 1000)); 12 | 13 | int len = str1.length(),num,sum=0; 14 | 15 | for(int i=0;i& strs) { 4 | int count=0,min=1000; 5 | string str; 6 | for(int i=0; i< strs.size(); i++){ 7 | int len = strs[i].length(); 8 | if(len < min){ 9 | min = len; 10 | str = strs[i]; 11 | } 12 | } 13 | 14 | for(int i=0; i> threeSum(vector& nums) { 4 | vector > result; 5 | int sum, n = nums.size(); 6 | sort(nums.begin(), nums.end()); 7 | for(int i=0; i& nums, int target) { 4 | int out,sum, n = nums.size(),min = INT_MAX; 5 | sort(nums.begin(), nums.end()); 6 | for(int i=0;i &res, string nums[], int i, string s){ 4 | if(i == digits.length()){ 5 | res.push_back(s); 6 | return; 7 | } 8 | string val = nums[digits[i] - '0']; 9 | for(int j=0;j letterCombinations(string digits) { 14 | vector res; 15 | if(digits.length() == 0) 16 | return res; 17 | string nums[10] = { 18 | "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" 19 | }; 20 | myLetterComb(digits, res, nums, 0, ""); 21 | return res; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/20.ValidParentheses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string str) { 4 | stack s; 5 | int len = str.length(); 6 | 7 | for(int i=0;ival < l2->val){ 24 | if(flag == 0){ 25 | head = l1; 26 | temp = head; 27 | } 28 | else{ 29 | temp->next = l1; 30 | temp = temp->next; 31 | } 32 | l1 = l1->next; 33 | } 34 | else{ 35 | if(flag == 0){ 36 | head = l2; 37 | temp = head; 38 | } 39 | else{ 40 | temp->next = l2; 41 | temp = temp->next; 42 | } 43 | l2 = l2->next; 44 | } 45 | flag = 1; 46 | } 47 | while(l1 != NULL){ 48 | temp->next = l1; 49 | temp = temp->next; 50 | l1 = l1->next; 51 | } 52 | while(l2 != NULL){ 53 | temp->next = l2; 54 | temp = temp->next; 55 | l2 = l2->next; 56 | } 57 | return head; 58 | } 59 | }; -------------------------------------------------------------------------------- /LeetCode/26.RemoveDuplicatesFromSortedArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | vector original; 5 | vector :: iterator it; 6 | for(it = nums.begin(); it != nums.end(); it++){ 7 | int ele = *it; 8 | if(find(original.begin(), original.end(), ele) != original.end()){ 9 | nums.erase(it); 10 | it--; 11 | } 12 | else{ 13 | original.push_back(*it); 14 | } 15 | 16 | } 17 | return nums.size(); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/27.RemoveElement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | vector :: iterator it; 5 | it = nums.begin(); 6 | for(int i=0;i v1; 7 | vector :: iterator it,it1; 8 | 9 | for(int i=0;i max) 32 | max = curlen; 33 | } 34 | return max; 35 | } 36 | }; -------------------------------------------------------------------------------- /LeetCode/5.LongestPalindromicSubstring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestPalindrome(string str) { 4 | int len = str.length(); 5 | if(len < 2) 6 | return str; 7 | vector T; 8 | T.push_back('$'); 9 | T.push_back('#'); 10 | int n=2,C=1,R=1,maxlen = 0; 11 | for(int i=0;i maxlen) 33 | maxlen = P[i]; 34 | 35 | if(i + P[i] > R){ 36 | C = i; 37 | R = i + P[i]; 38 | } 39 | } 40 | int l; 41 | for(int i=0;i= len){ 13 | col = j; 14 | break; 15 | } 16 | if(pos == 0) 17 | pos = numRows - 1; 18 | if(pos == numRows - 1){ 19 | for(int i=0;i= 2147483647 || x <= -2147483648) 7 | return 0; 8 | if(x > 0){ 9 | while(x > 0){ 10 | num = num * 10; 11 | if( num >= 2147483647 || num <= -2147483648) 12 | return 0; 13 | num = num + (x % 10); 14 | x = x / 10; 15 | } 16 | } 17 | else{ 18 | x = -1 * x; 19 | while(x > 0){ 20 | num = num * 10; 21 | if( num >= 2147483647 || num <= -2147483648) 22 | return 0; 23 | num = num + (x % 10); 24 | x = x / 10; 25 | } 26 | num = -1 * num; 27 | } 28 | return int(num); 29 | } 30 | }; -------------------------------------------------------------------------------- /LeetCode/8.StringToInteger.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string str) { 4 | if( str.empty()) 5 | return 0; 6 | int len = str.length(), i=0, sign = 1; 7 | 8 | while( i= '0' && str[i] <= '9'){ 24 | out = out * 10; 25 | if(out <= INT_MIN || out >= INT_MAX) 26 | break; 27 | out = out + (str[i] - '0'); 28 | i++; 29 | } 30 | 31 | if(sign == 0) 32 | out = -1 * out; 33 | if(out <= INT_MIN) 34 | return INT_MIN; 35 | if(out >= INT_MAX) 36 | return INT_MAX; 37 | return (int)out; 38 | } 39 | }; -------------------------------------------------------------------------------- /LeetCode/9.PalindromeNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // 2147483647 4 | bool isPalindrome(int x) { 5 | if( x < 0 || x >= 2147483647) 6 | return false; 7 | long int num = 0; 8 | int y = x; 9 | while(x > 0){ 10 | num = num * 10; 11 | if( num > 2147483647) 12 | return false; 13 | num = num + (x % 10); 14 | x = x / 10; 15 | } 16 | if(num == y) 17 | return true; 18 | else 19 | return false; 20 | } 21 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Coding 2 | My Codes from Codechef, GeeksForGeeks, HackerRank and LeetCode 3 | -------------------------------------------------------------------------------- /codes/DP/1.01Knapsack.cpp: -------------------------------------------------------------------------------- 1 | // 0-1 Knapsack using recursive approach with memoization 2 | 3 | // Input: 4 | // 1 5 | // 4(N) 7(W) 6 | // wt[] 1 3 4 5 7 | // val[] 1 4 5 7 8 | // Output: 9 | // 9 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int DP[101][101]; 16 | 17 | int Knapsack(int wt[],int val[],int w,int n){ 18 | if(n == 0 || w == 0) 19 | return 0; 20 | if(DP[n][w] != -1) 21 | return DP[n][w]; 22 | if(wt[n-1] <= w){ 23 | DP[n][w] = max(val[n-1] + Knapsack(wt,val,w-wt[n-1],n-1), Knapsack(wt,val,w,n-1)); 24 | return DP[n][w]; 25 | } 26 | else{ 27 | DP[n][w] = Knapsack(wt,val,w,n-1); 28 | return DP[n][w]; 29 | } 30 | } 31 | 32 | int main() { 33 | int T; 34 | cin>>T; 35 | while(T--){ 36 | memset(DP,-1,sizeof(DP)); 37 | int N,W,maxProfit; 38 | cin>>N>>W; 39 | int wt[N],val[N]; 40 | for(int i=0;i>wt[i]; 42 | for(int i=0;i>val[i]; 44 | 45 | maxProfit = Knapsack(wt,val,W,N); 46 | cout< 12 | #include 13 | using namespace std; 14 | 15 | int Knapsack(int wt[],int val[],int w,int n){ 16 | int DP[n+1][w+1]; 17 | 18 | for(int i=0;i<=n;i++){ 19 | for(int j=0;j<=w;j++){ 20 | if(i==0 || j==0) 21 | DP[i][j] = 0; 22 | else if(wt[i-1] <= j) 23 | DP[i][j] = max(val[i-1] + DP[i-1][j- wt[i-1]], DP[i-1][j]); 24 | else 25 | DP[i][j] = DP[i-1][j]; 26 | } 27 | } 28 | return DP[n][w]; 29 | } 30 | 31 | int main() { 32 | int T; 33 | cin>>T; 34 | while(T--){ 35 | int N,W,maxProfit; 36 | cin>>N>>W; 37 | int wt[N],val[N]; 38 | for(int i=0;i>wt[i]; 40 | for(int i=0;i>val[i]; 42 | 43 | maxProfit = Knapsack(wt,val,W,N); 44 | cout< 14 | #include 15 | using namespace std; 16 | 17 | int subsetSum(int A[],int sum,int n){ 18 | bool DP[n+1][sum+1]; 19 | 20 | for(int i=0;i<=n;i++){ 21 | for(int j=0;j<=sum;j++){ 22 | if(i==0 && j!=0) 23 | DP[i][j] = false; 24 | else if(j==0) 25 | DP[i][j] = true; 26 | else if(A[i-1] <= j) 27 | DP[i][j] = DP[i-1][j- A[i-1]] || DP[i-1][j]; 28 | else 29 | DP[i][j] = DP[i-1][j]; 30 | } 31 | } 32 | return DP[n][sum]; 33 | } 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N,sum; 40 | cin>>N>>sum; 41 | int A[N]; 42 | for(int i=0;i>A[i]; 44 | 45 | bool contains = subsetSum(A,sum,N); 46 | cout< 19 | #include 20 | using namespace std; 21 | 22 | int subsetSum(int A[],int sum,int n){ 23 | bool DP[n+1][sum+1]; 24 | 25 | for(int i=0;i<=n;i++){ 26 | for(int j=0;j<=sum;j++){ 27 | if(i==0 && j!=0) 28 | DP[i][j] = false; 29 | else if(j==0) 30 | DP[i][j] = true; 31 | else if(A[i-1] <= j) 32 | DP[i][j] = DP[i-1][j- A[i-1]] || DP[i-1][j]; 33 | else 34 | DP[i][j] = DP[i-1][j]; 35 | } 36 | } 37 | return DP[n][sum]; 38 | } 39 | 40 | int main() { 41 | int T; 42 | cin>>T; 43 | while(T--){ 44 | int N,sum=0; 45 | cin>>N; 46 | int A[N]; 47 | for(int i=0;i>A[i]; 49 | sum = sum + A[i]; 50 | } 51 | if(sum % 2 == 1) 52 | cout< 14 | #include 15 | using namespace std; 16 | 17 | int subsetSumCount(int A[],int sum,int n){ 18 | int DP[n+1][sum+1]; 19 | 20 | for(int i=0;i<=n;i++){ 21 | for(int j=0;j<=sum;j++){ 22 | if(i==0 && j!=0) 23 | DP[i][j] = 0; 24 | else if(j==0) 25 | DP[i][j] = 1; 26 | else if(A[i-1] <= j) 27 | DP[i][j] = DP[i-1][j- A[i-1]] + DP[i-1][j]; 28 | else 29 | DP[i][j] = DP[i-1][j]; 30 | } 31 | } 32 | return DP[n][sum]; 33 | } 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N,sum; 40 | cin>>N>>sum; 41 | int A[N]; 42 | for(int i=0;i>A[i]; 44 | 45 | cout< 18 | #include 19 | #include 20 | #include 21 | using namespace std; 22 | 23 | vector SubsetSum(int A[],int n, int sum){ 24 | bool DP[n+1][sum+1]; 25 | vector lastrow; 26 | for(int i=0;i<=n;i++){ 27 | for(int j=0;j<=sum;j++){ 28 | if(i==0 && j!= 0) 29 | DP[i][j] = false; 30 | else if(j == 0) 31 | DP[i][j] = true; 32 | else if(A[i-1] <= j) 33 | DP[i][j] = (DP[i-1][j - A[i-1]]) || DP[i-1][j]; 34 | else 35 | DP[i][j] = DP[i-1][j]; 36 | if(i==n) 37 | lastrow.push_back(DP[i][j]); 38 | } 39 | } 40 | return lastrow; 41 | } 42 | 43 | int MinimumSumPartition(int A[], int N, int sum){ 44 | vector lastrow; 45 | int minSum = INT_MAX; 46 | lastrow = SubsetSum(A,N,sum); 47 | for(int i=0;i<(lastrow.size()/2)+1;i++){ 48 | if(lastrow[i] == true) 49 | minSum = min(minSum, abs(sum - (2*i))); 50 | } 51 | return minSum; 52 | } 53 | 54 | int main() { 55 | int T; 56 | cin>>T; 57 | while(T--){ 58 | int N,sum=0; 59 | cin>>N; 60 | int A[N]; 61 | for(int i=0;i>A[i]; 63 | sum = sum + A[i]; 64 | } 65 | cout< 14 | #include 15 | using namespace std; 16 | 17 | int subsetSumCount(int A[],int sum,int n){ 18 | int DP[n+1][sum+1]; 19 | 20 | for(int i=0;i<=n;i++){ 21 | for(int j=0;j<=sum;j++){ 22 | if(i==0 && j!=0) 23 | DP[i][j] = 0; 24 | else if(j==0) 25 | DP[i][j] = 1; 26 | else if(A[i-1] <= j) 27 | DP[i][j] = DP[i-1][j- A[i-1]] + DP[i-1][j]; 28 | else 29 | DP[i][j] = DP[i-1][j]; 30 | } 31 | } 32 | return DP[n][sum]; 33 | } 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N,sum=0,dif; 40 | cin>>N>>dif; 41 | int A[N]; 42 | for(int i=0;i>A[i]; 44 | sum = sum + A[i]; 45 | } 46 | int sumS1 = (dif + sum)/2; 47 | cout< 14 | #include 15 | using namespace std; 16 | 17 | int subsetSumCount(int A[],int sum,int n){ 18 | int DP[n+1][sum+1]; 19 | 20 | for(int i=0;i<=n;i++){ 21 | for(int j=0;j<=sum;j++){ 22 | if(i==0 && j!=0) 23 | DP[i][j] = 0; 24 | else if(j==0) 25 | DP[i][j] = 1; 26 | else if(A[i-1] <= j) 27 | DP[i][j] = DP[i-1][j- A[i-1]] + DP[i-1][j]; 28 | else 29 | DP[i][j] = DP[i-1][j]; 30 | } 31 | } 32 | return DP[n][sum]; 33 | } 34 | 35 | int main() { 36 | int T; 37 | cin>>T; 38 | while(T--){ 39 | int N,sum=0,s; 40 | cin>>N>>s; 41 | int A[N]; 42 | for(int i=0;i>A[i]; 44 | sum = sum + A[i]; 45 | } 46 | int sumS1 = (s + sum)/2; 47 | cout< 12 | #include 13 | using namespace std; 14 | 15 | int Unbounded(int wt[],int val[],int w,int n){ 16 | int DP[n+1][w+1]; 17 | 18 | for(int i=0;i<=n;i++){ 19 | for(int j=0;j<=w;j++){ 20 | if(i==0 || j==0) 21 | DP[i][j] = 0; 22 | else if(wt[i-1] <= j) 23 | DP[i][j] = max(val[i-1] + DP[i][j- wt[i-1]], DP[i-1][j]); 24 | else 25 | DP[i][j] = DP[i-1][j]; 26 | } 27 | } 28 | return DP[n][w]; 29 | } 30 | 31 | int main() { 32 | int T; 33 | cin>>T; 34 | while(T--){ 35 | int N,W,maxProfit; 36 | cin>>N>>W; 37 | int wt[N],val[N]; 38 | for(int i=0;i>wt[i]; 40 | for(int i=0;i>val[i]; 42 | 43 | maxProfit = Unbounded(wt,val,W,N); 44 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int R_Partition(int A[],int l,int r){ 6 | int ran = l + rand() % (r-l+1); 7 | swap(A[ran],A[r]); 8 | 9 | int x=A[r]; 10 | int i=l-1; 11 | for(int j=l;j<=r-1;j++){ 12 | if(A[j]<=x){ 13 | i=i+1; 14 | swap(A[i],A[j]); 15 | } 16 | } 17 | swap(A[i+1],A[r]); 18 | return i+1; 19 | } 20 | 21 | void R_QuickSort(int A[],int l,int r){ 22 | int q; 23 | if(l < r){ 24 | q = R_Partition(A,l,r); 25 | R_QuickSort(A,l,q-1); 26 | R_QuickSort(A,q+1,r); 27 | } 28 | } 29 | 30 | int main() { 31 | int N; 32 | srand(time(NULL)); 33 | cin>>N; 34 | int arr[N]; 35 | for(int i=0;i>arr[i]; 37 | 38 | R_QuickSort(arr,0,N-1); 39 | 40 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void BubbleSort(int A[], int n){ 6 | for(int i=0;i A[j+1]) 9 | swap(A[j], A[j+1]); 10 | } 11 | } 12 | } 13 | 14 | int main() { 15 | int N; 16 | cin>>N; 17 | int arr[N]; 18 | for(int i=0;i>arr[i]; 20 | 21 | BubbleSort(arr,N); 22 | 23 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void CountingSort(int A[], int n, int range){ 6 | int count[range+1],B[n]; 7 | memset(count,0,sizeof(count)); 8 | 9 | for(int i=0;i>N; 25 | int arr[N]; 26 | for(int i=0;i>arr[i]; 28 | if(arr[i] > max) 29 | max = arr[i]; 30 | } 31 | 32 | CountingSort(arr,N,max); 33 | 34 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void heapify(int A[], int n, int i){ 6 | int l = 2*i + 1; 7 | int r = 2*i + 2; 8 | int large = i; 9 | if(l < n && A[l] > A[i]) 10 | large = l; 11 | if(r < n && A[r] > A[large]) 12 | large = r; 13 | if(large != i){ 14 | swap(A[i], A[large]); 15 | heapify(A, n, large); 16 | } 17 | } 18 | 19 | void buildHeap(int A[], int n){ 20 | for(int i=(n/2)-1;i>=0;i--) 21 | heapify(A, n, i); 22 | } 23 | 24 | 25 | void heapSort(int A[], int n){ 26 | buildHeap(A, n); 27 | for(int i=n-1;i>=0;i--){ 28 | swap(A[0], A[i]); 29 | heapify(A, i, 0); 30 | } 31 | } 32 | 33 | int main() { 34 | int n; 35 | cin>>n; 36 | int A[n]; 37 | for(int i=0;i>A[i]; 39 | heapSort(A, n); 40 | 41 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void InsertionSort(int A[], int n){ 6 | for(int i=1;i=0 && A[j]>key){ 10 | A[j+1] = A[j]; 11 | j = j-1; 12 | } 13 | A[j+1] = key; 14 | } 15 | } 16 | 17 | int main() { 18 | int N; 19 | cin>>N; 20 | int arr[N]; 21 | for(int i=0;i>arr[i]; 23 | 24 | InsertionSort(arr, N); 25 | 26 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void Merge(int A[],int L,int mid, int R){ 6 | int B[R+1]; 7 | int i=L,j=mid+1,k=L; 8 | while(i<=mid && j<=R){ 9 | if(A[i] < A[j]) 10 | B[k++] = A[i++]; 11 | else 12 | B[k++] = A[j++]; 13 | } 14 | while(i<= mid) 15 | B[k++] = A[i++]; 16 | while(j<= R) 17 | B[k++] = A[j++]; 18 | 19 | for(int j=L;j<=R;j++) 20 | A[j] = B[j]; 21 | } 22 | 23 | void MergeSort(int A[], int L, int R){ 24 | if(L < R){ 25 | int mid = (L+R)/2; 26 | MergeSort(A,L,mid); 27 | MergeSort(A,mid+1,R); 28 | Merge(A,L,mid,R); 29 | } 30 | } 31 | 32 | int main() { 33 | int N; 34 | cin>>N; 35 | int arr[N]; 36 | for(int i=0;i>arr[i]; 38 | 39 | MergeSort(arr, 0, N-1); 40 | 41 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | int Partition(int A[], int L, int R){ 6 | int pivot = A[L]; 7 | int i = R+1; 8 | 9 | for(int j=R; j>L; j--){ 10 | if(A[j] > pivot){ 11 | i--; 12 | swap(A[i], A[j]); 13 | } 14 | } 15 | swap(A[i-1], A[L]); 16 | return i-1; 17 | } 18 | 19 | void QuickSort(int A[], int L, int R){ 20 | if(L < R){ 21 | int Q = Partition(A,L,R); 22 | QuickSort(A,L,Q-1); 23 | QuickSort(A,Q+1,R); 24 | } 25 | } 26 | 27 | int main() { 28 | int N; 29 | cin>>N; 30 | int arr[N]; 31 | for(int i=0;i>arr[i]; 33 | 34 | QuickSort(arr,0,N-1); 35 | 36 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void SelectionSort(int A[], int n){ 6 | int min_id; 7 | 8 | for(int i=0;i>N; 21 | int arr[N]; 22 | for(int i=0;i>arr[i]; 24 | 25 | SelectionSort(arr,N); 26 | 27 | for(int i=0;i