Posts

Given the names and grades for each student in a class of students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.

 """ Sample Input 0 5 Harry 37.21 Berry 37.21 Tina 37.2 Akriti 41 Harsh 39 Sample Output 0 Berry Harry """ if __name__ == '__main__':     marks = []     for _ in range(int(input())):         name = input()         score = float(input())         n = [score, name]         marks.append(n)     marks.sort()     m = marks[0][0]     # print(m)     count = 0     for i in marks:         if m == i[0]:             count += 1     for i in range(count):         marks.remove(marks[0])     # print(marks)     z = marks[0][0]     for i in marks:      ...

Given a string S. The task is to print all permutations of a given string.

  /*   Example 1: Input: ABC Output: ABC ACB BAC BCA CAB CBA Explanation: Given string ABC has permutations in 6 forms as ABC, ACB, BAC, BCA, CAB and CBA . Example 2: Input: ABSG Output: ABGS ABSG AGBS AGSB ASBG ASGB BAGS BASG BGAS BGSA BSAG BSGA GABS GASB GBAS GBSA GSAB GSBA SABG SAGB SBAG SBGA SGAB SGBA Explanation: Given string ABSG has 24 permutations.   Your Task:   You don't need to read input or print anything. Your task is to complete the function find_permutaion() which takes the string S as input parameter and returns a vector of string in lexicographical order.   Expected Time Complexity: O(n! * n) Expected Space Complexity: O(n) */ // { Driver Code Starts #include <bits/stdc++.h> using namespace std; // } Driver Code Ends class Solution { public:     vector<string> find_permutation(string S)     {         vector<string> r;         // ...

Given two sorted arrays arr1[] of size N and arr2[] of size M. Each array is sorted in non-decreasing order. Merge the two arrays into one sorted array in non-decreasing order without using any extra space.

 """ Example 1: Input: N = 4, M = 5 arr1[] = {1, 3, 5, 7} arr2[] = {0, 2, 6, 8, 9} Output: 0 1 2 3 5 6 7 8 9 Explanation: Since you can't use any extra space, modify the given arrays to form arr1[] = {0, 1, 2, 3} arr2[] = {5, 6, 7, 8, 9} Example 2: Input: N = 2, M = 3 arr1[] = {10, 12} arr2[] = {5, 18, 20} Output: 5 10 12 18 20 Explanation: Since you can't use any extra space, modify the given arrays to form arr1[] = {5, 10} arr2[] = {12, 18, 20}   Your Task: You don't need to read input or print anything. Complete the function merge() which takes the two arrays arr1[], arr2[] and their sizes n and m, as input parameters. The function does not return anything. Use the given arrays to sort and merge arr1[] and arr2[] in-place. Note: The generated output will print all the elements of arr1[] followed by all the elements of arr[2]. Expected Time Complexity: O((n+m)*log(n+m)) Expected Auxiliary Space: O(1) """ # User function Template for python3 c...

Best Time to Buy and Sell Stock

 /* You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.   Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.   Constraints:     1 <= prices.length <= 105     0 <= prices[i] <= 104 */ class Solution { public:     int maxProfit(vector<int> &prices)     {         int l = p...

Move all negative numbers to beginning and positive to end with constant extra space

 """ An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers. Examples : Input: -12, 11, -13, -5, 6, -7, 5, -3, -6 Output: -12 -13 -5 -7 -3 -6 11 6 5 Note: Order of elements is not important here. """ inp = [-1, 0, 5, 7, 3, 45, 5, 56, 7, 6, -2, -4, 8, -3] for i in range(len(inp)):     if(inp[i] < 0):         x = inp.pop(i)         inp.insert(0, x) print(inp)

Given an array of n positive integers and a number k. Find the minimum number of swaps required to bring all the numbers less than or equal to k together

 /* Example 1: Input : arr[ ] = {2, 1, 5, 6, 3} and K = 3 Output : 1 Explanation: To bring elements 2, 1, 3 together, swap element '5' with '3' such that final array will be- arr[] = {2, 1, 3, 6, 5} Example 2: Input : arr[ ] = {2, 7, 9, 5, 8, 7, 4} and K = 6 Output :  2   Your Task: This is a function problem. The input is already taken care of by the driver code. You only need to complete the function minSwap() that takes an array (arr), sizeOfArray (n), an integer K, and return the minimum swaps required. The driver code takes care of the printing. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 105 1 ≤ Arri, K ≤107 */ // { Driver Code Starts // C++ program to find minimum swaps required // to club all elements less than or equals // to k together #include <iostream> using namespace std; int minSwap(int *arr, int n, int k); // Driver code int main() {     int t, n, k;     cin >> t;  ...

Given an array, rotate the array to the right by k steps, where k is non-negative.

 """ Example 1: Input: nums = [1, 2, 3, 4, 5, 6, 7], k = 3 Output: [5, 6, 7, 1, 2, 3, 4] Explanation: rotate 1 steps to the right: [7, 1, 2, 3, 4, 5, 6] rotate 2 steps to the right: [6, 7, 1, 2, 3, 4, 5] rotate 3 steps to the right: [5, 6, 7, 1, 2, 3, 4] Example 2: Input: nums = [-1, -100, 3, 99], k = 2 Output: [3, 99, -1, -100] Explanation: rotate 1 steps to the right: [99, -1, -100, 3] rotate 2 steps to the right: [3, 99, -1, -100] Constraints:     1 <= nums.length <= 105     -231 <= nums[i] <= 231 - 1     0 <= k <= 105 Follow up:     Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.     Could you do it in -place with O(1) extra space? """ class Solution:     def rotate(self, nums: List[int], k: int) -> None:         """         Do not retu...