Posts

Showing posts from August, 2020

I hate Even Subarrays (HACKEREARTH)

  You are given a binary string, (string which contains 0's and 1's), You have to perform several operations on this string, in one operation choose a non-empty   even length substring   containing only 0's or only 1's and remove it from the string. Your goal is to minimize the final length of the string after performing several operations.It is possible that the final string may become empty, in that case print  "KHALI"  without quotes. And it can be proved that there is always an unique string with minimal length after performing the operations. Input: First line of input contains an intger  T  denoting number of testcases. Next  T  lines of input contains a binary string  S . Output: for each testcase print the required minimal string. Constraints: 1 <= T <= 10 1 <= |S|  <= 10 5   SAMPLE INPUT   2 101001 1001 SAMPLE OUTPUT   10 KHALI Explanation for the first test case, first remove substring "00", now string will become "1011&quo

Missing number in array

  Missing number in array   Given an array  C  of size  N-1  and given that there are numbers from  1  to  N  with one element missing, the missing number is to be found. Input: The first line of input contains an integer  T  denoting the number of test cases. For each test case first line contains  N (size of array). The subsequent line contains N-1 array elements. Output: Print the missing number in array. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 10 7 1 ≤ C[i] ≤ 10 7 Example: Input: 2 5 1 2 3 5 10 1 2 3 4 5 6 7 8 10 Output: 4 9 Explanation: Testcase 1:  Given array : 1 2 3 5. Missing element is 4. SOLUTION: import math t=int(input()) for i in range (t):     n=int(input())     p=list(map(int,input().split()))     r=n*(n+1)/2     q=sum(p)     w=math.floor(r-q)     print(w)

Kth smallest element

  Kth smallest element   Given an array  arr[]  and a number  K  where K is smaller than size of array, the task is to find the  K th  smallest  element in the given array. It is given that all array elements are distinct. Input: The first line of input contains an integer  T,  denoting the number of testcases. Then T test cases follow. Each test case consists of three lines. First line of each testcase contains an integer  N  denoting size of the array. Second line contains N space separated integer denoting elements of the array. Third line of the test case contains an integer K. Output: Corresponding to each test case, print the k th  smallest element in a new line. Expected Time Complexity:  O(N). Expected Auxiliary Space:  O(1). Constraints: 1 <= T <= 100 1 <= N <= 10 5 1 <= arr[i] <= 10 5 1 <= K <= N Example: Input: 2 6 7 10 4 3 20 15 3 5 7 10 4 20 15 4 Output: 7 15 Explanation: Testcase 1:  3rd smallest element in the given array is 7. Testcase 2:  4th sm

Element appearing once

  Element appearing once   Given an sorted array  A[i]  of  N  positive integers having all the numbers occuring exactly twice, except for one number which will occur only once. Find the number occuring only once. Input 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 a positive integer  N , denoting the size of the array. The second line of each test case contains a  N  positive integers, denoting the elements of the array. Output Print out the singly occuring number. Constraints 1 <=  T   <= 100 0 <    N   <= 100 0 <=  A[i]  <= 100 Examples  Input 2 5 1 1 2 5 5 7 2 2 5 5 20 30 30 Output 2 20   Expected Complexity Time :  O(N) SOLUTION: t=int(input()) for i in range (t):     n=int(input())     p=list(map(int,input().split()))     for i in p:         if(p.count(i)==1):             print(i)             break

