Posts

Showing posts from April, 2021

heck if array contains contiguous integers with duplicates allowed

Easy  Accuracy:   65.11%  Submissions:   761  Points:   2 Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”. Example 1: รข€‹ Input : arr[ ] = {5, 2, 3, 6, 4, 4, 6, 6} Output : Yes Explanation: The elements  of array form a contiguous set of integers which is {2, 3, 4, 5, 6} so the output is "Yes". Example 2: Input : arr[ ] = {10, 14, 10, 12, 12,  13, 15} Output : No 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  areElementsContiguous()  that takes an array  (arr) , sizeOfArray  (n) , and return the  true  if it is a set of contiguous integers else print  false . The driver code takes care of the printing.

Delete without head pointer

You are given a pointer/ reference to the node which is to be deleted from the linked list of   N  nodes. The task is to delete the node. Pointer/ reference to head node is not given.  Note:  No head reference is given to you. It is guaranteed that the node to be deleted is   not a tail node   in the linked list. Example 1: Input: N = 2 value[] = {1,2} node = 1 Output: 2 Explanation: After deleting 1 from the linked list, we have remaining nodes as 2. Example 2: Input: N = 4 value[] = {10,20,4,30} node = 20 Output: 10 4 30 Explanation: After deleting 20 from the linked list, we have remaining nodes as 10, 4 and 30. Your Task: You only need to complete the  function deleteNode  that takes  reference  to the node that needs to be  deleted . The  printing  is done  automatically  by the  driver code .

Sum of Big Integers

Given two positive integers   X   and   Y . You have to add two integers and output their   sum .   Example 1: Input: X = 3, Y = 4 Output: 7 Explanation: Sum of X and Y is 7   Example 2: Input: X = 8, Y = 2 Output: 10 Explanation: Sum of X and Y is 10.   Your Task: Your task is to complete the function  add () which accepts BigIntegers x and y as input parameters, and returns their sum.

Find smallest values of x and y

Given two values   ‘a’   and   ‘b’   that represent coefficients in “ ax – by = 0 ”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. Example 1: Input: a = 25, b = 35 Output: 7 5 Explaination: 25*7 - 35*5 = 0. And x = 7 and y = 5 are the least possible values of x and y to get the equation solved. Example 2: Input: a = 3, b = 7 Output: 7 3 Explaination: For this case x = 7 and y = 3 are the least values of x and y to satisfy the equation. Your Task: You do not need to read input or print anything. Your task is to complete the function  findXY()  which takes a and b as input parameters and returns the least possible values of x and y to satisfy the equation.

Repeated sum of digits

Given an integer N, recursively sum digits of N until we get a single digit.  The process can be described below If N < 10 digSum(N) = N Else digSum(N) = Sum(digSum(N))   Example 1: Input: N = 1234 Output: 1 Explanation: The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1     Example 2: Input: N = 9999 Output: 9 Explanation: Check it yourself.   Your Task: You don't need to read input or print anything. Your task is to complete the function repeatedSumOfDigits() which takes an integer N and returns the repeated sum of digits of N.

occurrence 2 as a gidit

 x=0 for i in range(23):     x+=str(i).count('2') print(x)

Expression Tree

Given a full binary expression tree consisting of basic binary operators (+ , – ,*, /) and some integers, Your task is to evaluate the expression tree. Example 1: Input: + / \ * - / \ / \ 5 4 100 20 Output: 100 Explanation: ((5 * 4) + (100 - 20)) = 100 Example 2: Input: - / \ 4 7 Output: -3 Explanation: 4 - 7 = -3 Your Task:   You dont need to read input or print anything. Complete the function  evalTree()  which takes root node as input parameter and returns an integer denoting the result obtained by simplifying the expression tree. Expected Time Complexity:  O(N)

Count pairs from two linked lists whose sum is equal to a given value

