├── 0 - 1 Knapsack Problem ├── 1's Complement ├── 2. Power Set Using Recursion ├── Activity Selection ├── Adding Array Elements ├── Ana and Sweets ├── Anagram of String ├── Ancestors in Binary Tree ├── Array Subset of another array ├── BFS of graph ├── BST to max heap ├── Binary Array Sorting ├── Binary Tree to BST ├── Bipartite Graph ├── Bit Difference ├── Bitonic Point ├── Bleak Numbers ├── Bridge Edge in Graph ├── Brothers From Different Roots ├── Change Bits ├── Change all even bits in a number to 0 ├── Check If Circular Linked List ├── Check if Linked List is Palindrome ├── Check if actual binary representation of a number is palindrome ├── Check if array contains contiguous integers with duplicates allowed ├── Check if frequencies can be equal ├── Check if string is rotated by two places ├── Check set bits ├── Check whether K-th bit is set or not ├── Circular Linked List Traversal ├── Closest Neighbor in BST ├── Common elements ├── Completing tasks ├── Count More than n └── k OccurencesCount More than n ├── Count Pairs whose sum is equal to X ├── Count distinct elements in every window ├── Count nodes of linked list ├── Count number of hops ├── Count pairs Sum in matrices ├── Count pairs Sum in matrices without extraspace(using 2 pointers) ├── Count the number of possible triangles ├── Count total set bits ├── Count total set bits from 1 to n ├── Counting elements in two arrays ├── DFS of Graph ├── Delete Alternate Nodes ├── Delete Middle of Linked List ├── Delete a Node in Single Linked List ├── Delete a node from BST ├── Delete keys in a Linked list ├── Detect cycle in a directed graph ├── Detect cycle in an undirected graph ├── Does array represent Heap ├── Equal to product ├── Equilibrium Point ├── Even Subsets ├── Excel Sheet | Part - 1 ├── Exceptionally odd ├── Fake Profile ├── Find a pair with given target in BST ├── Find first set bit ├── Find first set bit 1 ├── Find n └── k th node in Linked list ├── Find position of set bit ├── Find the Closest Element in BST ├── Find the Sum of Last N nodes of the Linked List ├── Find the most frequent digit ├── Find the number of islands ├── Find unique element ├── Finding the numbers ├── First and last Bit ├── First and last occurrences of X ├── First and last occurrences of X function ├── First negative integer in every window of size k ├── First non-repeating character in a stream ├── Fixing Two nodes of a BST ├── Fractional Knapsack ├── Frequencies of Limited Range Array Elements ├── Game with String ├── Get minimum element from stack ├── Get minimum element from stack extra space allowed ├── Group Anagrams Together ├── Heap Sort ├── Height of Heap ├── Help Nobita ├── IPL 2021 - Match Day 2 ├── Implement Atoi ├── Implement strstr ├── Implementing Dijkstra Algorithm ├── In First But Second ├── Index Of an Extra Element ├── Insert a node in a BST ├── Insert in Middle of Linked List ├── Intersection of Two Linked Lists ├── Intersection of two arrays ├── Intersection of two arrays 1 ├── Is Binary Number Multiple of 3 ├── Is Binary Tree Heap ├── Ishaan Loves Chocolates ├── K largest element in a stream ├── K largest elements ├── Kadane's Algorithm ├── Key Pair using 2 pointer ├── Key Pair using map ├── Kth LSB ├── Kth element in Matrix ├── Kth largest element in BST ├── Kth smallest element ├── LRU Cache ├── Ladoo problem ├── Ladoo problem solved using map(hashing) ├── Largest Rectangle in Histogram (interviewbit) ├── Largest number possible ├── Largest subarray of 0's and 1's ├── Largest subarray with 0 sum ├── Leaf-Similar Trees ├── Level order traversal(BFS) ├── Linked List Length Even or Odd? ├── Longest Consecutive 1's ├── Longest Continuous Increasing Subsequence ├── Longest Repeating Subsequence ├── Longest consecutive subsequence ├── Longest consecutive subsequence (full implementation without pre defined function) ├── Longest subarray with sum divisible by K ├── Love For The Twins ├── Lowest Common Ancestor in a BST ├── Lowest Common Ancestor of a Binary Tree ├── Magical Number ├── Majority Element ├── Match specific pattern ├── Max Circular Subarray Sum ├── Max Sum Subarray of size K ├── Max and Second Max ├── Max distance between same elements ├── Max sum in the configuration ├── Maximize Toys ├── Maximum Difference ├── Maximum difference Indexes ├── Maximum distinct elements after removing K elements ├── Maximum number of characters between any two same character ├── Median of 2 Sorted Arrays of Different Sizes ├── Median of BST ├── Merge Without Extra Space ├── Merge k Sorted Arrays ├── Min sum formed by digits ├── Minimum Cost of ropes ├── Minimum Deletions ├── Minimum Distinct Ids ├── Minimum Number in a sorted rotated array ├── Minimum Platforms ├── Minimum Spanning Tree using prims algo ├── Minimum distance between two numbers ├── Minimum indexed character ├── Minimum number of deletions and insertions ├── Minimum sum of subarray ├── Missing number in array ├── Missing number in shuffled array ├── Mother Vertex ├── Multiply two strings ├── N meetings in one room ├── Nearly Sorted Algorithm ├── Needle in a Haystack ├── Next Greater Element ├── Nth node from end of linked list ├── Number of paths ├── Numbers containing 1, 2 and 3 ├── Numbers having two adjacent set bits ├── Occurence of an integer in a Linked List ├── Odd or Even using bitmasking ├── Pairs violating BST property ├── Pairwise swap elements of a linked list ├── Parenthesis Checker ├── Parity of unsigned integer ├── Path Sum ├── Path Sum II ├── Permutation with Spaces ├── Police and Thieves ├── Populating Next Right Pointers in Each Node ├── Possible Words From Phone Digits ├── Power Of Numbers ├── Power Set ├── Power of 2 using bitwise ├── Power of Four ├── Predict the Winner ├── Print 1 To N Without Loop ├── Print Common Nodes in two BSTs ├── Print adjacency list ├── Print adjacency list of Bidirectional Graph ├── Product array puzzle ├── Queue using stack ├── Queue using two Stacks ├── Rat in a Maze Problem - I ├── Rearrange an array with O(1) extra space ├── Relative Sorting ├── Remove Duplicates ├── Remove duplicate element from sorted Linked List ├── Remove duplicate elements from sorted Array ├── Remove duplicates in small prime array ├── Reverse a Doubly Linked List ├── Reverse a Linked List in groups of given size ├── Reverse a linked list ├── Reverse each word in a given string ├── Rightmost different bit ├── Rod Cutting ├── Rotate a Linked List ├── Rotate doubly Linked List ├── Rotten Oranges ├── Run Length Encoding ├── Search an element in sorted and rotated array ├── Search in a row-column sorted Matrix ├── Searching an element in a sorted array ├── Sherlock a Detective ├── Shop in Candy Store ├── Shortest Source to Destination Path ├── Smallest Positive Integer that can not be represented as Sum ├── Smallest Positive missing number ├── Smallest number by rearranging digits of a given number ├── Smallest number on left ├── Smallest number repeating K times ├── Smallest window in a string containing all the characters of another string ├── Solve the Sudoku ├── Sort an array of 0s, 1s and 2s ├── Sorting Elements of an Array by Frequency ├── Stack using two queues ├── Steps by Knight ├── Sub-Array sum divisible by K ├── Subarray with 0 sum ├── Subarrays with equal 1s and 0s ├── Subarrays with sum K ├── Subset Sum Problem ├── Sum of Root To Leaf Binary Numbers ├── Sum of XOR of all pairs ├── Sum of bit differences ├── Sum of k smallest elements in BST ├── Sum of leaf nodes in BST ├── Sum of two numbers without using arithmetic operators ├── Swap all odd and even bits ├── The Celebrity Problem ├── Theft at World Bank ├── Three Sum Closest ├── Tiger Zinda Hai ├── Top View of Binary Tree ├── Topological sort ├── Transfiguration ├── Trapping Rain Water ├── Triplet Sum in Array ├── Triplets with sum with given range ├── Twice counter ├── Union of Two Linked Lists ├── Union of Two Sorted Arrays ├── Union of two arrays ├── Unit Area of largest region of 1's ├── Using Recursion Check if array is sorted ├── Valid Expression ├── Winner of an election ├── Zero Sum Subarrays ├── check for Full binary tree ├── find the length of the longest Sub-Array with the sum of the elements equal to the given value K ├── greatest smaller on left ├── k largest elements ├── k-th smallest element in BST ├── linked list of strings forms a palindrome ├── no sibling nodes print ├── pth common ancestor in BST └── set-bits and number /1's Complement: -------------------------------------------------------------------------------- 1 | Given an N bit binary number, find the 1's complement of the number. The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa). 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 3 8 | S = 101 9 | Output: 10 | 010 11 | Explanation: 12 | We get the output by converting 1's in S 13 | to 0 and 0s to 1 14 | Example 2: 15 | 16 | Input: 17 | N = 2 18 | S = 10 19 | Output: 20 | 01 21 | Explanation: 22 | We get the output by converting 1's in S 23 | to 0 and 0s to 1 24 | 25 | Your Task: 26 | You don't need to read input or print anything. Your task is to complete the function onesComplement() which takes the binary string S, its size N as input parameters and returns 1's complement of S of size N. 27 | 28 | 29 | Expected Time Complexity: O(N) 30 | Expected Space Complexity: O(N) 31 | 32 | 33 | Constraints: 34 | 1<=N<=100 35 | // { Driver Code Starts 36 | 37 | #include 38 | using namespace std; 39 | 40 | // } Driver Code Ends 41 | 42 | 43 | 44 | class Solution{ 45 | public: 46 | string onesComplement(string S,int N){ 47 | string s1; 48 | for( int i=0;i>t; 69 | while(t--) 70 | { 71 | int n; 72 | cin>>n; 73 | string s; 74 | cin>>s; 75 | Solution ob; 76 | cout< 38 | using namespace std; 39 | 40 | int main() 41 | { 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | int n; 47 | cin>>n; 48 | int arr1[n]; 49 | int arr2[n]; 50 | int count=1; 51 | vector> v; 52 | for(int i=0;i>arr1[i]; 55 | } 56 | 57 | for(int i=0;i>arr2[i]; 61 | } 62 | 63 | for(int i=0;i 40 | using namespace std; 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int n,k,flag=0; 48 | cin>>n; 49 | map m; 50 | 51 | for(int i=0;i>x; 55 | m[x]++; 56 | 57 | } 58 | 59 | cin>>k; 60 | 61 | for(auto i=m.begin();i!=m.end();i++) 62 | { 63 | if(i->second>(2*k)) 64 | { 65 | flag=1; 66 | break; 67 | } 68 | 69 | } 70 | 71 | if(flag==1) 72 | cout<<"0"< 31 | using namespace std; 32 | const int CHARS = 26; 33 | 34 | // function to calculate minimum numbers of characters 35 | // to be removed to make two strings anagram 36 | int remAnagram(string str1, string str2); 37 | 38 | // Driver program to run the case 39 | int main() 40 | { 41 | int t; 42 | cin>>t; 43 | while(t--) 44 | { 45 | string str1,str2; 46 | cin>>str1>>str2; 47 | cout << remAnagram(str1, str2); 48 | cout< 36 | using namespace std; 37 | int main() 38 | { 39 | int t; 40 | cin>>t; 41 | while(t--) 42 | { 43 | int m,n,count=0; 44 | cin>>m>>n; 45 | int arr1[m],arr2[n]; 46 | map mapee; 47 | for(int i=0;i>arr1[i]; 49 | 50 | for(int j=0;j>arr2[j]; 53 | mapee[arr2[j]]=1; 54 | } 55 | 56 | for(int i=0;i 34 | using namespace std; 35 | vector SortBinaryArray(vector binArray); 36 | 37 | 38 | // } Driver Code Ends 39 | //User function template for C++ 40 | 41 | // binArray is an array that consists only 0s and 1s 42 | // return sorted binary array 43 | vector SortBinaryArray(vector binArray) 44 | { 45 | int count=0; 46 | for(int i=0;i>t; 67 | 68 | while(t--) 69 | { 70 | int n; 71 | cin>>n; 72 | vector binArray(n); 73 | 74 | for(int i = 0; i < n; i++) 75 | cin>>binArray[i]; 76 | 77 | vector result = SortBinaryArray(binArray); 78 | for(int i=0; i 35 | using namespace std; 36 | 37 | 38 | // } Driver Code Ends 39 | 40 | 41 | //User function Template for C++ 42 | 43 | // Function to find number of bits to be flip 44 | // to convert A to B 45 | int countBitsFlip(int a, int b){ 46 | int count=0; 47 | while(a>0 || b>0) 48 | { 49 | if((a&1)!=(b&1)) 50 | { 51 | count++; 52 | } 53 | a=a>>1; 54 | b=b>>1; 55 | 56 | } 57 | return count; 58 | } 59 | 60 | // { Driver Code Starts. 61 | 62 | // Driver Code 63 | int main() 64 | { 65 | int t; 66 | cin>>t;// input the testcases 67 | while(t--) //while testcases exist 68 | { 69 | int a,b; 70 | cin>>a>>b; //input a and b 71 | 72 | cout< 30 | 31 | using namespace std; 32 | 33 | 34 | // } Driver Code Ends 35 | //User function template for C++ 36 | class Solution{ 37 | public: 38 | 39 | int findMaximum(int arr[], int n) { 40 | int l=0; 41 | int r=n-1; 42 | while(l<=r) 43 | { 44 | int mid=l+(r-l)/2; 45 | if(arr[mid]>arr[mid-1] && arr[mid]>arr[mid+1]) 46 | return arr[mid]; 47 | else if(arr[mid]arr[mid-1] && arr[mid+1]>arr[mid]) 53 | { 54 | l=mid+1; 55 | } 56 | } 57 | 58 | } 59 | }; 60 | 61 | // { Driver Code Starts. 62 | 63 | int main() { 64 | int t; 65 | cin >> t; 66 | while (t--) { 67 | int n, i; 68 | cin >> n; 69 | int arr[n]; 70 | for (i = 0; i < n; i++) { 71 | cin >> arr[i]; 72 | } 73 | Solution ob; 74 | auto ans = ob.findMaximum(arr, n); 75 | cout << ans << "\n"; 76 | } 77 | return 0; 78 | } 79 | // } Driver Code Ends 80 | -------------------------------------------------------------------------------- /Bleak Numbers: -------------------------------------------------------------------------------- 1 | Given an integer, check whether it is Bleak or not. 2 | 3 | A number ‘n’ is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x. 4 | 5 | Example 1: 6 | 7 | Input: 4 8 | Output: 1 9 | Explanation: There is no any possible x 10 | such that x + countSetbit(x) = 4 11 | Example 2: 12 | 13 | Input: 3 14 | Output: 0 15 | Explanation: 3 is not a Bleak number as 16 | 2 + countSetBit(2) = 3. 17 | 18 | 19 | Your Task: 20 | You don't need to read or print anything. Your task is to complete the function is_bleak() which takes n as input parameter and returns 1 if n is not a Bleak number otherwise returns 0. 21 | 22 | 23 | Expected Time Complexity: O(log(n) * log(n)) 24 | Expected Space Complexity: O(1) 25 | 26 | 27 | Constraints: 28 | 1 <= n <= 104// { Driver Code Starts 29 | 30 | 31 | #include 32 | using namespace std; 33 | 34 | // } Driver Code Ends 35 | 36 | 37 | 38 | class Solution 39 | { 40 | public: 41 | int is_bleak(int n) 42 | { 43 | int count=0; 44 | for(int i=n;i>=1;i--) 45 | { 46 | int x=i; 47 | while(x>0) 48 | { 49 | x=x&(x-1); 50 | count++; 51 | } 52 | if((i+count)==n) 53 | return 0; 54 | else i; 55 | count=0; 56 | } 57 | return 1; 58 | } 59 | }; 60 | 61 | 62 | // { Driver Code Starts. 63 | int main(){ 64 | int T; 65 | cin >> T; 66 | while(T--) 67 | { 68 | int n; 69 | cin >> n; 70 | Solution ob; 71 | int ans = ob.is_bleak(n); 72 | cout << ans << "\n"; 73 | } 74 | return 0; 75 | } 76 | // } Driver Code Ends 77 | -------------------------------------------------------------------------------- /Brothers From Different Roots: -------------------------------------------------------------------------------- 1 | Given two BSTs containing N1 and N2 distinct nodes respectively and given a value x. Your task is to complete the function countPairs(), that returns the count of all pairs from both the BSTs whose sum is equal to x. 2 | 3 | Examples: 4 | 5 | Input : BST 1: 5 6 | / \ 7 | 3 7 8 | / \ / \ 9 | 2 4 6 8 10 | 11 | BST 2: 10 12 | / \ 13 | 6 15 14 | / \ / \ 15 | 3 8 11 18 16 | x = 16 17 | 18 | Output : 3 19 | The pairs are: 20 | (5, 11), (6, 10) and (8, 8) 21 | Input: 22 | The function takes three arguments as input, first the reference pointer to the root(root1) of the BST1, then reference pointer to the root(root2) of the BST2 and last the element X. 23 | There will be T test cases and for each test case the function will be called separately. 24 | 25 | Output: 26 | For each test cases print the required number of pairs on new line. 27 | 28 | Constraints: 29 | 1<=T<=100 30 | 1<=N<=103 31 | 32 | Example: 33 | Input: 34 | 2 35 | 7 36 | 5 3 7 2 4 6 8 37 | 7 38 | 10 6 15 3 8 11 18 39 | 16 40 | 6 41 | 10 20 30 40 5 1 42 | 5 43 | 25 35 10 15 5 44 | 30 45 | Output: 46 | 3 47 | 2 48 | 49 | int count1=0; 50 | void find( Node* root2, int y) 51 | { 52 | if(root2==NULL) 53 | return; 54 | if(root2->data==y) 55 | count1++; 56 | if(root2->data>y) 57 | { 58 | find(root2->left,y); 59 | } 60 | else if(root2->dataright, y); 63 | } 64 | 65 | } 66 | void fun(Node* root1, Node* root2, int x) 67 | { 68 | if(root1==NULL) 69 | return; 70 | fun(root1->left,root2,x); 71 | 72 | int y= x-root1->data; 73 | if(y>=0) 74 | { 75 | find(root2,y); 76 | } 77 | fun(root1->right,root2,x); 78 | } 79 | 80 | int countPairs(Node* root1, Node* root2, int x) 81 | { 82 | count1=0; 83 | fun(root1,root2,x); 84 | return count1; 85 | 86 | } 87 | -------------------------------------------------------------------------------- /Change Bits: -------------------------------------------------------------------------------- 1 | Given a number N, complete the following tasks, 2 | Task 1. Generate a new number from N by changing the zeroes in the binary representation of N to 1. 3 | Task 2. Find the difference between N and the newly generated number. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: N = 8 10 | Output: 7 15 11 | Explanation: 12 | There are 3 zeroes in binary representation 13 | of 8. Changing them to 1 will give 15. 14 | Difference between these two is 7. 15 | 16 | Example 2: 17 | 18 | Input: N = 6 19 | Output: 1 7 20 | Explanation: 21 | There is 1 zero in binary representation 22 | of 6. Changing it to 1 will give 7. 23 | Difference between these two is 1. 24 | 25 | 26 | Your Task: 27 | You don't need to read input or print anything. Your task is to complete the function changeBits() which takes an integer N as input parameter and returns a list of two integers containing the difference and the generated number respectively. 28 | 29 | 30 | 31 | Expected Time Complexity: O(log(N)) 32 | Expected Auxiliary Space: O(1) 33 | 34 | 35 | 36 | Constraints: 37 | 0 <= N <= 108 38 | // { Driver Code Starts 39 | //Initial Template for C++ 40 | 41 | #include 42 | using namespace std; 43 | 44 | // } Driver Code Ends 45 | 46 | 47 | //User function Template for C++ 48 | 49 | class Solution { 50 | public: 51 | vector changeBits(int N) { 52 | int count=0,i=0; 53 | int num=N; 54 | vector v; 55 | while(N) 56 | { 57 | if((N&1)==0) 58 | { 59 | count=count+pow(2,i); 60 | } 61 | i++; 62 | N=N>>1; 63 | } 64 | v.push_back(count); 65 | v.push_back(count+num); 66 | return v; 67 | } 68 | }; 69 | 70 | // { Driver Code Starts. 71 | int main() { 72 | int t; 73 | cin >> t; 74 | while (t--) { 75 | int N; 76 | cin>>N; 77 | Solution ob; 78 | auto ans = ob.changeBits(N); 79 | cout< 30 | using namespace std; 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | // User function Template for C++ 36 | class Solution { 37 | public: 38 | long long int convertEvenBitToZero(long long int n) { 39 | return (n& 0xaaaaaaaa); 40 | 41 | } 42 | }; 43 | 44 | // { Driver Code Starts. 45 | int main() { 46 | int t; 47 | cin >> t; 48 | while (t--) { 49 | long long int n; 50 | cin >> n; 51 | Solution ob; 52 | cout << ob.convertEvenBitToZero(n) << endl; 53 | } 54 | return 0; 55 | } 56 | // } Driver Code Ends 57 | -------------------------------------------------------------------------------- /Check if actual binary representation of a number is palindrome: -------------------------------------------------------------------------------- 1 | Check if actual binary representation of a number is palindrome 2 | Easy Accuracy: 55.72% Submissions: 287 Points: 2 3 | Given a non-negative integer N. Check whether the Binary Representation of the number is Palindrome or not. 4 | Note: No leading 0’s are being considered. 5 | 6 | Example 1: 7 | 8 | Input: 9 | N = 5 10 | Output: 1 11 | Explanation: The Binary Representation of 12 | 5 is 101 which is a Palindrome. 13 | 14 | ​Example 2: 15 | 16 | Input: 17 | N = 10 18 | Output: 0 19 | Explanation: The Binary Representation of 20 | 10 is 1010 which is not a Palindrome. 21 | 22 | Your Task: 23 | You don't need to read input or print anything. Your task is to complete the function binaryPalin() which takes an integer N as input and returns 1 if the binary representation of N is a palindrome. Else, return 0 . 24 | 25 | 26 | Expected Time Complexity: Log(N). 27 | Expected Auxiliary Space: O(1). 28 | 29 | 30 | Constraints: 31 | 0<=N<=263-1// { Driver Code Starts 32 | 33 | #include 34 | using namespace std; 35 | 36 | 37 | // } Driver Code Ends 38 | 39 | 40 | //User function template for C++ 41 | class Solution{ 42 | public: 43 | 44 | int binaryPalin (long long int N) 45 | { 46 | string s; 47 | long long int n=N; 48 | while(n) 49 | { 50 | char x= (n&1); 51 | s.push_back(x); 52 | n=n>>1; 53 | 54 | } 55 | int i=s.length()/2; 56 | int j=0; 57 | int k=s.length()-1; 58 | while(j> t; 75 | while (t--) 76 | { 77 | long long int n; cin >> n; 78 | Solution ob; 79 | cout << ob.binaryPalin (n) << endl; 80 | } 81 | } 82 | 83 | // Contributed By: Pranay Bansal 84 | // } Driver Code Ends 85 | -------------------------------------------------------------------------------- /Check if array contains contiguous integers with duplicates allowed: -------------------------------------------------------------------------------- 1 | Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”. 2 | 3 | 4 | INPUT: The first line consists of an integer T i.e. the number of test cases. First line of each test case consists of an integer n, denoting the size of array. Next line consists of n spaced integers, denoting elements of array. 5 | 6 | 7 | OUTPUT: Print “Yes” if it is a set of contiguous integers else print “No”. 8 | 9 | 10 | 11 | CONSTRAINTS: 12 | 1<=T<=100 13 | 1<=n<100000 14 | a[i]<=105 15 | 16 | Example: 17 | 18 | 2 19 | 8 20 | 5 2 3 6 4 4 6 6 21 | 7 22 | 10 14 10 12 12 13 15 23 | 24 | Output : 25 | Yes 26 | No 27 | 28 | Explanation: 29 | Test Case 1 : The elements of array form a contiguous set of integers which is {2, 3, 4, 5, 6} so the output is Yes. 30 | Test Case 2: We are unable to form contiguous set of integers using elements of array. 31 | #include 32 | using namespace std; 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int n,flag=0; 40 | cin>>n; 41 | set s; 42 | for(int i=0;i>x; 46 | s.insert(x); 47 | } 48 | auto i=s.begin(); 49 | int pos=(*i); 50 | for(;i!=s.end();i++) 51 | { 52 | flag=abs((*i)-pos); 53 | pos=(*i); 54 | if(flag>1) 55 | { 56 | cout<<"No"< 32 | using namespace std; 33 | 34 | // } Driver Code Ends 35 | 36 | 37 | 38 | class Solution{ 39 | public: 40 | int isBitSet(int N){ 41 | int count=0; 42 | int pos=log2(N)+1; 43 | 44 | while(N) 45 | { 46 | N=N&(N-1); 47 | count++; 48 | } 49 | if(count==pos) 50 | return 1; 51 | else 52 | return 0; 53 | } 54 | }; 55 | 56 | // { Driver Code Starts. 57 | int main() 58 | { 59 | int t; 60 | cin>>t; 61 | while(t--) 62 | { 63 | int N; 64 | cin>>N; 65 | Solution ob; 66 | cout << ob.isBitSet(N) << endl; 67 | } 68 | return 0; 69 | } // } Driver Code Ends 70 | -------------------------------------------------------------------------------- /Check whether K-th bit is set or not: -------------------------------------------------------------------------------- 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 | Example, Consider N = 4(100): 0th bit = 0, 1st bit = 0, 2nd bit = 1. 3 | 4 | Example 1: 5 | 6 | Input: N = 4, K = 0 7 | Output: false 8 | Explanation: Binary representation of 4 is 100, 9 | in which 0th bit from LSB is not set. 10 | So, return false. 11 | Example 2: 12 | 13 | Input: N = 4, K = 2 14 | Output: true 15 | Explanation: Binary representation of 4 is 100, 16 | in which 2nd bit from LSB is set. 17 | So, return true. 18 | Example 3: 19 | 20 | Input: N = 500, K = 3 21 | Output: false 22 | Explanation: Binary representation of 500 is 23 | 111110100, in which 3rd bit from LSB is not set. 24 | So, return false. 25 | 26 | Your Task: This is a function problem. You only need to complete the function checkKthbit that takes n and k as parameters and returns either true (if kth bit is set) or false(if kth bit is not set). 27 | 28 | Expected Time Complexity: O(1). 29 | Expected Auxiliary Space: O(1). 30 | 31 | Constraints: 32 | 1 ≤ N ≤ 109 33 | 0 ≤ K ≤ floor(log2(N) + 1 34 | // { Driver Code Starts 35 | //Initial Template for C++ 36 | 37 | #include 38 | using namespace std; 39 | 40 | 41 | // } Driver Code Ends 42 | 43 | 44 | //User function Template for C++ 45 | 46 | // Function to check if Kth bit is set or not 47 | bool checkKthBit(int n, int k){ 48 | 49 | 50 | if((n&(1<>t;//taking testcases 64 | while(t--) 65 | { 66 | long long n; 67 | cin>>n;//input n 68 | int k; 69 | cin>>k;//bit number k 70 | 71 | if(checkKthBit(n, k)) 72 | cout << "Yes" << endl; 73 | else 74 | cout << "No" << endl; 75 | } 76 | return 0; 77 | } // } Driver Code Ends 78 | -------------------------------------------------------------------------------- /Count More than n/k OccurencesCount More than n: -------------------------------------------------------------------------------- 1 | Given an array arr[] of size N and an element k. The task is to find all elements in array that appear more than n/k times. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 8 7 | arr[] = {3,1,2,2,1,2,3,3} 8 | k = 4 9 | Output: 2 10 | Explanation: In the given array, 3 and 11 | 2 are the only elements that appears 12 | more than n/k times. 13 | Example 2: 14 | 15 | Input: 16 | N = 4 17 | arr[] = {2,3,3,2} 18 | k = 3 19 | Output: 2 20 | Explanation: In the given array, 3 and 2 21 | are the only elements that appears more 22 | than n/k times. So the count of elements 23 | are 2. 24 | Your Task: 25 | The task is to complete the function countOccurence() which returns count of elements with more than n/k times appearance. 26 | 27 | Expected Time Complexity: O(N). 28 | Expected Auxiliary Space: O(N). 29 | 30 | Constraints: 31 | 1 <= N <= 10^4 32 | 1 <= a[i] <= 10^6 33 | 1 <= k <= N 34 | 35 | 36 | // { Driver Code Starts 37 | // A C++ program to print elements with count more than n/k 38 | 39 | #include 40 | #include 41 | using namespace std; 42 | 43 | // } Driver Code Ends 44 | 45 | // Function to find elements with count more than n/k times 46 | // arr: input array 47 | class Solution{ 48 | public: 49 | int countOccurence(int arr[], int n, int k) { 50 | unordered_map m; 51 | int count=0; 52 | for(int i=0;in/k) 60 | { 61 | count++; 62 | m[arr[i]]=0; 63 | } 64 | } 65 | return count; 66 | } 67 | }; 68 | 69 | // { Driver Code Starts. 70 | int main() { 71 | int t, k; 72 | cin >> t; 73 | while (t--) { 74 | int n, i; 75 | cin >> n; 76 | 77 | int arr[n]; 78 | 79 | for (i = 0; i < n; i++) cin >> arr[i]; 80 | cin >> k; 81 | Solution obj; 82 | cout << obj.countOccurence(arr, n, k) << endl; 83 | } 84 | return 0; 85 | } 86 | // } Driver Code Ends 87 | -------------------------------------------------------------------------------- /Count nodes of linked list: -------------------------------------------------------------------------------- 1 | Given a singly linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list. 2 | 3 | Example 1: 4 | 5 | Input: 6 | LinkedList: 1->2->3->4->5 7 | Output: 5 8 | Explanation: Count of nodes in the 9 | linked list is 5, which is its length. 10 | Example 2: 11 | 12 | Input: 13 | LinkedList: 2->4->6->7->5->1->0 14 | Output: 7 15 | Explanation: Count of nodes in the 16 | linked list is 7. Hence, the output 17 | is 7. 18 | Your Task: 19 | Your task is to complete the given function getCount(), which takes a head reference as an argument and should return the length of the linked list. 20 | 21 | Expected Time Complexity : O(N) 22 | Expected Auxilliary Space : O(1) 23 | 24 | Constraints: 25 | 1 <= N <= 105 26 | 1 <= value <= 103 27 | #include 28 | using namespace std; 29 | 30 | struct Node 31 | { 32 | int data; 33 | struct Node* next; 34 | 35 | Node(int x){ 36 | data = x; 37 | next = NULL; 38 | } 39 | }; 40 | 41 | int getCount(struct Node* head); 42 | 43 | int main() 44 | { 45 | int t; 46 | cin>>t; 47 | while(t--) 48 | { 49 | int n; 50 | cin>>n; 51 | 52 | int data; 53 | cin>>data; 54 | struct Node *head = new Node(data); 55 | struct Node *tail = head; 56 | for (int i = 0; i < n-1; ++i) 57 | { 58 | cin>>data; 59 | tail->next = new Node(data); 60 | tail = tail->next; 61 | } 62 | cout << getCount(head) << endl; 63 | } 64 | return 0; 65 | }// } Driver Code Ends 66 | 67 | 68 | /* Link list node */ 69 | /* 70 | struct Node 71 | { 72 | int data; 73 | Node* next; 74 | Node(int x) { data = x; next = NULL; } 75 | }; */ 76 | 77 | // head : reference to head of linked list 78 | int getCount(struct Node* head){ 79 | 80 | int count=0; 81 | Node *start=head; 82 | while(start!=NULL) 83 | { 84 | count++; 85 | start=start->next; 86 | } 87 | 88 | return count; 89 | } 90 | -------------------------------------------------------------------------------- /Count number of hops: -------------------------------------------------------------------------------- 1 | A frog jumps either 1, 2, or 3 steps to go to the top. In how many ways can it reach the top. As the answer will be large find the answer modulo 1000000007. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 1 7 | Output: 1 8 | Example 2: 9 | 10 | Input: 11 | N = 4 12 | Output: 7 13 | Explanation:Below are the 7 ways to reach 14 | 4 15 | 1 step + 1 step + 1 step + 1 step 16 | 1 step + 2 step + 1 step 17 | 2 step + 1 step + 1 step 18 | 1 step + 1 step + 2 step 19 | 2 step + 2 step 20 | 3 step + 1 step 21 | 1 step + 3 step 22 | Your Task: 23 | Your task is to complete the function countWays() which takes 1 argument(N) and returns the answer%(10^9 + 7). 24 | 25 | 26 | / { Driver Code Starts 27 | #include 28 | using namespace std; 29 | 30 | // } Driver Code Ends 31 | 32 | 33 | class Solution 34 | { 35 | public: 36 | //Function to count the number of ways in which frog can reach the top. 37 | long long countWays(int n) 38 | { 39 | 40 | 41 | if(n==0 || n==1 || n==2) 42 | return n; 43 | if(n==3) 44 | return 4; 45 | 46 | vector dp(n+1,0); 47 | 48 | 49 | dp[1]=1; 50 | dp[2]=2; 51 | dp[3]=4; 52 | 53 | for(int i=4;i<=n;i++) 54 | { 55 | dp[i]=(dp[i-1]+dp[i-2]+dp[i-3])%1000000007; 56 | } 57 | 58 | return dp[n]; 59 | } 60 | }; 61 | 62 | 63 | // { Driver Code Starts. 64 | int main() 65 | { 66 | //taking testcases 67 | int t; 68 | cin >> t; 69 | 70 | while(t--) 71 | { 72 | //taking number of steps in stair 73 | int n; 74 | cin>>n; 75 | Solution ob; 76 | //calling function countWays() 77 | cout << ob.countWays(n) << endl; 78 | } 79 | 80 | return 0; 81 | 82 | } 83 | // } Driver Code Ends 84 | -------------------------------------------------------------------------------- /Count the number of possible triangles: -------------------------------------------------------------------------------- 1 | Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles. 2 | 3 | Example 1: 4 | 5 | Input: 6 | n = 3 7 | arr[] = {3, 5, 4} 8 | Output: 9 | 1 10 | Explanation: 11 | A triangle is possible 12 | with all the elements 5, 3 and 4. 13 | Example 2: 14 | 15 | Input: 16 | n = 5 17 | arr[] = {6, 4, 9, 7, 8} 18 | Output: 19 | 10 20 | Explanation: 21 | There are 10 triangles 22 | possible with the given elements like 23 | (6,4,9), (6,7,8),... 24 | 25 | Your Task: This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and N as input parameters and returns the count of total possible triangles. 26 | 27 | Expected Time Complexity: O(N2). 28 | Expected Space Complexity: O(1). 29 | 30 | Constraints: 31 | 3 <= N <= 10^3 32 | 1 <= arr[i] <= 10^3 33 | 34 | // { Driver Code Starts 35 | #include 36 | using namespace std; 37 | 38 | 39 | // } Driver Code Ends 40 | 41 | class Solution 42 | { 43 | public: 44 | //Function to count the number of possible triangles. 45 | int findNumberOfTriangles(int arr[], int n) 46 | { 47 | sort(arr,arr+n); 48 | int count=0; 49 | for(int i=n-1;i>1;i--) 50 | { 51 | int l=0; 52 | int r=i-1; 53 | while(larr[i]) 56 | { 57 | count+=r-l; 58 | r--; 59 | } 60 | else 61 | l++; 62 | } 63 | 64 | 65 | } 66 | 67 | return count; 68 | 69 | 70 | } 71 | }; 72 | 73 | 74 | // { Driver Code Starts. 75 | 76 | int main() 77 | { 78 | int T; 79 | cin>>T; 80 | while(T--) 81 | { 82 | int n; 83 | cin>>n; 84 | int arr[n]; 85 | for(int i=0; i>arr[i]; 87 | Solution ob; 88 | cout< 38 | using namespace std; 39 | 40 | 41 | // } Driver Code Ends 42 | 43 | 44 | //User function Template for C++ 45 | 46 | // Function to count set bits in the given number x 47 | // n: input to count the number of set bits 48 | 49 | int countSetBits(int n) 50 | { 51 | int count=0; 52 | for(int i=1;i<=n;i++) 53 | { 54 | int x=i; 55 | while(x) 56 | { 57 | x=x&(x-1); 58 | count++; 59 | } 60 | } 61 | return count; 62 | } 63 | 64 | 65 | // { Driver Code Starts. 66 | 67 | // Driver code 68 | int main() 69 | { 70 | int t; 71 | cin>>t;// input testcases 72 | while(t--) //while testcases exist 73 | { 74 | int n; 75 | cin>>n; //input n 76 | 77 | cout << countSetBits(n) << endl;// print the answer 78 | } 79 | return 0; 80 | } 81 | // } Driver Code Ends 82 | -------------------------------------------------------------------------------- /Count total set bits from 1 to n: -------------------------------------------------------------------------------- 1 | You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). 2 | 3 | Example 1: 4 | 5 | Input: N = 4 6 | Output: 5 7 | Explanation: 8 | For numbers from 1 to 4. 9 | For 1: 0 0 1 = 1 set bits 10 | For 2: 0 1 0 = 1 set bits 11 | For 3: 0 1 1 = 2 set bits 12 | For 4: 1 0 0 = 1 set bits 13 | Therefore, the total set bits is 5. 14 | Example 2: 15 | 16 | Input: N = 17 17 | Output: 35 18 | Explanation: From numbers 1 to 17(both inclusive), 19 | the total number of set bits is 35. 20 | 21 | Your Task: The task is to complete the function countSetBits() that takes n as a parameter and returns the count of all bits. 22 | 23 | Expected Time Complexity: O(log N). 24 | Expected Auxiliary Space: O(1). 25 | 26 | Constraints: 27 | 1 ≤ N ≤ 106// { Driver Code Starts 28 | //Initial Template for C++ 29 | 30 | #include 31 | using namespace std; 32 | 33 | 34 | // } Driver Code Ends 35 | 36 | 37 | //User function Template for C++ 38 | 39 | // Function to count set bits in the given number x 40 | // n: input to count the number of set bits 41 | 42 | int sol(int n) 43 | { 44 | 45 | 46 | if(n==0) 47 | return 0; 48 | int i=log2(n); 49 | int ans=n-(1<>t;// input testcases 73 | while(t--) //while testcases exist 74 | { 75 | int n; 76 | cin>>n; //input n 77 | 78 | cout << countSetBits(n) << endl;// print the answer 79 | } 80 | return 0; 81 | } 82 | // } Driver Code Ends 83 | -------------------------------------------------------------------------------- /Does array represent Heap: -------------------------------------------------------------------------------- 1 | Given an array A of size N, the task is to check if the given array represents a Binary Max Heap. 2 | 3 | 4 | Example 1: 5 | 6 | Input: arr[] = {90, 15, 10, 7, 12, 2} 7 | Output: True 8 | The given array represents below tree 9 | 90 10 | / \ 11 | 15 10 12 | / \ / 13 | 7 12 2 14 | The tree follows max-heap property as every 15 | node is greater than all of its descendants. 16 | 17 | Example 2: 18 | Input: arr[] = {9, 15, 10, 7, 12, 11} 19 | Output: False 20 | The given array represents below tree 21 | 9 22 | / \ 23 | 15 10 24 | / \ / 25 | 7 12 11 26 | The tree doesn't follows max-heap property 9 is 27 | smaller than 15 and 10, and 10 is smaller than 11. 28 | 29 | 30 | Your Task: 31 | You don't need to read input or print anything. Your task is to complete the function isMaxHeap() which takes the array A[] and its size N as inputs and returns "1", else print "0" (without quotes). 32 | 33 | 34 | Expected Time Complexity: O(N) 35 | Expected Auxiliary Space: O(1) 36 | 37 | 38 | 39 | Constraints: 40 | 1 ≤ N ≤ 10^6 41 | 1 ≤ Ai ≤ 10^15 42 | 43 | // { Driver Code Starts 44 | #include 45 | using namespace std; 46 | 47 | // } Driver Code Ends 48 | 49 | 50 | class Solution{ 51 | public: 52 | bool isMaxHeap(int a[], int n) 53 | { 54 | for(int i=0;ia[i] && ia[i]) && i> t; 69 | while(t--) 70 | { 71 | int n; 72 | cin >> n; 73 | int a[4*n]={0}; 74 | for(int i =0;i> a[i]; 76 | } 77 | Solution ob; 78 | cout< 32 | using namespace std; 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | long int n,flag=0; 40 | cin>>n; 41 | unsigned long long int arr[n],k; 42 | cin>>k; 43 | for(int i=0;i>arr[i]; 45 | sort(arr,arr+n); 46 | long int i=0,j=n-1; 47 | while(ik) 50 | { 51 | j--; 52 | } 53 | 54 | else if(arr[i]*arr[j] 25 | using namespace std; 26 | 27 | // } Driver Code Ends 28 | //User function template for C++ 29 | 30 | class Solution{ 31 | public: 32 | string ExcelColumn(int N) 33 | { 34 | string s=""; 35 | while(N) 36 | { 37 | if((N%26)==0) 38 | { 39 | s='Z'+s; 40 | N--; 41 | } 42 | else 43 | { 44 | s= char(64+ (N%26))+s; 45 | } 46 | 47 | N=N/26; 48 | } 49 | return s; 50 | } 51 | }; 52 | 53 | // { Driver Code Starts. 54 | int main() 55 | { 56 | int t; 57 | cin>>t; 58 | while(t--) 59 | { 60 | int n; 61 | cin>>n; 62 | Solution ob; 63 | cout< 34 | using namespace std; 35 | 36 | // } Driver Code Ends 37 | 38 | 39 | //User function template for C++ 40 | 41 | class Solution{ 42 | public: 43 | int getOddOccurrence(int arr[], int n) { 44 | int x=0; 45 | for(int i=0;i> t; 58 | while (t--) { 59 | int n; 60 | cin >> n; 61 | int arr[n]; 62 | for (int i = 0; i < n; i++) { 63 | cin >> arr[i]; 64 | } 65 | Solution ob; 66 | auto ans = ob.getOddOccurrence(arr, n); 67 | cout << ans << "\n"; 68 | } 69 | return 0; 70 | } 71 | 72 | // } Driver Code Ends 73 | -------------------------------------------------------------------------------- /Fake Profile: -------------------------------------------------------------------------------- 1 | There is a hacker named "Vijay" who has developed a method to check whether an id at some social networking site is fake or real using its username. 2 | 3 | His method includes: if the number of distinct characters in one's user name is odd, then the user is a male, otherwise a female. You are given the string that denotes the user name, please help Vijay to determine the gender of this user by his method. Ignore the vowels. 4 | 5 | Input: 6 | The first line contains a integer T i.e. the number of test cases. Each test case contains string S that contains only lowercase English letters — the user name. 7 | 8 | Output: 9 | If it is a female by Vijay's method, print "SHE!" (without the quotes), otherwise, print "HE!" (without the quotes). 10 | 11 | Constraints: 12 | 1<=T<=100 13 | 1<=Length of string<=1000 14 | 15 | 16 | Time Complexity:O(n) 17 | 18 | Examples: 19 | Input : 20 | 3 21 | jpmztf 22 | plkaitw 23 | gfuyg 24 | 25 | Output: 26 | SHE! 27 | HE! 28 | HE! 29 | 30 | Explanation: 31 | For the first test case. There are 6 distinct characters in "jpmztf". These characters are: "j", "p", "m", "z", "t", "f". So jpmztf is a female and you should print "SHE!". 32 | 33 | For the second example. There are 5 distinct characters in "plkaitw". These characters are: "p"," l ","k","t"and"w" as others are vowels. So plkaitw is a male and you should print "HE!". 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | #include 42 | using namespace std; 43 | int main() 44 | { 45 | int t; 46 | cin>>t; 47 | while(t--) 48 | { 49 | string s; 50 | cin>>s; 51 | set s1; 52 | int count=0; 53 | for(int i=0;i 29 | using namespace std; 30 | 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | //User function Template for C++ 36 | 37 | /* function to find position of first set 38 | bit in the given number 39 | * n: given input for which we want to get 40 | the position of first set bit 41 | */ 42 | unsigned int getFirstSetBit(int n){ 43 | int count=0; 44 | while(n) 45 | { 46 | count++; 47 | if((n&1)==1) 48 | { 49 | break; 50 | } 51 | n=n>>1; 52 | 53 | } 54 | 55 | return count; 56 | 57 | } 58 | 59 | // { Driver Code Starts. 60 | 61 | // Driver code 62 | int main() 63 | { 64 | int t; 65 | cin>>t; // testcases 66 | while(t--) 67 | { 68 | int n; 69 | cin>>n; //input n 70 | printf("%u\n", getFirstSetBit(n)); // function to get answer 71 | } 72 | return 0; 73 | } 74 | // } Driver Code Ends 75 | -------------------------------------------------------------------------------- /Find first set bit 1: -------------------------------------------------------------------------------- 1 | Given an integer an N. The task is to print the position of first set bit found from right side in the binary representation of the number. 2 | 3 | Input: 4 | The first line of the input contains an integer T, denoting the number of test cases. Then T test cases follow. The only line of the each test case contains an integer N. 5 | 6 | Output: 7 | For each test case print in a single line an integer denoting the position of the first set bit found form right side of the binary representation of the number. If there is no set bit print "0". 8 | 9 | Constraints: 10 | 1 <= T <= 200 11 | 0 <= N <= 106 12 | 13 | Example: 14 | Input: 15 | 2 16 | 18 17 | 12 18 | 19 | Output: 20 | 2 21 | 3 22 | 23 | Explanation: 24 | Testcase 1: Binary representation of the 18 is 010010, the first set bit from the right side is at position 2. 25 | #include 26 | using namespace std; 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | 34 | int n; 35 | cin>>n; 36 | int count=0; 37 | int i=0; 38 | while(n) 39 | { 40 | i++; 41 | if(n&1) 42 | { 43 | break; 44 | } 45 | n=n>>1; 46 | 47 | 48 | } 49 | cout< 34 | using namespace std; 35 | 36 | // } Driver Code Ends 37 | 38 | 39 | class Solution { 40 | public: 41 | int findPosition(int N) { 42 | 43 | int i=1; 44 | int pos=-1; 45 | int count=0; 46 | while(N) 47 | { 48 | if(N&1) 49 | { 50 | pos=i; 51 | count++; 52 | } 53 | N=N>>1; 54 | i++; 55 | } 56 | if(count==1) 57 | return pos; 58 | else 59 | return -1; 60 | } 61 | }; 62 | 63 | // { Driver Code Starts. 64 | int main() { 65 | int t; 66 | cin >> t; 67 | while (t--) { 68 | int N; 69 | 70 | cin>>N; 71 | 72 | Solution ob; 73 | cout << ob.findPosition(N) << endl; 74 | } 75 | return 0; 76 | } // } Driver Code Ends 77 | -------------------------------------------------------------------------------- /Find the most frequent digit: -------------------------------------------------------------------------------- 1 | 2 | Given a number N, find the most occurring digit in it. If two or more digits occur same number of times, then return the highest of them. Input number is given as a string. 3 | 4 | Input: 5 | The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains the number N 6 | 7 | Output: 8 | Print the most occurring digit in the number. If two or more digits occur same number of times, then print the highest of them. Print the answer for each test case in a new line. 9 | 10 | Constraints: 11 | 1<= T <=100 12 | 1<= N <=101000 13 | 14 | Example: 15 | Input: 16 | 1 17 | 12234 18 | 19 | Output: 20 | 2 21 | #include 22 | using namespace std; 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | string s; 30 | cin>>s; 31 | int m[10]={0}; 32 | for(long int i=0;i=0;i--) 40 | { 41 | if(maxi==m[i]) 42 | { 43 | cout< 27 | using namespace std; 28 | int main() 29 | { 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | int n,k; 35 | cin>>n>>k; 36 | int x; 37 | map m; 38 | 39 | for(int i=0;i>x; 42 | if(m[x]>=1) 43 | { 44 | m[x]=m[x]+1; 45 | } 46 | else 47 | { 48 | m[x]=1; 49 | } 50 | 51 | } 52 | 53 | for(auto i=m.begin();i!=m.end();i++) 54 | { 55 | if(i->second==1) 56 | { 57 | cout<first< 27 | using namespace std; 28 | 29 | // } Driver Code Ends 30 | 31 | 32 | //User function template for C++ 33 | class Solution{ 34 | public: 35 | 36 | int onlyFirstAndLastAreSet(long long int n) { 37 | 38 | long long int x=log2(n); 39 | if(((1<> t; 51 | while(t--){ 52 | long long n; 53 | cin >> n; 54 | Solution ob; 55 | cout << ob.onlyFirstAndLastAreSet(n) << endl; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | // } Driver Code Ends 62 | -------------------------------------------------------------------------------- /First and last occurrences of X: -------------------------------------------------------------------------------- 1 | Given a sorted array with possibly duplicate elements, the task is to find indexes of first and last occurrences of an element x in the given array. 2 | 3 | Note: If the number x is not found in the array just print '-1'. 4 | 5 | Input: 6 | The first line consists of an integer T i.e number of test cases. The first line of each test case contains two integers n and x. The second line contains n spaced integers. 7 | 8 | Output: 9 | Print index of the first and last occurrences of the number x with a space in between. 10 | 11 | Constraints: 12 | 1<=T<=100 13 | 1<=n,a[i]<=1000 14 | 15 | Example: 16 | Input: 17 | 2 18 | 9 5 19 | 1 3 5 5 5 5 67 123 125 20 | 9 7 21 | 1 3 5 5 5 5 7 123 125 22 | 23 | Output: 24 | 2 5 25 | 6 6 26 | 27 | #include 28 | using namespace std; 29 | 30 | int upsearch(int mid,int x,int *arr,int n) 31 | { 32 | while(arr[mid]==x && mid-1) 44 | { 45 | mid--; 46 | } 47 | return mid+1; 48 | } 49 | 50 | 51 | int main() 52 | { 53 | int t; 54 | cin>>t; 55 | while(t--) 56 | { 57 | int n,x; 58 | cin>>n>>x; 59 | int arr[n]; 60 | for(int i=0;i>arr[i]; 62 | int lower,upper; 63 | int l=0; 64 | int r=n-1; 65 | int flag=0; 66 | while(l<=r) 67 | { 68 | int mid=l+(r-l)/2; 69 | if(arr[mid]==x) 70 | { 71 | flag=1; 72 | upper=upsearch(mid,x,arr,n); 73 | lower=losearch(mid,x,arr,n); 74 | 75 | break; 76 | } 77 | else if(arr[mid]x) 82 | { 83 | r=mid-1; 84 | } 85 | } 86 | 87 | if(flag==0) 88 | { 89 | cout<<"-1"< 40 | using namespace std; 41 | int main() 42 | { 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int arr[26]={0}; 50 | int i; 51 | char ch; 52 | queueque; 53 | for(i=0;i>ch; 56 | arr[ch-97]++; 57 | que.push(ch); 58 | while(que.size() && arr[que.front()-97]>1) 59 | { 60 | que.pop(); 61 | } 62 | if(que.empty()) 63 | cout<<"-1 "; 64 | else 65 | cout< 36 | using namespace std; 37 | int main() 38 | { 39 | int t; 40 | cin>>t; 41 | 42 | 43 | 44 | 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int m[n]; 50 | for(int i=1;i<=n;i++) 51 | { 52 | m[i]=0; 53 | } 54 | 55 | for(int i=0;i>x; 59 | m[x]++; 60 | } 61 | 62 | for(int i=1;i<=n;i++) 63 | { 64 | cout< 28 | using namespace std; 29 | int main() 30 | { 31 | 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | unordered_map m; 39 | vector v; 40 | string s; 41 | for(int i=0;i>s; 44 | v.push_back(s); 45 | s.clear(); 46 | } 47 | for(int i=0;isecond<<" "; 58 | } 59 | cout< 41 | using namespace std; 42 | 43 | // } Driver Code Ends 44 | // User function Template for C++ 45 | 46 | class Solution{ 47 | public: 48 | int heapHeight(int N, int arr[]){ 49 | return log2(N); 50 | } 51 | }; 52 | 53 | // { Driver Code Starts. 54 | 55 | int main() { 56 | int t; 57 | cin>>t; 58 | while(t--){ 59 | int N; 60 | cin >> N; 61 | int arr[N]; 62 | for(int i = 0; i < N; i++) 63 | cin >> arr[i]; 64 | 65 | Solution ob; 66 | cout << ob.heapHeight(N, arr) << endl; 67 | } 68 | return 0; 69 | } 70 | // } Driver Code Ends 71 | -------------------------------------------------------------------------------- /Implement Atoi: -------------------------------------------------------------------------------- 1 | Your task is to implement the function atoi. The function takes a string(str) as argument and converts it to an integer and returns it. 2 | 3 | Example 1: 4 | 5 | Input: 6 | str = 123 7 | Output: 123 8 | 9 | Example 2: 10 | 11 | Input: 12 | str = 21a 13 | Output: -1 14 | Explanation: Output is -1 as all 15 | characters are not digit only. 16 | Your Task: 17 | Complete the function atoi() which takes a string as input parameter and returns integer value of it. if the input string is not a numerical string then returns 1.. 18 | 19 | Expected Time Complexity: O(|S|), |S| = length of string S. 20 | Expected Auxiliary Space: O(1) 21 | 22 | Constraints: 23 | 1<=length of S<=10 24 | 25 | // { Driver Code Starts 26 | #include 27 | using namespace std; 28 | 29 | // } Driver Code Ends 30 | 31 | 32 | class Solution{ 33 | public: 34 | /*You are required to complete this method */ 35 | int atoi(string str) 36 | { 37 | int ans=0; 38 | int i=0; 39 | 40 | if(str[0]=='-') 41 | i=1; 42 | 43 | 44 | while(i='0' && str[i]<='9') 47 | ans=ans*10+(str[i]-'0'); 48 | else 49 | return -1; 50 | i++; 51 | } 52 | 53 | 54 | if(str[0]=='-') 55 | return -ans; 56 | else 57 | return ans; 58 | } 59 | }; 60 | 61 | // { Driver Code Starts. 62 | int main() 63 | { 64 | int t; 65 | cin>>t; 66 | while(t--) 67 | { 68 | string s; 69 | cin>>s; 70 | Solution ob; 71 | cout< 41 | using namespace std; 42 | 43 | int strstr(string ,string); 44 | 45 | int main() 46 | { 47 | int t; 48 | cin>>t; 49 | while(t--) 50 | { 51 | string a; 52 | string b; 53 | 54 | cin>>a; 55 | cin>>b; 56 | 57 | cout<s.length()) 69 | return -1; 70 | for(int i=0;i 31 | using namespace std; 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int i,n,m; 39 | cin>>n>>m; 40 | long int arr1[n],arr2[m]; 41 | unordered_map map1; 42 | for(i=0;i>arr1[i]; 44 | for(i=0;i>arr2[i]; 47 | map1[arr2[i]]=1; 48 | } 49 | for(i=0;i 43 | using namespace std; 44 | int main() 45 | { 46 | int t; 47 | cin>>t; 48 | while(t--) 49 | { 50 | int n,m,count=0; 51 | cin>>n>>m; 52 | int m1[100001]={0}; 53 | 54 | for(int i=0;i>x; 58 | m1[x]=1; 59 | } 60 | for(int i=0;i>y; 64 | if(m1[y]==1) 65 | { 66 | count++; 67 | m1[y]=0; 68 | } 69 | 70 | } 71 | 72 | cout< 26 | using namespace std; 27 | 28 | // } Driver Code Ends 29 | 30 | 31 | //User function template for C++ 32 | class Solution{ 33 | public: 34 | 35 | int isDivisible(string s){ 36 | int even=0; 37 | int odd=0; 38 | for(int i=0;i> t; 61 | while(t--){ 62 | string s; 63 | cin >> s; 64 | Solution ob; 65 | cout << ob.isDivisible(s) << endl; 66 | } 67 | return 0; 68 | } 69 | 70 | // } Driver Code Ends 71 | -------------------------------------------------------------------------------- /Ishaan Loves Chocolates: -------------------------------------------------------------------------------- 1 | As we know, Ishaan has a love for chocolates. He has bought a huge chocolate bar which contains N chocolate squares. Each of the square has a tastiness level which is denoted by an array A[]. 2 | Ishaan can eat the first or the last square of the chocolate at once. Ishaan has a sister who loves chocolates too and she demands the last chocolate square. Now, Ishaan being greedy eats the more tasty square first. 3 | Determine the tastiness level of the square which his sister gets. 4 | 5 | Input : 6 | First line of input contains a single integer T denoting the number of test cases. 7 | The first line of each test case contains an integer N. 8 | The second line contains N space-separated integers denoting the array A. 9 | 10 | Output : 11 | For each test case, print the required answer in a new line. 12 | 13 | Constraints : 14 | 1 <= T <= 100 15 | 1 <= N <= 250 16 | 1 <= A[i] <= 1000 17 | 18 | Example : 19 | Input : 20 | 3 21 | 5 22 | 5 3 1 6 9 23 | 6 24 | 2 6 4 8 1 6 25 | 4 26 | 2 2 2 2 27 | Output : 28 | 1 29 | 1 30 | 2 31 | 32 | Explaination : 33 | Case 1 : 34 | Initially : 5 3 1 6 9 35 | 5 3 1 6 36 | 5 3 1 37 | 3 1 38 | 1 39 | 40 | Case 2 : 41 | Initially : 2 6 4 8 1 6 42 | 2 6 4 8 1 43 | 6 4 8 1 44 | 4 8 1 45 | 8 1 46 | 1 47 | 48 | Case 3 : 49 | Initially : 2 2 2 2 50 | 2 2 2 51 | 2 2 52 | 2 53 | 54 | #include 55 | using namespace std; 56 | int main() 57 | { 58 | int t; 59 | cin>>t; 60 | while(t--) 61 | { 62 | int n,x,mini=INT_MAX; 63 | cin>>n; 64 | for(int i=0;i>x; 67 | mini=min(mini,x); 68 | } 69 | cout< 33 | using namespace std; 34 | 35 | 36 | // } Driver Code Ends 37 | 38 | 39 | 40 | 41 | class Solution 42 | { 43 | public: 44 | //Function to return k largest elements from an array. 45 | vector kLargest(int arr[], int n, int k) 46 | { 47 | priority_queue,greater> pq; 48 | vector v; 49 | for(int i=0;ik) 53 | pq.pop(); 54 | 55 | } 56 | 57 | while(!pq.empty()) 58 | { 59 | v.insert(v.begin(),pq.top()); 60 | pq.pop(); 61 | } 62 | 63 | return v; 64 | 65 | } 66 | }; 67 | 68 | // { Driver Code Starts. 69 | 70 | int main(){ 71 | int t; 72 | cin >> t; 73 | while(t--){ 74 | int n, k; 75 | cin >> n >> k; 76 | 77 | int arr[n]; 78 | for(int i = 0; i < n;i++) 79 | cin>>arr[i]; 80 | Solution ob; 81 | vector result = ob.kLargest(arr, n, k); 82 | for (int i = 0; i < result.size(); ++i) 83 | cout< 45 | using namespace std; 46 | 47 | 48 | // } Driver Code Ends 49 | 50 | 51 | // Function to find subarray with maximum sum 52 | // arr: input array 53 | // n: size of array 54 | int maxSubarraySum(int arr[], int n){ 55 | 56 | int curr=arr[0]; 57 | int best=arr[0]; 58 | for(int i=1;i>t; //input testcases 76 | while(t--) //while testcases exist 77 | { 78 | 79 | cin>>n; //input size of array 80 | 81 | int a[n]; 82 | 83 | for(int i=0;i>a[i]; //inputting elements of array 85 | 86 | cout << maxSubarraySum(a, n) << endl; 87 | } 88 | } 89 | // } Driver Code Ends 90 | 91 | 92 | -------------------------------------------------------------------------------- /Key Pair using 2 pointer: -------------------------------------------------------------------------------- 1 | Given an array Arr of N positive integers and another number X. Determine whether or not there exist two elements in Arr whose sum is exactly X. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 6, X = 16 7 | Arr[] = {1, 4, 45, 6, 10, 8} 8 | Output: Yes 9 | Explanation: Arr[3] + Arr[4] = 6 + 10 = 16 10 | Example 2: 11 | 12 | Input: 13 | N = 5, X = 10 14 | Arr[] = {1, 2, 4, 3, 6} 15 | Output: Yes 16 | Explanation: Arr[2] + Arr[4] = 4 + 6 = 10 17 | Your Task: 18 | You don't need to read input or print anything. Your task is to complete the function hasArrayTwoCandidates() which takes the array of integers arr, n and x as parameters and returns boolean denoting the answer. 19 | 20 | Expected Time Complexity: O(N*logN) 21 | Expected Auxiliary Space: O(1) 22 | 23 | Constraints: 24 | 1 ≤ N ≤ 105 25 | 1 ≤ Arr[i] ≤ 105 26 | 27 | // { Driver Code Starts 28 | #include 29 | 30 | using namespace std; 31 | 32 | 33 | // } Driver Code Ends 34 | 35 | bool hasArrayTwoCandidates(int arr[], int n, int x) { 36 | sort(arr,arr+n); 37 | int l=0,r=n-1; 38 | while(lx) 45 | { 46 | r--; 47 | } 48 | else if(arr[l]+arr[r]> t; 65 | while (t--) { 66 | int n, x; 67 | cin >> n >> x; 68 | int arr[n]; 69 | for (int i = 0; i < n; i++) { 70 | cin >> arr[i]; 71 | } 72 | Solution ob; 73 | auto ans = ob.hasArrayTwoCandidates(arr, n, x); 74 | cout << (ans ? "Yes\n" : "No\n"); 75 | } 76 | return 0; 77 | } 78 | // } Driver Code Ends 79 | -------------------------------------------------------------------------------- /Key Pair using map: -------------------------------------------------------------------------------- 1 | Given an array Arr of N positive integers and another number X. Determine whether or not there exist two elements in Arr whose sum is exactly X. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 6, X = 16 7 | Arr[] = {1, 4, 45, 6, 10, 8} 8 | Output: Yes 9 | Explanation: Arr[3] + Arr[4] = 6 + 10 = 16 10 | Example 2: 11 | 12 | Input: 13 | N = 5, X = 10 14 | Arr[] = {1, 2, 4, 3, 6} 15 | Output: Yes 16 | Explanation: Arr[2] + Arr[4] = 4 + 6 = 10 17 | Your Task: 18 | You don't need to read input or print anything. Your task is to complete the function hasArrayTwoCandidates() which takes the array of integers arr, n and x as parameters and returns boolean denoting the answer. 19 | 20 | Expected Time Complexity: O(N*logN) 21 | Expected Auxiliary Space: O(1) 22 | 23 | Constraints: 24 | 1 ≤ N ≤ 105 25 | 1 ≤ Arr[i] ≤ 105 26 | // { Driver Code Starts 27 | #include 28 | 29 | using namespace std; 30 | 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | //User function template for C++ 36 | class Solution{ 37 | public: 38 | // Function to check if array has 2 elements 39 | // whose sum is equal to the given value 40 | bool hasArrayTwoCandidates(int arr[], int n, int x) { 41 | map m; 42 | for(int i=0;i> t; 63 | while (t--) { 64 | int n, x; 65 | cin >> n >> x; 66 | int arr[n]; 67 | for (int i = 0; i < n; i++) { 68 | cin >> arr[i]; 69 | } 70 | Solution ob; 71 | auto ans = ob.hasArrayTwoCandidates(arr, n, x); 72 | cout << (ans ? "Yes\n" : "No\n"); 73 | } 74 | return 0; 75 | } 76 | // } Driver Code Ends 77 | -------------------------------------------------------------------------------- /Kth LSB: -------------------------------------------------------------------------------- 1 | A number N is given. Find its ‘K’th Least Significant Bit. 2 | 3 | Example 1: 4 | 5 | Input: N = 10, K = 4 6 | Output: 1 7 | Explanation: Binary Representation 8 | of 10 is 1010. 4th LSB is 1. 9 | ​Example 2: 10 | 11 | Input: N = 16, K = 3 12 | Output: 1 13 | Explanation: Binary Representation of 14 | 16 is 10000. 3rd LSB is 0. 15 | Your Task: 16 | You don't need to read input or print anything. Your task is to complete the function KthLSB() which takes the N and K as inputs and returns the bit (1 or 0) in K'th LSB. 17 | Expected Time Complexity: O(K) 18 | Expected Auxiliary Space: O(1) 19 | 20 | Constraints: 21 | 1 ≤ N ≤ 109 22 | 1 ≤ K ≤ 32 23 | // { Driver Code Starts 24 | #include 25 | using namespace std; 26 | 27 | // } Driver Code Ends 28 | 29 | 30 | //User function template for C++ 31 | class Solution{ 32 | public: 33 | 34 | int KthLSB(long long int n, int k){ 35 | k=k-1; 36 | n=n>>k; 37 | return n&1; 38 | 39 | } 40 | }; 41 | 42 | // { Driver Code Starts. 43 | 44 | int main(){ 45 | int t; 46 | cin >> t; 47 | while(t--){ 48 | long long n; 49 | int k; 50 | cin >> n >> k; 51 | Solution ob; 52 | 53 | cout << ob.KthLSB(n, k) << endl; 54 | } 55 | return 0; 56 | } 57 | 58 | // } Driver Code Ends 59 | -------------------------------------------------------------------------------- /Kth smallest element: -------------------------------------------------------------------------------- 1 | Given an array arr[] and a number K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 6 7 | arr[] = 7 10 4 3 20 15 8 | K = 3 9 | Output : 7 10 | Explanation : 11 | 3rd smallest element in the given 12 | array is 7. 13 | Example 2: 14 | 15 | Input: 16 | N = 5 17 | arr[] = 7 10 4 20 15 18 | K = 4 19 | Output : 15 20 | Explanation : 21 | 4th smallest element in the given 22 | array is 15. 23 | Your Task: 24 | You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array, it's size and an integer k as input and returns the kth smallest element. 25 | 26 | 27 | Expected Time Complexity: O(n) 28 | Expected Auxiliary Space: O(1) 29 | Constraints: 30 | 1 <= N <= 105 31 | 1 <= arr[i] <= 10^5 32 | 1 <= K <= N 33 | 34 | // { Driver Code Starts 35 | //Initial function template for C++ 36 | 37 | #include 38 | using namespace std; 39 | 40 | int kthSmallest(int *, int, int, int); 41 | 42 | int main() 43 | { 44 | // ios_base::sync_with_stdio(false); 45 | // cin.tie(NULL); 46 | 47 | int test_case; 48 | cin>>test_case; 49 | while(test_case--) 50 | { 51 | int number_of_elements; 52 | cin>>number_of_elements; 53 | int a[number_of_elements]; 54 | 55 | for(int i=0;i>a[i]; 57 | 58 | int k; 59 | cin>>k; 60 | cout< p; 74 | for(int i=l;i<=r;i++) 75 | { 76 | p.push(arr[i]); 77 | if(p.size()>k) 78 | p.pop(); 79 | } 80 | return p.top(); 81 | } 82 | -------------------------------------------------------------------------------- /Ladoo problem: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bheem promised all his friends that if he won the tournament so he will give ladoos. But he knew that he can afford only one ladoo per day. If he is unable to give ladoo to any of his friend he will loose his friendship with them (if more than one his friend demanded for ladoo on same day). As he has won the tournament now he has to give ladoos to his friends. Now your task is to tell how many friends he will be able to save. 4 | 5 | Input: 6 | First line consists of T test case. 7 | First line of every test case 'N', represents how many friends he has. 8 | Second line of every test case Ai, represents which friend asked for ladoo on which day. 9 | 10 | Output: 11 | For each test case, Single line output denoting how many friends he is able to save. 12 | 13 | Constraints: 14 | 1<=T<=100 15 | 1<=N<=1000 16 | 1<=Ai<=1000 17 | 18 | Example: 19 | Input: 20 | 1 21 | 5 22 | 3 3 1 2 4 23 | Output: 24 | 4 25 | #include 26 | using namespace std; 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | set s; 34 | int n; 35 | cin>>n; 36 | for(int i=0;i>x; 40 | s.insert(x); 41 | 42 | } 43 | cout< 25 | using namespace std; 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | map m; 33 | int n; 34 | cin>>n; 35 | int count=0; 36 | for(int i=0;i>x; 40 | if(m[x]>=1) 41 | { 42 | m[x]=1; 43 | } 44 | else 45 | { 46 | m[x]=1; 47 | } 48 | } 49 | 50 | for(auto i=m.begin();i!=m.end();i++) 51 | { 52 | if(i->second==1) 53 | count++; 54 | } 55 | cout< 23 | using namespace std; 24 | 25 | int maxLen(int A[], int n); 26 | 27 | int main() 28 | { 29 | int T; 30 | cin >> T; 31 | while (T--) 32 | { 33 | int N; 34 | cin >> N; 35 | int A[N]; 36 | for (int i = 0; i < N; i++) 37 | cin >> A[i]; 38 | cout << maxLen(A, N) << endl; 39 | } 40 | } 41 | // } Driver Code Ends 42 | 43 | 44 | /*You are required to complete this function*/ 45 | 46 | int maxLen(int A[], int n) 47 | { 48 | unordered_map m; 49 | m[0]=-1; 50 | int sum=0; 51 | int ans=0; 52 | for(int i=0;i 29 | using namespace std; 30 | 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | //User function Template for C++ 36 | 37 | /* Function to calculate the largest consecutive ones 38 | * x: given input to calculate the largest consecutive ones 39 | */ 40 | int maxConsecutiveOnes(int x) 41 | { 42 | 43 | int count=0,maxi=0; 44 | 45 | while(x>0) 46 | { 47 | if((x&1)!=0) 48 | { 49 | count++; 50 | } 51 | else 52 | { 53 | count=0; 54 | } 55 | maxi=max(maxi,count); 56 | x=x>>1; 57 | 58 | } 59 | return maxi; 60 | 61 | } 62 | 63 | 64 | 65 | // { Driver Code Starts. 66 | 67 | // Driver Code 68 | int main() { 69 | int t; 70 | cin>>t;//testcases 71 | while(t--) 72 | { 73 | int n; 74 | cin>>n;//input n 75 | 76 | //calling maxConsecutiveOnes() function 77 | cout<& nums) { 30 | int n=nums.size(); 31 | 32 | int count=1,res=1; 33 | 34 | for(int i=0;i 32 | using namespace std; 33 | 34 | // } Driver Code Ends 35 | class Solution { 36 | public: 37 | int LongestRepeatingSubsequence(string str){ 38 | int n=str.length(); 39 | string t=str; 40 | 41 | vector>dp(n+1,vector (n+1,0)); 42 | 43 | for(int i=1;i> tc; 68 | while(tc--){ 69 | string str; 70 | cin >> str; 71 | Solution obj; 72 | int ans = obj.LongestRepeatingSubsequence(str); 73 | cout << ans << "\n"; 74 | } 75 | return 0; 76 | } // } Driver Code Ends 77 | -------------------------------------------------------------------------------- /Longest consecutive subsequence (full implementation without pre defined function): -------------------------------------------------------------------------------- 1 | Given an array arr[] of positive integers. Find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order. 2 | 3 | Input: 4 | The first line of input contains T, number of test cases. First line of line each test case contains a single integer N. 5 | Next line contains N integer array. 6 | 7 | Output: 8 | Print the output of each test case in a seprate line. 9 | 10 | Constraints: 11 | 1 <= T <= 100 12 | 1 <= N <= 105 13 | 0 <= a[i] <= 105 14 | 15 | Example: 16 | Input: 17 | 2 18 | 7 19 | 2 6 1 9 4 5 3 20 | 7 21 | 1 9 3 10 4 20 2 22 | 23 | Output: 24 | 6 25 | 4 26 | 27 | Explanation: 28 | Testcase 1: The consecutive numbers here are 1, 2, 3, 4, 5, 6. These 6 numbers form the longest consecutive subsquence. 29 | 30 | Testcase2: 1, 2, 3, 4 is the longest consecutive subsequence. 31 | #include 32 | using namespace std; 33 | 34 | int findLongestConseqSubseq(int arr[], int N) 35 | { 36 | vector v; 37 | map m; 38 | int count=0,res=0; 39 | for(int i=0;ifirst); 47 | 48 | } 49 | 50 | for(int i=0;i>t; 74 | while(t--) 75 | { 76 | cin>>n; 77 | for(i=0;i>a[i]; 79 | cout< 34 | 35 | using namespace std; 36 | 37 | // } Driver Code Ends 38 | //User function template for C++ 39 | class Solution{ 40 | public: 41 | int longSubarrWthSumDivByK(int arr[], int n, int k) 42 | { 43 | unordered_map m; 44 | long long int sum=0; 45 | m[0]=-1; 46 | int maxi=0; 47 | for(int i=0;i>t; 73 | while(t--) 74 | { 75 | int n,k,i; 76 | cin>>n>>k; int arr[n]; 77 | for(i=0;i>arr[i]; 79 | Solution ob; 80 | cout< 44 | using namespace std; 45 | 46 | // } Driver Code Ends 47 | 48 | 49 | class Solution { 50 | public: 51 | 52 | int getTwinCount(int N , int Arr[]) { 53 | int ar[100001] = {0}; 54 | for(int i=0;i 1) 60 | cnt += ar[i] % 2 ? ar[i] - 1 : ar[i]; 61 | } 62 | return cnt; 63 | 64 | } 65 | 66 | }; 67 | 68 | // { Driver Code Starts. 69 | int main() { 70 | int t; 71 | cin >> t; 72 | while (t--) { 73 | int N; 74 | 75 | cin>>N; 76 | int Arr[N]; 77 | for(int i=0 ; i>Arr[i]; 79 | 80 | Solution ob; 81 | cout << ob.getTwinCount(N,Arr) << endl; 82 | } 83 | return 0; 84 | } // } Driver Code Ends 85 | -------------------------------------------------------------------------------- /Lowest Common Ancestor of a Binary Tree: -------------------------------------------------------------------------------- 1 | Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 2 | 3 | According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” 4 | 5 | 6 | 7 | Example 1: 8 | 9 | 10 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 11 | Output: 3 12 | Explanation: The LCA of nodes 5 and 1 is 3. 13 | Example 2: 14 | 15 | 16 | Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 17 | Output: 5 18 | Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. 19 | Example 3: 20 | 21 | Input: root = [1,2], p = 1, q = 2 22 | Output: 1 23 | 24 | 25 | Constraints: 26 | 27 | The number of nodes in the tree is in the range [2, 10^5]. 28 | -10^9 <= Node.val <= 10^9 29 | All Node.val are unique. 30 | p != q 31 | p and q will exist in the tree. 32 | 33 | /** 34 | * Definition for a binary tree node. 35 | * struct TreeNode { 36 | * int val; 37 | * TreeNode *left; 38 | * TreeNode *right; 39 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 40 | * }; 41 | */ 42 | class Solution { 43 | public: 44 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 45 | if(root==NULL) 46 | return root; 47 | if(root->val==p->val || root->val==q->val) 48 | return root; 49 | TreeNode *l=lowestCommonAncestor(root->left,p,q); 50 | TreeNode *r=lowestCommonAncestor(root->right,p,q); 51 | if(l && r) 52 | return root; 53 | if(l) 54 | return l; 55 | if(r) 56 | return r; 57 | return NULL; 58 | 59 | } 60 | }; 61 | -------------------------------------------------------------------------------- /Magical Number: -------------------------------------------------------------------------------- 1 | Your friend loves magic and he has coined a new term - "Magical number". To perform his magic, he needs that Magic number. There are N number of people in the magic show, seated according to their ages in an ascending order. Magical number is that seat no. where the person has the same age as that of the given seat number. 2 | Help your friend in finding out that "Magical number" 3 | 4 | Input: 5 | The first line of input contains an integer T denoting the number of test cases. 6 | The first line of each test case is N, size of an array. 7 | The second line of each test case contains N input A[]. 8 | 9 | Output: 10 | 11 | Print "Magical Number" 12 | Print "-1" when index value does not match with value. 13 | 14 | Constraints: 15 | 16 | 1 ≤ T ≤ 100 17 | 1 ≤ N ≤ 1000 18 | -1000 ≤ A[i] ≤ 1000 19 | 20 | Example: 21 | Input: 22 | 1 23 | 10 24 | -10 -1 0 3 10 11 30 50 100 150 25 | 26 | Output: 27 | 3 28 | 29 | // { Driver Code Starts 30 | #include 31 | using namespace std; 32 | int binarySearch(int arr[], int low, int high); 33 | int main() 34 | { 35 | int test =0; 36 | cin>>test; 37 | while(test--) 38 | { 39 | int n, i; 40 | cin>>n; 41 | int a[n]; 42 | for(i=0; i>a[i]; 44 | 45 | cout<mid) 64 | high=mid-1; 65 | else if(arr[mid] 41 | using namespace std; 42 | 43 | 44 | // } Driver Code Ends 45 | class Solution{ 46 | public: 47 | // Function to find majority element in the array 48 | // a: input array 49 | // size: size of input array 50 | int majorityElement(int a[], int size) 51 | { 52 | 53 | unordered_map m; 54 | for(int i=0;isize/2) 63 | return a[i]; 64 | } 65 | return -1; 66 | } 67 | }; 68 | 69 | // { Driver Code Starts. 70 | 71 | int main(){ 72 | 73 | int t; 74 | cin >> t; 75 | 76 | while(t--){ 77 | int n; 78 | cin >> n; 79 | int arr[n]; 80 | 81 | for(int i = 0;i> arr[i]; 83 | } 84 | Solution obj; 85 | cout << obj.majorityElement(arr, n) << endl; 86 | } 87 | 88 | return 0; 89 | } 90 | // } Driver Code Ends 91 | -------------------------------------------------------------------------------- /Max sum in the configuration: -------------------------------------------------------------------------------- 1 | Given an array(0-based indexing), you have to find the max sum of i*A[i] where A[i] is the element at index i in the array. The only operation allowed is to rotate(clock-wise or counter clock-wise) the array any number of times. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 4 7 | A[] = {8,3,1,2} 8 | Output: 29 9 | Explanation: Above the configuration 10 | possible by rotating elements are 11 | 3 1 2 8 here sum is 3*0+1*1+2*2+8*3 = 29 12 | 1 2 8 3 here sum is 1*0+2*1+8*2+3*3 = 27 13 | 2 8 3 1 here sum is 2*0+8*1+3*2+1*3 = 17 14 | 8 3 1 2 here sum is 8*0+3*1+1*2+2*3 = 11 15 | Here the max sum is 29 16 | Your Task: 17 | Your task is to complete the function max_sum which takes two arguments which is the array A [ ] and its size and returns an integer value denoting the required max sum. 18 | 19 | Expected Time Complexity: O(N). 20 | Expected Auxiliary Space: O(1). 21 | 22 | Constraints: 23 | 1<=N<=10^4 24 | 1<=A[]<1000 25 | 26 | // { Driver Code Starts 27 | #include 28 | using namespace std; 29 | 30 | int max_sum(int A[],int N); 31 | 32 | int main() 33 | { 34 | int T; 35 | cin>>T; 36 | while(T--) 37 | { 38 | int N; 39 | cin>>N; 40 | int A[N]; 41 | for(int i=0;i>A[i]; 44 | } 45 | 46 | cout< 30 | using namespace std; 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | //User function Template for C++ 36 | 37 | class Solution{ 38 | public: 39 | int toyCount(int N, int K, int arr[]) 40 | { 41 | int count=0; 42 | sort(arr,arr+N); 43 | for(int i=0;i=0) 46 | { 47 | K=K-arr[i]; 48 | count++; 49 | } 50 | } 51 | return count; 52 | } 53 | }; 54 | 55 | // { Driver Code Starts. 56 | 57 | int main(){ 58 | int t; 59 | cin>>t; 60 | while(t--){ 61 | int N, K; 62 | cin>>N>>K; 63 | int arr[N]; 64 | for(int i = 0;i < N;i++) 65 | cin>>arr[i]; 66 | 67 | Solution ob; 68 | cout< 28 | using namespace std; 29 | int main() 30 | { 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | int n; 36 | cin>>n; 37 | int arr[n]; 38 | for(int i=0;i>arr[i]; 41 | } 42 | 43 | int diff=arr[1]-arr[0]; 44 | int mini=arr[0]; 45 | 46 | for(int i=0;idiff) 49 | diff=arr[i]-mini; 50 | 51 | if(arr[i]0) 57 | cout< 26 | using namespace std; 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | int n,maxi=0; 34 | cin>>n; 35 | int arr[n]; 36 | map m; 37 | for(int i=0;i>arr[i]; 40 | m[arr[i]]=i; 41 | } 42 | 43 | for(int i=0;i 34 | using namespace std; 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int n,k; 42 | cin>>n>>k; 43 | 44 | set s; 45 | for(int i=0;i>x; 49 | s.insert(x); 50 | } 51 | int siz=s.size(); 52 | int rep=n-siz; 53 | int res; 54 | if(k<=rep) 55 | res=siz; 56 | else 57 | { 58 | res=siz-(k-rep); 59 | } 60 | 61 | cout< 22 | using namespace std; 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | string s; 30 | cin>>s; 31 | int i=0; 32 | map m; 33 | while(i 32 | using namespace std; 33 | double MedianOfArrays(vector& array1, vector& array2); 34 | 35 | 36 | // } Driver Code Ends 37 | //User function Template for C++ 38 | 39 | double MedianOfArrays(vector& array1, vector& array2) 40 | { 41 | vector v; 42 | for(int i=0;i>t; 62 | while(t--) 63 | { 64 | int m,n; 65 | cin>>m; 66 | vector array1(m); 67 | for (int i = 0; i < m; ++i) 68 | cin>>array1[i]; 69 | cin>>n; 70 | vector array2(n); 71 | for (int i = 0; i < n; ++i) 72 | cin>>array2[i]; 73 | cout< 39 | using namespace std; 40 | int main() 41 | { 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | int n; 47 | cin>>n; 48 | long long int sum1=0,sum2=0; 49 | 50 | 51 | priority_queue,greater> pq; 52 | for(int i=0;i>x; 56 | 57 | pq.push(x); 58 | 59 | } 60 | 61 | 62 | 63 | while(!pq.empty()) 64 | { 65 | int n1=pq.top(); 66 | pq.pop(); 67 | 68 | 69 | if(!pq.empty()) 70 | { 71 | int n2=pq.top(); 72 | pq.pop(); 73 | sum2=sum2*10+n2; 74 | n2=0; 75 | } 76 | sum1=sum1*10+n1; 77 | 78 | 79 | n1=0; 80 | 81 | 82 | } 83 | 84 | cout< 28 | using namespace std; 29 | 30 | 31 | // } Driver Code Ends 32 | 33 | 34 | class Solution{ 35 | public: 36 | int minimumNumberOfDeletions(string S) { 37 | int n=S.length(); 38 | string t=S; 39 | reverse(t.begin(),t.end()); 40 | 41 | vector> dp(n+1,vector (n+1,0)); 42 | 43 | for(int i=1;i> t; 67 | while(t--){ 68 | string S; 69 | cin >> S; 70 | Solution obj; 71 | cout << obj.minimumNumberOfDeletions(S) << endl; 72 | } 73 | return 0; 74 | } // } Driver Code Ends 75 | -------------------------------------------------------------------------------- /Minimum Distinct Ids: -------------------------------------------------------------------------------- 1 | Given an array of items, an i-th index element denotes the item id’s and given a number m, the task is to remove m elements such that there should be minimum distinct id’s left.Print the number of distinct id’s. 2 | 3 | Input: 4 | The first line of the input contains a single integer T, denoting the number of test cases. Then T test case follows, the three lines of the input, the first line contains N, denoting number of elements in an array,second line contains N elements/ids, and third line contains the number M. 5 | 6 | Output: 7 | For each test case, print the minimum number of distinct ids. 8 | 9 | Constraints: 10 | 1<=T<=100 11 | 1<=N<=100 12 | 1<=arr[i]<=10^6 13 | 1<=M<=100 14 | 15 | Example: 16 | Input: 17 | 2 18 | 6 19 | 2 2 1 3 3 3 20 | 3 21 | 8 22 | 2 4 1 5 3 5 1 3 23 | 2 24 | Output: 25 | 1 26 | 3 27 | #include 28 | using namespace std; 29 | int main() 30 | { 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | int n,count=0; 36 | cin>>n; 37 | int arr[n]; 38 | unordered_map m1; 39 | vector v; 40 | for(int i=0;i>arr[i]; 43 | m1[arr[i]]++; 44 | } 45 | int m; 46 | cin>>m; 47 | for(auto i=m1.begin();i!=m1.end();i++) 48 | { 49 | v.push_back(i->second); 50 | 51 | } 52 | 53 | sort(v.begin(),v.end()); 54 | 55 | for(int i=0;i0) 63 | { 64 | v[i]--; 65 | m--; 66 | } 67 | 68 | } 69 | 70 | for(int i=0;i 35 | using namespace std; 36 | 37 | // } Driver Code Ends 38 | 39 | 40 | class Solution{ 41 | public: 42 | int minDist(int a[], int n, int x, int y) { 43 | 44 | int posx=-1; 45 | int posy=-1; 46 | int diff=INT_MAX; 47 | for(int i=0;i> t; 67 | while (t--) { 68 | int n; 69 | cin >> n; 70 | int a[n]; 71 | for (int i = 0; i < n; i++) cin >> a[i]; 72 | int x, y; 73 | cin >> x >> y; 74 | Solution obj; 75 | cout << obj.minDist(a, n, x, y) << endl; 76 | } 77 | return 0; 78 | } 79 | // } Driver Code Ends 80 | -------------------------------------------------------------------------------- /Minimum indexed character: -------------------------------------------------------------------------------- 1 | Given a string str and another string patt. Find the character in patt that is present at the minimum index in str. If no character of patt is present in str then print ‘No character present’. 2 | 3 | Example 1: 4 | 5 | Input: 6 | str = geeksforgeeks 7 | patt = set 8 | Output: e 9 | Explanation: e is the character which is 10 | present in given patt "geeksforgeeks" 11 | and is first found in str "set". 12 | Example 2: 13 | 14 | Input: 15 | str = adcffaet 16 | patt = onkl 17 | Output: No character present 18 | Explanation: There are none of the 19 | characters which is common in patt 20 | and str. 21 | Your Task: 22 | You only need to complete the function minIndexChar() that returns the index of answer in str or returns -1 in case no character of patt is present in str. 23 | 24 | Expected Time Complexity: O(N). 25 | Expected Auxiliary Space: O(Number of distinct characters). 26 | 27 | Constraints: 28 | 1 <= |str|,|patt| <= 105 29 | // { Driver Code Starts 30 | // C++ implementation to find the character in first 31 | // string that is present at minimum index in second 32 | // string 33 | #include 34 | using namespace std; 35 | 36 | 37 | // } Driver Code Ends 38 | 39 | int minIndexChar(string str, string patt) 40 | { 41 | map m; 42 | for(int i=0;i>t; 63 | while(t--) 64 | { 65 | string str; 66 | string patt; 67 | cin>>str; 68 | cin>>patt; 69 | int ans = minIndexChar(str, patt); 70 | if(ans == -1)cout<<"No character present"; 71 | else cout< 35 | using namespace std; 36 | int main() 37 | { 38 | int t; 39 | cin>>t; 40 | while(t--) 41 | { 42 | int n; 43 | cin>>n; 44 | int arr[n]; 45 | for(int i=0;i>arr[i]; 47 | 48 | vector v1(n,0); 49 | 50 | for(int i=0;i 27 | using namespace std; 28 | 29 | int MissingNumber(vector& array, int n); 30 | 31 | // Position this line where user code will be pasted. 32 | 33 | int main() { 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | int n; 38 | cin >> n; 39 | 40 | vector array(n - 1); 41 | for (int i = 0; i < n - 1; ++i) cin >> array[i]; 42 | 43 | cout << MissingNumber(array, n) << "\n"; 44 | } 45 | return 0; 46 | }// } Driver Code Ends 47 | 48 | 49 | // User function template for C++ 50 | 51 | int MissingNumber(vector& array, int n) { 52 | int x=0; 53 | for(int i=1;i<=n;i++) 54 | { 55 | x=x^i; 56 | } 57 | 58 | for(int i=0;i 30 | using namespace std; 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | int res=0; 40 | for(int i=0;i>x; 44 | res=res^x; 45 | } 46 | for(int i=0;i>y; 50 | res=res^y; 51 | } 52 | cout< 25 | using namespace std; 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n,k; 33 | cin>>n>>k; 34 | priority_queue,greater> pq; 35 | for(int i=0;i>x; 39 | pq.push(x); 40 | if(pq.size()>k) 41 | { 42 | cout< 30 | using namespace std; 31 | int NeedleinHaystack(string haystack, string needle) ; 32 | 33 | 34 | // } Driver Code Ends 35 | //User function Template for C++ 36 | 37 | int NeedleinHaystack(string haystack, string needle) 38 | { 39 | int i=0; 40 | if(needle.length()==0) 41 | return 0; 42 | while(i>t; 58 | cin.ignore(); 59 | while(t--) 60 | { 61 | string haystack, needle; 62 | getline(cin,haystack); 63 | getline(cin,needle); 64 | cout< 42 | using namespace std; 43 | 44 | 45 | // } Driver Code Ends 46 | 47 | 48 | long long numberOfPaths(int m, int n) 49 | { 50 | if(n==0 && m==0) 51 | return 0; 52 | 53 | if(n==1 || m==1) 54 | return 1; 55 | return numberOfPaths(m,n-1)+numberOfPaths(m-1,n); 56 | 57 | 58 | } 59 | 60 | // { Driver Code Starts. 61 | 62 | 63 | int main() 64 | { 65 | int t; 66 | cin>>t; 67 | while(t--) 68 | { 69 | int n,m; 70 | cin>>m>>n; 71 | cout << numberOfPaths(m, n)< 33 | using namespace std; 34 | int main() 35 | { 36 | int t; 37 | cin>>t; 38 | while(t--) 39 | { 40 | 41 | int n,count=0; 42 | cin>>n; 43 | int arr[n]; 44 | for(int i=0;i>arr[i]; 46 | sort(arr,arr+n); 47 | 48 | for(int i=0;i0) 53 | { 54 | int r=x%10; 55 | if(r!=1 && r!=2 && r!=3 ) 56 | { 57 | flag=1; 58 | count++; 59 | break; 60 | } 61 | x=x/10; 62 | } 63 | if(flag==0 && arr[i]!=0) 64 | cout< 30 | using namespace std; 31 | 32 | // } Driver Code Ends 33 | 34 | 35 | // User function Template for C++ 36 | class Solution { 37 | public: 38 | string isAdjacentSetBits(long long int n) { 39 | 40 | if(n&(n>>1)) 41 | return "Yes"; 42 | else 43 | 44 | return "No"; 45 | } 46 | }; 47 | 48 | // { Driver Code Starts. 49 | int main() { 50 | int t; 51 | cin >> t; 52 | while (t--) { 53 | long long int n; 54 | cin >> n; 55 | Solution ob; 56 | cout << ob.isAdjacentSetBits(n) << endl; 57 | } 58 | return 0; 59 | } 60 | // } Driver Code Ends 61 | -------------------------------------------------------------------------------- /Odd or Even using bitmasking: -------------------------------------------------------------------------------- 1 | Given a positive integer N, determine whether it is odd or even. 2 | // { Driver Code Starts 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | 9 | class Solution{ 10 | public: 11 | string oddEven(int N){ 12 | if(N&1) 13 | return "odd"; 14 | else 15 | return "even"; 16 | } 17 | }; 18 | 19 | // { Driver Code Starts. 20 | int main() 21 | { 22 | int t; 23 | cin>>t; 24 | while(t--) 25 | { 26 | int N; 27 | cin >> N; 28 | Solution ob; 29 | cout << ob.oddEven(N) << endl; 30 | } 31 | return 0; 32 | } // } Driver Code Ends 33 | -------------------------------------------------------------------------------- /Parity of unsigned integer: -------------------------------------------------------------------------------- 1 | Given an integer N, find it's parity. 2 | Parity of a number refers to the number of 1 bits it contains. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 13 8 | Output: odd 9 | Explanation: 10 | (13)10 = (1101)2 The binary representation 11 | has three 1-bits. So, it's parity is odd. 12 | 13 | Example 2: 14 | 15 | Input: 16 | N = 9 17 | Output: even 18 | Explanation: 19 | (9)10 = (1001)2 The binary representation 20 | has two 1-bits. So, it's parity is even. 21 | 22 | 23 | Your Task: 24 | You don't need to read input or print anything. Your task is to complete the function computeParity() which takes an Integer N as input parameter and returns string "odd" or "even". 25 | 26 | 27 | 28 | Expected Time Complexity: O(log(N)) 29 | Expected Auxiliary Space: O(1) 30 | 31 | 32 | 33 | Constraints: 34 | 1 <= N <= 105 35 | // { Driver Code Starts 36 | #include 37 | using namespace std; 38 | 39 | // } Driver Code Ends 40 | 41 | 42 | class Solution { 43 | public: 44 | string computeParity(int N) { 45 | int count=0; 46 | while(N) 47 | { 48 | N=N&(N-1); 49 | count++; 50 | } 51 | 52 | if((count&1)!=0) 53 | return "odd"; 54 | else 55 | return "even"; 56 | 57 | 58 | 59 | } 60 | }; 61 | 62 | // { Driver Code Starts. 63 | int main() { 64 | int t; 65 | cin >> t; 66 | while (t--) { 67 | int N; 68 | 69 | cin>>N; 70 | 71 | Solution ob; 72 | cout << ob.computeParity(N) << endl; 73 | } 74 | return 0; 75 | } // } Driver Code Ends 76 | -------------------------------------------------------------------------------- /Path Sum: -------------------------------------------------------------------------------- 1 | Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. 2 | 3 | A leaf is a node with no children. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | 10 | Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 11 | Output: true 12 | Example 2: 13 | 14 | 15 | Input: root = [1,2,3], targetSum = 5 16 | Output: false 17 | Example 3: 18 | 19 | Input: root = [1,2], targetSum = 0 20 | Output: false 21 | 22 | 23 | Constraints: 24 | 25 | The number of nodes in the tree is in the range [0, 5000]. 26 | -1000 <= Node.val <= 1000 27 | -1000 <= targetSum <= 1000 28 | 29 | /** 30 | * Definition for a binary tree node. 31 | * struct TreeNode { 32 | * int val; 33 | * TreeNode *left; 34 | * TreeNode *right; 35 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 36 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 37 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 38 | * }; 39 | */ 40 | class Solution { 41 | public: 42 | bool flag=false; 43 | void solve(TreeNode* root, int targetSum,int sum) 44 | { 45 | if(root==NULL) 46 | return; 47 | 48 | sum=sum+root->val; 49 | 50 | if(root->left==NULL && root->right==NULL) 51 | { 52 | if(sum==targetSum) 53 | { 54 | flag=true; 55 | return; 56 | } 57 | else 58 | return; 59 | } 60 | 61 | solve(root->left,targetSum,sum); 62 | solve(root->right,targetSum,sum); 63 | 64 | } 65 | bool hasPathSum(TreeNode* root, int targetSum) { 66 | if(root==NULL) 67 | return false; 68 | int sum=0; 69 | solve(root,targetSum,sum); 70 | return flag; 71 | } 72 | }; 73 | -------------------------------------------------------------------------------- /Permutation with Spaces: -------------------------------------------------------------------------------- 1 | Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them. 2 | Output should be printed in sorted increasing order of strings. 3 | 4 | #include 5 | using namespace std; 6 | void solve(string ip ,string op) 7 | { 8 | if(ip.length()==0) 9 | { 10 | op.push_back(')'); 11 | cout<>t; 30 | while(t--) 31 | { 32 | string ip; 33 | cin>>ip; 34 | string op=""; 35 | op.push_back('('); 36 | op.push_back(ip[0]); 37 | ip.erase(ip.begin()+0); 38 | solve(ip,op); 39 | cout< 33 | using namespace std; 34 | # define mod 1000000007 35 | 36 | 37 | // compute reverse of a number 38 | long long rev(long long n) 39 | { 40 | long long rev_num = 0; 41 | while(n > 0) 42 | { 43 | rev_num = rev_num*10 + n%10; 44 | n = n/10; 45 | } 46 | return rev_num; 47 | } 48 | 49 | 50 | 51 | long long power(int N,int R); 52 | 53 | int main() 54 | { 55 | int T; 56 | cin>>T;//testcases 57 | 58 | while(T--) 59 | { 60 | long long N; 61 | cin>>N;//input N 62 | 63 | long long R = 0; 64 | 65 | // reverse the given number n 66 | R = rev(N); 67 | 68 | //power of the number to it's reverse 69 | long long ans =power(N,R); 70 | cout << ans< 26 | using namespace std; 27 | 28 | // } Driver Code Ends 29 | 30 | 31 | class Solution{ 32 | public: 33 | vector AllPossibleStrings(string s){ 34 | vector vs; 35 | string s1; 36 | for(int i=1;i<(1<> tc; 62 | while(tc--){ 63 | string s; 64 | cin >> s; 65 | Solution ob; 66 | vector res = ob.AllPossibleStrings(s); 67 | for(auto i : res) 68 | cout << i <<" "; 69 | cout << "\n"; 70 | 71 | } 72 | return 0; 73 | } // } Driver Code Ends 74 | -------------------------------------------------------------------------------- /Power of 2 using bitwise: -------------------------------------------------------------------------------- 1 | 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. 2 | 3 | 4 | Example 1: 5 | 6 | Input: N = 1 7 | Output: true 8 | Explanation: 1 is equal to 2 raised to 0 (20 == 1). 9 | Example 2: 10 | 11 | Input: N = 98 12 | Output: false 13 | Explanation: 14 | 98 cannot be obtained by any power of 2. 15 | 16 | Your Task: Your task is to complete the function isPowerofTwo() which takes n as a parameter and returns true or false by checking is given number can be represented as a power of two or not. 17 | 18 | Expected Time Complexity: O(log N). 19 | Expected Auxiliary Space: O(1). 20 | 21 | Constraints: 22 | 0 <= N <= 1018 23 | // { Driver Code Starts 24 | //Initial Template for C++ 25 | 26 | #include 27 | using namespace std; 28 | 29 | 30 | // } Driver Code Ends 31 | 32 | 33 | //User function Template for C++ 34 | 35 | // Function to check power of two 36 | bool isPowerofTwo(long long n){ 37 | if((n&(n-1))==0 && (n!=0)) 38 | return true; 39 | else 40 | return false; 41 | } 42 | 43 | // { Driver Code Starts. 44 | 45 | // Driver code 46 | int main() 47 | { 48 | 49 | int t; 50 | cin>>t;//testcases 51 | 52 | for(int i=0;i>n; 56 | if(n<=0) // if n is less than equal to zero then it can't be a power of 2 57 | { 58 | cout<<"NO"< 29 | using namespace std; 30 | 31 | void printNos(int N); 32 | /* Driver program to test printNos */ 33 | int main() 34 | { 35 | int T; 36 | 37 | //taking testcases 38 | cin>>T; 39 | 40 | while(T--) 41 | { 42 | int N; 43 | 44 | //input N 45 | cin>>N; 46 | 47 | //calling printNos() function 48 | printNos(N); 49 | 50 | cout< 1-> 4 32 | 1-> 0-> 2-> 3-> 4 33 | 2-> 1-> 3 34 | 3-> 1-> 2-> 4 35 | 4-> 0-> 1-> 3 36 | 0-> 1-> 2 37 | 1-> 0-> 2 38 | 2-> 1-> 0 39 | 40 | Explanation: 41 | Testcase 1: Given graph has 5 nodes and 7 edges. After creating adjacency list of given graph, we have list as: 42 | 0-> 1-> 4 43 | 1-> 0-> 2-> 3-> 4 44 | 2-> 1-> 3 45 | 3-> 1-> 2-> 4 46 | 4-> 0-> 1-> 3 47 | #include 48 | using namespace std; 49 | int main() 50 | { 51 | int t; 52 | cin>>t; 53 | while(t--) 54 | { 55 | int v,e; 56 | cin>>v>>e; 57 | vector vec[v]; 58 | for(int i=0;i>x>>y; 62 | vec[x].push_back(y); 63 | vec[y].push_back(x); 64 | } 65 | 66 | for(int i=0;i "; 70 | else 71 | { 72 | cout< "; 80 | else 81 | cout<<*j; 82 | } 83 | cout< 32 | using namespace std; 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int n,m; 40 | cin>>n>>m; 41 | int arr1[n],arr2[m]; 42 | map mapee; 43 | for(int i=0;i>arr1[i]; 46 | mapee[arr1[i]]++; 47 | } 48 | for(int j=0;j>arr2[j]; 50 | 51 | for(int i=0;isecond!=0) 64 | { 65 | int j=i->second; 66 | while(j--) 67 | { 68 | cout<first<<" "; 69 | } 70 | } 71 | } 72 | 73 | cout< 30 | using namespace std; 31 | 32 | 33 | // } Driver Code Ends 34 | //User function template for C++ 35 | class Solution{ 36 | public: 37 | 38 | string removeDups(string S) 39 | { 40 | unordered_map m; 41 | string str; 42 | for(int i=0;i> t; 68 | while(t--) 69 | { 70 | string s; 71 | cin >> s; 72 | 73 | 74 | Solution ob; 75 | cout << ob.removeDups(s) << "\n"; 76 | 77 | } 78 | 79 | return 0; 80 | } // } Driver Code Ends 81 | -------------------------------------------------------------------------------- /Remove duplicates in small prime array: -------------------------------------------------------------------------------- 1 | Given an array consisting of only prime numbers, remove all duplicate numbers from it. 2 | Note: Retain the first occurrence of the duplicate element. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 6 8 | A[] = {2,2,3,3,7,5} 9 | Output: 2 3 7 5 10 | Explanation: After removing the duplicate 11 | 2 and 3 we get 2 3 7 5. 12 | Your Task: 13 | Complete the function removeDuplicate() that takes given array and N as input parameters and returns modified array which has no duplicates. 14 | 15 | Note: In case of cpp the return type is vector 16 | 17 | Expected Time Complexity: O(N). 18 | Expected Auxiliary Space: O(N). 19 | 20 | Constraints: 21 | 1<=N=1000 22 | 2<=A[i]<100 23 | // { Driver Code Starts 24 | #include 25 | using namespace std; 26 | vector removeDuplicate(vector& arr, int n); 27 | 28 | int main() 29 | { 30 | int T; 31 | cin>>T; 32 | while(T--) 33 | { 34 | int N; 35 | cin>>N; 36 | vector A(N); 37 | for(int i=0;i>A[i]; 40 | } 41 | 42 | vectorresult = removeDuplicate(A,N); 43 | for(int i =0;i removeDuplicate(vector& arr, int n) 53 | { 54 | vector v; 55 | map m; 56 | for(int i=0;i 32 | using namespace std; 33 | 34 | // } Driver Code Ends 35 | class Solution 36 | { 37 | public: 38 | string reverseWords (string s) 39 | { 40 | string ans; 41 | stack res; 42 | for(int i=0;i> t; 76 | while (t--) 77 | { 78 | string s; cin >> s; 79 | Solution ob; 80 | cout << ob.reverseWords (s) << endl; 81 | } 82 | return 0; 83 | } 84 | 85 | // Contributed By: Pranay Bansal 86 | // } Driver Code Ends 87 | -------------------------------------------------------------------------------- /Rightmost different bit: -------------------------------------------------------------------------------- 1 | Given two numbers M and N. The task is to find the position of the rightmost different bit in the binary representation of numbers. 2 | 3 | Example 1: 4 | 5 | Input: M = 11, N = 9 6 | Output: 2 7 | Explanation: Binary representation of the given 8 | numbers are: 1011 and 1001, 9 | 2nd bit from right is different. 10 | Example 2: 11 | 12 | Input: M = 52, N = 4 13 | Output: 5 14 | Explanation: Binary representation of the given 15 | numbers are: 110100‬ and 0100, 16 | 5th-bit from right is different. 17 | User Task: 18 | The task is to complete the function posOfRightMostDiffBit() which takes two arguments m and n and returns the position of first different bits in m and n. If both m and n are the same then return -1 in this case. 19 | 20 | Expected Time Complexity: O(max(log m, log n)). 21 | Expected Auxiliary Space: O(1). 22 | 23 | Constraints: 24 | 1 <= M <= 103 25 | 1 <= N <= 103 26 | // { Driver Code Starts 27 | //Initial Template for C++ 28 | 29 | #include 30 | using namespace std; 31 | 32 | 33 | // } Driver Code Ends 34 | 35 | 36 | //User function Template for C++ 37 | 38 | /* Function to find the first position with different bits 39 | * This function returns the position with different bit 40 | */ 41 | int posOfRightMostDiffBit(int m, int n) 42 | { 43 | int i=1; 44 | while(m>0 || n>0) 45 | { 46 | 47 | if((m&1)!=(n&1)) 48 | { 49 | 50 | return i; 51 | } 52 | else 53 | { 54 | m=m>>1; 55 | n=n>>1; 56 | i++; 57 | } 58 | 59 | } 60 | return -1; 61 | } 62 | 63 | // { Driver Code Starts. 64 | 65 | // Driver Code 66 | int main() 67 | { 68 | int t; 69 | cin>>t; //input number of testcases 70 | while(t--) 71 | { 72 | int m,n; 73 | cin>>m>>n; //input m and n 74 | cout << posOfRightMostDiffBit(m, n)< 28 | using namespace std; 29 | 30 | char *encode(char *src); 31 | 32 | int main() { 33 | 34 | int T; 35 | cin>>T; 36 | while(T--) 37 | { 38 | char str[10000]; 39 | cin>>str; 40 | 41 | cout< 42 | using namespace std; 43 | 44 | 45 | // } Driver Code Ends 46 | 47 | 48 | // Function to find element in sorted array 49 | // arr: input array 50 | // N: size of array 51 | // K: element to be searche 52 | int searchInSorted(int arr[], int N, int K) 53 | { 54 | 55 | 56 | int l=0; 57 | int r=N-1; 58 | 59 | 60 | 61 | while(l<=r) 62 | { 63 | int mid=(l+r)/2; 64 | if(arr[mid]==K) 65 | return 1; 66 | else if(arr[mid]K) 69 | { 70 | r=mid-1; 71 | } 72 | } 73 | return -1; 74 | 75 | } 76 | 77 | // { Driver Code Starts. 78 | 79 | 80 | int main(void) 81 | { 82 | 83 | int t; 84 | cin >> t; 85 | while(t--){ 86 | int n, k; 87 | cin >> n >> k; 88 | 89 | int arr[n]; 90 | 91 | for(int i = 0;i> arr[i]; 93 | } 94 | 95 | cout << searchInSorted(arr, n, k) << endl; 96 | 97 | } 98 | 99 | return 0; 100 | } 101 | // } Driver Code Ends 102 | -------------------------------------------------------------------------------- /Sherlock a Detective: -------------------------------------------------------------------------------- 1 | Sherlock is a famous detective. This time he's working to catch a team of gangsters. Sherlock knows that the head of gangsters will be caught if he catches his underlings. The gangsters work under a hierarchical system. Each member reports exactly to one other member of the town. It's clear that there are no cycles in their reporting system.There are N people in the town, for simplicity indexed from 1 to N, and Sherlock knows who each of them report to. Member i reports to member Ai, and head of Gangsters does not report to anybody. Sherlock wants to find the members to whom nobody reports as these members could help him bring down the organization. 2 | 3 | Input: 4 | First line consists of T test cases. 5 | The first line every test case contains of one integer N. 6 | Next line has N space-separated integers. The i-th integer denotes Ai, the person whom the i-th member reports to. 7 | 8 | Output: 9 | Single line output in ascending order, denoting the members of gangsters who nobody reports to. 10 | 11 | Constraints: 12 | 1<=T<=100 13 | 1<= N<=10^5 14 | 1<=Ai<=N except for leader of gangsters, whose Ai equals to 0. 15 | 16 | Example: 17 | Input: 18 | 1 19 | 6 20 | 0 1 1 2 2 3 21 | Output: 22 | 4 5 6 23 | 24 | Explanation: 25 | For testcase1: N=6 and A={0,1,1,2,2,3} 26 | A[0]=0, A[1]=1, A[2]=1, A[3]=2, A[4]=2, A[5]=3. 27 | A[0] is the head. 1 reports to 1. 2 reports to 1. 3 reports to 2. 4 reports to 2. 5 reports to 3. 28 | So, the people not being being reported are 4, 5 and 6. 29 | #include 30 | using namespace std; 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | int arr[n]; 40 | map m; 41 | for(int i=0;i>arr[i]; 48 | m[arr[i]]=1; 49 | } 50 | 51 | for(auto i=m.begin();i!=m.end();i++) 52 | { 53 | if(i->second!=1) 54 | cout<first<<" "; 55 | 56 | } 57 | 58 | cout< 41 | using namespace std; 42 | 43 | 44 | // } Driver Code Ends 45 | //User function Template for C++ 46 | 47 | class Solution 48 | { 49 | public: 50 | long long smallestpositive(vector array, int n) 51 | { 52 | sort(array.begin(),array.end()); 53 | long long x=1; 54 | for(long long i=0;i>t; 68 | while(t--) 69 | { 70 | int n; 71 | cin>>n; 72 | vector array(n); 73 | for (int i = 0; i < n; ++i) 74 | { 75 | cin>>array[i]; 76 | } 77 | Solution ob; 78 | cout< 39 | using namespace std; 40 | 41 | 42 | // } Driver Code Ends 43 | 44 | 45 | class Solution{ 46 | public: 47 | // Function to find missing integer in array 48 | // arr: input array 49 | // n: size of array 50 | int findMissing(int arr[], int n) { 51 | 52 | sort(arr,arr+n); 53 | int flag=0; 54 | for(int i=0;i0) 57 | { 58 | flag=i; 59 | break; 60 | } 61 | } 62 | 63 | if(arr[flag]!=1) 64 | return 1; 65 | for(int j=flag;j1) 68 | return arr[j]+1; 69 | } 70 | return arr[n-1]+1; 71 | } 72 | }; 73 | 74 | // { Driver Code Starts. 75 | 76 | int main() { 77 | int t; 78 | cin>>t; 79 | while(t--){ 80 | int n; 81 | cin>>n; 82 | int arr[n]; 83 | 84 | for(int i=0; i>arr[i]; 85 | 86 | Solution ob; 87 | cout< 33 | using namespace std; 34 | 35 | // } Driver Code Ends 36 | // User function Template for C++ 37 | 38 | class Solution{ 39 | public: 40 | vector leftSmaller(int n, int a[]){ 41 | vector v(n,-1); 42 | stack s; 43 | 44 | for(int i=0;i0 && s.top()>=a[i]) 47 | { 48 | s.pop(); 49 | } 50 | if(s.size()>0) 51 | v[i]=s.top(); 52 | 53 | s.push(a[i]); 54 | } 55 | return v; 56 | } 57 | }; 58 | 59 | // { Driver Code Starts. 60 | 61 | int main(){ 62 | int t; 63 | cin>>t; 64 | while(t--){ 65 | int n; 66 | cin>>n; 67 | int a[n]; 68 | for(int i = 0;i < n;i++) 69 | cin>>a[i]; 70 | 71 | Solution ob; 72 | vector ans = ob.leftSmaller(n, a); 73 | for(int i = 0;i < n;i++) 74 | cout< 27 | using namespace std; 28 | int main() 29 | { 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | int n,k,flag=0; 35 | cin>>n>>k; 36 | map m; 37 | for(int i=0;i>x; 41 | if(m[x]>=1) 42 | { 43 | m[x]=m[x]+1; 44 | } 45 | else 46 | { 47 | m[x]=1; 48 | } 49 | } 50 | 51 | for(auto i=m.begin();i!=m.end();i++) 52 | { 53 | if(i->second==k) 54 | { 55 | cout<first< 42 | using namespace std; 43 | 44 | 45 | 46 | // } Driver Code Ends 47 | 48 | 49 | class Solution{ 50 | public: 51 | //Complete this function 52 | //Function to check whether there is a subarray present with 0-sum or not. 53 | bool subArrayExists(int arr[], int n) 54 | { 55 | unordered_map m; 56 | m[0]=1; 57 | int sum=0; 58 | 59 | for(int i=0;i>t; 80 | while(t--) 81 | { 82 | int n; 83 | cin>>n; 84 | int arr[n]; 85 | for(int i=0;i>arr[i]; 87 | Solution obj; 88 | if (obj.subArrayExists(arr, n)) 89 | cout << "Yes\n"; 90 | else 91 | cout << "No\n"; 92 | } 93 | return 0; 94 | } // } Driver Code Ends 95 | 96 | -------------------------------------------------------------------------------- /Subarrays with equal 1s and 0s: -------------------------------------------------------------------------------- 1 | Given an array containing 0s and 1s. Find the number of subarrays having equal number of 0s and 1s. 2 | 3 | Example 1: 4 | 5 | Input: 6 | n = 7 7 | A[] = {1,0,0,1,0,1,1} 8 | Output: 8 9 | Explanation: The index range for the 8 10 | sub-arrays are: (0, 1), (2, 3), (0, 3), (3, 4), 11 | (4, 5) ,(2, 5), (0, 5), (1, 6) 12 | Example 2: 13 | 14 | Input: 15 | n = 5 16 | A[] = {1,1,1,1,0} 17 | Output: 1 18 | Explanation: The index range for the 19 | subarray is (3,4). 20 | Your Task: 21 | You don't need to read input or print anything. Your task is to complete the function countSubarrWithEqualZeroAndOne() which takes the array arr[] and the size of the array as inputs and returns the number of subarrays with equal number of 0s and 1s. 22 | 23 | Expected Time Complexity: O(n). 24 | Expected Auxiliary Space: O(n). 25 | 26 | Constraints: 27 | 1 <= n <= 106 28 | 0 <= A[i] <= 1 29 | 30 | // { Driver Code Starts 31 | #include 32 | using namespace std; 33 | 34 | 35 | // } Driver Code Ends 36 | 37 | 38 | class Solution{ 39 | public: 40 | //Function to count subarrays with 1s and 0s. 41 | long long int countSubarrWithEqualZeroAndOne(int arr[], int n) 42 | { 43 | unordered_map m; 44 | m[0]=1; 45 | long long int sum=0,res=0; 46 | for(int i=0;i0) 56 | { 57 | res=res+m[sum]; 58 | } 59 | 60 | m[sum]++; 61 | } 62 | return res; 63 | 64 | } 65 | }; 66 | 67 | // { Driver Code Starts. 68 | 69 | int main() 70 | { 71 | int t; 72 | cin>>t; 73 | while(t--) 74 | { 75 | int n,i; 76 | cin>>n; 77 | int arr[n]; 78 | for(i=0;i>arr[i]; 80 | Solution obj; 81 | cout<< obj.countSubarrWithEqualZeroAndOne(arr, n)<<"\n"; 82 | } 83 | return 0; 84 | } 85 | // } Driver Code Ends 86 | -------------------------------------------------------------------------------- /Subarrays with sum K: -------------------------------------------------------------------------------- 1 | Given an unsorted array of integers, find the number of subarrays having sum exactly equal to a given number k. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 5 8 | Arr = {10 , 2, -2, -20, 10} 9 | k = -10 10 | Output: 3 11 | Explaination: 12 | Subarrays: arr[0...3], arr[1...4], arr[3..4] 13 | have sum exactly equal to -10. 14 | 15 | Example 2: 16 | 17 | Input: 18 | N = 6 19 | Arr = {9, 4, 20, 3, 10, 5} 20 | k = 33 21 | Output: 2 22 | Explaination: 23 | Subarrays : arr[0...2], arr[2...4] have sum 24 | exactly equal to 33. 25 | 26 | Your Task: 27 | You don't need to read input or print anything. Your task is to complete the function findSubArraySum() which takes the array Arr[] and its size N and k as input parameters and returns the count of subarrays. 28 | 29 | 30 | Expected Time Complexity: O(NlogN) 31 | Expected Auxiliary Space: O(N) 32 | 33 | 34 | Constraints: 35 | 1 ≤ N ≤ 2*10^4 36 | -10^3 ≤ Arr[i] ≤ 10^3 37 | -10^7 ≤ k ≤ 10^7 38 | 39 | // { Driver Code Starts 40 | #include 41 | using namespace std; 42 | 43 | // } Driver Code Ends 44 | //User function Template for C++ 45 | 46 | class Solution{ 47 | public: 48 | int findSubArraySum(int Arr[], int N, int k) 49 | { 50 | unordered_map m; 51 | m[0]=1; 52 | int res=0,sum=0; 53 | for(int i=0;i>t; 74 | while(t--) 75 | { 76 | int N; 77 | cin >> N; 78 | int Arr[N]; 79 | for(int i=0;i>Arr[i]; 81 | int k; 82 | cin>>k; 83 | Solution obj; 84 | cout< 29 | using namespace std; 30 | int main() 31 | { 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | int arr[n]; 39 | int x=0; 40 | for(int i=0;i>arr[i]; 43 | } 44 | 45 | unsigned long long int ans=0; 46 | for(int i=0;i<32;i++) 47 | { 48 | unsigned long long count=0,count1=0; 49 | for(int j=0;j 37 | using namespace std; 38 | int main() 39 | { 40 | int t; 41 | cin>>t; 42 | while(t--) 43 | { 44 | int n,k,sum=0,x; 45 | cin>>n; 46 | set s; 47 | for(int i=0;i>x; 50 | s.insert(x); 51 | 52 | 53 | } 54 | cin>>k; 55 | auto it=s.begin(); 56 | for(int i=0;i 29 | using namespace std; 30 | 31 | // } Driver Code Ends 32 | //User function Template for C++ 33 | 34 | class Solution 35 | { 36 | public: 37 | int sum(int a , int b) 38 | { 39 | int diff,carry; 40 | while(b) 41 | { 42 | diff=a^b; 43 | carry=(a&b); 44 | a=diff; 45 | b=carry<<1; 46 | 47 | } 48 | return a; 49 | } 50 | }; 51 | 52 | // { Driver Code Starts. 53 | int main() 54 | { 55 | int t; 56 | cin >> t; 57 | while (t--) 58 | { 59 | int a,b; 60 | cin >> a>>b; 61 | 62 | Solution ob; 63 | cout<< ob.sum(a,b) <<"\n"; 64 | 65 | } 66 | return 0; 67 | } 68 | // } Driver Code Ends 69 | -------------------------------------------------------------------------------- /Swap all odd and even bits: -------------------------------------------------------------------------------- 1 | Given an unsigned integer N. The task is to swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43(00101011). Here, every even position bit is swapped with adjacent bit on the right side(even position bits are highlighted in the binary representation of 23), and every odd position bit is swapped with an adjacent on the left side. 2 | 3 | Example 1: 4 | 5 | Input: N = 23 6 | Output: 43 7 | Explanation: 8 | Binary representation of the given number 9 | is 00010111 after swapping 10 | 00101011 = 43 in decimal. 11 | Example 2: 12 | 13 | Input: N = 2 14 | Output: 1 15 | Explanation: 16 | Binary representation of the given number 17 | is 10 after swapping 01 = 1 in decimal. 18 | 19 | Your Task: Your task is to complete the function swapBits() which takes an integer and returns an integer with all the odd and even bits swapped. 20 | 21 | 22 | Expected Time Complexity: O(log N). 23 | Expected Auxiliary Space: O(1). 24 | 25 | Constraints: 26 | 1 ≤ N ≤ 109// { Driver Code Starts 27 | //Initial Template for C++ 28 | 29 | #include 30 | using namespace std; 31 | 32 | 33 | // } Driver Code Ends 34 | 35 | 36 | //User function Template for C++ 37 | 38 | // function to swap odd and even bits 39 | unsigned int swapBits(unsigned int n) 40 | { 41 | 42 | int odd=0x55555555; 43 | int even=0xAAAAAAAA; 44 | int x=n&odd; 45 | int y=n&even; 46 | x<<=1; 47 | y>>=1; 48 | return x|y; 49 | 50 | } 51 | 52 | // { Driver Code Starts. 53 | 54 | // Driver code 55 | int main() 56 | { 57 | int t; 58 | cin>>t;//testcases 59 | while(t--) 60 | { 61 | unsigned int n; 62 | cin>>n;//input n 63 | 64 | //calling swapBits() method 65 | cout << swapBits(n) << endl; 66 | } 67 | return 0; 68 | } // } Driver Code Ends 69 | -------------------------------------------------------------------------------- /Tiger Zinda Hai: -------------------------------------------------------------------------------- 1 | Rohan is downloading the movie Tiger Zinda Hai using a torrent website, but he is new to torrent, so he doesn't know the difference between a fake download button and a real download button; therefore, he keeps pressing every button in excitement. 2 | Now he has clicked N buttons, and many tabs are opened , if a opened tab is clicked again then it closes it. 3 | Your task is to tell how many tabs are open at the end. 4 | 5 | Input: 6 | First line consists of T test cases. First line of every test case consists of an integer N. Next line will be the N numbers of Tab clicked or "END" . The "END" button means that all the tabs will be closed. 7 | 8 | Output: 9 | Single line output, print how many tabs are open at the end. 10 | 11 | Constraints: 12 | 13 | 1<=T<=100 14 | 15 | 1<=N<=10000 16 | 17 | Example: 18 | Input: 19 | 1 20 | 5 21 | 1 2 1 END 2 22 | Output: 23 | 1 24 | 25 | Explanation: 26 | In the above test case, firstly tab 1st is opened then 2nd is opened then 1st is closed then all are closed then again 2nd is opened. 27 | 28 | 29 | #include 30 | using namespace std; 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | vector s; 40 | map m; 41 | for(int i=0;i>str; 45 | s.push_back(str); 46 | } 47 | 48 | for(int i=0;isecond==1) 69 | count++; 70 | } 71 | 72 | cout<=0;i--) 62 | { 63 | right[i]=max(right[i+1],arr[i]); 64 | 65 | } 66 | 67 | for(int i=0;i 30 | using namespace std; 31 | 32 | 33 | // } Driver Code Ends 34 | 35 | 36 | 37 | 38 | 39 | // function to find the triplet which sum to x 40 | // arr[] : The input Array 41 | // N : Size of the Array 42 | // X : Sum which you need to search for 43 | 44 | bool find3Numbers(int arr[], int N, int X) 45 | { 46 | sort(arr,arr+N); 47 | 48 | for(int k=0;kX) 55 | { 56 | j--; 57 | } 58 | 59 | else if(arr[i]+arr[j]+arr[k]>T; 82 | while(T--) 83 | { 84 | int N,sum; 85 | cin>>N>>sum; 86 | int i,A[N]; 87 | for(i=0;i>A[i]; 89 | cout << find3Numbers(A, N, sum) << endl; 90 | } 91 | } 92 | // } Driver Code Ends 93 | -------------------------------------------------------------------------------- /Twice counter: -------------------------------------------------------------------------------- 1 | Given an array of n words. Some words are repeated twice, we need count such words. 2 | 3 | Input: 4 | The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the number of words in the string. The next line contains n space separated words forming the string. 5 | 6 | Output: 7 | Print the count of the words which are repeated twice in the string. 8 | 9 | Constraints: 10 | 1<=T<=105 11 | 1<=no of words<=105 12 | 1<=length of each word<=105 13 | 14 | Example: 15 | Input: 16 | 2 17 | 10 18 | hate love peace love peace hate love peace love peace 19 | 8 20 | Tom Jerry Thomas Tom Jerry Courage Tom Courage 21 | 22 | Output: 23 | 1 24 | 2 25 | 26 | 27 | #include 28 | using namespace std; 29 | int main() 30 | { 31 | int t; 32 | cin>>t; 33 | while(t--) 34 | { 35 | map m; 36 | int n,count=0; 37 | cin>>n; 38 | for(int i=0;i>s; 42 | m[s]++; 43 | 44 | } 45 | 46 | for(auto i=m.begin();i!=m.end();i++) 47 | { 48 | if(i->second==2) 49 | count++; 50 | 51 | } 52 | 53 | 54 | cout< 37 | using namespace std; 38 | int main() 39 | { 40 | int t; 41 | cin>>t; 42 | while(t--) 43 | { 44 | int n,m; 45 | cin>>n>>m; 46 | set s; 47 | 48 | for(int i=0;i>x; 52 | s.insert(x); 53 | } 54 | 55 | for(int j=0;j>y; 59 | s.insert(y); 60 | } 61 | cout< 34 | using namespace std; 35 | bool valid(string str); 36 | int main() 37 | { 38 | int t; 39 | cin>>t; 40 | cin.ignore(); 41 | while(t--) 42 | { 43 | string str; 44 | getline(cin,str); 45 | cout< res; 54 | for(int i=0;i 31 | using namespace std; 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int n; 39 | cin>>n; 40 | int count=0; 41 | map m; 42 | for(int i=0;i>s; 46 | if(m[s]>=1) 47 | { 48 | m[s]=m[s]+1; 49 | } 50 | 51 | else 52 | { 53 | m[s]=1; 54 | } 55 | 56 | } 57 | for(auto i=m.begin();i!=m.end();i++) 58 | { 59 | count=max(count,i->second); 60 | } 61 | 62 | for(auto i=m.begin();i!=m.end();i++) 63 | { 64 | if(i->second==count) 65 | { 66 | cout<first<<" "<second< 34 | using namespace std; 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | long int n; 42 | cin>>n; 43 | int arr[n]; 44 | unordered_map m; 45 | m[0]=1; 46 | int sum=0,res=0; 47 | for(long int i=0;i>arr[i]; 50 | sum=sum+arr[i]; 51 | if(m[sum]>0) 52 | { 53 | res=res+m[sum]; 54 | } 55 | 56 | m[sum]++; 57 | } 58 | cout< 35 | using namespace std; 36 | 37 | 38 | // } Driver Code Ends 39 | 40 | class Solution{ 41 | public: 42 | int lenOfLongSubarr(int A[], int N, int K) 43 | { 44 | unordered_map m; 45 | long long int sum=0; 46 | int maxi=0; 47 | m[0]=-1; 48 | for(int i=0;i>t; 70 | while(t--) 71 | { 72 | int n, k; 73 | cin>> n >> k; 74 | int a[n]; 75 | 76 | for(int i=0;i>a[i]; 78 | Solution ob; 79 | cout << ob.lenOfLongSubarr(a, n , k)<< endl; 80 | 81 | } 82 | 83 | return 0; 84 | } // } Driver Code Ends 85 | -------------------------------------------------------------------------------- /greatest smaller on left: -------------------------------------------------------------------------------- 1 | Given an array arr[] of N positive integers. The task is to find the greatest smaller on left of every element in the array. 2 | 3 | Input: 4 | First line of input contains number of testcases T. For each testcase, first line of input contains size of array N and next line contains array elements. 5 | 6 | Output: 7 | For each testcase, print the array which contains greatest smaller element on left of every corresponding element in the array. For the element whose smaller on left doesn't exists, consider -1 as greatest smaller. 8 | 9 | Constraints: 10 | 1 <= T <= 100 11 | 1 <= N <= 106 12 | 1 <= arr[i] <= 108 13 | 14 | Example: 15 | Input: 16 | 1 17 | 5 18 | 2 3 4 5 1 19 | 20 | Output: 21 | -1 2 3 4 -1 22 | 23 | Explanation: 24 | Testcase 1: Smallest element on left of 3 is 2, 4 is 3 and 5 is 1. Since 2 is the first element and no element on left is present, so it's greatest smaller element will be -1 and that for 1, no greatest smaller element is present on left, so it's greatest smaller element is -1. 25 | #include 26 | using namespace std; 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | int n; 34 | cin>>n; 35 | set s; 36 | for(int i=0;i>x; 40 | s.insert(x); 41 | auto it=s.lower_bound(x); 42 | if(s.begin()==it) 43 | cout<<"-1"<<" "; 44 | else 45 | { 46 | --it; 47 | cout<<*it<<" "; 48 | } 49 | } 50 | cout< 31 | 32 | using namespace std; 33 | 34 | 35 | // } Driver Code Ends 36 | 37 | 38 | //User function template for C++ 39 | class Solution{ 40 | public: 41 | vector kLargest(int arr[], int n, int k) { 42 | priority_queue pq; 43 | vector v; 44 | for(int i=0;i> t; 66 | while (t--) { 67 | int n, k; 68 | cin >> n >> k; 69 | int arr[n]; 70 | for (int i = 0; i < n; i++) { 71 | cin >> arr[i]; 72 | } 73 | Solution ob; 74 | auto ans = ob.kLargest(arr, n, k); 75 | for (auto x : ans) { 76 | cout << x << " "; 77 | } 78 | cout << "\n"; 79 | } 80 | return 0; 81 | } 82 | // } Driver Code Ends 83 | -------------------------------------------------------------------------------- /set-bits and number: -------------------------------------------------------------------------------- 1 | Given a number N. Find the product of the number of setbits and the number itself. 2 | 3 | 4 | Example 1: 5 | 6 | Input: N = 2 7 | Output: 2 8 | Explaination: 2 has one setbit. So, 2*1 = 2. 9 | 10 | Example 2: 11 | 12 | Input: N = 3 13 | Output: 6 14 | Explaination: 3 has 2 setbits. 3*2 = 6. 15 | 16 | Your Task: 17 | You do not need to read input or print anything. Your task is to complete the function bitMultiply() which takes the number N as input parameters and returns the product. 18 | 19 | 20 | Expected Time Complexity: O(Log N) 21 | Expected Auxiliary Space: O(1) 22 | 23 | 24 | Constraints: 25 | 1 <= N <= 106 26 | // { Driver Code Starts 27 | // Initial Template for C++ 28 | #include 29 | using namespace std; 30 | 31 | // } Driver Code Ends 32 | 33 | 34 | // User function Template for C++ 35 | class Solution{ 36 | public: 37 | int bitMultiply(int N){ 38 | int num=N; 39 | int count=0; 40 | while(num) 41 | { 42 | count++; 43 | num=num&(num-1); 44 | } 45 | return N*count; 46 | } 47 | }; 48 | 49 | // { Driver Code Starts. 50 | int main(){ 51 | int t; 52 | cin>>t; 53 | while(t--){ 54 | int N; 55 | cin>>N; 56 | 57 | Solution ob; 58 | cout<