Count the Zeros

  Count the Zeros   Given an array of size  N  consisting of only 0's and 1's ,which is  sorted  in such a manner that all the 1's are placed first and then they are followed by all the 0's. You have to find  the  count  of all the 0's.   Input: 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 an integer  N , where  N  is the size of the array  A[ ] . The second line of each test case contains  N  space separated integers of all 1's follwed by all the 0's, denoting elements of the array  A[ ] . Output: Print out the number of 0's in the array.    Constraints: 1 <=  T  <= 100 1 <=  N  <= 30 0 <=  A[i]  <= 1   Example : Input: 3 12 1 1 1 1 1 1 1 1 1 0 0 0  5 0 0 0 0 0 6 1 1 1 1 1 1 Output: 3 5 0   Expected Complexity: O(logN) SOLUTION: t=int(input()) for i in range (t):     n=int(input())     p=list(map(int,input().split()))     print(

Find Transition Point

  Find Transition Point   You are given a sorted array containing only numbers 0 and 1. Find the transition point efficiently. The transition point is a point where "0" ends and "1" begins (0 based indexing). Note that, if there is no "1" exists, print -1. Note that, in case of all 1s print 0. Example 1: Input: N = 5 C[] = {0,0,0,1,1} Output: 3 Explanation: position 3 is 0 and next one is 1, so answer is 3. Example 2: Input: N = 4 C[] = {0,0,0,0} Output: -1 Explanation: Since, there is no "1",so the answer is -1. Your Task: The task is to complete the function  transitionPoint () that takes array and N as input and returns the value of the position where "0" ends and "1" begins. Expected Time Complexity:  O(LogN). Expected Auxiliary Space:  O(1). Constraints: 1 ≤ N ≤ 500000 0 ≤ C[i] ≤ 1 SOLUTION: # your task is to complete this function # finvtion should return an integer def transitionPoint(arr, n):     #Code here     if(a

Last index of One

  Last index of One   Given a string  S  consisting only ' 0 's and ' 1 's,  print the last index of the '1' present in it. Input: First line of the input contains the number of test cases  T , T lines follow each containing a stream of characters. Output: Corresponding to every test case, output the last index of 1. If 1 is not present, print "-1" (without quotes). Constraints: 1 <= T <= 110 1 <= |S| <= 10 6 Example: Input: 2 00001 0 Output: 4 -1 Explanation: Testcase 1:  Last index of  1 in given string is 4. Testcase 2:  Since, 1 is not present, so output is -1. SOLUTION: t=int(input()) for i in range (t):     s=input()     c=-1     for j in range(0,len(s)):         if s[j]=="1":             c=j     print (c)

Number of occurrence (GFG)

  Given a sorted array   A  of size   N  and a number   X , you need to find the number of occurrences of  X i n   A . Input: The first line of input contains an integer  T  denoting the number of test cases.  T  testcases follow. Each testcase contains two lines of input: The first line contains N and X(element whose occurrence needs to be counted). The second line contains the   elements of the array separated by spaces. Output: For each testcase, print the count of the occurrences of  X  in the array, if count is zero then print  -1 . Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 10 5 1 ≤ A[i] ≤ 10 3 1 <= X <= 10 3 Example: Input: 2 7 2 1 1 2 2 2 2 3 7 4 1 1 2 2 2 2 3 Output: 4 -1 Explanation: Testcase 1:  2 occurs 4 times in 1 1 2 2 2 2 3 Testcase 2:  4 is not present in 1 1 2 2 2 2 3 SOLUTION: t=int(input()) for i in range(t):     x,y = map(int,input().split())     li = list(map(int,input().split()))     if y in li:         print(li.count(y))     else:         print('-1')

Square root (GFG)

  Given an integer   x . The task is to find the square root of x. If   x   is not a perfect square, then return floor(√x). Example 1: Input: x = 5 Output: 2 Explanation: Since, 5 is not perfect square, so floor of square_root of 5 is 2. Example 2: Input: x = 4 Output: 2 Explanation: Since, 4 is a perfect square, so its square root is 2. Your Task: The task is to complete the function  floorSqrt () which should return the square root of given number x. Expected Time Complexity:  O(log N). Expected Auxiliary Space:  O(1). Constraints: 1 ≤ x ≤ 10 7 SOLUTION: #User function Template for python3 #Complete this function def floorSqrt(x):      #Your code here     return math.floor(math.sqrt(x))

Remove consonants from a string (GFG)

  Remove consonants from a string   Given a string s, remove all consonants and prints the string s that contains vowels only. Input : The first line of input contains integer T denoting the number of test cases. For each test case, we input a string. Output:  For each test case, we get a string containing only vowels .  If the string doesn't contain any vowels, then print "No Vowel" Constraints: 1<=T<=100 The string should consist of only alphabets. Examples: Input: geEks Output: eE Input: what are you doing Output: a ae ou oi SOLUTION: t= int(input()) for i in range (t):     s=input()     ans = ''     for l in s:         if l in "aeiouAEIOU ":             ans += l     if ans == '':         print("No Vowel")     else:         print(ans)

Product is even or odd (GFG)

  Product is even or odd?   You are given two numbers N1 and N2. You need to find out if the product of these numbers generate an even number or an odd number. Input: The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each test case contains two lines of input:- The first line contains N1 The second line contains N2 Output: For each test case, Print 1 or 0  according to the result. if even print 1 else print 0. Constraints: 1<=T<=120 1<=N1,N2<=10 99 Example: Input: 3 5 8 2 98 773 13 Output: 1 1 0 Explanation: For test case 1: N1 is 5, N2 is 8; 5*8=40 is even, so we print 1. SOLUTION: t=int(input()) for i in range (t):     b=int(input())     c=int(input())     d=b*c     if d%2==0:         print("1")     else:         print("0")

Length of Last word (GFG)

  Given a string   S   consisting of upper/lower-case alphabets and empty space characters ‘ ‘. The string may contain spaces at the end. You will have return the length of last word which consists of alphabets only. Input: First line of input consists of  T  test cases. The only line of every test case consists of String S. Output: You have to return the length of last word of the string. Constraints: 1<=T<=100 1<=|String length|<=100 Example: Input: 2 Geeks for Geeks Start Coding Here Output: 5 4 SOLUTION: t=int(input()) for i in range (t):     s=list(map(str,input().split()))     n=len(s)     if n==1:         print(len(s[0]))     else:         print(len(s[-1]))

Pattern Searching (GFG)

Pattern Searching  Given a text and a pattern. Find whether the pattern exist in the text or not. If it is present print "found" without quotes else print "not found" without quotes. Input: The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a separate line. Output: Print "found" or "not found" in a separate line. Constraints: 1 ≤ T ≤ 30 1 ≤ |s| ≤ 100 Example: Input 1 geeksforgeeks geeks Output found SOLUTION: t=int(input()) for i in range (t):     a=input()     b=input()     if b in a :         print("found")     else:         print("not found")

Reverse words in a given string (GFG)

  Given a String S, reverse the string without reversing its individual words. Words are separated by dots. Example 1: Input: S = i.like.this.program.very.much Output: much.very.program.this.like.i Explanation: After reversing the whole string(not individual words), the input string becomes much.very.program.this.like.i Example 2: Input: S = pqr.mno Output: mno.pqr Explanation: After reversing the whole string , the input string becomes mno.pqr Your Task: You dont need to read input or print anything. Complete the function  reverseWords()  which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.'  Expected Time Complexity:  O(|S|) Expected Auxiliary Space:  O(|S|) Constraints: 1 <= |S| <= 2000   Solution: def reverseWords(s):     a = ".".join(         reversed(s.split('.')))     print(a, end='')     return ''

Replace by X (GFG)

Image
  Replace by X   Given a string and a pattern, Replace all the continuous occurrence of pattern with a single X in the string. For a clear view see the example below. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is string str. The second line of each test case contains a string s,which is a pattern. Output: Print the modified string str. Constraints: 1 ≤ T ≤ 100 1 ≤ size of str,s ≤ 1000 Example: Input 2 abababcdefababcdab ab geeksforgeeks geeks Output XcdefXcdX XforX SOLUTION: t=int(input()) for i in range (t):     s=input()     p=input()     if p in s:         h=s.replace(p,"X")         print(h) #parakram ek question yeh atlest bata do..... Wrong Answer. !!! Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: hwllosdhhjhrajatttttteejbarajarajaahsdghjraja raja Its Correct output is: hwllosdhhjhXtttttteejbaXahsdghjX And Your Co