Posts

Showing posts from September, 2021

Subsets of arr bitwise

 void subarray(int*arr, int n){     //hashmap     for(int i=0;i<=(1<<n);i++){         for(int j=0;j<n;j++){             if(i&(1<<j)){                 cout<<arr[j]<<" ";             }         }         cout<<endl;     }      }

Set bits in log(n)

 int set_bits(int n){     int  count=0;     while(n){         n=n&(n-1);         count++;     }     return count; }

is power of 2 (Bitwise)

 bool is_pow2(int n){         while((n/2)>0){         int m=(n%2);                  //cout<<m;         if(m==1){             return false;         }         n=n/2;              }     return true; }

Precomputation

 // Online C++ compiler to run C++ program online #include <iostream> using namespace std; const int m=1e9+7; const int n=1e5+10; long long fact[n]; int main() {     fact[0]=fact[1]=1;     for(int i=2;i<n;i++){         fact[i]=fact[i-1]*i;     }     int num;     cin>>num;     cout<<fact[num];               return 0; }

Jiya's Sequence

  Problem Jiya likes a sequence if all its elements when multiplied yields a number , whose least significant digit is either 2, 3 or 5. Given the number of  test case t.The first line of each test case is a number n.Next line contains n integers - A 1 ,A 2 ,......A n .For each given test case tell whether the given sequence will be liked by Jiya. INPUT FORMAT- First line constains t number of test cases. Every test case has first line as the number of intergers the sequence contains, followed by sequence in the next line. OUTPUT FORMAT - Print "YES" or "NO",whether Jiya likes the sequence or not. CONSTRAINTS- 1≤t≤100 1≤n≤15 1≤A i ≤10, for all i   HINT -  Least significant digit of a number can be obtained by taking the number % 10. Mind the case of output to be printed. Sample Input 2 5 2 2 2 2 2 4 2 2 2 2 Sample Output YES NO Time Limit: 1 Memory Limit: 256 Source Limit: Explanation For the first testcase,considering the sequence 2 2 2 2 2, when we multi...

CapitalBaazi

  Problem Cheems doesn't like extra content so coming straight to the point. Given the input of one line, having words separated by a single space, print each word of the input, each in a new line. Also, change all the alphabets to uppercase along with doing this. Input:- Given in one line, all letters are lowercase only, separated by a single space. Characters used in input are from 'a' to 'z' only ( 26 in total ) , both included. Length of input   ≤   10 4 Output:- Output each word with characters in uppercase. Note:- A testcase contains only one word as the input to get partial marks. Also, the ideal solution has been provided, you can test any of your legal input to get its answer. Sample Input subscribe to the luv channel Sample Output SUBSCRIBE TO THE LUV CHANNEL Time Limit: 1 Memory Limit: 256 Source Limit: Explanation Every word of the input has been placed in the new line with characters in uppercase as well. #include < iostream > #incl...

Subarray with given sum

  Given an unsorted array   A  of size   N   that contains only non-negative integers, find a continuous sub-array which adds to a given number   S .   Example 1: Input: N = 5, S = 12 A[] = {1,2,3,7,5} Output: 2 4 Explanation: The sum of elements from 2nd position to 4th position is 12.   Example 2: Input: N = 10, S = 15 A[] = {1,2,3,4,5,6,7,8,9,10} Output: 1 5 Explanation: The sum of elements from 1st position to 5th position is 15.   Your Task: You don't need to read input or print anything. The task is to complete the function  subarraySum () which takes arr, N and S as input parameters and returns a list containing the starting and ending positions of the first such occurring subarray from the left where sum equals to S. The two indexes in the list should be according to 1-based indexing. If no such subarray is found, return an array consisting only one element that is -1.   Expected Time Complexity:...

MOZSATLA - Sharmeen and the Lost Array

  MOZSATLA - Sharmeen and the Lost Array no tags   Sharmeen loves array very much. Many days ago she wrote an array ( of n elements and the index of this array is 1 based ) in her notebook, but unfortunately she lost the notebook. She want to restore the array. The only clue she know that is if in any position(i) of the array (1<= i < n ), the element in this position is greater than, equal or less than the next position(i+1) element.             Can you help her to restore the array? Input In the first line given an integer t ( 1 <= t <= 100 ), which is the number of test cases. For each test case, In the first line there will be given a positive integer n ( 1 <= n <= 10^5 ) which is the size of the lost array. In the next line there will be n - 1 intergers X i ( 0 <= X i  <= 2 ). If, X i  = 0 , then i th   element is equal to (i+1) th  element of the lost array. If, X i ...

Micro and Array Update

  Problem Micro purchased an array  A  having  N  integer values. After playing it for a while, he got bored of it and decided to update value of its element. In one second he can increase value of each array element by  1 . He wants each array element's value to become greater than or equal to  K . Please help Micro to find out the minimum amount of time it will take, for him to do so. Input: First line consists of a single integer,  T , denoting the number of test cases. First line of each test case consists of two space separated integers denoting  N  and  K . Second line of each test case consists of  N  space separated integers denoting the array  A . Output: For each test case, print the minimum time in which all array elements will become greater than or equal to  K . Print a new line after each test case. Constraints: 1 ≤ T ≤ 5 1 ≤ N ≤ 10 5 1 ≤ A [ i ] , K ≤ 10 6 Sample Input 2 3 4 1 2 5 3 2 2 5 5 Sample ...

Jewels and Stones

  Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are not. Her King requested for her help in mining precious stones, so she has told him which all stones are jewels and which are not. Given her description, your task is to count the number of jewel stones. More formally, you're given a string J composed of latin characters where each character is a jewel. You're also given a string S composed of latin characters where each character is a mined stone. You have to find out how many characters of S are in J as well. Input First line contains an integer T denoting the number of test cases. Then follow T test cases. Each test case consists of two lines, each of which contains a string composed of English lower case and upper characters. First of these is the jewel string J and the second o...

Goal

  You own a   Goal Parser   that can interpret a string   command . The   command   consists of an alphabet of   "G" ,   "()"   and/or   "(al)"   in some order. The Goal Parser will interpret   "G"   as the string   "G" ,   "()"   as the string   "o" , and   "(al)"   as the string   "al" . The interpreted strings are then concatenated in the original order. Given the string  command , return  the  Goal Parser 's interpretation of  command .   class Solution { public:     string interpret(string command) {         string res;         for(int i=0;i<command.size();i++){             if(command[i]=='G'){                 res = res +'G';             }             else if(command[i]=='(' ...