Posts

Showing posts from September, 2020

Linux

 Use in linux 1)regular user :- home directory 2) Root user:- Full access (super user) sudo command ka use hota h root user mai 3) service user : eg - apache Absolute  vs Relative path : pwd - print working directory for example cd/bin/f01 (absolute path) cd fo2(relative path) $ (matlab regular user)

Max sum in sub-arrays (GFG)

  Max sum in sub-arrays   Given an array, find maximum sum of smallest and second smallest elements chosen from all possible sub-arrays. More formally, if we write all (nC2) sub-arrays of array of size >=2 and find the sum of smallest and second smallest, then our answer will be maximum sum among them. Input: The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array. Output: For each test case in  a new line print an integer denoting the maximum sum of smallest and second smallest elements chosen from all possible subarrays. Constraints: 1<=T<=10 3 2<=N<=10 3 1<=A[ ]<=10 18 Example: Input: 2 3 4 5 1 5 4 3 1 5 6 Output: 9 11 SOLUTION: t = int(input()) for _ in range(t):     n = int(input())     res = []     l = list(map(int,input().split()))     for i in range(len(l)-1):         res.append(l[i]

Reverse a string using Stack (GFG)

Reverse a string using Stack  An string of words is given, the task is to reverse the string using stack. Input Format: The first line of input will contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains a string s of words without spaces. Output Format: For each test case ,print the reverse of the string in new line.  Your Task: Since this is a function problem, you don't need to take any input. Just complete the provided function. Constraints: 1 <= T <= 100 1 <= length of the string <= 100 Example: Input: 2 GeeksQuiz GeeksforGeeks Output: ziuQskeeG skeeGrofskeeG SOLUTION: def reverse(string):          #Add code here     return string[::-1]

Pairwise Consecutive Elements (GFG)

  Pairwise Consecutive Elements   Given a stack of integers of size  N , your task is to complete the function  pairWiseConsecutive(),  that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should retain the original stack content. Only following standard operations are allowed on  stack . push(X): Enter  a element  X on top of  stack . pop(): Removes top element of the stack. empty(): To check if stack is empty. Input Format: The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains n denoting the number of elements to be inserted into the stack. The second line contains the elements to be inserted into the stack. Output Format: For each  testcase, in a new line, print  " Yes "(without quote) if the elements of

Immediate Smaller Element (GFG)

  Immediate Smaller Element   Given an integer array of size  N . For each element in the array, check whether the right adjacent element (on the next immediate position) of the array is smaller. If next element is smaller, print that element. If not, then print -1. Input: The first line of input contains an integer  T  denoting the number of test cases.  T  testcases follow. Each testcase contains 2 lines of input: The first line contains an integer  N , where  N  is the size of array. The second line contains  N  integers(elements of the array) sperated with spaces. Output: For each test case, print the next immediate smaller elements for each element in the array. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 10 7 1 ≤ arr[i] ≤ 1000 Example: Input 2 5 4 2 1 5 3 6 5 6 2 3 1 7 Output 2 1 -1 3 -1 -1 2 -1 1 -1 -1 Explanation: Testcase 1: Array elements are 4, 2, 1, 5, 3. Next to 4 is 2 which is smaller, so we print 2. Next of 2 is 1 which is smaller, so we print 1. Next of 1 is 5 which is greater, so

Remove repeated digits in a given number

  Remove repeated digits in a given number   Given an integer N, remove consecutive repeated digits from it. 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 the integer N. Output: Print the number after removing consecutive digits. Print the answer for each test case in a new line. Constraints: 1<= T <=100 1<= N <=10 18 Example: Input: 1 12224 Output: 124 Solution: t=int(input()) for i in range (t):     stack =[]     number = input()     for i in number:         if len(stack)>0:             if stack[-1] != i:                 stack.append(i)         else:             stack.append(i)     print("".join(stack))        

Implement stack using array

  Implement stack using array   Write a program to implement a Stack using Array. Your task is to use the class as shown in the comments in the code editor and complete the functions push() and pop() to implement a stack.  Example 1 : Input : push(2) push(3) pop() push(4) pop() Output : 3, 4 Explanation : push(2) the stack will be {2} push(3) the stack will be {2 3} pop() poped element will be 3,   the stack will be {2} push(4) the stack will be {2 4} pop() poped element will be 4 Example 2: Input : pop() push(4) push(5) pop() Output : -1, 5 Your Task: You are required to complete two methods  push() and pop().  The push() method takes one argument, an integer  'x'  to be pushed into the stack and  pop()  which returns an integer present at the top and popped out from the stack. If the stack is empty then return  -1  from the pop() method. Expected Time Complexity  : O(1) for both  push()  and  pop() . Expected Auixilliary Space  : O(1) for both