Suppose we have given function like f(x) = (x^6 + x^2 + 9894845) % 971, now for a given value of x, we have to find the value of f(x).So, if the input is like 5, then the output will be 469To solve this, we will follow these steps −Define a function power_mod(), this will take base, exponent, modulus, base := base mod modulusresult := 1while exponent > 0, do −if exponent is odd, then −result := (result * base) mod modulusbase := (base * base) mod modulusexponent = exponent /2return resultFrom the main method do the following −return power_mod(n, ... Read More
Suppose we have a board of length p and width q; we have to break this board into p*q number of squares such that cost of breaking is as minimum as possible. Cutting cost for each edge will be given.So, if the input is like X_slice = [3, 2, 4, 2, 5], Y_slice = [5, 2, 3]then the output will be 65To solve this, we will follow these steps −res := 0horizontal := 1, vertical := 1i := 0, j := 0while i < m and j < n, doif X_slice[i] > Y_slice[j], thenres := res + X_slice[i] * verticalhorizontal ... Read More
Suppose we have a string s that represents a time in the 24 hours format as HH:MM so that HH will be in range 0 to 23 and MM will be in range 0 to 59, We have to find the next closest time which is a palindrome when read as a string. If there is no such string, then return -1.So, if the input is like "22:22", then the output will be "23:32".To solve this, we will follow these steps −n := size of shour_string := substring of s[from index 0 to 2]minute := substring of s[from index 3 ... Read More
Suppose we have a 2D array. Where each cell of which consists number cost which represents a cost to visit through that cell, we have to find a path from top-left cell to bottom-right cell by which total cost consumed is minimum.So, if the input is like32101661319111448158710111141751234891254221141100331124221then the output will be 340 as (32 + 11 + 14 + 48 + 66 + 13 + 19 + 7 + 34 + 12 + 21 + 42 + 21) = 340To solve this, we will follow these steps −Define cell with x, y coordinate and distance parameter.Define an array matrix of ... Read More
Suppose we have a N*M matrix A, this is the representation of 3D figure. The height of the building at point (i, j) is A[i][j]. We have to find the surface area of the figure.So, if the input is like N = 3, M = 3, A = [[1, 4, 5], [3, 3, 4], [1, 3, 5]], then the output will be 72.To solve this, we will follow these steps −res := 0for i in range 0 to N, dofor j in range 0 to M, doup_side := 0left_side := 0if i > 0, thenup_side := array[i - 1, j]if ... Read More
Suppose we have an array of integers A; we have to find all the values for sum so that for a value sum[i] the array can be divided into sub-arrays of sum sum[i]. If we cannot divide the array into sub-arrays of equal sum then return -1.So, if the input is like A = [2, 4, 2, 2, 2, 4, 2, 6], then the output will be [6, 8, 12] as the array can be divided into sub-arrays of sum 6, 8 and 12. These are as follows: [{2, 4}, {2, 2, 2}, {4, 2}, {6}] [{2, 4, 2}, {2, ... Read More
Suppose we have three sorted arrays A, B, and C (these can be of different sizes), we have to find compute the minimum absolute difference between the maximum and minimum number of any triplet (A[i], B[j], C[k]) such that they are under arrays A, B and C respectively, So, if the input is like A : [ 2, 5, 6, 9, 11 ], B : [ 7, 10, 16 ], C : [ 3, 4, 7, 7 ] , then the output will be 1 as by selecting A[i] = 6 B[j] = 7 and C[k] = 7, we will ... Read More
Suppose we have an array A of n values (elements may not be distinct). We have to find the sum of maximum difference possible from all subsets of given array. Now consider max(s) denotes the maximum value in any subset, and min(s) denotes the minimum value in the set. We have to find the sum of max(s)-min(s) for all possible subsets.So, if the input is like A = [1, 3, 4], then the output will be 9.To solve this, we will follow these steps −n := size of Asort the list Asum_min := 0, sum_max := 0for i in range ... Read More
Suppose we have a given integer N; we have to find the sum of all Truncatable primes less than N. As we know the truncatable prime is a number which is left-truncatable prime (if the leading "left" digit is successively removed, then all resulting numbers are treated as prime) as well as right-truncatable prime (if the last "right" digit is successively removed, then all the resulting numbers are treated as prime). An example of truncatable prime is 9137 as this is lefttruncatable prime because 9137, 137, 37 and 7 are primes. Hence 9137 is a truncatable prime.So, if the input ... Read More
ConceptWith respect of given three sorted arrays A, B, and C of not necessarily same sizes, compute the lowest i.e. minimum absolute difference between the maximum and minimum number of any triplet A[i], B[j], C[k] such that they are under arrays A, B and C respectively, i.e., minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])).Input−A : [ 2, 5, 6, 9, 11 ] B : [ 7, 10, 16 ] C : [ 3, 4, 7, 7 ]Output−1ExplanationWhen we select A[i] = 6 , B[j] = 7, C[k] = 7, we get the minimum difference as max(A[i], B[j], C[k]) - ... Read More