├── ALGORITHMS └── KadanesAlgo.cpp ├── Array ├── BubbleSort_ArraySorting.cpp ├── CheckBothEqual_Array.cpp ├── CheckIfSorted.cpp ├── CyclicallyRotate.cpp ├── DeleteAndUpdate.cpp ├── EquilibriumPoint.cpp ├── ExceptionallyOdd.cpp ├── FacingTheSun.cpp ├── FindLargestDesc.cpp ├── FindNoOfNo.cpp ├── FirstRepeatingElement.cpp ├── GreaterThanX.cpp ├── ImmediateSmallerThanX.cpp ├── InsertAtIndex_Array.cpp ├── LargestElement.cpp ├── LeftRotation_Array.cpp ├── MajorityFreq.cpp ├── MaxMinElement_Array.cpp ├── MaximumInStructArray.cpp ├── MaximumIndex.cpp ├── MeanMedian_Array.cpp ├── MergeWithoutExtraSpace.cpp ├── MissingInSecondArray.cpp ├── RemoveDuplicatesFromSorted.cpp ├── RemoveDuplicatesInPrimeArray.cpp ├── RemoveDuplicates_Array.cpp ├── ReplaceNumWithOther_Array.cpp ├── ReverseArrayInGroups.cpp ├── Reverse_Array.cpp ├── RightRotation_Array.cpp ├── SingleNum.cpp ├── SmallerThanX_Array.cpp ├── SquareRoot_Array.cpp ├── SumOfMiddle2.cpp ├── UnionOfTwoSortedArrays.cpp └── WaveArray.cpp ├── Bit_Magic ├── 1sComplement.cpp ├── AllSetBits.cpp ├── BinaryShiftDivide.cpp ├── BinaryToGray.cpp ├── BitMultiply.cpp ├── CheckNthBit.cpp ├── CheckSetUnsetBits.cpp ├── FirstSetBit.cpp ├── GrayToBinary.cpp ├── LongestCons1.cpp ├── NoOf1Bits.cpp ├── SetKthBit.cpp ├── SwapNibbles.cpp └── ToCheckPowerOf2.cpp ├── Graph ├── DirectedAdjacencyList_Graph.cpp ├── DirectedAdjacencyMatrix_Graph.cpp ├── JustPrint_AdjancencyList.cpp ├── UndirectedAdjacencyList_Graph.cpp └── UndirectedAdjacencyMatrix_Graph.cpp ├── Linked_Lists ├── Check_Circularll.cpp ├── DeleteAtPos_Circularll.cpp ├── DeleteHead_Circularll.cpp ├── DeleteHead_Doublyll.cpp ├── DeleteHead_LinkedList.cpp ├── DeleteTail_Circular.cpp ├── DeleteTail_Doublyll.cpp ├── DeleteTail_LinkedList.cpp ├── Display_DoublyCircularll.cpp ├── Display_Doublyll.cpp ├── Display_LinkedList.cpp ├── DoublyCircular-LinkedList.cpp ├── DoublyLinear-LinkedList.cpp ├── InsertAtPos_Circular.cpp ├── InsertHead_Circularll.cpp ├── InsertHead_Doubly;;.cpp ├── InsertTail_Circularll.cpp ├── InsertTail_Doublyll.cpp ├── Insertion_LinkedList.cpp ├── JoinTwo_LinkedzLists.cpp ├── Length_Circularll.cpp ├── Max&Min_LinkedList.cpp ├── MiddlePosition.cpp ├── NodesSum_LinkedList.cpp ├── Searching_LinkedList.cpp ├── SinglyCircular-LinkedList.cpp └── SinglyLinear-LinkedList.cpp ├── Mathematics ├── AbsoluteValue.cpp ├── AddWithoutPlusOp.cpp ├── AdditionUnderModulo.cpp ├── AmicablePair.cpp ├── AmicablePair.py ├── BalancingNo.cpp ├── CelciusToFarenheit.cpp ├── CheckIfDivisibleBy4.cpp ├── CheckIfDivisibleBy5.cpp ├── CheckPerfectSquare.cpp ├── ConvertToRoman.cpp ├── DecimalToBin.cpp ├── DisariumNo.cpp ├── Exactly3Divisors.cpp ├── GPTerm.cpp ├── LargeFactorial.cpp ├── MakeNumberOdd.cpp ├── MaximumArea.cpp ├── MindGame.cpp ├── MissingNum.cpp ├── ModularMultiplicativeInverse.cpp ├── MultiplyTwoPloynomials.cpp ├── NthTerm.cpp ├── PlayWithOr.cpp ├── PrimalityTest.cpp ├── RemainderWith7.cpp ├── Return2Prime.cpp ├── ReverseCoding.cpp ├── SmallestDivisibleNo.cpp ├── SquareNoWithoutInbuiltFun.cpp ├── StrongNumber.cpp ├── Subtraction2Nos.cpp ├── SumOfDigitModified.cpp └── SumOfDigitsDivisible.cpp ├── Must_Do_Interview_Preparation ├── 30-Days-Of-Code │ ├── KPairs.cpp │ ├── MinSum.cpp │ ├── NthNaturalNo.cpp │ └── RearrangeGeeks&Classmates.cpp ├── MD-Arrays │ ├── BinarySearch.cpp │ ├── EqulibriumPoint.cpp │ └── MissingsNos.cpp └── MD-LinkedLists │ └── PairWiseSwap.cpp ├── Prime_Numbers ├── LargestPrimeFactor.cpp ├── LeastPrimeFactor.cpp ├── PrimeFactors.cpp ├── PrimeFactorsPower.cpp ├── PrimeNumber.cpp ├── ProductOfPrimeFactors.cpp ├── SumOfDivisors.cpp └── SumOfPrimes.cpp ├── Queue ├── CircularQueue.cpp ├── LinearQueue.cpp └── QueueUsing_Array.cpp ├── README.md ├── Recursion ├── fibonacci.cpp ├── palindrome.cpp └── print_array.cpp ├── Searching_Algorithms ├── BinarySearch.cpp ├── Binary_Search.cpp ├── CeilingOfNum.cpp ├── Count1s_Array.cpp ├── FindElementOccuringOnce.cpp ├── FindRotationCountOfSortedArray.cpp ├── FloorOfNum.cpp ├── HighestNum.cpp ├── LeetCode#1095.cpp ├── LeetCode#162.cpp ├── LeetCode#33.cpp ├── LeetCode#34.cpp ├── LeetCode#410.cpp ├── LeetCode#744.cpp ├── Leetcode#852.cpp └── Linear_Search.cpp ├── Sorting_Algorithms ├── BubbleSort.cpp ├── Bubble_Sort.cpp ├── CPPGenericSort.cpp ├── CyclicSort.cpp ├── Insertion_Sort.cpp ├── LeetCode#268.cpp ├── LeetCode#287.cpp ├── LeetCode#41.cpp ├── LeetCode#448.cpp ├── LeetCode#645.cpp ├── SelectionSort.cpp ├── Selection_Sort.cpp ├── Shell_Sort.cpp ├── _README_BubbleSort.txt ├── _README_InsertionSort.txt ├── _README_MergeSort ├── _README_SelectionSort.txt ├── _README_ShellSort.txt └── _selection_sort.cpp ├── Stack ├── DynamicStack.cpp ├── MiddleOf_Stack.cpp ├── ReverseArrauUsing_Stack.cpp ├── StackUsing_Array.cpp └── Stack_Operations.cpp ├── Standard_Template_Library ├── Map_Operations.cpp ├── Multimap_Operations.cpp ├── Multiset_Operations.cpp ├── Pair_Sum.cpp ├── Removing_Duplicate_Using_Set.cpp ├── ReverseVector.cpp ├── Rotating_ArrayLeft.cpp ├── Set1.cpp ├── Set2.cpp ├── Set_Operations.cpp ├── SortVector.cpp ├── Vector_FindFrequency.cpp ├── Vector_Iterator.cpp ├── Vector_Iterator1.cpp ├── Vector_Iterator2.cpp ├── Vector_ReverseVector.cpp └── Vector_SortVector.cpp ├── String ├── implement_atoi.cpp ├── max_occur_string.cpp ├── sort_a_string.cpp ├── sort_string.cpp ├── string_intro.cpp ├── string_length.cpp └── vowel_count.cpp ├── Tree ├── BinarySearch_Tree.cpp ├── Binary_Tree.cpp ├── BreadthFirstTraversal_Tree.cpp ├── Heap_Tree.cpp ├── Height_BST.cpp ├── LevelOrder_Display.cpp ├── MaxHeightBinTree.cpp ├── MinDepthBinTree.cpp └── RedBlack_Tree.cpp └── Tricky_Problems ├── CheckArrayEquality.cpp ├── CheckIfPowerOf2.cpp ├── CheckIfPowerOf4.cpp ├── Checking_IntersectionPointOfTwo_LinkedList.cpp ├── CommonDivisors.py ├── CppTemplate.cpp ├── Delete_DuplicateElementsFrom_LinkedList.cpp ├── DetectLoop_LinkedList.cpp ├── FindNth_node_LinkedList.cpp ├── HalfNbyM.cpp ├── KaratsubaAlgo.cpp ├── MedianOf2SortedArrays.cpp ├── MoveZeros.cpp ├── PerfectNum-Bruteforce.cpp ├── PrintNonRepeated.cpp ├── Reversing_LinkedList.cpp ├── SecondLargestNo.cp ├── TotalRectangles.cpp ├── TotalSquares.cpp └── TwoSum.cpp /ALGORITHMS/KadanesAlgo.cpp: -------------------------------------------------------------------------------- 1 | // Given an array arr of N integers. Find the contiguous sub-array with maximum sum. 2 | 3 | // Solution: 4 | // Function to find subarray with maximum sum 5 | // arr: input array 6 | // n: size of array 7 | int maxSubarraySum(int arr[], int n){ 8 | int sum=arr[0], //tillnow 9 | maxx=arr[0]; //curr 10 | for(int i=1;i 5 | #include 6 | 7 | using namespace std; 8 | bool check(long long int arr[],long long brr[],long long int n) 9 | { 10 | sort(arr,arr+n); 11 | sort(brr,brr+n); 12 | 13 | for(int i=0;i>t; 23 | 24 | while(t--) 25 | { 26 | long long int n; 27 | cin>>n; 28 | long long int arr[n],brr[n]; 29 | for(int i=0;i>arr[i]; 32 | } 33 | for(int i=0;i>brr[i]; 36 | } 37 | if(check(arr,brr,n)) 38 | cout<<"1\n"; 39 | else 40 | cout<<"0\n"; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Array/CheckIfSorted.cpp: -------------------------------------------------------------------------------- 1 | //Given an array arr[] of size N, check if it is sorted in non-decreasing order or not. 2 | //solution: 3 | bool arraySortedOrNot(int arr[], int n) { 4 | int flag=0; 5 | for(int i=0;iarr[i+1]) flag=1; 8 | } 9 | return flag==1?false:true; 10 | } -------------------------------------------------------------------------------- /Array/CyclicallyRotate.cpp: -------------------------------------------------------------------------------- 1 | //Given an array, 2 | //Rotate the array by one position in clock-wise direction 3 | 4 | //solution: 5 | void rotate(int arr[], int n) 6 | { 7 | int temp = arr[n-1]; 8 | for(int i=n-1;i>0;i--) 9 | { 10 | arr[i]=arr[i-1]; 11 | } 12 | arr[0]=temp; 13 | } -------------------------------------------------------------------------------- /Array/DeleteAndUpdate.cpp: -------------------------------------------------------------------------------- 1 | //You are given an array arr(0-based indexing). 2 | //The size of the array is given by n. You need to delete an element at given index and print the modified array. The arr[i] of array is initially set to i+1. 3 | //Deletion means you need to shift all the elements after that index to the left by 1 position and set the last element as zero. 4 | 5 | void deleteFromArray(int arr[], int n, int index){ 6 | 7 | if(index==n-1) 8 | { 9 | arr[n-1]=0; 10 | } 11 | else 12 | { 13 | int i=0; 14 | 15 | while(i!=index) 16 | i++; 17 | 18 | for(int j=i;j0) 8 | { 9 | if(n%10>x) 10 | return false; 11 | x=n%10; 12 | n=n/10; 13 | } 14 | return true; 15 | } 16 | int find(int N){ 17 | while(N>1) 18 | { 19 | if(check(N)) 20 | return N; 21 | N--; 22 | } 23 | return N; 24 | } 25 | }; -------------------------------------------------------------------------------- /Array/FindNoOfNo.cpp: -------------------------------------------------------------------------------- 1 | // Given an array A[] of n elements. 2 | // Your task is to complete the Function num which returns an integer denoting the total number of times digit k appears in the whole array. 3 | 4 | int num(int a[], int n, int k) 5 | { 6 | int ct=0; 7 | for(int i=0;i0) 10 | { 11 | if(a[i]%10==k) 12 | ct++; 13 | a[i]=a[i]/10; 14 | } 15 | } 16 | return ct; 17 | } -------------------------------------------------------------------------------- /Array/FirstRepeatingElement.cpp: -------------------------------------------------------------------------------- 1 | // Given an array arr[] of size N, find the first repeating element. The element should occurs more than once and the index of its first occurrence should be the smallest. 2 | 3 | //Solution: 4 | int firstRepeated(int arr[], int n) { 5 | for(int i=0;i 1) 8 | return i+1; 9 | } 10 | return -1; 11 | } 12 | -------------------------------------------------------------------------------- /Array/GreaterThanX.cpp: -------------------------------------------------------------------------------- 1 | //Given an unsorted array arr[] of size N containing non-negative integers. You will also be given an integer X, the task is to count the number of elements which are strictly greater than X. 2 | //The given integer may or not be present in the array given. 3 | 4 | // arr[]: input array 5 | // n: size of the array 6 | // x: element for which you need to return the count 7 | int greaterThanX(int arr[],int n,int x) 8 | { 9 | int ct=0; 10 | for(int i=0;ix) 13 | ct++; 14 | } 15 | return ct; 16 | } -------------------------------------------------------------------------------- /Array/ImmediateSmallerThanX.cpp: -------------------------------------------------------------------------------- 1 | // User function to complete 2 | // arr[]: input array 3 | // n: size of array 4 | // k: find element smaller and closest to it. 5 | int immediateSmaller(int arr[], int n, int x) 6 | { 7 | int mi=INT_MAX; 8 | int y; 9 | for(int i=0;i=x) 21 | flag = true; 22 | else 23 | { 24 | flag = false; 25 | break; 26 | 27 | } 28 | } 29 | if(flag) return -1; 30 | else return y; 31 | } -------------------------------------------------------------------------------- /Array/InsertAtIndex_Array.cpp: -------------------------------------------------------------------------------- 1 | //You are given an array arr(0-based index). The size of the array is given b 2 | 3 | void insertAtIndex(int arr[], int sizeOfArray, int index, int element) 4 | { 5 | if(sizeOfArray-1==index)//last position 6 | { 7 | arr[sizeOfArray-1]=element; 8 | } 9 | 10 | else 11 | { 12 | for(int j=sizeOfArray;j>=index;j--) 13 | { 14 | arr[j]=arr[j-1]; 15 | } 16 | 17 | arr[index]=element; 18 | } 19 | return; 20 | } -------------------------------------------------------------------------------- /Array/LargestElement.cpp: -------------------------------------------------------------------------------- 1 | // Given an array A[] of size n. 2 | // The task is to find the largest element in it. 3 | 4 | // C function to find maximum in arr[] of size n 5 | int largest(int arr[], int n) { 6 | int max=0; 7 | for(int i=0;i 8 | #define MAX 50 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | void Rotateleft(int arr[],int size,int freq) 13 | { 14 | int rotation=1; 15 | while(freq>0) 16 | { 17 | int temp=arr[size-1]; 18 | for(int i=size-1;i>=0;i--) 19 | { 20 | arr[i]=arr[i-1]; 21 | } 22 | arr[0]=temp; 23 | freq--; 24 | cout<<"\nFor "<>size; 41 | cout<<"\nEnter elements: "; 42 | for(int i=0;i>arr[i]; 45 | } 46 | cout<<"\nFrequency of rotating elements through left: "; 47 | cin>>freq; 48 | Rotateleft(arr,size,freq); 49 | return 0; 50 | } 51 | /////////////////////////////////////////////////////////////////////////////////// 52 | //End of program 53 | /////////////////////////////////////////////////////////////////////////////////// 54 | -------------------------------------------------------------------------------- /Array/MajorityFreq.cpp: -------------------------------------------------------------------------------- 1 | //You are given an array arr[] of size N. 2 | //You are also given two elements x and y. Now, you need to tell which element (x or y) appears most in the array. 3 | //In other words, return the element, x or y, that has higher frequency in the array. 4 | //If both elements have the same frequency, then just return the smaller element. 5 | 6 | //NOTE : We need to return the elements, not their counts. 7 | 8 | int majorityWins(int arr[], int n, int x,int y) 9 | { 10 | int count_x=0;//counter to count frequency of x 11 | int count_y=0;//counter to count frequency of y 12 | 13 | for(int i=0;icount_y) 26 | return x; 27 | else if(count_y>count_x) 28 | return y; 29 | else 30 | return x=0 && y arr2[y]){ 15 | swap(arr1[x] , arr2[y]); 16 | x--; 17 | y++; 18 | } 19 | else{ 20 | x--; 21 | } 22 | } 23 | sort(arr1 , arr1+n); 24 | sort(arr2 , arr2+m); 25 | } -------------------------------------------------------------------------------- /Array/MissingInSecondArray.cpp: -------------------------------------------------------------------------------- 1 | // Given two arrays A and B contains integers of size N and M, the task is to find numbers which are present in the first array, but not present in the second array. 2 | 3 | vector findMissing(long long A[], long long B[], 4 | int N, int M) 5 | { 6 | unordered_sets; 7 | for(long long i=0;iv; 15 | for(int i=0;i removeDuplicate(vector& arr, int n) 5 | { 6 | unordered_sets; //by default set is ordered 7 | vectorv; 8 | for(int i=0;i 8 | #define MAX 50 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | void RemoveDuplicates(int arr[],int size) 13 | { 14 | for(int i=0;i>size; 27 | cout<<"\nEnter elements: "; 28 | for(int i=0;i>arr[i]; 31 | } 32 | RemoveDuplicates(arr,size); 33 | return 0; 34 | } 35 | 36 | /////////////////////////////////////////////////////////////////////////////////// 37 | //End of program 38 | /////////////////////////////////////////////////////////////////////////////////// 39 | -------------------------------------------------------------------------------- /Array/ReplaceNumWithOther_Array.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/replace-all-0s-with-5/1/?track=amazon-arrays&batchId=192 2 | 3 | //You are given an integer N. You need to convert all zeroes of N to 5. 4 | 5 | //solution 6 | 7 | int convertFive(int n) { 8 | int i=0; 9 | int ans=n; 10 | while(n) 11 | { 12 | int temp = n%10; 13 | if(temp==0) 14 | { 15 | ans = (ans + (5*pow(10,i))); 16 | } 17 | i++; 18 | n=n/10; 19 | } 20 | return ans; 21 | } -------------------------------------------------------------------------------- /Array/ReverseArrayInGroups.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/reverse-array-in-groups0255/1/?track=ppc-arrays&batchId=221 2 | 3 | //Given an array arr[] of positive integers of size N. Reverse every sub-array of K group elements. 4 | 5 | 6 | vector reverseInGroups(vector mv, int n, int k){ 7 | int i; 8 | for(i=0;i 8 | #define MAX 50 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | void RotateRight(int arr[],int size,int freq) 13 | { 14 | int rotation=1; 15 | while(freq>0) 16 | { 17 | int temp=arr[0]; 18 | for(int i=0;i>size; 41 | cout<<"\nEnter elements: "; 42 | for(int i=0;i>arr[i]; 45 | } 46 | cout<<"\nFrequency of rotating elements through left: "; 47 | cin>>freq; 48 | RotateRight(arr,size,freq); 49 | return 0; 50 | } 51 | /////////////////////////////////////////////////////////////////////////////////// 52 | //End of program 53 | /////////////////////////////////////////////////////////////////////////////////// 54 | -------------------------------------------------------------------------------- /Array/SingleNum.cpp: -------------------------------------------------------------------------------- 1 | // Given an array Arr of positive integers of size N where every element appears even times except for one. 2 | // Find that number occuring odd number of times. 3 | 4 | //User function template for C++ 5 | class Solution{ 6 | public: 7 | int getSingle(int arr[], int n) { 8 | int res=arr[0]; 9 | for(int i=1;iv; 8 | for(int i=0;i findUnion(int arr1[], int arr2[], int n, int m) 7 | { 8 | vectorv; 9 | vector::iterator ip; 10 | for(int i=0;i= a2 <= a3 >= a4 <= a5..... (considering the increasing lexicographical order). 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | long int t; 10 | cin>>t; 11 | while(t>0) 12 | { 13 | vectorv; 14 | long long int n,val; 15 | cin>>n; 16 | for(int i=0;i>val; 19 | v.push_back(val); 20 | } 21 | sort(v.begin(),v.end()); 22 | for(int i=1;i>1; 9 | } 10 | return 1; 11 | } 12 | }; -------------------------------------------------------------------------------- /Bit_Magic/BinaryShiftDivide.cpp: -------------------------------------------------------------------------------- 1 | // Halving the number by using binary shift operator 2 | 3 | class Solution{ 4 | public: 5 | int half(int N){ 6 | if(N==1 || N==0) 7 | return N; 8 | else if(N%2 ==1) 9 | { 10 | return floor(N>>1); 11 | } 12 | else 13 | { 14 | return N>>1; 15 | } 16 | } 17 | }; -------------------------------------------------------------------------------- /Bit_Magic/BinaryToGray.cpp: -------------------------------------------------------------------------------- 1 | //You are given a decimal number N. You need to find the gray code of the number N and convert it into decimal. 2 | 3 | //solution: 4 | 5 | int greyConverter(int n) 6 | { 7 | return (n^(n>>1)); 8 | } -------------------------------------------------------------------------------- /Bit_Magic/BitMultiply.cpp: -------------------------------------------------------------------------------- 1 | //Given a number N. 2 | //Find the product of the number of setbits and the number itself. 3 | 4 | class Solution{ 5 | public: 6 | int bitMultiply(int N){ 7 | int ans = 0, sn=N; 8 | while(N) 9 | { 10 | if((N&1) == 1) 11 | ans++; 12 | N=N>>1; 13 | } 14 | return ans*n; 15 | } 16 | }; -------------------------------------------------------------------------------- /Bit_Magic/CheckNthBit.cpp: -------------------------------------------------------------------------------- 1 | //Given a number N and a bit number K, check if Kth bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number. 2 | 3 | //User function Template for C++ 4 | 5 | // Function to check if Kth bit is set or not 6 | bool checkKthBit(int n, int k){ 7 | return ((n>>k)&1); 8 | } -------------------------------------------------------------------------------- /Bit_Magic/CheckSetUnsetBits.cpp: -------------------------------------------------------------------------------- 1 | class GfG{ 2 | public void count(long n){ 3 | int setb=0; 4 | int nonsetb=0; 5 | while(n>0) 6 | { 7 | if((n&1)==0) 8 | nonsetb++; 9 | else 10 | setb++; 11 | n=n>>1; 12 | } 13 | System.out.println(setb+" "+nonsetb); 14 | } 15 | } -------------------------------------------------------------------------------- /Bit_Magic/FirstSetBit.cpp: -------------------------------------------------------------------------------- 1 | //To get the position of first set bit 2 | //solution: 3 | 4 | int res=0; 5 | while(n!=0) 6 | { 7 | res++; 8 | if(n&1==1){ 9 | break; 10 | } 11 | n=n>>1; 12 | } 13 | return res; -------------------------------------------------------------------------------- /Bit_Magic/GrayToBinary.cpp: -------------------------------------------------------------------------------- 1 | //Given N in Gray code equivalent. Find its binary equivalent. 2 | 3 | //solution: 4 | 5 | int grayToBinary(int n) 6 | { 7 | int num = n; 8 | 9 | while (n>=1){ 10 | n=n>>1; 11 | num = (num^n); 12 | 13 | } 14 | return num; 15 | } -------------------------------------------------------------------------------- /Bit_Magic/LongestCons1.cpp: -------------------------------------------------------------------------------- 1 | //Given a number N. 2 | //Find the length of the longest consecutive 1s in its binary representation. 3 | 4 | class Solution 5 | { 6 | public: 7 | int maxConsecutiveOnes(int N) 8 | { 9 | int ans=0; 10 | int maxx=0; 11 | while(N) 12 | { 13 | if((N&1)==0) 14 | ans=0; 15 | else 16 | ans++; 17 | maxx = max(maxx,ans); 18 | N=N>>1; 19 | } 20 | return maxx; 21 | } 22 | }; -------------------------------------------------------------------------------- /Bit_Magic/NoOf1Bits.cpp: -------------------------------------------------------------------------------- 1 | // Given a positive integer N, print count of set bits in it. 2 | 3 | int setBits(int N) { 4 | int ct=0; 5 | while(N>0) 6 | { 7 | if(N%2 == 1) 8 | ct++; 9 | N=N/2; 10 | } 11 | return ct; 12 | } -------------------------------------------------------------------------------- /Bit_Magic/SetKthBit.cpp: -------------------------------------------------------------------------------- 1 | //CISCO 2 | class Solution 3 | { 4 | public: 5 | int setKthBit(int N, int K) 6 | { 7 | int oneShift = 1<>4; 7 | return second | first; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Bit_Magic/ToCheckPowerOf2.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/power-of-2-1587115620/1/?track=ppc-bit-magic&batchId=221 2 | //Given a positive integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2x for some x. 3 | 4 | //solution: 5 | 6 | bool isPowerofTwo(long long n){ 7 | 8 | return (n&(n-1))?false:true; 9 | 10 | } -------------------------------------------------------------------------------- /Graph/DirectedAdjacencyList_Graph.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :Graph: Directed Adjacency List // 4 | //Language :C++ (Object Oriented Approach) // 5 | /////////////////////////////////////////////////////////////////////////////////// 6 | 7 | #include 8 | #define MAX 30 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | class node 13 | { 14 | public: 15 | int data; 16 | node* next; 17 | }*arr[MAX]; 18 | 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | class DirAdj_List:public node 21 | { 22 | int v,v1,v2; 23 | public: 24 | DirAdj_List() 25 | { 26 | v=v1=v2=0; 27 | } 28 | void CreateList(); 29 | void DisplayList(); 30 | }; 31 | /////////////////////////////////////////////////////////////////////////////////// 32 | void DirAdj_List::CreateList() 33 | { 34 | int choice; 35 | cout<<"\nEnter total vertices: "; 36 | cin>>v; 37 | do{ 38 | cout<<"\nEnter both vertices and cost: "; 39 | cin>>v1>>v2; 40 | if(v1>=v || v2>=v) 41 | { 42 | cout<<"\nEdge not present in graph!"; 43 | continue; 44 | } 45 | node* ptr1=new node; 46 | ptr1->data=v2; 47 | ptr1->next=NULL; 48 | 49 | if(arr[v1]==NULL) 50 | { 51 | arr[v1]=ptr1; 52 | } 53 | else 54 | { 55 | node* temp=arr[v1]; 56 | while(temp->next!=NULL) 57 | { 58 | temp=temp->next; 59 | } 60 | temp->next=ptr1; 61 | } 62 | cout<<"\nWant to continue?[y/Y]"; 63 | cin>>choice; 64 | }while(choice=='Y' || choice=='y'); 65 | } 66 | /////////////////////////////////////////////////////////////////////////////////// 67 | void DirAdj_List::DisplayList() 68 | { 69 | for(int i=0;i>"<data; 76 | temp=temp->next; 77 | } 78 | } 79 | } 80 | /////////////////////////////////////////////////////////////////////////////////// 81 | int main() 82 | { 83 | DirAdj_List obj; 84 | int ch; 85 | do{ 86 | cout<<"\n\nSelect: \n1] Create Adjacency List of Directed graph\n2] Display Adjacency List of Directed graph\n3] Exit\n"; 87 | cin>>ch; 88 | switch(ch) 89 | { 90 | case 1: 91 | obj.CreateList(); 92 | break; 93 | case 2: 94 | obj.DisplayList(); 95 | break; 96 | case 3: 97 | return 0; 98 | default: 99 | cout<<"\nPlease enter correct choice!!"; 100 | break; 101 | } 102 | }while(1); 103 | } 104 | 105 | /////////////////////////////////////////////////////////////////////////////////// 106 | //End of program // 107 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Graph/DirectedAdjacencyMatrix_Graph.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :Graph: Directed Adjacency Matrix // 4 | //Language :C++ (Object Oriented Approach) // 5 | /////////////////////////////////////////////////////////////////////////////////// 6 | 7 | #include 8 | #define MAX 50 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | class DirAdj_Matrix 13 | { 14 | int v,v1,v2,flag,cost; 15 | int G[MAX][MAX]; 16 | public: 17 | //constructor: initializing the member variables 18 | DirAdj_Matrix() 19 | { 20 | v=v1=v2=0; 21 | 22 | for(int i=0;i>v; 40 | do{ 41 | cout<<"\nEnter edges: (both vertices): "; 42 | cin>>v1>>v2; 43 | cout<<"\nEnter cost: "; 44 | cin>>cost; 45 | if(v1>= v|| v2>=v) 46 | { 47 | cout<<"\nEdge not Valid"; 48 | continue; 49 | } 50 | G[v1][v2]=cost; 51 | 52 | cout<<"\nWant to continue? [y/Y]"; 53 | cin>>choice; 54 | 55 | }while(choice=='y' || choice=='Y'); 56 | } 57 | /////////////////////////////////////////////////////////////////////////////////// 58 | void DirAdj_Matrix::DisplayMatrix() 59 | { 60 | for(int i=0;i>ch; 77 | switch(ch) 78 | { 79 | case 1: 80 | obj.CreateMatrix(); 81 | break; 82 | case 2: 83 | obj.DisplayMatrix(); 84 | break; 85 | case 3: 86 | return 0; 87 | default: 88 | cout<<"\nPlease enter correct choice!!"; 89 | break; 90 | } 91 | }while(1); 92 | } 93 | 94 | /////////////////////////////////////////////////////////////////////////////////// 95 | //End of program 96 | ///////////////////////////////////////////////////////////////////////////////////1 -------------------------------------------------------------------------------- /Graph/JustPrint_AdjancencyList.cpp: -------------------------------------------------------------------------------- 1 | //Given the adjacency list of a bidirectional graph. Your task is to print the adjacency list for each vertex. 2 | 3 | // Function to print graph 4 | // adj: array of vectors to represent graph 5 | // V: number of vertices 6 | void printGraph(vector adj[], int V) 7 | { 8 | for(int i=0;i "< 8 | #define MAX 30 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | class node 13 | { 14 | public: 15 | int data; 16 | node* next; 17 | }*arr[MAX]; 18 | 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | class UndirAdj_List:public node 21 | { 22 | int v,v1,v2; 23 | public: 24 | UndirAdj_List() 25 | { 26 | v=v1=v2=0; 27 | } 28 | void CreateList(); 29 | void DisplayList(); 30 | }; 31 | /////////////////////////////////////////////////////////////////////////////////// 32 | void UndirAdj_List::CreateList() 33 | { 34 | int choice; 35 | cout<<"\nEnter total vertices: "; 36 | cin>>v; 37 | do{ 38 | cout<<"\nEnter both vertices: "; 39 | cin>>v1>>v2; 40 | if(v1>=v || v2>=v) 41 | { 42 | cout<<"\nEdge not present in graph!"; 43 | continue; 44 | } 45 | node* ptr1=new node; 46 | ptr1->data=v2; 47 | ptr1->next=NULL; 48 | 49 | if(arr[v1]==NULL) 50 | { 51 | arr[v1]=ptr1; 52 | } 53 | else 54 | { 55 | node* temp=arr[v1]; 56 | while(temp->next!=NULL) 57 | { 58 | temp=temp->next; 59 | } 60 | temp->next=ptr1; 61 | } 62 | //undirected mechanism 63 | node* ptr2=new node; 64 | ptr2->data=v1; 65 | ptr2->next=NULL; 66 | 67 | if(arr[v2]==NULL) 68 | { 69 | arr[v2]=ptr2; 70 | } 71 | else 72 | { 73 | node* temp=arr[2]; 74 | while(temp->next!=NULL) 75 | { 76 | temp=temp->next; 77 | } 78 | temp->next=ptr2; 79 | } 80 | cout<<"\nWant to continue?[y/Y]"; 81 | cin>>choice; 82 | }while(choice=='Y' || choice=='y'); 83 | } 84 | /////////////////////////////////////////////////////////////////////////////////// 85 | void UndirAdj_List::DisplayList() 86 | { 87 | for(int i=0;i>"<data; 94 | temp=temp->next; 95 | } 96 | } 97 | } 98 | /////////////////////////////////////////////////////////////////////////////////// 99 | int main() 100 | { 101 | UndirAdj_List obj; 102 | int ch; 103 | do{ 104 | cout<<"\n\nSelect: \n1] Create Adjacency List of Undirected graph\n2] Display Adjacency List of UnDirected graph\n3] Exit\n"; 105 | cin>>ch; 106 | switch(ch) 107 | { 108 | case 1: 109 | obj.CreateList(); 110 | break; 111 | case 2: 112 | obj.DisplayList(); 113 | break; 114 | case 3: 115 | return 0; 116 | default: 117 | cout<<"\nPlease enter correct choice!!"; 118 | break; 119 | } 120 | }while(1); 121 | } 122 | 123 | /////////////////////////////////////////////////////////////////////////////////// 124 | //End of program // 125 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Graph/UndirectedAdjacencyMatrix_Graph.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :Graph: Undirected Adjacency Matrix // 4 | //Language :C++ (Object Oriented Approach) // 5 | /////////////////////////////////////////////////////////////////////////////////// 6 | 7 | #include 8 | #define MAX 50 9 | using namespace std; 10 | 11 | /////////////////////////////////////////////////////////////////////////////////// 12 | class UndirAdj_Matrix 13 | { 14 | int v,v1,v2,flag,cost; 15 | int G[MAX][MAX]; 16 | public: 17 | //constructor: initializing the member variables 18 | UndirAdj_Matrix() 19 | { 20 | v=v1=v2=0; 21 | 22 | for(int i=0;i>v; 40 | do{ 41 | cout<<"\nEnter edges: (both vertices): "; 42 | cin>>v1>>v2; 43 | cout<<"\nEnter cost: "; 44 | cin>>cost; 45 | if(v1>= v|| v2>=v) 46 | { 47 | cout<<"\nEdge not Valid"; 48 | continue; 49 | } 50 | G[v1][v2]=cost; 51 | G[v2][v1]=cost; 52 | 53 | cout<<"\nWant to continue? [y/Y]"; 54 | cin>>choice; 55 | 56 | }while(choice=='y' || choice=='Y'); 57 | } 58 | /////////////////////////////////////////////////////////////////////////////////// 59 | void UndirAdj_Matrix::DisplayMatrix() 60 | { 61 | for(int i=0;i>ch; 78 | switch(ch) 79 | { 80 | case 1: 81 | obj.CreateMatrix(); 82 | break; 83 | case 2: 84 | obj.DisplayMatrix(); 85 | break; 86 | case 3: 87 | return 0; 88 | default: 89 | cout<<"\nPlease enter correct choice!!"; 90 | break; 91 | } 92 | }while(1); 93 | } 94 | 95 | /////////////////////////////////////////////////////////////////////////////////// 96 | //End of program 97 | ///////////////////////////////////////////////////////////////////////////////////1 -------------------------------------------------------------------------------- /Linked_Lists/Check_Circularll.cpp: -------------------------------------------------------------------------------- 1 | //Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. An empty linked list is considered as circular. 2 | //solution 3 | 4 | bool isCircular(Node *head) 5 | { 6 | if(head==NULL || head->next==head) return true; 7 | Node* temp=head; 8 | while(temp->next!=NULL && temp->next!=head) 9 | temp=temp->next; 10 | 11 | return (temp->next==head)?true:false; 12 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteAtPos_Circularll.cpp: -------------------------------------------------------------------------------- 1 | //Given a linked list of size n, you have to delete the node at position pos of the linked list and return the new head. The position of initial node is 1. 2 | //The tail of the circular linked list is connected to the head using next pointer. 3 | 4 | //solution 5 | Node * deleteAtPosition(Node *head,int pos) 6 | { 7 | if(pos==1) 8 | { 9 | if(head->next==head) 10 | return NULL; 11 | Node *temp=head->next; 12 | head->next=head->next->next; 13 | delete(temp); 14 | return head; 15 | } 16 | else 17 | { 18 | Node *temp=head; 19 | Node *cur; 20 | for(int i=0;inext; 21 | cur=temp->next; 22 | temp->next=temp->next->next; 23 | delete(cur); 24 | return head; 25 | } 26 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteHead_Circularll.cpp: -------------------------------------------------------------------------------- 1 | //Given a circular linked list of size n, you have to delete the head of the linked list and return the new head. 2 | //In the circular linked list the tail of the list is connected to the head using the next pointer. 3 | 4 | //Note: Please also set the next of the original head to null. 5 | //solution 6 | Node * deleteHead(Node *head) 7 | { 8 | if(head==NULL) 9 | return NULL; 10 | 11 | Node* temp=head; 12 | Node* temp1=head; 13 | while(temp->next!=head) temp=temp->next; 14 | temp->next=temp1->next; 15 | head=temp1->next; 16 | temp1->next=NULL; 17 | return head; 18 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteHead_Doublyll.cpp: -------------------------------------------------------------------------------- 1 | //Given a doubly linked list of size n, you have to delete the head of the linked list and return the new head. 2 | //Note: Please set the previous of new head to null, and set the next of old head to null, and then delete the old head. 3 | 4 | Node *deleteHead(Node * head) 5 | { 6 | if(head==NULL) return NULL; 7 | Node* temp=head; 8 | 9 | head=temp->next; 10 | head->prev=NULL; 11 | 12 | delete temp; 13 | return head; 14 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteHead_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Given a linked list of size n, you have to delete the head of the linked list and return the new head. 2 | //Note: Please also set the next of the original head to null. 3 | 4 | Node* deleteHead(Node *head) 5 | { 6 | Node* temp=head->next; 7 | head->next=NULL; 8 | 9 | delete head; 10 | 11 | return temp; 12 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteTail_Circular.cpp: -------------------------------------------------------------------------------- 1 | //Given a circular linked list of size n, you have to delete the tail (last element) in the linked list. 2 | //In a circular linked list, the tail is connect to the head using the next pointer. 3 | 4 | //solution 5 | Node * deleteTail(Node * head) 6 | { 7 | if(head==NULL) return NULL; 8 | 9 | Node* temp=head; 10 | while(temp->next->next!=head) temp=temp->next; 11 | temp->next=head; 12 | return head; 13 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteTail_Doublyll.cpp: -------------------------------------------------------------------------------- 1 | //Given a doubly linked list of size n, you have to delete the tail (last element) in the linked list. 2 | 3 | Node *deleteTail(Node * head) 4 | { 5 | if(head==NULL) return NULL; 6 | 7 | Node* temp=head; 8 | 9 | while(temp->next->next) temp=temp->next; 10 | 11 | delete temp->next; 12 | temp->next=NULL; 13 | 14 | return head; 15 | } -------------------------------------------------------------------------------- /Linked_Lists/DeleteTail_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Given a linked list of size n, you have to delete the tail (last element) in the linked list. 2 | //solution 3 | 4 | Node* deleteTail(Node *head) 5 | { 6 | //empty linked list 7 | if(head==NULL) 8 | return head; 9 | 10 | Node* temp=head; 11 | while(temp->next->next!=NULL) 12 | temp=temp->next; 13 | 14 | delete temp->next; 15 | temp->next=NULL; 16 | 17 | return head; 18 | } -------------------------------------------------------------------------------- /Linked_Lists/Display_DoublyCircularll.cpp: -------------------------------------------------------------------------------- 1 | void displayList(Node *head) 2 | { 3 | vectorv; 4 | Node* temp=head; 5 | while(temp->next!=head) 6 | { 7 | v.push_back(temp->data); 8 | cout<data<<" "; 9 | temp=temp->next; 10 | } 11 | cout<data<<" "; 12 | v.push_back(temp->data); 13 | cout<=0;i--) 15 | cout< displayList(Node *head) 5 | { 6 | vectorv; 7 | if(head==NULL) 8 | return v; 9 | Node* temp=head; 10 | while(temp) 11 | { 12 | v.push_back(temp->data); 13 | temp=temp->next; 14 | } 15 | return v; 16 | 17 | } -------------------------------------------------------------------------------- /Linked_Lists/Display_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Given a singly linked list of integers. The task is to display the linked list. 2 | //solution code snippet 3 | 4 | 5 | vector displayList(Node *head) 6 | { 7 | vectorv; 8 | //empty linked list 9 | if(head==NULL) 10 | return v; 11 | Node* temp=head; 12 | while(temp!=NULL) 13 | { 14 | int val=temp->data; 15 | v.push_back(val); 16 | temp=temp->next; 17 | } 18 | return v; 19 | } -------------------------------------------------------------------------------- /Linked_Lists/InsertAtPos_Circular.cpp: -------------------------------------------------------------------------------- 1 | //You are given a circular linked list of size N. You need to insert an element data just after the given position pos. 2 | //The position of first element is 1. If the given position is greater than N, then don't insert anything as it is not possible. 3 | //As the given linked list is circular, it means that the tail is connected to the head of the lis 4 | 5 | //solution 6 | void insertAtPosition(Node *head, int pos, int data) 7 | { 8 | Node *temp=new Node(data); 9 | Node *cur=head; 10 | int i; 11 | if(pos==1) 12 | { 13 | if(head==NULL) 14 | return ; 15 | if(head->next==head) 16 | { 17 | temp->next=head; 18 | head->next=temp; 19 | return ; 20 | } 21 | temp->next=head->next; 22 | head->next=temp; 23 | } 24 | else{ 25 | for(i=0;inext; 27 | if(cur==head){ 28 | return ; 29 | } 30 | 31 | } 32 | temp->next=cur->next; 33 | cur->next=temp; 34 | } 35 | } -------------------------------------------------------------------------------- /Linked_Lists/InsertHead_Circularll.cpp: -------------------------------------------------------------------------------- 1 | //Given a circular linked list of size N, you need to insert an element data before the head and make it the new head. The tail of the linked list is connected to head. 2 | 3 | //solution 4 | Node *insertInHead(Node * head, int data) 5 | { 6 | Node* newn=new Node(data); 7 | if(head==NULL) 8 | { 9 | head=newn; 10 | newn->next=head; 11 | } 12 | 13 | else 14 | { 15 | Node* temp=head; 16 | do{ 17 | temp=temp->next; 18 | }while(temp->next!=head); 19 | newn->next=head; 20 | head=newn; 21 | temp->next=head; 22 | } 23 | return head; 24 | } -------------------------------------------------------------------------------- /Linked_Lists/InsertHead_Doubly;;.cpp: -------------------------------------------------------------------------------- 1 | //Given a doubly linked list of size n, you need to insert an element data before the head and make it the new head. 2 | 3 | //solution 4 | Node *insertInHead(Node * head, int data) 5 | { 6 | Node* newn = new Node(data); 7 | 8 | if(head==NULL) 9 | head=newn; 10 | else 11 | { 12 | Node* temp=head; 13 | newn->next=head; 14 | head->prev=newn; 15 | head=newn; 16 | } 17 | return head; 18 | } -------------------------------------------------------------------------------- /Linked_Lists/InsertTail_Circularll.cpp: -------------------------------------------------------------------------------- 1 | //Given a circular linked list of size N, you need to insert an element data after the tail. 2 | //The tail of the linked list is connected to head. 3 | 4 | //solution 5 | Node *insertInTail(Node * head, int data) 6 | { 7 | Node* newn=new Node(data); 8 | if(head==NULL) 9 | { 10 | head=newn; 11 | newn->next=head; 12 | } 13 | else 14 | { 15 | Node* temp=head; 16 | 17 | while(temp->next!=head) 18 | { 19 | temp=temp->next; 20 | } 21 | temp->next=newn; 22 | newn->next=head; 23 | } 24 | return head; 25 | } -------------------------------------------------------------------------------- /Linked_Lists/InsertTail_Doublyll.cpp: -------------------------------------------------------------------------------- 1 | //Given a doubly linked list of size n, you need to insert an element data after the tail. 2 | //solution 3 | 4 | Node *insertInTail(Node * head, int data) 5 | { 6 | Node* newn=new Node(data); 7 | 8 | if(head==NULL) 9 | head=newn; 10 | else 11 | { 12 | Node* temp=head; 13 | while(temp->next!=NULL) 14 | temp=temp->next; 15 | 16 | temp->next=newn; 17 | newn->prev=temp; 18 | } 19 | return head; 20 | } -------------------------------------------------------------------------------- /Linked_Lists/Insertion_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Insertion at front and end of Linked List 2 | //solution 3 | 4 | //function inserts the data in front of the list 5 | Node *insertAtBegining(Node *head, int newData) { 6 |    Node *newn=new Node(newData); 7 |    if(head==NULL) 8 |         head=newn; 9 |       else 10 |    { 11 |        newn->next=head; 12 |        head=newn; 13 |    } 14 |    return head; 15 | } 16 | 17 | // function appends the data at the end of the list 18 | Node *insertAtEnd(Node *head, int newData)  { 19 |   Node *newn=new Node(newData); 20 |   if(head==NULL) 21 |      head=newn; 22 |    else 23 |    { 24 |        Node* temp=head; 25 |        while(temp->next!=NULL) 26 |            temp=temp->next; 27 | 28 |        temp->next=newn; 29 |    } 30 |    return head; 31 | } -------------------------------------------------------------------------------- /Linked_Lists/JoinTwo_LinkedzLists.cpp: -------------------------------------------------------------------------------- 1 | //Given two linked lists of size n1 and n2 respectively, 2 | //you have to join the head of second list to the tail of first so that we can traverse both the lists using head of 1st list. 3 | 4 | //solution 5 | Node * joinTheLists(Node * head1, Node * head2) 6 | { 7 | if(head1==NULL) 8 | head1=head2; 9 | 10 | Node* temp=head1; 11 | 12 | while(temp->next!=NULL) 13 | temp=temp->next; 14 | 15 | temp->next=head2; 16 | 17 | return head1; 18 | } -------------------------------------------------------------------------------- /Linked_Lists/Length_Circularll.cpp: -------------------------------------------------------------------------------- 1 | 2 | //solution 3 | int length(Node* head) 4 | { 5 | int ct=0; 6 | if(head==NULL) return ct; 7 | 8 | Node* temp=head; 9 | do{ 10 | ct++; 11 | temp=temp->next; 12 | }while(temp!=head); 13 | 14 | return ct; 15 | } -------------------------------------------------------------------------------- /Linked_Lists/Max&Min_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Given a singly linked list of N elements. The task is to find the maximum and minimum element. 2 | //solution 3 | 4 | //to find max element 5 | int maximum(Node *head) 6 | { 7 | vectorv; 8 | int max=0; 9 | if(head==NULL) 10 | return max; 11 | Node* temp=head; 12 | while(temp!=NULL) 13 | { 14 | v.push_back(temp->data); 15 | temp=temp->next; 16 | } 17 | return *max_element(v.begin(),v.end()); 18 | } 19 | 20 | //to find min element 21 | int minimum(Node *head) 22 | { 23 | vectorv; 24 | int max=0; 25 | if(head==NULL) 26 | return max; 27 | Node* temp=head; 28 | while(temp!=NULL) 29 | { 30 | v.push_back(temp->data); 31 | temp=temp->next; 32 | } 33 | return *min_element(v.begin(),v.end()); 34 | } -------------------------------------------------------------------------------- /Linked_Lists/MiddlePosition.cpp: -------------------------------------------------------------------------------- 1 | int getMiddle(Node *head) 2 | { 3 | if(!head) return 0; 4 | Node* slow = head; 5 | Node* fast = head->next; 6 | while(fast!=NULL) 7 | { 8 | fast=fast->next; 9 | if(fast!=NULL) 10 | fast=fast->next; 11 | slow=slow->next; 12 | } 13 | return slow->data; 14 | } 15 | -------------------------------------------------------------------------------- /Linked_Lists/NodesSum_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Given a singly linked list of size N. The task is to sum the elements of the linked list. 2 | //solution 3 | 4 | int sumOfElements(Node *head) 5 | { 6 | int sum=0; 7 | if(head==NULL) 8 | return 0; 9 | Node* temp=head; 10 | while(temp!=NULL) 11 | { 12 | sum=sum+temp->data; 13 | temp=temp->next; 14 | } 15 | return sum; 16 | } -------------------------------------------------------------------------------- /Linked_Lists/Searching_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | //Search given element in Linked list 2 | //solution 3 | 4 | bool searchLinkedList(Node *head, int x) 5 | { 6 |    int pos=1; 7 |    Node* temp=head; 8 |    while(temp!=NULL) 9 |    { 10 |        if(temp->data==x) 11 |        return pos; 12 |        else 13 |        { 14 |            pos++; 15 |            temp=temp->next; 16 |        } 17 |    } 18 |     return 0;    19 | } -------------------------------------------------------------------------------- /Mathematics/AbsoluteValue.cpp: -------------------------------------------------------------------------------- 1 | ///solution: https://practice.geeksforgeeks.org/problems/absolute-value/1/?track=ppc-mathematics&batchId=221 2 | 3 | //You are given an interger I. You need to print the absolute value of the interger I. 4 | 5 | int absolute(int I) { 6 | 7 | return abs(I); 8 | } -------------------------------------------------------------------------------- /Mathematics/AddWithoutPlusOp.cpp: -------------------------------------------------------------------------------- 1 | // Given two integers a and b. 2 | // Find the sum of two numbers without using arithmetic operators. 3 | 4 | class Solution 5 | { 6 | public: 7 | int sum(int x , int y) 8 | { 9 | // if(a>b) 10 | // { 11 | // while(b--) 12 | // a++; 13 | // return a; 14 | // } 15 | // else 16 | // { 17 | // while(a--) 18 | // b++; 19 | // return b; 20 | // } 21 | while (y != 0) 22 | { 23 | // carry now contains common 24 | //set bits of x and y 25 | int carry = x & y; 26 | 27 | // Sum of bits of x and y where at 28 | //least one of the bits is not set 29 | x = x ^ y; 30 | 31 | // Carry is shifted by one so that adding 32 | // it to x gives the required sum 33 | y = carry << 1; 34 | } 35 | return x; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /Mathematics/AdditionUnderModulo.cpp: -------------------------------------------------------------------------------- 1 | ///You are given two numbers a and b. You need to find the sum of a and b under modulo M (M = 109+7) 2 | 3 | int sumUnderModulo(long long a,long long b) 4 | { 5 | int M=1000000007; 6 | return ((a%M + b%M)%M); 7 | } -------------------------------------------------------------------------------- /Mathematics/AmicablePair.cpp: -------------------------------------------------------------------------------- 1 | //Solution: 2 | 3 | class Solution { 4 | public: 5 | int isAmicable(int A , int B) { 6 | if(A==B) return 0; 7 | int sum1=1,sum2=1; 8 | for(int i=2;i0 && i<18) 12 | { 13 | if(n-roman[i]>=0) 14 | { 15 | n=n-roman[i]; 16 | s=s+ans[i]; 17 | } 18 | else 19 | i++; 20 | } 21 | return s; 22 | } -------------------------------------------------------------------------------- /Mathematics/DecimalToBin.cpp: -------------------------------------------------------------------------------- 1 | void toBinary(int N) 2 | { 3 | int bin=0,i=0; 4 | while(N>0) 5 | { 6 | int p = N%2; 7 | bin += p*pow(10,i); 8 | 9 | i++; 10 | N=N/2; 11 | } 12 | printf("%d",bin); 13 | } -------------------------------------------------------------------------------- /Mathematics/DisariumNo.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N, find if it is Disarium or not. A number is called Disarium if sum of its digits powered with their respective positions is equal to the number itself. 2 | // Output 1 if it's Disarium, and 0 if not. 3 | 4 | // SOlution 5 | int isDisarium(int N) { 6 | int i=floor(log10(N)+1); 7 | int n=N; 8 | int sum=0; 9 | while(n>0) 10 | { 11 | sum+=pow(n%10,i); 12 | i--; 13 | n=n/10; 14 | } 15 | if(sum==N) 16 | return 1; 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Mathematics/Exactly3Divisors.cpp: -------------------------------------------------------------------------------- 1 | //equal to N have numbers of divisors exactly equal to 3. 2 | 3 | bool isPrime(int n) { 4 | int x=pow(n,0.5); 5 | while(x>1){ 6 | if(n%x==0) 7 | return false; 8 | x--; 9 | } 10 | return true; 11 | } 12 | int exactly3Divisors(int N) 13 | { 14 | //Your code here 15 | int ct = 0; 16 | for(int i=2;i*i<=N;i++) 17 | { 18 | if(isPrime(i)) 19 | ct++; 20 | } 21 | return ct; 22 | } -------------------------------------------------------------------------------- /Mathematics/GPTerm.cpp: -------------------------------------------------------------------------------- 1 | //solution: https://practice.geeksforgeeks.org/problems/gp-term/1/?track=ppc-mathematics&batchId=221 2 | //Given the first 2 terms A and B of a Geometric Series. The task is to find the Nth term of the series. 3 | 4 | double termOfGP(int A,int B,int N) 5 | { 6 | double ratio=(double)B/(double)A; 7 | double ans=pow(ratio,N-1); 8 | return A*ans; 9 | } -------------------------------------------------------------------------------- /Mathematics/LargeFactorial.cpp: -------------------------------------------------------------------------------- 1 | // Given an integer, the task is to find factorial of the number. 2 | 3 | //Using boost library in c++ for larger datatype 4 | #include 5 | #include 6 | using namespace std; 7 | namespace mp=boost::multiprecision; 8 | mp::cpp_int fact(int n) 9 | { 10 | if(n<=1) return 1; 11 | return n*fact(n-1); 12 | } 13 | int main() { 14 | int t; 15 | cin>>t; 16 | while(t--) 17 | { 18 | int n; 19 | cin>>n; 20 | cout< 4 | using namespace std; 5 | int main() { 6 | int t; 7 | cin>>t; 8 | while(t--) 9 | { 10 | int n; 11 | cin>>n; 12 | int arr[n]; 13 | for(int i=0;i>arr[i]; 16 | } 17 | for(int i=1;i<=n;i++) 18 | { 19 | if(count(arr,arr+n,i) == 0) 20 | { 21 | cout<polyMultiply(int Arr1[], int Arr2[], int M, int N) 7 | { 8 | vectorv(M+N-1); 9 | 10 | for(int i=0;i1) 7 | { 8 | if(N%x==0) 9 | return false; 10 | x--; 11 | } 12 | return true; 13 | } -------------------------------------------------------------------------------- /Mathematics/RemainderWith7.cpp: -------------------------------------------------------------------------------- 1 | // Given a number as string(n) , 2 | // find the remainder of the number whe it is divided by 7 3 | 4 | //Solution 5 | int remainderWith7(string n) 6 | { 7 | int x=0; 8 | for(int i=0;i primeDivision(int N){ 15 | vectorv; 16 | for(int i=2;i<=N;i++) 17 | { 18 | if(isPrime(i)) 19 | { 20 | if(isPrime(N-i)) 21 | { 22 | v.emplace_back(i); 23 | v.emplace_back(N-i); 24 | break; 25 | } 26 | } 27 | } 28 | return v; 29 | } 30 | }; -------------------------------------------------------------------------------- /Mathematics/ReverseCoding.cpp: -------------------------------------------------------------------------------- 1 | // Sherlock Being tired with the usual coding rounds started growing his interest towards reverse coding when he won the event in his college symposium. 2 | // He wondered if his friend has the brain to quickly identify the pattern and verify if his inputs are correct or not. 3 | // From the example portion given below, where you will be given a number(n) and its output m, Using this find the pattern. 4 | // Your task is that from the pattern you identified above, You have to tell if for the given n whether the given m is the correct answer or not. 5 | 6 | // Example to identify the pattern : 7 | // Input Output 8 | // 10 55 9 | // 20 210 10 | // 5 15 11 | // 0 0 12 | // 1 1 13 | // 2 3 14 | 15 | //Solution: 16 | class Solution { 17 | public: 18 | int revCoding(int n, int m) { 19 | int p=(n*(n+1))/2; 20 | return p==m?1:0; 21 | } 22 | }; -------------------------------------------------------------------------------- /Mathematics/SmallestDivisibleNo.cpp: -------------------------------------------------------------------------------- 1 | //solution: https://practice.geeksforgeeks.org/problems/smallest-divisible-number/1/?track=ppc-mathematics&batchId=221 2 | 3 | //Given a number n the task is to complete the function which returns an integer denoting the smallest number evenly divisible by each number from 1 to n. 4 | 5 | long long getSmallestDivNum(long long n) 6 | { 7 | long long result=1; 8 | for(long long int i=1;i<=n;i++) 9 | { 10 | result=(result*i)/__gcd(result,i); 11 | } 12 | return result; 13 | } 14 | -------------------------------------------------------------------------------- /Mathematics/SquareNoWithoutInbuiltFun.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int n) { 4 | for(unsigned int i=1;i*i<=n;i++) 5 | { 6 | if(n%i == 0 && (n/i)==i) 7 | { 8 | return true; 9 | } 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /Mathematics/StrongNumber.cpp: -------------------------------------------------------------------------------- 1 | // Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Given a number, 2 | // The task is to check if it is a Strong Number or not. 3 | 4 | //Solution: 5 | class Solution 6 | { 7 | public: 8 | public: 9 | int factorial(int n) 10 | { 11 | if(n > 1) 12 | return n * factorial(n - 1); 13 | else 14 | return 1; 15 | } 16 | int is_StrongNumber(int n) 17 | { 18 | int sum = 0,N=n; 19 | while(n>0) 20 | { 21 | int p = n%10; 22 | sum += factorial(p); 23 | n /= 10; 24 | } 25 | 26 | if(sum == N) 27 | return 1; 28 | return 0; 29 | } 30 | }; -------------------------------------------------------------------------------- /Mathematics/Subtraction2Nos.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedSubtraction(int A,int B){ 4 | int ct=0; 5 | if(A==B) 6 | return 1; 7 | 8 | while(A!=0 && B!=0) 9 | { 10 | if(A>B) 11 | A=A-B; 12 | else 13 | B=B-A; 14 | ct++; 15 | } 16 | return ct; 17 | } 18 | }; -------------------------------------------------------------------------------- /Mathematics/SumOfDigitModified.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N. Check whether it is a magic number or not. 2 | // Note:- A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition and the single digit comes out to be 1. 3 | 4 | // Solution: 5 | class Solution { 6 | public: 7 | int getsum(int n) 8 | { 9 | int sum=0; 10 | while(n>0) 11 | { 12 | sum+=n%10; 13 | n=n/10; 14 | } 15 | return sum; 16 | } 17 | int isMagic(int N) { 18 | int sum=getsum(N);//6 19 | while(N>0){ 20 | if(sum == 1) 21 | return 1; 22 | sum=getsum(sum); 23 | N=N/10; 24 | } 25 | return 0; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Mathematics/SumOfDigitsDivisible.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N. 2 | // Check whether it is divisible by the sum of its digits or not. 3 | 4 | // Solution: 5 | class Solution { 6 | public: 7 | int isDivisible(int N) { 8 | int sum=0; 9 | int n=N; 10 | while(n>0) 11 | { 12 | sum+=n%10; 13 | n/=10; 14 | } 15 | if(N%sum == 0) 16 | return 1; 17 | return 0; 18 | } 19 | }; -------------------------------------------------------------------------------- /Must_Do_Interview_Preparation/30-Days-Of-Code/KPairs.cpp: -------------------------------------------------------------------------------- 1 | //Solution 2 | class Solution{ 3 | 4 | public: 5 | int find_min(int a[], int n, int k) { 6 | int s = 0, pair = 0; 7 | for (int i = 0; i < n; i++) 8 | { 9 | pair = pair + (a[i] / 2); 10 | if (a[i] % 2 == 0) 11 | s += (a[i] - 2) / 2; 12 | else 13 | s += (a[i] - 1) / 2; 14 | } 15 | if (k > pair) return -1; 16 | if (k <= s) return 2 * (k - 1) + n + 1; 17 | return 2 * s + n + (k - s); 18 | } 19 | 20 | }; -------------------------------------------------------------------------------- /Must_Do_Interview_Preparation/30-Days-Of-Code/MinSum.cpp: -------------------------------------------------------------------------------- 1 | //solution -------------------------------------------------------------------------------- /Must_Do_Interview_Preparation/30-Days-Of-Code/NthNaturalNo.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given a positive integer N. 3 | You have to find Nth natural number after removing all the numbers containing digit 9. 4 | */ 5 | 6 | class Solution{ 7 | public: 8 | long long findNth(long long N) 9 | { 10 | long long ct=1,ans=0; 11 | while(N!=0) 12 | { 13 | ans+=(ct*(N%9)); 14 | N=N/9; 15 | ct=ct*10; 16 | } 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /Must_Do_Interview_Preparation/30-Days-Of-Code/RearrangeGeeks&Classmates.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Geek and his classmates are playing a prank on their Computer Science teacher. They change places every time the teacher turns to look at the blackboard. 3 | Each of the N students in the class can be identified by a unique roll number X and each desk has a number i associated with it. Only one student can sit on one desk. 4 | Each time the teacher turns her back, a student with roll number X sitting on desk number i gets up and takes the place of the student with roll number i. 5 | If the current position of N students in the class is given to you in an array, such that i is the desk number and a[i] is the roll number of the student sitting on the desk, 6 | */ 7 | 8 | //Solution 9 | class Solution { 10 | public: 11 | void prank(long long a[], int n){ 12 | vectorv; 13 | for(long long int i=0;ilt) 20 | { 21 | lt=lt+a[j]; 22 | j--; 23 | } 24 | else if(st& array, int n) { 7 | 8 | sort(array.begin(),array.end()); 9 | for(int i=1;i<=n;i++) 10 | { 11 | if(array[i-1]==i && array[i-1]next!=NULL){ 9 | int t=temp->data; 10 | temp->data=temp->next->data; 11 | temp->next->data=t; 12 | temp=temp->next->next; 13 | } 14 | else 15 | return head; 16 | } 17 | return head; 18 | } -------------------------------------------------------------------------------- /Prime_Numbers/LargestPrimeFactor.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N, 2 | // the task is to find the largest prime factor of that number. 3 | 4 | class Solution 5 | { 6 | public: 7 | int isPrime(int no) 8 | { 9 | if (no == 2) 10 | return 1; 11 | if (no % 2 == 0 || no == 1) 12 | return 0; 13 | int flag = 0; 14 | for (int i = 2; i < no; i++) 15 | { 16 | if (no % i == 0) 17 | flag = 1; 18 | } 19 | return flag == 1 ? 0 : 1; 20 | } 21 | long long int largestPrimeFactor(int no) 22 | { 23 | 24 | vector v; 25 | for (int i = 1; i <= no; i++) 26 | { 27 | if (no % i == 0) 28 | { 29 | if (isPrime(i)) 30 | v.emplace_back(i); 31 | } 32 | } 33 | 34 | return v[v.size() - 1]; 35 | } 36 | }; -------------------------------------------------------------------------------- /Prime_Numbers/LeastPrimeFactor.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N, find least prime factors for all numbers from 1 to N. The least prime factor of an integer N is the smallest prime number that divides it. 2 | // Note : The least prime factor of all even numbers is 2. A prime number is its own least prime factor (as well as its own greatest prime factor).1 needs to be printed for 1 3 | 4 | //Solution: 5 | class Solution { 6 | public: 7 | bool isPrime(int n) 8 | { 9 | if(n==2) return true; 10 | if(n%2==0 || n==1) return false; 11 | for(int i=2;i leastPrimeFactor(int n) { 19 | vectorv,b; 20 | if(n>=1) v.emplace_back(1); 21 | if(n>=2) v.emplace_back(2); 22 | if(n>=3) v.emplace_back(3); 23 | if(n>=4) v.emplace_back(2); 24 | if(n>=5) 25 | { 26 | for(int i=5;i<=n;i++) 27 | { 28 | int j=2; 29 | while(i%j!=0) 30 | { 31 | j++; 32 | } 33 | if(isPrime(j)) 34 | v.emplace_back(j); 35 | } 36 | } 37 | for(int i=v.size();i>=0;i--) 38 | { 39 | v[i]=v[i-1]; 40 | } 41 | return v; 42 | } 43 | }; 44 | 45 | /////ref: gfg -------------------------------------------------------------------------------- /Prime_Numbers/PrimeFactors.cpp: -------------------------------------------------------------------------------- 1 | // Given a number N. 2 | // Find its unique prime factors in increasing order. 3 | 4 | //solution: 5 | class Solution{ 6 | public: 7 | bool isPrime(int n) 8 | { 9 | if(n==2) return true; 10 | if(n==1 || n%2==0) return false; 11 | for(int i=2;i AllPrimeFactors(int N) { 18 | vectorv; 19 | if(isPrime(N)) 20 | { 21 | v.emplace_back(N); 22 | return v; 23 | } 24 | for(int i=2;i 4 | using namespace std; 5 | 6 | bool isPrime(int n) 7 | { 8 | if(n==2) return true; 9 | if(n%2==0) return false; 10 | for(int i=2;i>t; 20 | while(t--) 21 | { 22 | int n; 23 | cin>>n; 24 | vectorv; 25 | 26 | for(int i=2;i<=n;i++) 27 | { 28 | int ct=0; 29 | if(n%i == 0) 30 | { 31 | if(isPrime(i)) 32 | { 33 | while(n%i == 0) 34 | { 35 | n=n/i; 36 | ct++; 37 | } 38 | v.push_back(i); 39 | v.push_back(ct); 40 | } 41 | } 42 | } 43 | for(int i=0;i0) 24 | { 25 | int p=N%10; 26 | if(isPrime(p)) 27 | { 28 | sum+=p; 29 | } 30 | N=N/10; 31 | } 32 | return sum; 33 | } 34 | }; -------------------------------------------------------------------------------- /Queue/CircularQueue.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :Circular Queue // 4 | //Language :C++ (Object Oriented Approach) // 5 | //Approach :Dynamic Approach using Linked Lists. // 6 | //Functions:First In First Out (FIFO) fashion of inserting and removing elements // 7 | // in and from the queue. // 8 | /////////////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | // Slightly different approach ,with 'void' return type // 15 | /////////////////////////////////////////////////////////////////////////////////// 16 | 17 | typedef struct node 18 | { 19 | int data; 20 | struct node* next; 21 | }NODE,*PNODE; 22 | /////////////////////////////////////////////////////////////////////////////////// 23 | 24 | class CircularQueue 25 | { 26 | PNODE head,tail; //maintaining two pointers 27 | //there is no as such termination in CLL unlike linear linkedlist 28 | int ct; 29 | public: 30 | CircularQueue(); //constructor 31 | ~CircularQueue(); //To deallocate the used memory 32 | 33 | void Insert(int); 34 | 35 | void Remove(); 36 | 37 | void DisplayQueue(); //to Display the Queue 38 | int Count(); //Display total number of nodes present in Linked List 39 | }; 40 | /////////////////////////////////////////////////////////////////////////////////// 41 | //Initializing memory 42 | CircularQueue::CircularQueue() 43 | { 44 | head=tail=NULL; 45 | ct=0; 46 | } 47 | /////////////////////////////////////////////////////////////////////////////////// 48 | //To get total number of nodes/elements present in the Linked List 49 | int CircularQueue::Count() 50 | { 51 | ct=0; 52 | PNODE temp=head; 53 | while(temp->next!=head) 54 | { 55 | ct++; 56 | temp=temp->next; 57 | } 58 | ct++; 59 | return ct; 60 | } 61 | /////////////////////////////////////////////////////////////////////////////////// 62 | //Display entire Linked List 63 | void CircularQueue::DisplayQueue() 64 | { 65 | PNODE temp=head; 66 | while(temp->next!=head) 67 | { 68 | cout<data<<" "; 69 | temp=temp->next; 70 | } 71 | cout<data<<" "; 72 | } 73 | /////////////////////////////////////////////////////////////////////////////////// 74 | //Inserting element at the end of Linked List 75 | void CircularQueue::Insert(int n) 76 | { 77 | PNODE temp=head; 78 | PNODE newn=new node; 79 | newn->data=n; 80 | newn->next=NULL; 81 | if(head==NULL && tail==NULL) 82 | { 83 | head=newn; 84 | tail=newn; 85 | } 86 | else 87 | { 88 | tail->next=newn; 89 | tail=tail->next; 90 | } 91 | tail->next=head; 92 | 93 | } 94 | /////////////////////////////////////////////////////////////////////////////////// 95 | //Delete first element of Linked List 96 | void CircularQueue::Remove() 97 | { 98 | //Empty Linked list 99 | if(head==NULL && tail==NULL) 100 | { 101 | cout<<"\nNothing to delete"; 102 | return; 103 | } 104 | 105 | //Linked list containing only one element 106 | else if(head==tail) 107 | { 108 | delete head; 109 | head=tail=NULL; 110 | } 111 | //Multiple nodes 112 | else 113 | { 114 | head=head->next; 115 | delete tail->next; 116 | tail->next=head; 117 | } 118 | } 119 | /////////////////////////////////////////////////////////////////////////////////// 120 | //Destructor: to deallocate the used memory 121 | CircularQueue::~CircularQueue() 122 | { 123 | PNODE temp=head; 124 | if(head==NULL && tail==NULL) 125 | return; 126 | else 127 | { 128 | while(head!=tail) 129 | { 130 | head=head->next; 131 | delete temp; 132 | temp=head; 133 | } 134 | } 135 | } 136 | /////////////////////////////////////////////////////////////////////////////////// 137 | //main function of the program 138 | /////////////////////////////////////////////////////////////////////////////////// 139 | int main() 140 | { 141 | CircularQueue obj; 142 | 143 | //Here one can opt for menu driven approach also! 144 | cout<<"\n"; 145 | obj.Insert(40); 146 | obj.DisplayQueue(); 147 | obj.Insert(70);cout<<"\n"; 148 | obj.DisplayQueue(); 149 | obj.Insert(100);cout<<"\n"; 150 | obj.DisplayQueue(); 151 | cout<<"\nSuccessfully inserted in queue"; 152 | obj.DisplayQueue();cout<<"\n"; 153 | cout<<"\nTotal number of elements "< 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | // Slightly different approach ,with 'void' return type // 15 | /////////////////////////////////////////////////////////////////////////////////// 16 | 17 | typedef struct node 18 | { 19 | int data; 20 | struct node* next; 21 | }NODE,*PNODE; 22 | /////////////////////////////////////////////////////////////////////////////////// 23 | 24 | class LinearQueue 25 | { 26 | PNODE head; 27 | int ct; 28 | public: 29 | LinearQueue(); //constructor 30 | ~LinearQueue(); //To deallocate the used memory 31 | 32 | void Insert(int); 33 | 34 | void Remove(); 35 | 36 | void DisplayQueue(); //to Display the Queue 37 | int Count(); //Display total number of elements in queue 38 | }; 39 | /////////////////////////////////////////////////////////////////////////////////// 40 | //Constructor: Initializing memory 41 | LinearQueue::LinearQueue() 42 | { 43 | head=NULL; 44 | ct=0; 45 | } 46 | /////////////////////////////////////////////////////////////////////////////////// 47 | //To get total number of nodes/elements present Queue 48 | int LinearQueue::Count() 49 | { 50 | ct=0; 51 | PNODE temp=head; 52 | while(temp!=NULL) 53 | { 54 | ct++; 55 | temp=temp->next; 56 | } 57 | return ct; 58 | } 59 | /////////////////////////////////////////////////////////////////////////////////// 60 | //Display entire Queue elements 61 | void LinearQueue::DisplayQueue() 62 | { 63 | PNODE temp=head; 64 | while(temp!=NULL) 65 | { 66 | cout<data<<" "; 67 | temp=temp->next; 68 | } 69 | } 70 | /////////////////////////////////////////////////////////////////////////////////// 71 | //Inserting element in the Queue 72 | void LinearQueue::Insert(int n) 73 | { 74 | PNODE temp=head; 75 | PNODE newn=new node; 76 | newn->data=n; 77 | newn->next=NULL; 78 | if(head==NULL) 79 | { 80 | head=newn; 81 | } 82 | else 83 | { 84 | while(temp->next!=NULL) 85 | { 86 | temp=temp->next; 87 | } 88 | temp->next=newn; 89 | } 90 | } 91 | /////////////////////////////////////////////////////////////////////////////////// 92 | //Delete element from the Queue 93 | void LinearQueue::Remove() 94 | { 95 | if(head==NULL) 96 | { 97 | cout<<"\nNothing to delete"; 98 | return; 99 | } 100 | else 101 | { 102 | PNODE temp=head; 103 | head=head->next; 104 | delete temp; 105 | } 106 | } 107 | /////////////////////////////////////////////////////////////////////////////////// 108 | //Destructor: to deallocate the used memory 109 | LinearQueue::~LinearQueue() 110 | { 111 | PNODE temp=head; 112 | if(head==NULL) 113 | return; 114 | else if(head!=NULL) 115 | { 116 | while(head!=NULL) 117 | { 118 | head=head->next; 119 | delete temp; 120 | temp=head; 121 | } 122 | } 123 | } 124 | /////////////////////////////////////////////////////////////////////////////////// 125 | //main function of the program 126 | /////////////////////////////////////////////////////////////////////////////////// 127 | int main() 128 | { 129 | //creating instance of class 130 | LinearQueue obj; 131 | 132 | //Here one can opt for menu driven approach also! 133 | obj.Insert(10); 134 | cout<<"\n"; 135 | obj.DisplayQueue(); 136 | cout<<"\n"; 137 | obj.Insert(20); 138 | obj.DisplayQueue(); 139 | cout<<"\n"; 140 | obj.Insert(30); 141 | obj.DisplayQueue(); 142 | cout<<"\nSuccessfully inserted element in the Queue\n"; 143 | obj.DisplayQueue(); 144 | cout<<"\nTotal number of elements "< 2 | using namespace std; 3 | 4 | int orderAgnosticinarySearch(int arr[],int n, int key) 5 | { 6 | int s=0, e=n-1, mid=0; 7 | //check wheather array is sorted in ascending order or descending order 8 | 9 | if(arr[0] arr[mid]){ 17 | s=mid+1; 18 | } 19 | else 20 | { 21 | return mid; 22 | } 23 | } 24 | // element not found 25 | return -1; 26 | } 27 | else if(arr[0]>arr[n-1]) 28 | { 29 | //descending sort 30 | while(s<=e){ 31 | mid=s+(e-s)/2; 32 | if(key > arr[mid]){ 33 | e=mid-1; 34 | } 35 | else if(key < arr[mid]){ 36 | s=mid+1; 37 | } 38 | else 39 | { 40 | return mid; 41 | } 42 | } 43 | // element not found 44 | return -1; 45 | } 46 | else 47 | { 48 | //all array elements are equal 49 | if(key==arr[0]) 50 | return 0; 51 | else 52 | return -1; 53 | } 54 | } 55 | int main() 56 | { 57 | int n; 58 | cin>>n; 59 | int arr[n]; 60 | int target; 61 | for(int i=0;i>arr[i]; 63 | cin>>target; 64 | cout<<"Element found at position: "< 2 | using namespace std; 3 | 4 | int ceiling(int arr[], int n,int target) 5 | { 6 | int mid=0,s=0,e=n-1; 7 | while(s<=e) 8 | { 9 | mid = s+(e-s)/2; 10 | if(targetarr[mid]) 15 | { 16 | s=mid+1; 17 | } 18 | else 19 | { 20 | return arr[mid]; 21 | } 22 | } 23 | //return value pointed by start pointer 24 | return arr[s]; 25 | } 26 | int main() 27 | { 28 | int arr[] = {2,3,5,9,14,16,18}; 29 | int target; 30 | cin>>target; 31 | cout<<"Ceiling of the target element is: "<arr[mid+1]) return mid; 16 | if(mid>s && arr[mid]=arr[mid]) e=mid-1; 18 | else s=mid+1; 19 | //else s=mid+1; 20 | } 21 | return -1; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Searching_Algorithms/FloorOfNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ceiling(int arr[], int n,int target) 5 | { 6 | int mid=0,s=0,e=n-1; 7 | while(s<=e) 8 | { 9 | mid = s+(e-s)/2; 10 | if(targetarr[mid]) 15 | { 16 | s=mid+1; 17 | } 18 | else 19 | { 20 | return arr[mid]; 21 | } 22 | } 23 | //return value pointed by end pointer 24 | return arr[e]; 25 | } 26 | int main() 27 | { 28 | int arr[] = {2,3,5,9,14,16,18}; 29 | int target; 30 | cin>>target; 31 | cout<<"Ceiling of the target element is: "<& a) 8 | { 9 | int s=0,e=a.size()-1; 10 | 11 | while(s<=e) 12 | { 13 | int mid = s+(e-s)/2; 14 | if(a[mid]>a[mid+1] && a[mid-1]a[mid-1] && a[mid]mountainArr.get(mid+1)){ 30 | e=mid; 31 | } 32 | else{ 33 | s=mid+1; 34 | } 35 | } 36 | return mid; 37 | } 38 | int agnosticSearch(MountainArray &mountainArr, int target, int s, int e){ 39 | int mid=0,ans=-1; 40 | bool isAscendingArray = mountainArr.get(s) < mountainArr.get(e); 41 | 42 | while(s<=e){ 43 | mid=s+(e-s)/2; 44 | if(target==mountainArr.get(mid)) return mid; 45 | if(isAscendingArray){ 46 | if(targetmountainArr.get(mid)){ 54 | e=mid-1; 55 | } 56 | else{ 57 | s=mid+1; 58 | } 59 | } 60 | } 61 | return -1; 62 | } 63 | }; -------------------------------------------------------------------------------- /Searching_Algorithms/LeetCode#162.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-peak-element/ 2 | class Solution { 3 | public: 4 | int findPeakElement(vector& nums) { 5 | int mid=0, s=0, e=nums.size()-1; 6 | while(snums[mid+1]) e=mid; 9 | else s=mid+1; 10 | } 11 | return s; 12 | } 13 | }; -------------------------------------------------------------------------------- /Searching_Algorithms/LeetCode#33.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/search-in-rotated-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | int search(vector& nums, int target) { 6 | int n = nums.size(); 7 | if(!count(nums.begin(),nums.end(),target)) return -1; 8 | if(n==1){ 9 | return nums[0]==target?0:-1; 10 | } 11 | if(n==2){ 12 | if(nums[0]==target) return 0; 13 | return (nums[1]==target)?1:-1; 14 | } 15 | int pivot = getPivot(nums,n); 16 | //cout<& nums, int n){ 24 | int mid=0,s=0,e=n-1; 25 | while(s<=e){ 26 | mid=s+(e-s)/2; 27 | if(midnums[mid+1]) return mid; 28 | if(mid>s && nums[mid]=nums[mid]){ //case 3 start>=mid 30 | e=mid-1; 31 | } 32 | else //case 4 33 | { 34 | s=mid+1; 35 | } 36 | } 37 | return -1; 38 | } 39 | int binarySearch(vector& nums, int s,int e,int target){ 40 | while(s<=e){ 41 | int mid=s+(e-s)/2; 42 | if(nums[mid]==target) return mid; 43 | else if(target>nums[mid]) s=mid+1; 44 | else e=mid-1; 45 | } 46 | return -1; 47 | } 48 | }; -------------------------------------------------------------------------------- /Searching_Algorithms/LeetCode#34.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 2 | 3 | class Solution { 4 | public: 5 | vector searchRange(vector& nums, int target) { 6 | //initiall vector 7 | vectorv={-1,-1}; 8 | 9 | //brute force: start from 0th index with one ptr, n from end-1 index with another ptr 10 | 11 | //efficient approach 12 | 13 | v[0]=getElement(nums,target,true); //search in first half 14 | 15 | // if target element is present 16 | if(v[0]!=-1) 17 | v[1]=getElement(nums,target,false); //search in second half 18 | 19 | return v; 20 | } 21 | int getElement(vector& nums, int target, bool isStartIndex){ 22 | int mid=0, ans=-1, s=0, e=nums.size()-1; 23 | while(s<=e){ 24 | mid=s+(e-s)/2; 25 | if(targetnums[mid]){ 29 | s=mid+1; 30 | } 31 | else{ //target == arr[mid] 32 | ans=mid; 33 | if(isStartIndex){ 34 | e=mid-1; 35 | } 36 | else{ 37 | s=mid+1; 38 | } 39 | } 40 | } 41 | return ans; 42 | } 43 | 44 | }; -------------------------------------------------------------------------------- /Searching_Algorithms/LeetCode#410.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int splitArray(vector& nums, int m) { 4 | int s=*max_element(nums.begin(),nums.end()); //max element from array (m==n-1) 5 | int e=accumulate(nums.begin(),nums.end(),0); //sum of all elements in array (m==0) 6 | 7 | while(smid){ 15 | //cannot add this into subarray, make new one 16 | sum=i; 17 | p++; 18 | } 19 | else{ 20 | sum+=i; 21 | } 22 | } 23 | if(p>m){ //p<=m 24 | s=mid+1; 25 | }else{ 26 | e=mid; 27 | } 28 | } 29 | return e; //s == e, return s 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /Searching_Algorithms/LeetCode#744.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-smallest-letter-greater-than-target/ 2 | 3 | class Solution { 4 | public: 5 | char nextGreatestLetter(vector& letters, char target) { 6 | int mid=0,s=0,e=letters.size()-1; 7 | while(s<=e) 8 | { 9 | mid=s+(e-s)/2; 10 | if(target& arr) { 5 | int mid=0, s=0, e=nums.size()-1; 6 | while(snums[mid+1]) e=mid; 9 | else s=mid+1; 10 | } 11 | return s; 12 | }; 13 | 14 | //Peak Index (sub 1095) -------------------------------------------------------------------------------- /Searching_Algorithms/Linear_Search.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WaderManasi/Knowing-DataStructures-Algorithms/19dc5e36de2cea0e1e2a641930d3404755a89d81/Searching_Algorithms/Linear_Search.cpp -------------------------------------------------------------------------------- /Sorting_Algorithms/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | 3 | Bubble Sort/ Sinking sort/ Exchange Sort 4 | (stable sort technique) (in-place sort) 5 | 6 | *******************************************************************************/ 7 | #include 8 | using namespace std; 9 | void bubbleSort(int arr[],int n){ 10 | bool swapp = false; 11 | for(int i=0;i 10 | #define MAX 30 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | void BubbleSort(int arr[],int size) 15 | { 16 | int temp; //for swapping of elements 17 | int flag; 18 | for(int i=0;iarr[j+1]) 25 | { 26 | temp=arr[j]; 27 | arr[j]=arr[j+1]; 28 | arr[j+1]=temp; 29 | flag=1; 30 | } 31 | } 32 | } 33 | } 34 | 35 | /////////////////////////////////////////////////////////////////////////////////// 36 | int main() 37 | { 38 | int size; 39 | int arr[MAX]; 40 | cin>>size; 41 | for(int i=0;i>arr[i]; 44 | } 45 | cout<<"\nInitial Array : "; 46 | for(int i=0;i 9 | void sortArray(T a[], int n) 10 | { 11 | sort(a,a+n); 12 | } 13 | 14 | template 15 | void printArray(T a[], int n) 16 | { 17 | for(int i=0;i 9 | using namespace std; 10 | void cyclicSort(int arr[],int n){ 11 | 12 | // in this we need to sort array in a single pass O(n) 13 | 14 | for(int i=0;i 10 | #define MAX 30 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | void InsertionSort(int arr[],int size) 15 | { 16 | int temp; //for swapping of elements 17 | int flag; 18 | for(int i=0;iarr[j+1]) 25 | { 26 | temp=arr[j]; 27 | arr[j]=arr[j+1]; 28 | arr[j+1]=temp; 29 | flag=1; 30 | } 31 | } 32 | } 33 | } 34 | 35 | /////////////////////////////////////////////////////////////////////////////////// 36 | int main() 37 | { 38 | int size; 39 | int arr[MAX]; 40 | cin>>size; 41 | for(int i=0;i>arr[i]; 44 | } 45 | cout<<"\nInitial Array : "; 46 | for(int i=0;i& nums) { 8 | sort(nums.begin(),nums.end()); 9 | for(int i=0;i& nums) { 6 | int i=0; 7 | int n = nums.size(); 8 | while(i 4 | class Solution { 5 | public: 6 | int firstMissingPositive(vector& nums) { 7 | int i=0; 8 | while(i0 && nums[i]<=nums.size() && nums[i]!=nums[nums[i]-1]){ 11 | swap(nums[i],nums[nums[i]-1]); 12 | }else{ 13 | i++; 14 | } 15 | } 16 | int ans=nums.size(); 17 | for(int i=0;i 4 | 5 | class Solution { 6 | public: 7 | vector findDisappearedNumbers(vector& nums) { 8 | vectorv; 9 | int i=0; 10 | int n = nums.size(); 11 | while(i findErrorNums(vector& nums) { 6 | vectorv; 7 | int i=0; 8 | while(i 8 | using namespace std; 9 | 10 | // get index of maximum element 11 | int getMaxIndex(int arr[],int s,int e){ 12 | int max=s; 13 | for(int i=s+1;i<=e;i++){ 14 | if(arr[max] 10 | #define MAX 30 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | void SelectionSort(int arr[],int size) 15 | { 16 | int temp,min; //for swapping of elements 17 | for(int i=0;i>size; 39 | for(int i=0;i>arr[i]; 42 | } 43 | cout<<"\nInitial Array : "; 44 | for(int i=0;i 10 | #define MAX 30 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | void ShellSort(int arr[],int size) 15 | { 16 | for(int gap=size/2;gap>=1;gap=gap/2) 17 | { 18 | for(int j=gap;j=0;i=i-gap) 21 | { 22 | if(arr[i]>size; 43 | for(int i=0;i>arr[i]; 46 | } 47 | cout<<"\nInitial Array : "; 48 | for(int i=0;i=0;x--) 7 | { 8 | if(arr[x]>arr[i]) 9 | i=x; 10 | } 11 | return i; 12 | } 13 | 14 | 15 | void selectionSort(int arr[], int n) 16 | { 17 | int i, j; 18 | for (i = n-1; i >=0; i--) 19 | { 20 | int j = select(arr, i); 21 | swap(&arr[i], &arr[j]); 22 | } 23 | } -------------------------------------------------------------------------------- /Stack/DynamicStack.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader. // 3 | //Program :Stack Ooerations // 4 | //Approach :Dynamic Approach using Linked Lists. // 5 | //Language :C++ (Object Oriented Approach) // 6 | //Functions:Last In First Out (LIFO) fashion to perform Push and Pop operations // 7 | // on Stack. // 8 | /////////////////////////////////////////////////////////////////////////////////// 9 | 10 | #include 11 | using namespace std; 12 | 13 | /////////////////////////////////////////////////////////////////////////////////// 14 | // Slighly different approach ,with 'void' return type // 15 | /////////////////////////////////////////////////////////////////////////////////// 16 | 17 | typedef struct node 18 | { 19 | int data; 20 | struct node* next; 21 | }NODE,*PNODE; 22 | /////////////////////////////////////////////////////////////////////////////////// 23 | 24 | class DynamicStack 25 | { 26 | PNODE head; 27 | int ct; 28 | public: 29 | DynamicStack(); //constructor 30 | ~DynamicStack(); //destructor: To deallocate the used memory 31 | 32 | void Push(int); 33 | 34 | void Pop(); 35 | 36 | void DisplayStack(); //to Display the entire stack 37 | int Count(); //Display total number of elements present in Stack. 38 | }; 39 | /////////////////////////////////////////////////////////////////////////////////// 40 | //Initializing memory 41 | DynamicStack::DynamicStack() 42 | { 43 | head=NULL; 44 | ct=0; 45 | } 46 | /////////////////////////////////////////////////////////////////////////////////// 47 | //To get total number of nodes/elements present Stack 48 | int DynamicStack::Count() 49 | { 50 | ct=0; 51 | PNODE temp=head; 52 | while(temp!=NULL) 53 | { 54 | ct++; 55 | temp=temp->next; 56 | } 57 | return ct; 58 | } 59 | /////////////////////////////////////////////////////////////////////////////////// 60 | //Display entire Stack 61 | void DynamicStack::DisplayStack() 62 | { 63 | PNODE temp=head; 64 | while(temp!=NULL) 65 | { 66 | cout<data<<" "; 67 | temp=temp->next; 68 | } 69 | } 70 | /////////////////////////////////////////////////////////////////////////////////// 71 | //Push element onto Stack 72 | void DynamicStack::Push(int n) 73 | { 74 | PNODE temp=head; 75 | PNODE newn=new node; 76 | newn->data=n; 77 | newn->next=NULL; 78 | if(head==NULL) 79 | { 80 | head=newn; 81 | } 82 | else 83 | { 84 | while(temp->next!=NULL) 85 | { 86 | temp=temp->next; 87 | } 88 | temp->next=newn; 89 | } 90 | } 91 | /////////////////////////////////////////////////////////////////////////////////// 92 | //Pop element from the Stack 93 | void DynamicStack::Pop() 94 | { 95 | if(head==NULL) 96 | { 97 | cout<<"\nNothing to delete\n"; 98 | return; 99 | } 100 | else if(head->next==NULL) 101 | { 102 | delete head; 103 | head=NULL; 104 | } 105 | else 106 | { 107 | PNODE temp=head; 108 | while(temp->next->next!=NULL) 109 | { 110 | temp=temp->next; 111 | } 112 | delete temp->next; 113 | temp->next=NULL; 114 | } 115 | } 116 | /////////////////////////////////////////////////////////////////////////////////// 117 | //Destructor: to deallocate the used memory 118 | DynamicStack::~DynamicStack() 119 | { 120 | PNODE temp=head; 121 | if(head==NULL) 122 | return; 123 | else if(head!=NULL) 124 | { 125 | while(head!=NULL) 126 | { 127 | head=head->next; 128 | delete temp; 129 | temp=head; 130 | } 131 | } 132 | } 133 | /////////////////////////////////////////////////////////////////////////////////// 134 | //main function of the program 135 | /////////////////////////////////////////////////////////////////////////////////// 136 | int main() 137 | { 138 | DynamicStack obj; 139 | 140 | //Here one can opt for menu driven approach also! 141 | obj.Push(10); 142 | obj.DisplayStack(); 143 | cout<<"\n"; 144 | 145 | obj.Push(20); 146 | obj.DisplayStack(); 147 | cout<<"\n"; 148 | 149 | obj.Push(30); 150 | obj.DisplayStack(); 151 | cout<<"\n"; 152 | 153 | cout<<"\nSuccessfully Pushed element onto stack"; 154 | cout<<"\nTotal number of elements "< st){ 4 | n=st.size(); 5 | for(int i=0;is; 5 | for(int i=0;i=0) 10 | top--; 11 | else 12 | cout<<"Stack Empty\n"; 13 | } 14 | //Function to display the stack from index 0 15 | void display() 16 | { 17 | if(top<0) 18 | cout<<"-1\n"; 19 | else{ 20 | for(int i=0;i<=top ;i++) 21 | cout< &s,int x) 4 | { 5 | s.push(x); 6 | } 7 | 8 | void remove(stack &s) 9 | { 10 | s.pop(); 11 | } 12 | 13 | void headOf_Stack(stack &s) 14 | { 15 | int x=s.top(); 16 | cout< s, int val) 20 | { 21 | bool exists=false; 22 | 23 | while(!s.empty()) 24 | { 25 | if(s.top()==val) 26 | return true; 27 | s.pop(); 28 | } 29 | 30 | 31 | return false; 32 | 33 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Map_Operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | map element are accesed using "->" operator 3 | Array A of size N. 4 | You need to insert the elements of A into a map(element as key and index as value) and display the results. 5 | Also, you need to erase a given element x from the map and print "erased x" if successfully erased, else print "not found". 6 | */ 7 | 8 | map mapInsert(int arr[],int n) 9 | { 10 | mapmp; 11 | for(int i=0;imp) 21 | { 22 | for(auto it=mp.begin();it!=mp.end();++it) 23 | { 24 | cout<first<<" "<second; 25 | cout<<"\n"; 26 | } 27 | } 28 | 29 | 30 | void mapErase(map&mp,int x) 31 | { 32 | if(mp.erase(x)) 33 | cout<<"erased "<>n; 44 | int arr[n]; 45 | for(int i=0;i>arr[i]; 47 | int x; 48 | cin>>x; 49 | 50 | mapmp=mapInsert(arr,n); 51 | mapDisplay(mp,x); 52 | 53 | mapErase(mp,x); 54 | mapDisplay(mp); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Multimap_Operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Array A of size N. 3 | You need to insert the elements of A into a multimap(element as key and index as value) and display the results. 4 | Also, you need to erase a given element x from the multimap and print "erased x" if successfully erased, else print "not found". 5 | */ 6 | 7 | multimap multimapInsert(int arr[],int n) 8 | { 9 | multimapmp; 10 | for(int i=0;i(arr[i],i)); 13 | } 14 | return mp; 15 | } 16 | 17 | void multimapDisplay(multimapmp) 18 | { 19 | for(auto i=mp.begin();i!=mp.end();++i) 20 | { 21 | cout<first<<" "<second; 22 | cout<&mp,int x) 27 | { 28 | if(mp.erase(x)) 29 | cout<<"erased "<>n; 39 | int arr[n]; 40 | for(int i=0;i>arr[i]; 42 | int x; 43 | cin>>x; 44 | 45 | multimapmp=multimapInsert(arr,n); 46 | multimapDisplay(mp,x); 47 | 48 | multimapErase(mp,x); 49 | multimapDisplay(mp); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Multiset_Operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | array A of size N. 3 | you need to insert the elements of A into a multiset and display the results. 4 | Also, you need to erase a given element x from the multiset and print "erased x" if successfully erased, else print "not found". 5 | */ 6 | 7 | multiset multisetInsert(int arr[],int n) 8 | { 9 | multisets; 10 | for(int i=0;is) 19 | { 20 | for(auto itr=s.begin();itr!=s.end();++itr) 21 | { 22 | cout<<*itr<<" "; 23 | } 24 | cout<&s,int x) 29 | { 30 | if(s.erase(x)!=NULL) 31 | cout<<"erased "<>n; 42 | int arr[n]; 43 | for(int i=0;i>arr[i]; 45 | 46 | multisets=multisetInsert(arr,n); 47 | multisetDisplay(s); 48 | int x; 49 | cin>>x; 50 | setErase(s,x); 51 | setDisplay(s); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Pair_Sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a vector V of size n. The vector hold pair of integers. 3 | Example V={(1,2),(3,4)...}. Now, you need to sum the second elements. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | void sum(vector>v) 12 | { 13 | long long sum=0; 14 | for(long int i=0;i>n; 26 | vector>v; 27 | for(int i=0;i>x>>y; 31 | v.push_back(make_pair(x,y)); 32 | } 33 | sum(v); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Removing_Duplicate_Using_Set.cpp: -------------------------------------------------------------------------------- 1 | //https://practice.geeksforgeeks.org/problems/remove-duplicate-elements-from-sorted-array/1/?track=ppc-arrays&batchId=221 2 | 3 | //Given a sorted array A of size N. The function remove_duplicate takes two arguments . The first argument is the sorted array A[ ] and the second argument is 'N' the size of the array and returns the size of the new converted array A[ ] with no duplicate element. 4 | 5 | //solution 6 | int remove_duplicate(int A[],int N) 7 | { 8 | set v(A, A+N); 9 | copy(v.begin(), v.end(), A); 10 | 11 | return v.size(); 12 | } -------------------------------------------------------------------------------- /Standard_Template_Library/ReverseVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector reverseVector(vectorv) 6 | { 7 | reverse(v.begin(),v.end()); 8 | return v; 9 | } 10 | 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | vectorv; 16 | for(int i=0;i>val; 20 | v.push_back(val); 21 | } 22 | 23 | v=reverseVector(v); //reverse the vector elements 24 | for(auto x:v) 25 | cout< 2 | #include 3 | using namespace std; 4 | 5 | vector split_string(string); 6 | 7 | // Complete the rotLeft function below. 8 | void rotLeft(vector a, int d) { 9 | 10 | //rotate() method to left rotate elememts. 11 | rotate(a.begin(),a.begin()+d,a.end()); 12 | for(int i=0;i>n>>rot; 22 | vectorv; 23 | for(int i=0;i>val; 26 | v.push_back(val); 27 | } 28 | rotLeft(v,rot); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Set1.cpp: -------------------------------------------------------------------------------- 1 | void print(list &A) 2 | { 3 | //Your code here 4 | list::iterator i; 5 | if(A.empty()) cout<<"-1"; 6 | else{ 7 | for(i=A.begin();i!=A.end();i++) 8 | cout<<*i<<" "; 9 | } 10 | cout<<"\n"; 11 | } 12 | /*remove element from 13 | back of list A*/ 14 | void remove_from_back(list &A) 15 | { 16 | A.pop_back(); 17 | } 18 | /*remove element from 19 | front of list A*/ 20 | void remove_from_front(list &A) 21 | { 22 | A.pop_front(); 23 | } 24 | 25 | /*inserts an element x at 26 | the back of the list A */ 27 | void add_to_list(list &A,int x) 28 | { 29 | A.push_back(x); 30 | } 31 | 32 | /*sort the list A in ascending order*/ 33 | void sort_list(list &A) 34 | { 35 | A.sort(); 36 | } 37 | 38 | /*reverses the list A*/ 39 | void reverse_list(list &A) 40 | { 41 | //Your code here 42 | A.reverse(); 43 | } 44 | 45 | /*returns the size of the list A */ 46 | int size_of_list(list &A) 47 | { 48 | //Your code here 49 | return A.size(); 50 | } 51 | 52 | /*inserts an element x at 53 | the front of the list A*/ 54 | void add_from_front(list &A,int x) 55 | { 56 | //Your code here 57 | A.push_front(x); 58 | } 59 | -------------------------------------------------------------------------------- /Standard_Template_Library/Set2.cpp: -------------------------------------------------------------------------------- 1 | /*inserts an element x to the set s */ 2 | void insert(set &s,int x) 3 | { 4 | //Your code here 5 | s.insert(x); 6 | } 7 | 8 | /*prints the contents of the set s */ 9 | void print_contents(set &s) 10 | { 11 | for(int i:s) 12 | cout< &s,int x) 17 | { 18 | //Your code here 19 | if(s.find(x)!=s.end()) 20 | s.erase(x); 21 | } 22 | 23 | /*returns 1 if the element x is 24 | present in set s else returns -1 */ 25 | int find(set &s,int x) 26 | { 27 | //Your code here 28 | if(s.find(x)!=s.end()) 29 | return 1; 30 | return -1; 31 | } 32 | 33 | /*returns the size of the set s */ 34 | int size(set &s) 35 | { 36 | //Your code here 37 | return s.size(); 38 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Set_Operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array A of size N. 3 | you need to insert the elements of A into a set and display the results. 4 | Also, you need to erase a given element x from the set and print "erased x" if successfully erased, else print "not found 5 | */ 6 | 7 | 8 | set setInsert(int arr[],int n) 9 | { 10 | sets; 11 | for(int i=0;is) 20 | { 21 | set::iterator it; 22 | 23 | for(it=s.begin();it!=s.end();it++) 24 | { 25 | cout<<*it<<" "; 26 | } 27 | cout<&s,int x) 32 | { 33 | bool flag = false; 34 | for(auto i=s.begin();i!=s.end();i++) 35 | { 36 | if(x == *i) 37 | { 38 | flag = true; 39 | s.erase(i); 40 | } 41 | } 42 | if(flag) 43 | 44 | cout<<"erased "<>n; 54 | int arr[n]; 55 | for(int i=0;i>arr[i]; 57 | sets=setInsert(arr,n); 58 | setDisplay(s); 59 | int x; 60 | cin>>x; 61 | setErase(s,x); 62 | setDisplay(s); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /Standard_Template_Library/SortVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | vector sortVector(vectorv) 7 | { 8 | sort(v.begin(),v.end()); 9 | return v; 10 | } 11 | 12 | int main() 13 | { 14 | int n; 15 | cin>>n; 16 | vectorv; 17 | for(int i=0;i>val; 21 | v.push_back(val); 22 | } 23 | v=sortVector(v); //sort vector and store in v4 24 | for(auto x:v) 25 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int findFrequency(vector v, int x){ 6 | 7 | int freq=count(v.begin(),v.end(),x); 8 | return freq; 9 | } 10 | 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | vectorv; 16 | for(int i=0;i>val; 20 | v.push_back(val); 21 | } 22 | int numberToFind; 23 | cin>>numberToFind; 24 | cout< 2 | #include 3 | using namespace std; 4 | 5 | void iter(vector::iterator it1,vector::iterator it2) 6 | { 7 | vector::iterator it; 8 | for(it=it1;it>n; 17 | vectorv; 18 | for(int i=0;i>val; 22 | v.push_back(val); 23 | } 24 | iter(v.begin(),v.end()); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Vector_Iterator1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void iter(vector::iterator it1,vector::iterator it2) 6 | { 7 | vector::iterator it; 8 | for(it=it1;it>n; 17 | vectorv; 18 | for(int i=0;i>val; 22 | v.push_back(val); 23 | } 24 | iter(v.begin(),v.end()); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Vector_Iterator2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void iter(vector::iterator it1,vector::iterator it2) 6 | { 7 | vector::reverse_iterator it; 8 | for(it = it1; it != it2; it++) 9 | cout<< *it <<" "; 10 | cout<>n; 17 | vectorv; 18 | for(int i=0;i>val; 22 | v.push_back(val); 23 | } 24 | iter(v.rbegin(),v.rend()); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Standard_Template_Library/Vector_ReverseVector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | vector reverseVector(vectorv) 6 | { 7 | reverse(v.begin(),v.end()); 8 | return v; 9 | } 10 | 11 | int main() 12 | { 13 | int n; 14 | cin>>n; 15 | vectorv; 16 | for(int i=0;i>val; 20 | v.push_back(val); 21 | } 22 | 23 | v=reverseVector(v); //reverse the vector elements 24 | for(auto x:v) 25 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | vector sortVector(vectorv) 7 | { 8 | sort(v.begin(),v.end()); 9 | return v; 10 | } 11 | 12 | int main() 13 | { 14 | int n; 15 | cin>>n; 16 | vectorv; 17 | for(int i=0;i>val; 21 | v.push_back(val); 22 | } 23 | v=sortVector(v); //sort vector and store in v4 24 | for(auto x:v) 25 | cout<=48 && str[i]<=57) 15 | { 16 | s[k]=str[i]; 17 | k++; 18 | } 19 | else 20 | flag=0; 21 | } 22 | if(flag) return stoi(s)*-1; 23 | return -1; 24 | } 25 | else{ 26 | for(int i=0;i=48 && str[i]<=57) 29 | continue; 30 | else 31 | flag=0; 32 | } 33 | if(flag) return stoi(str); 34 | return -1; 35 | } 36 | } -------------------------------------------------------------------------------- /String/max_occur_string.cpp: -------------------------------------------------------------------------------- 1 | // Given a string str. The task is to find the maximum occurring character in the string str. 2 | // If more than one character occurs the maximum number of time then print the lexicographically smaller character. 3 | 4 | //Solution: 5 | char getMaxOccuringChar(char* str) 6 | { 7 | int m=0; 8 | int count[26]={0}; 9 | for(int i=0;str[i]!='\0';i++) 10 | { 11 | count[str[i]-'a']++; 12 | m=max(m,count[str[i]-'a']); 13 | 14 | } 15 | for(int i=0;i<26;i++) 16 | { 17 | if(count[i]==m) 18 | return 'a'+i; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /String/sort_a_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin>>t; 7 | while(t--) 8 | { 9 | int n; 10 | cin>>n; 11 | string str; 12 | cin>>str; 13 | sort(str.begin(),str.end()); 14 | cout< 4 | using namespace std; 5 | 6 | int main() { 7 | //code 8 | int t; 9 | cin>>t; 10 | while(t--) 11 | { 12 | string str; 13 | cin>>str; 14 | sort(str.begin(),str.end()); 15 | cout< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char str[]="comp"; 7 | cout<<"\nDisplaying string: "; 8 | cout<>name; 22 | cout< 8 | #include 9 | //standard library 10 | using namespace std; 11 | class Node 12 | { 13 | public: 14 | int data; 15 | Node* right; 16 | Node* left; 17 | Node(int x) 18 | { 19 | data=x; 20 | right=NULL; 21 | left=NULL; 22 | } 23 | }*root; 24 | class BST 25 | { 26 | int ct; 27 | public: 28 | BST() 29 | { 30 | ct=0; 31 | } 32 | void Create(Node* root,int data); 33 | void BFS(Node* root); 34 | }; 35 | void BST::Create(Node* temp,int x) 36 | { 37 | Node* newn=new Node(x); 38 | 39 | if(root==NULL) 40 | { 41 | root=newn; 42 | cout<<"\nRoot inserted!\n"; 43 | } 44 | else 45 | { 46 | if(xdata) 47 | { 48 | while(temp->left!=NULL) 49 | temp=temp->left; 50 | temp->left=newn; 51 | } 52 | else if(x>temp->data) 53 | { 54 | while(temp->right!=NULL) 55 | temp=temp->right; 56 | temp->right=newn; 57 | } 58 | } 59 | 60 | } 61 | void BST::BFS(Node* temp) 62 | { 63 | if(root==NULL) return; 64 | queueq; 65 | q.push(root); 66 | while(!q.empty()) 67 | { 68 | temp=q.front(); 69 | cout<data<<" "; 70 | ct++; 71 | q.pop(); 72 | if(temp->left!=NULL) q.push(temp->left); 73 | if(temp->right!=NULL) q.push(temp->right); 74 | 75 | } 76 | 77 | cout<<"\n\nTotal no. of nodes in tree: "<left); 9 | int h2=height(root->right); 10 | 11 | return max(h1,h2) + 1; 12 | } -------------------------------------------------------------------------------- /Tree/LevelOrder_Display.cpp: -------------------------------------------------------------------------------- 1 | //Given a binary tree, find its level order traversal. 2 | //Level order traversal of a tree is breadth-first traversal for the tree. 3 | 4 | vector levelOrder(Node* root) 5 | { 6 | vectorv; 7 | if(root==NULL) return v; 8 | 9 | queueq; 10 | q.push(root); 11 | 12 | while(!q.empty()) 13 | { 14 | Node* temp=q.front(); 15 | v.push_back(temp->data); 16 | q.pop(); 17 | if(temp->left!=NULL) q.push(temp->left); 18 | if(temp->right!=NULL) q.push(temp->right); 19 | 20 | } 21 | return v; 22 | } -------------------------------------------------------------------------------- /Tree/MaxHeightBinTree.cpp: -------------------------------------------------------------------------------- 1 | //Leetcode: 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * struct TreeNode { 6 | * int val; 7 | * TreeNode *left; 8 | * TreeNode *right; 9 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 10 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 11 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 12 | * }; 13 | */ 14 | class Solution { 15 | public: 16 | int maxDepth(TreeNode* root) { 17 | if(root==NULL) return 0; 18 | int l = maxDepth(root->left); 19 | int r = maxDepth(root->right); 20 | if(l>r) 21 | return l+1; 22 | else 23 | return r+1; 24 | } 25 | }; -------------------------------------------------------------------------------- /Tree/MinDepthBinTree.cpp: -------------------------------------------------------------------------------- 1 | // Given a binary tree, find its minimum depth. 2 | 3 | /*You are required to complete this method*/ 4 | int minDepth(Node *node) { 5 | //empty tree 6 | if(node == NULL) return 0; 7 | 8 | //single node i.e root 9 | if(node->left==NULL && node->right==NULL) return 1; 10 | 11 | //more than one node 12 | if(node->left && node->right) 13 | return 1+min(minDepth(node->left),minDepth(node->right)); 14 | else if(node->left) 15 | return 1+minDepth(node->left); 16 | else 17 | return 1+minDepth(node->right); 18 | } -------------------------------------------------------------------------------- /Tree/RedBlack_Tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WaderManasi/Knowing-DataStructures-Algorithms/19dc5e36de2cea0e1e2a641930d3404755a89d81/Tree/RedBlack_Tree.cpp -------------------------------------------------------------------------------- /Tricky_Problems/CheckArrayEquality.cpp: -------------------------------------------------------------------------------- 1 | //Given two arrays A and B of equal size, the task is to find 2 | //if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though. 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | bool check(long long int arr[],long long brr[],long long int n) 9 | { 10 | sort(arr,arr+n); 11 | sort(brr,brr+n); 12 | 13 | for(int i=0;i>t; 23 | 24 | while(t--) 25 | { 26 | long long int n; 27 | cin>>n; 28 | long long int arr[n],brr[n]; 29 | for(int i=0;i>arr[i]; 32 | } 33 | for(int i=0;i>brr[i]; 36 | } 37 | if(check(arr,brr,n)) 38 | cout<<"1\n"; 39 | else 40 | cout<<"0\n"; 41 | } 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Tricky_Problems/CheckIfPowerOf2.cpp: -------------------------------------------------------------------------------- 1 | //Leetcode solutions 2 | 3 | class Solution { 4 | public: 5 | bool isPowerOfTwo(int n) { 6 | if(n==0) return false; 7 | if(n==1) return true; 8 | return (ceil(log2(n))==floor(log2(n))); 9 | // if (n == 0) 10 | // return 0; 11 | // while (n != 1) 12 | // { 13 | // if (n%2 != 0) 14 | // return 0; 15 | // n = n/2; 16 | // } 17 | // return 1; 18 | // } 19 | }; -------------------------------------------------------------------------------- /Tricky_Problems/CheckIfPowerOf4.cpp: -------------------------------------------------------------------------------- 1 | //Leetcode solution 2 | 3 | class Solution { 4 | public: 5 | bool isPowerOfFour(int n) { 6 | if(n==0) return false; 7 | if(n==1) return true; 8 | int flag=0; 9 | while(n%4 == 0) 10 | { 11 | n=n/4; 12 | if(n==1) 13 | { 14 | flag=1; 15 | break; 16 | } 17 | } 18 | return flag==1?true:false; 19 | } 20 | }; -------------------------------------------------------------------------------- /Tricky_Problems/Checking_IntersectionPointOfTwo_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :To Find the merge point between two (Singly Linear) Linked List. // 4 | //Language :C++ (Object Oriented Approach) // 5 | /////////////////////////////////////////////////////////////////////////////////// 6 | 7 | #include 8 | using namespace std; 9 | 10 | /////////////////////////////////////////////////////////////////////////////////// 11 | // Slightly different approach ,with 'void' return type // 12 | /////////////////////////////////////////////////////////////////////////////////// 13 | 14 | typedef struct node 15 | { 16 | int data; 17 | struct node* next; 18 | }NODE,*PNODE; 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | 21 | class SinglyLinearLL 22 | { 23 | PNODE head; 24 | int ct; 25 | public: 26 | SinglyLinearLL(); //constructor 27 | ~SinglyLinearLL(); //To deallocate the used memory 28 | 29 | void Insert(int,int); 30 | 31 | void Delete(int); 32 | 33 | void MergePoint(); //to find merge point of two linked lists 34 | 35 | void DisplayLL(); //to Display the Linked List 36 | int Count(); //Display total number of nodes present in Linked List 37 | }; 38 | /////////////////////////////////////////////////////////////////////////////////// 39 | //Initializing memory 40 | SinglyLinearLL::SinglyLinearLL() 41 | { 42 | head=NULL; 43 | ct=0; 44 | } 45 | /////////////////////////////////////////////////////////////////////////////////// 46 | //To get total number of nodes/elements present in the Linked List 47 | int SinglyLinearLL::Count() 48 | { 49 | ct=0; 50 | PNODE temp=head; 51 | while(temp!=NULL) 52 | { 53 | ct++; 54 | temp=temp->next; 55 | } 56 | return ct; 57 | } 58 | /////////////////////////////////////////////////////////////////////////////////// 59 | //Display entire Linked List 60 | void SinglyLinearLL::DisplayLL() 61 | { 62 | PNODE temp=head; 63 | while(temp!=NULL) 64 | { 65 | cout<data<<" "; 66 | temp=temp->next; 67 | } 68 | } 69 | 70 | /////////////////////////////////////////////////////////////////////////////////// 71 | //Inserting element at desired Position in the Linked List 72 | void SinglyLinearLL::Insert(int pos,int n) 73 | { 74 | int tot=Count(); 75 | PNODE temp=head; 76 | PNODE newn=new node; 77 | newn->data=n; 78 | newn->next=NULL; 79 | if(pos<1 || pos>tot+1) 80 | { 81 | cout<<"\nPosition invalid (element cannot be inserted"; 82 | return; 83 | } 84 | else if(pos==1) 85 | { 86 | //To insert element at beginning 87 | if(temp==NULL) 88 | { 89 | head=newn; 90 | }else if(temp!=NULL) 91 | { 92 | newn->next=head; 93 | head=newn; 94 | } 95 | } 96 | else if(pos==tot+1) 97 | { 98 | //To insert element at end 99 | if(head==NULL) 100 | { 101 | head=newn; 102 | } 103 | 104 | else 105 | { 106 | while(temp->next!=NULL) 107 | { 108 | temp=temp->next; 109 | } 110 | temp->next=newn; 111 | } 112 | } 113 | else 114 | { 115 | for(int i=1;inext; 118 | } 119 | newn->next=temp->next; 120 | temp->next=newn; 121 | } 122 | } 123 | /////////////////////////////////////////////////////////////////////////////////// 124 | // 125 | void SinglyLinearLL::MergePoint() 126 | { 127 | PNODE temp,curr; 128 | } 129 | /////////////////////////////////////////////////////////////////////////////////// 130 | //Destructor: to deallocate the used memory 131 | SinglyLinearLL::~SinglyLinearLL() 132 | { 133 | PNODE temp=head; 134 | if(head==NULL) 135 | return; 136 | else if(head!=NULL) 137 | { 138 | while(head!=NULL) 139 | { 140 | head=head->next; 141 | delete temp; 142 | temp=head; 143 | } 144 | } 145 | } 146 | /////////////////////////////////////////////////////////////////////////////////// 147 | //main function of the program 148 | /////////////////////////////////////////////////////////////////////////////////// 149 | int main() 150 | { 151 | SinglyLinearLL first; 152 | SinglyLinearLL second; 153 | 154 | 155 | return 0; 156 | } 157 | 158 | /////////////////////////////////////////////////////////////////////////////////// 159 | //End of program // 160 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Tricky_Problems/CommonDivisors.py: -------------------------------------------------------------------------------- 1 | #solution: 2 | 3 | class Solution: 4 | def commDiv (self, a, b): 5 | s1=set() 6 | s2=set() 7 | for i in range(1,a+1): 8 | if(a%i == 0): 9 | s1.add(i) 10 | for i in range(1,b+1): 11 | if(b%i == 0): 12 | s2.add(i) 13 | return len(s1.intersection(s2)) 14 | -------------------------------------------------------------------------------- /Tricky_Problems/CppTemplate.cpp: -------------------------------------------------------------------------------- 1 | //SOlution: 2 | 3 | template 4 | class minElement 5 | { 6 | T input; 7 | public: 8 | minElement(T in) 9 | { 10 | input=in; 11 | } 12 | void check(T in) 13 | { 14 | if(input > in) 15 | cout< 8 | using namespace std; 9 | 10 | /////////////////////////////////////////////////////////////////////////////////// 11 | // Slightly different approach ,with 'void' return type // 12 | /////////////////////////////////////////////////////////////////////////////////// 13 | 14 | typedef struct node 15 | { 16 | int data; 17 | struct node* next; 18 | }NODE,*PNODE; 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | 21 | class SinglyLinearLL 22 | { 23 | PNODE head; 24 | int ct; 25 | public: 26 | SinglyLinearLL(); //constructor 27 | ~SinglyLinearLL(); //To deallocate the used memory 28 | 29 | void Insert(int,int); 30 | 31 | void Delete(int); 32 | 33 | void DeleteDuplicate(); //to reverse the given Linked List 34 | 35 | void DisplayLL(); //to Display the Linked List 36 | int Count(); //Display total number of nodes present in Linked List 37 | }; 38 | /////////////////////////////////////////////////////////////////////////////////// 39 | //Initializing memory 40 | SinglyLinearLL::SinglyLinearLL() 41 | { 42 | head=NULL; 43 | ct=0; 44 | } 45 | /////////////////////////////////////////////////////////////////////////////////// 46 | //To get total number of nodes/elements present in the Linked List 47 | int SinglyLinearLL::Count() 48 | { 49 | ct=0; 50 | PNODE temp=head; 51 | while(temp!=NULL) 52 | { 53 | ct++; 54 | temp=temp->next; 55 | } 56 | return ct; 57 | } 58 | /////////////////////////////////////////////////////////////////////////////////// 59 | //Display entire Linked List 60 | void SinglyLinearLL::DisplayLL() 61 | { 62 | PNODE temp=head; 63 | while(temp!=NULL) 64 | { 65 | cout<data<<" "; 66 | temp=temp->next; 67 | } 68 | } 69 | 70 | /////////////////////////////////////////////////////////////////////////////////// 71 | //Inserting element at desired Position in the Linked List 72 | void SinglyLinearLL::Insert(int pos,int n) 73 | { 74 | int tot=Count(); 75 | PNODE temp=head; 76 | PNODE newn=new node; 77 | newn->data=n; 78 | newn->next=NULL; 79 | if(pos<1 || pos>tot+1) 80 | { 81 | cout<<"\nPosition invalid (element cannot be inserted"; 82 | return; 83 | } 84 | else if(pos==1) 85 | { 86 | //To insert element at beginning 87 | if(temp==NULL) 88 | { 89 | head=newn; 90 | }else if(temp!=NULL) 91 | { 92 | newn->next=head; 93 | head=newn; 94 | } 95 | } 96 | else if(pos==tot+1) 97 | { 98 | //To insert element at end 99 | if(head==NULL) 100 | { 101 | head=newn; 102 | } 103 | 104 | else 105 | { 106 | while(temp->next!=NULL) 107 | { 108 | temp=temp->next; 109 | } 110 | temp->next=newn; 111 | } 112 | } 113 | else 114 | { 115 | for(int i=1;inext; 118 | } 119 | newn->next=temp->next; 120 | temp->next=newn; 121 | } 122 | } 123 | /////////////////////////////////////////////////////////////////////////////////// 124 | // 125 | void SinglyLinearLL::DeleteDuplicate() 126 | { 127 | PNODE temp,curr; 128 | } 129 | /////////////////////////////////////////////////////////////////////////////////// 130 | //Destructor: to deallocate the used memory 131 | SinglyLinearLL::~SinglyLinearLL() 132 | { 133 | PNODE temp=head; 134 | if(head==NULL) 135 | return; 136 | else if(head!=NULL) 137 | { 138 | while(head!=NULL) 139 | { 140 | head=head->next; 141 | delete temp; 142 | temp=head; 143 | } 144 | } 145 | } 146 | /////////////////////////////////////////////////////////////////////////////////// 147 | //main function of the program 148 | /////////////////////////////////////////////////////////////////////////////////// 149 | int main() 150 | { 151 | SinglyLinearLL obj; 152 | obj.Insert(1,100); 153 | obj.Insert(2,200); 154 | obj.Insert(3,300); 155 | obj.Insert(4,400); 156 | obj.Insert(5,500); 157 | obj.Insert(6,600); 158 | 159 | cout<<"\nOriginal Linked List: "; 160 | obj.DisplayLL(); 161 | 162 | cout<<"\nModified Linked List (with duplicate elements removed): "; 163 | obj.DeleteDuplicate(); 164 | obj.DisplayLL(); 165 | 166 | return 0; 167 | } 168 | 169 | /////////////////////////////////////////////////////////////////////////////////// 170 | //End of program // 171 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Tricky_Problems/DetectLoop_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :To detect whether there is loop in (Singly Linear) Linked List // 4 | // or is it NULL terminated // 5 | //Language :C++ (Object Oriented Approach) // 6 | //Solution :Floyd Algorithm. // 7 | //SpaceComplexity :O(1) // 8 | //TimeComplexity :O(n) // 9 | /////////////////////////////////////////////////////////////////////////////////// 10 | 11 | #include 12 | using namespace std; 13 | 14 | /////////////////////////////////////////////////////////////////////////////////// 15 | // Slightly different approach ,with 'void' return type // 16 | /////////////////////////////////////////////////////////////////////////////////// 17 | 18 | typedef struct node 19 | { 20 | int data; 21 | struct node* next; 22 | }NODE,*PNODE; 23 | /////////////////////////////////////////////////////////////////////////////////// 24 | 25 | class SinglyLinearLL 26 | { 27 | PNODE head; 28 | int ct; 29 | public: 30 | SinglyLinearLL(); //constructor 31 | ~SinglyLinearLL(); //To deallocate the used memory 32 | 33 | void Insert(int,int); 34 | 35 | void Delete(int); 36 | 37 | void DetectLoop(); //to reverse the given Linked List 38 | 39 | void DisplayLL(); //to Display the Linked List 40 | int Count(); //Display total number of nodes present in Linked List 41 | }; 42 | /////////////////////////////////////////////////////////////////////////////////// 43 | //Initializing memory 44 | SinglyLinearLL::SinglyLinearLL() 45 | { 46 | head=NULL; 47 | ct=0; 48 | } 49 | /////////////////////////////////////////////////////////////////////////////////// 50 | //To get total number of nodes/elements present in the Linked List 51 | int SinglyLinearLL::Count() 52 | { 53 | ct=0; 54 | PNODE temp=head; 55 | while(temp!=NULL) 56 | { 57 | ct++; 58 | temp=temp->next; 59 | } 60 | return ct; 61 | } 62 | /////////////////////////////////////////////////////////////////////////////////// 63 | //Display entire Linked List 64 | void SinglyLinearLL::DisplayLL() 65 | { 66 | PNODE temp=head; 67 | while(temp!=NULL) 68 | { 69 | cout<data<<" "; 70 | temp=temp->next; 71 | } 72 | } 73 | 74 | /////////////////////////////////////////////////////////////////////////////////// 75 | //Inserting element at desired Position in the Linked List 76 | void SinglyLinearLL::Insert(int pos,int n) 77 | { 78 | int tot=Count(); 79 | PNODE temp=head; 80 | PNODE newn=new node; 81 | newn->data=n; 82 | newn->next=NULL; 83 | if(pos<1 || pos>tot+1) 84 | { 85 | cout<<"\nPosition invalid (element cannot be inserted"; 86 | return; 87 | } 88 | else if(pos==1) 89 | { 90 | //To insert element at beginning 91 | if(temp==NULL) 92 | { 93 | head=newn; 94 | }else if(temp!=NULL) 95 | { 96 | newn->next=head; 97 | head=newn; 98 | } 99 | } 100 | else if(pos==tot+1) 101 | { 102 | //To insert element at end 103 | if(head==NULL) 104 | { 105 | head=newn; 106 | } 107 | 108 | else 109 | { 110 | while(temp->next!=NULL) 111 | { 112 | temp=temp->next; 113 | } 114 | temp->next=newn; 115 | } 116 | } 117 | else 118 | { 119 | for(int i=1;inext; 122 | } 123 | newn->next=temp->next; 124 | temp->next=newn; 125 | } 126 | } 127 | /////////////////////////////////////////////////////////////////////////////////// 128 | // 129 | void SinglyLinearLL::DetectLoop() 130 | { 131 | PNODE slowPtr=head,fastPtr=head; 132 | while(slowPtr && fastPtr && fastPtr->next) 133 | { 134 | slowPtr=slowPtr->next; 135 | fastPtr=fastPtr->next->next; 136 | if(slowPtr==fastPtr){ 137 | cout<<"\nLoop Detected in linked list"; 138 | break; 139 | }} 140 | cout<<"\nNo loop Detected."; 141 | } 142 | /////////////////////////////////////////////////////////////////////////////////// 143 | //Destructor: to deallocate the used memory 144 | SinglyLinearLL::~SinglyLinearLL() 145 | { 146 | PNODE temp=head; 147 | if(head==NULL) 148 | return; 149 | else if(head!=NULL) 150 | { 151 | while(head!=NULL) 152 | { 153 | head=head->next; 154 | delete temp; 155 | temp=head; 156 | } 157 | } 158 | } 159 | /////////////////////////////////////////////////////////////////////////////////// 160 | //main function of the program 161 | /////////////////////////////////////////////////////////////////////////////////// 162 | int main() 163 | { 164 | SinglyLinearLL obj; 165 | obj.Insert(1,100); 166 | obj.Insert(2,200); 167 | obj.Insert(3,300); 168 | obj.Insert(4,400); 169 | obj.Insert(5,500); 170 | obj.Insert(6,600); 171 | 172 | cout<<"\nOriginal Linked List: "; 173 | obj.DisplayLL(); 174 | 175 | obj.DetectLoop(); 176 | 177 | return 0; 178 | } 179 | 180 | /////////////////////////////////////////////////////////////////////////////////// 181 | //End of program // 182 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Tricky_Problems/FindNth_node_LinkedList.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////// 2 | //Name :Manasi Mohan Wader // 3 | //Program :To Find nth node from end/start of a (Singly Linear) Linked List. // 4 | //Language :C++ (Object Oriented Approach) // 5 | /////////////////////////////////////////////////////////////////////////////////// 6 | 7 | #include 8 | using namespace std; 9 | 10 | /////////////////////////////////////////////////////////////////////////////////// 11 | // Slightly different approach ,with 'void' return type // 12 | /////////////////////////////////////////////////////////////////////////////////// 13 | 14 | typedef struct node 15 | { 16 | int data; 17 | struct node* next; 18 | }NODE,*PNODE; 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | 21 | class SinglyLinearLL 22 | { 23 | PNODE head; 24 | int ct; 25 | public: 26 | SinglyLinearLL(); //constructor 27 | ~SinglyLinearLL(); //To deallocate the used memory 28 | 29 | void Insert(int,int); 30 | 31 | void Delete(int); 32 | 33 | int NodeFromEnd(int); //get nth node from ending of Linked List 34 | int NodeFromStart(int); //get nth node from starting of Linked List 35 | 36 | void DisplayLL(); //to Display the Linked List 37 | int Count(); //Display total number of nodes present in Linked List 38 | }; 39 | /////////////////////////////////////////////////////////////////////////////////// 40 | //Initializing memory 41 | SinglyLinearLL::SinglyLinearLL() 42 | { 43 | head=NULL; 44 | ct=0; 45 | } 46 | /////////////////////////////////////////////////////////////////////////////////// 47 | //To get total number of nodes/elements present in the Linked List 48 | int SinglyLinearLL::Count() 49 | { 50 | ct=0; 51 | PNODE temp=head; 52 | while(temp!=NULL) 53 | { 54 | ct++; 55 | temp=temp->next; 56 | } 57 | return ct; 58 | } 59 | /////////////////////////////////////////////////////////////////////////////////// 60 | //Display entire Linked List 61 | void SinglyLinearLL::DisplayLL() 62 | { 63 | PNODE temp=head; 64 | while(temp!=NULL) 65 | { 66 | cout<data<<" "; 67 | temp=temp->next; 68 | } 69 | } 70 | 71 | /////////////////////////////////////////////////////////////////////////////////// 72 | //Inserting element at desired Position in the Linked List 73 | void SinglyLinearLL::Insert(int pos,int n) 74 | { 75 | int tot=Count(); 76 | PNODE temp=head; 77 | PNODE newn=new node; 78 | newn->data=n; 79 | newn->next=NULL; 80 | if(pos<1 || pos>tot+1) 81 | { 82 | cout<<"\nPosition invalid (element cannot be inserted"; 83 | return; 84 | } 85 | else if(pos==1) 86 | { 87 | //To insert element at beginning 88 | if(temp==NULL) 89 | { 90 | head=newn; 91 | }else if(temp!=NULL) 92 | { 93 | newn->next=head; 94 | head=newn; 95 | } 96 | } 97 | else if(pos==tot+1) 98 | { 99 | //To insert element at end 100 | if(head==NULL) 101 | { 102 | head=newn; 103 | } 104 | 105 | else 106 | { 107 | while(temp->next!=NULL) 108 | { 109 | temp=temp->next; 110 | } 111 | temp->next=newn; 112 | } 113 | } 114 | else 115 | { 116 | for(int i=1;inext; 119 | } 120 | newn->next=temp->next; 121 | temp->next=newn; 122 | } 123 | } 124 | /////////////////////////////////////////////////////////////////////////////////// 125 | // 126 | int SinglyLinearLL::NodeFromStart(int pos) 127 | { 128 | PNODE temp=head; 129 | for(int i=0;inext; 132 | } 133 | return temp->data; 134 | } 135 | /////////////////////////////////////////////////////////////////////////////////// 136 | // 137 | int SinglyLinearLL::NodeFromEnd(int pos) 138 | { 139 | PNODE temp=head; 140 | int tot=Count()-pos; 141 | if(head==NULL) 142 | { 143 | return ; 144 | } 145 | else{ 146 | for(int i=0;inext; 149 | } 150 | return temp->data; 151 | } 152 | } 153 | /////////////////////////////////////////////////////////////////////////////////// 154 | //Destructor: to deallocate the used memory 155 | SinglyLinearLL::~SinglyLinearLL() 156 | { 157 | PNODE temp=head; 158 | if(head==NULL) 159 | return; 160 | else if(head!=NULL) 161 | { 162 | while(head!=NULL) 163 | { 164 | head=head->next; 165 | delete temp; 166 | temp=head; 167 | } 168 | } 169 | } 170 | /////////////////////////////////////////////////////////////////////////////////// 171 | //main function of the program 172 | /////////////////////////////////////////////////////////////////////////////////// 173 | int main() 174 | { 175 | SinglyLinearLL obj; 176 | obj.Insert(1,100); 177 | obj.Insert(2,200); 178 | obj.Insert(3,300); 179 | obj.Insert(4,400); 180 | obj.Insert(5,500); 181 | obj.Insert(6,600); 182 | 183 | cout<<"\nLinked List: "; 184 | obj.DisplayLL(); 185 | 186 | cout<<"\n1st Node from End: "<0) 10 | // { 11 | // N=N/2; 12 | // } 13 | // return N; 14 | // } 15 | // }; 16 | 17 | //Optimize solution to find halving of no 18 | class Solution{ 19 | public: 20 | int mthHalf(int N, int M){ 21 | return N/pow(2,M-1); 22 | } 23 | }; -------------------------------------------------------------------------------- /Tricky_Problems/KaratsubaAlgo.cpp: -------------------------------------------------------------------------------- 1 | // Given two binary strings A and B that represent value of two integers, find the product of two strings in Decimal Value. 2 | 3 | //Solution: 4 | class Solution { 5 | public: 6 | long long int karatsubaAlgo(string A, string B) { 7 | long long int num1=0,num2=0; 8 | int k=0; 9 | for(int i=A.length()-1;i>=0;i--) 10 | { 11 | if(A[i] == '1') 12 | num1 += pow(2,k)*1; 13 | k++; 14 | //cout<=0;i--) 18 | { 19 | if(B[i] == '1') 20 | num2 += pow(2,k)*1; 21 | //cout<v; 9 | for(int i=0;i& nums) { 8 | vectorv; 9 | int ct=0; 10 | for(int i=0;i printNonRepeated(int arr[],int n) 9 | { 10 | vectorv; 11 | unordered_mapm; 12 | for(int i=0;i 8 | using namespace std; 9 | 10 | /////////////////////////////////////////////////////////////////////////////////// 11 | // Slightly different approach ,with 'void' return type // 12 | /////////////////////////////////////////////////////////////////////////////////// 13 | 14 | typedef struct node 15 | { 16 | int data; 17 | struct node* next; 18 | }NODE,*PNODE; 19 | /////////////////////////////////////////////////////////////////////////////////// 20 | 21 | class SinglyLinearLL 22 | { 23 | PNODE head; 24 | int ct; 25 | public: 26 | SinglyLinearLL(); //constructor 27 | ~SinglyLinearLL(); //To deallocate the used memory 28 | 29 | void Insert(int,int); 30 | 31 | void Delete(int); 32 | 33 | void ReverseLinkedList(); //to reverse the given Linked List 34 | 35 | void DisplayLL(); //to Display the Linked List 36 | int Count(); //Display total number of nodes present in Linked List 37 | }; 38 | /////////////////////////////////////////////////////////////////////////////////// 39 | //Initializing memory 40 | SinglyLinearLL::SinglyLinearLL() 41 | { 42 | head=NULL; 43 | ct=0; 44 | } 45 | /////////////////////////////////////////////////////////////////////////////////// 46 | //To get total number of nodes/elements present in the Linked List 47 | int SinglyLinearLL::Count() 48 | { 49 | ct=0; 50 | PNODE temp=head; 51 | while(temp!=NULL) 52 | { 53 | ct++; 54 | temp=temp->next; 55 | } 56 | return ct; 57 | } 58 | /////////////////////////////////////////////////////////////////////////////////// 59 | //Display entire Linked List 60 | void SinglyLinearLL::DisplayLL() 61 | { 62 | PNODE temp=head; 63 | while(temp!=NULL) 64 | { 65 | cout<data<<" "; 66 | temp=temp->next; 67 | } 68 | } 69 | 70 | /////////////////////////////////////////////////////////////////////////////////// 71 | //Inserting element at desired Position in the Linked List 72 | void SinglyLinearLL::Insert(int pos,int n) 73 | { 74 | int tot=Count(); 75 | PNODE temp=head; 76 | PNODE newn=new node; 77 | newn->data=n; 78 | newn->next=NULL; 79 | if(pos<1 || pos>tot+1) 80 | { 81 | cout<<"\nPosition invalid (element cannot be inserted"; 82 | return; 83 | } 84 | else if(pos==1) 85 | { 86 | //To insert element at beginning 87 | if(temp==NULL) 88 | { 89 | head=newn; 90 | }else if(temp!=NULL) 91 | { 92 | newn->next=head; 93 | head=newn; 94 | } 95 | } 96 | else if(pos==tot+1) 97 | { 98 | //To insert element at end 99 | if(head==NULL) 100 | { 101 | head=newn; 102 | } 103 | 104 | else 105 | { 106 | while(temp->next!=NULL) 107 | { 108 | temp=temp->next; 109 | } 110 | temp->next=newn; 111 | } 112 | } 113 | else 114 | { 115 | for(int i=1;inext; 118 | } 119 | newn->next=temp->next; 120 | temp->next=newn; 121 | } 122 | } 123 | /////////////////////////////////////////////////////////////////////////////////// 124 | // 125 | void SinglyLinearLL::ReverseLinkedList() 126 | { 127 | PNODE temp,curr; 128 | } 129 | /////////////////////////////////////////////////////////////////////////////////// 130 | //Destructor: to deallocate the used memory 131 | SinglyLinearLL::~SinglyLinearLL() 132 | { 133 | PNODE temp=head; 134 | if(head==NULL) 135 | return; 136 | else if(head!=NULL) 137 | { 138 | while(head!=NULL) 139 | { 140 | head=head->next; 141 | delete temp; 142 | temp=head; 143 | } 144 | } 145 | } 146 | /////////////////////////////////////////////////////////////////////////////////// 147 | //main function of the program 148 | /////////////////////////////////////////////////////////////////////////////////// 149 | int main() 150 | { 151 | SinglyLinearLL obj; 152 | obj.Insert(1,100); 153 | obj.Insert(2,200); 154 | obj.Insert(3,300); 155 | obj.Insert(4,400); 156 | obj.Insert(5,500); 157 | obj.Insert(6,600); 158 | 159 | cout<<"\nOriginal Linked List: "; 160 | obj.DisplayLL(); 161 | 162 | cout<<"\nReversed Linked List: "; 163 | obj.ReverseLinkedList(); 164 | obj.DisplayLL(); 165 | 166 | return 0; 167 | } 168 | 169 | /////////////////////////////////////////////////////////////////////////////////// 170 | //End of program // 171 | /////////////////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /Tricky_Problems/SecondLargestNo.cp: -------------------------------------------------------------------------------- 1 | int getMiddle(Node *head) 2 | { 3 | if(!head) return 0; 4 | Node* slow = head; 5 | Node* fast = head->next; 6 | while(fast!=NULL) 7 | { 8 | fast=fast->next; 9 | if(fast!=NULL) 10 | fast=fast->next; 11 | slow=slow->next; 12 | } 13 | return slow->data; 14 | } 15 | -------------------------------------------------------------------------------- /Tricky_Problems/TotalRectangles.cpp: -------------------------------------------------------------------------------- 1 | //Find total number of Rectangles (excluding squares) in a N*N cheesboard. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | long int t; 9 | cin>>t; 10 | while(t>0) 11 | { 12 | long long int num; 13 | cin>>num; 14 | 15 | long long p = (num*(num+1)/2)*(num*(num+1)/2); 16 | 17 | long long s=0; 18 | while(num>0) 19 | { 20 | s= s +(num*num); 21 | num--; 22 | } 23 | cout<<(p-s)<<"\n"; 24 | t--; 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Tricky_Problems/TotalSquares.cpp: -------------------------------------------------------------------------------- 1 | //Find total number of Squares in a N*N cheesboard. 2 | 3 | #include 4 | #define lli long long int 5 | using namespace std; 6 | 7 | int main() { 8 | int t; 9 | cin>>t; 10 | while(t>0) 11 | { 12 | int n; 13 | cin>>n; 14 | lli sum = 0; 15 | while(n>0) 16 | { 17 | sum = sum + (n*n); 18 | n--; 19 | } 20 | cout< A, int N, int X) 10 | { 11 | if (X == 0 || X == 1) 12 | return false; 13 | int j = 0; 14 | for (int i = 0; i < N; i++) 15 | { 16 | int temp = X - A[i]; 17 | int ex = A[i]; 18 | A[i] = 0; 19 | if (temp > 0) 20 | { 21 | //cout<<"temp:"<