Union and Intersection of two sorted arrays

Problem Find Union and Intersection of two sorted arrays Example For example, if the input arrays are: arr1[] = {1, 3, 4, 5, 7} arr2[] = {2, 3, 5, 6} Then your program should print Union as {1, 2, 3, 4, 5, 6, 7} and Intersection as {3, 5}. Solution Algorithm Union(arr1[], arr2[]): For union of two arrays, follow the following merge procedure. Use two index variables i and j, initial values i = 0, j = 0 If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i. [Read More]

How many time function gets called?

Problem Consider the function below: void foo1() { if(A < B) Then { } else if(C < D) then foo2() } How many time foo2 () would get called given A < B 25% of the times and C < D 75% of the times and foo1 () is called 5000 times Solution This is the question of basic probability. As foo2() occurs in 2nd branch of if else, the probability of it being called is . [Read More]

Create the REV function from Swap

We have a function REV(“string”,m,n).This function is capable of reversing the caharacters in the string from mth location to nth location. e.g. REV(“abcd”,2,3); the output will be acbd We need to swap a string from a position,e.g. SWAP(“abcdefg”,4)  output needs to be efgabcd.
How can the REV function used do this.

Solution
 ANS : L = string length,N= position given in SWAP function.
SWAP(“abcdefg”,4) = REV(REV(REV(“abcdefg”,N+1,L),1,N),1,L).

Is there multiply function in 8085 microprocessor

There is no multiplication instruction in the 8085 - I believe that was added in the 8086 & 8088 version of the series. This has the entire instruction set for the 8085: These operations are equivilent to multiplying by 2: Shift left by 1 (shifting left by n is multiplying by 2^n) Add the value to itself once. Other than that you can write a loop to add the number to itself ‘n’ times. [Read More]

Set all bits of a number in a range equal to another number

Problem You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e.g., M becomes a substring of N located at i and starting at j). **Example ** Consider the integer N, where we want to fit in M between bits i and j of N. Input: N = 10000000000, M = 10101, i = 2, j = 6 Output: N = 10001010100 [Read More]

Median of 2 sorted arrays of equal size n

Question  There are 2 sorted arrays A and B. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)) Solution Before going to solution what is the median. What is Median? Median: In probability theory and statistics, a median is described as the number separating the higher half of a sample, a population, or a probability distribution, from the lower half. [Read More]

WAP to check whether string is palindrome

Problem Write a method which will accept a string and return true if the string is a palindrome and false if it isn’t. Follow up: a) your method should consider lower case and upper case characters to be the same. b) your method should ignore special characters and white spaces, for e.g. if your input were the strings were “Madam, I’m Adam!!”, then you should consider it a palindrome and hence return true ignoring case and special characters. [Read More]