Difficulty Level :   Easy Last Updated :   07 Jan, 2021 Given two linked lists(can be sorted or unsorted) of size  n1  and  n2  of distinct elements. Given a value  x . The problem is to count all pairs from both lists whose sum is equal to the given value  x . Note:  The pair has an element from each linked list. Examples:    Input : list1 = 3->1->5->7 list2 = 8->2->5->3 x = 10 Output : 2 The pairs are: (5, 5) and (7, 3) Input : list1 = 4->3->5->7->11->2->1 list2 = 2->3->4->5->6->8-12 x = 9 Output : 5 # Python3 implementation to count pairs from both linked # lists whose sum is equal to a given value # A Linked list node class Node: def __init__(self,data): self.data = data self.next = None # function to insert a node at the # beginning of the linked list def push(head_ref,new_data): new_node=Node(new_data) #new_node.data = new_data new_node.next = head_ref head_ref = new_node r

Trie | (Insert and Search)

Trie is an efficient information retrieval data structure. Use this data structure to store Strings and search strings. Your task is to use TRIE data structure and search the given string A. If found print 1 else 0. Example 1: Input: N = 8 key[] = {the,a,there,answer,any,by,   bye,their} search = the Output: 1 Explanation: the is present in the given string "the a there answer any by bye their" Example 2: Input: N = 8 key[] = {the,a,there,answer,any,by,   bye,their} search = geeks Output: 0 Explanation: geeks is not present in the given string "the a there answer any by bye their" Your Task: Complete  insert  and  search  function and return  true  if key is present in the formed trie else  false  in the search function. (In case of true, 1 is printed and false, 0 is printed by the driver's code. Expected Time Complexity:  O(M+|search|). Expected Auxiliary Space:  O(M). M = sum of the length of all strings which is present in the key[]  |search|

Generate Binary Numbers

Given a number   N . The task is to generate and print all   binary numbers with decimal values   from   1 to N . Example 1: Input: N = 2 Output: 1 10 Explanation: Binary numbers from 1 to 2 are 1 and 10. Example 2: Input: N = 5 Output: 1 10 11 100 101 Explanation: Binary numbers from 1 to 5 are 1 , 10 , 11 , 100 and 101.   Your Task: You only need to complete the  function  generate() that takes  N  as  parameter  and returns vector of strings denoting binary numbers. Expected Time Complexity :  O(N log 2 N) Expected Auxilliary Space :  O(N log 2 N)

Maximum money

Given a street of  N   houses (a row of houses), each house having   K  amount of money kept inside; now there is a thief who is going to steal this money but he has a constraint/rule that he cannot steal/rob two adjacent houses. Find the maximum money he can rob. Example 1: Input: N = 5 , K = 10 Output: 30 Explanation: The Robber can rob from the first, third and fifth houses which will result in 30. Example 2: Input: N = 2 , K = 12 Output: 12 Explanation: The Robber can only rob from the first or second which will result in 12. Your Task: You don't need to read input or print anything. Your task is to complete the function  maximizeMoney()  which takes 2 Integers N and K as input and returns the answer. Expected Time Complexity:  O(1) Expected Auxiliary Space:  O(1)

Top K Frequent Elements in Array - |

Top K Frequent Elements in Array - |  Given a non-empty array of integers, find the top k elements which have the highest frequency in the array. If two numbers have the same frequency then the larger number should be given preference.  Example 1: Input: nums = {1,1,1,2,2,3}, k = 2 Output: {1 , 2 } Example 2: Input: nums = {1,1,2,2,3,3,3,4}, k = 2 Output: {3, 2} Explanation: Elements 1 and 2 have the same frequency ie. 2. Therefore, in this case, the answer includes the element 2 as 2 > 1. User Task: The task is to complete the function  topK()  that takes the array and integer k as input and returns a list of top k frequent elements. Expected Time Complexity  : O(NlogN)

rapping Rain Water

Image
Given an array   arr[]  of   N   non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season.    Example 1: Input: N = 6 arr[] = {3,0,0,2,0,4} Output: 10 Explanation: Example 2: Input: N = 4 arr[] = {7,4,0,9} Output: 10 Explanation: Water trapped by above block of height 4 is 3 units and above block of height 0 is 7 units. So, the total unit of water trapped is 10 units. Example 3: Input: N = 3 arr[] = {6,9,9} Output: 0 Explanation: No water will be trapped. Your Task: You don'y need to read input or print anything. The task is to complete the function  trappingWater () which takes arr and N as input parameters and returns the total amount of water that can be trapped.

Matrix Interchange

Working with 2D arrays is quite important. Here we will do swapping of columns in a 2D array. You are given a   matrix arr[][] of r rows and c columns . You need to   swap the first column with the last column . Example: Input: r = 3 c = 4 arr[][] = {{1, 2, 3, 4},   {4, 3, 2, 1},   {6, 7, 8, 9}} Output: 4 2 3 1 1 3 2 4 9 7 8 6   Your Task: Since this is a function problem, you don't need to take any input. Just complete the provided function  interchange()  that take rows and columns number as parameter. In a new line, print the modified matrix.

Play with an array

Given an unsorted array   arr   of size   N , rearrange the elements of array such that number at the   odd   index is greater than the number at the   previous even   index. The task is to complete the function formatArray() which will return formatted array. NOTE: If the array formed is according to rules print  1 , else  0 . Example 1: Input: n = 5 arr[] = {5, 4, 3, 2, 1} Output: 1 Explanation: The given array after modification will be as such: 4 5 2 3 1. Example 2: Input: n = 4 arr[] = {4, 3, 2, 1} Output: รข€‹1 User task: Since this is a functional problem you don't have to worry about the input, you just have to complete the function  formatArray() , which accepts array arr and its size 

Total count

Given an array   Arr   of   N   positive integers and a number   K   where   K   is used as a threshold value to divide each element of the array into sum of different numbers. Find the sum of count of the numbers in which array elements are divided. Example 1: Input: N = 4, K = 3 Arr[] = {5, 8, 10, 13} Output: 14 Explanation: Each number can be expressed as sum of different numbers less than or equal to K as 5 (3 + 2), 8 (3 + 3 + 2), 10 (3 + 3 + 3 + 1), 13 (3 + 3 + 3 + 3 + 1). So, the sum of count of each element is (2+3+4+5)=14. Example 2: Input: N = 5, K = 4 Arr[] = {10, 2, 3, 4, 7} Output: 8 Explanation:  Each number can be expressed as sum of different numbers less than or equal to K as 10 (4 + 4 + 2), 2 (2), 3 (3), 4 (4) and 7 (4 + 3). So, the sum of count of each element is (3 + 1 + 1 + 1 + 2) = 8. Your Task: You don't need to read input or print anything. Your task is to complete the function  totalCount()  which takes the array of integers  arr  and  n  as parameter

Rearrange Array Alternately

Given a sorted array of positive integers. Your task is to rearrange  the array elements alternatively i.e first element should be max value, second should be min value, third should be second max, fourth should be second min and so on. Example 1: Input: N = 6 arr[] = {1,2,3,4,5,6} Output: 6 1 5 2 4 3 Explanation: Max element = 6, min = 1, second max = 5, second min = 2, and so on... Modified array is : 6 1 5 2 4 3. Example 2: Input: N = 11 arr[]={10,20,30,40,50,60,70,80,90,100,110} Output: 110 10 100 20 90 30 80 40 70 50 60 Explanation: Max element = 110, min = 10, second max = 100, second min = 20, and so on... Modified array is : 110 10 100 20 90 30 80 40 70 50 60. Your Task: The task is to complete the function  rearrange () which rearranges elements as explained above. Printing of the modified array will be handled by driver code. Expected Time Complexity:  O(N). Expected Auxiliary Space:  O(1).

mean of array or matrix

Numpy is a very powerful Python library for numerical data processing. It mostly takes in the data in form of arrays and applies various functions including statistical functions to get the result out of the array. In this article, we will see how to get the mean value of a given array. with mean The mean function can take in an array and give the mathematical mean value of all the elements in it. So we design a for loop to keep track of the length of the input and go through each array calculating its mean. Example  Live Demo import numpy as np # GIven Array Arrays_In = [ np . array ([ 11 , 5 , 41 ]),          np . array ([ 12 , 13 , 26 ]),          np . array ([ 56 , 20 , 51 ])] # Resultihg Array Arrays_res = [] # With np.mean() for x in range ( len ( Arrays_In )):     Arrays_res . append ( np . mean ( Arrays_In [ x ])) # Result print ( "The means of the arrays: \n" , Arrays_res ) Output Running the above code gives us the following result − The