You are given a function,
int* ReverseArray(int* arr, int length);
The function takes an integer array and its length as input. Implement the function to return the array such that the array is reversed i.e. the first element of the array occupies the last position, second element occupies the second last position and so on.
The re-arrangement is to be done in-place i.e you cannot use another array.
You may assume that the array is of even length.
2 4 6 8 20 15 10 5
5 10 15 20 8 6 4 2
int FindMaxProduct(int** arr, int n);
The function takes a two-dimensional array having equal number of rows and columns (i.e. a square matrix) and its dimension, ‘n’, as input. Implement the function such that it returns the maximum product that can be formed from four adjacent numbers. Numbers can be adjacent to each other in either of the given directions: up, down, left, right, diagonal or anti-diagonal. Ensure that four adjacent numbers are chosen in such a way that the direction does not change while choosing the numbers. Assume ‘n’ >= 4.
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 0
9 6 4 2 3
Here, the numbers 6, 7, 8, 9 in the second row, which are horizontally adjacent form the product 3024 which is the maximum product of any four adjacent numbers in the given array.
void FindOnesInBinaryString(char* str);
The function takes a binary string i.e. a string comprising of ‘0’s and ‘1’s as input. Implement the function such that it prints the indices of the first occurrences of three equi-spaced ones, to the standard output (STDOUT). The algorithm to find out the indices of the evenly spaced ones is given as follows:
Find the distance between the first and second ‘1’s and the second and third ‘1’s. If these distances are equal, then their indices are the answer. Else repeat the process for the entire array until three such ‘1’s are found.
1. Indices start from 0.
2. If no equi-spaced ‘1’s are found, then print ‘-1’
The distance between the ‘1’s at positions 2, 5 and 8 is 2, hence these are the indices of first occurrence of three equi-spaced 1’s.
void FindOnesInBinaryString(char* str)
If(count>=3 && (second1-first1)==(third1 – second1 ))
Printf(“ %d %d %d”,first1,second1,third1);
Least Common Multiple (LCM) of three integers x, y, z is the smallest positive integer that is divisible by all three numbers x, y and z.
You are given a function,
int FindLeastCommonMultiple(int x, int y, int z);
The function takes three integers ‘x’, ‘y’, ‘z’ as input. Implement the function to return the least common multiple of the three numbers.
You may assume that LCM of negative numbers is the same as that of their positive equivalents.
The smallest number divisible by 2, 3 and 4 is 12, hence it it the output.
int FindLeastCommonMultiple(int x, int y, int z)
If(i%x==0 && i%y==0 && I %z==0)
int OctalToDecimal(int n);
The function takes an integer number, each of whose digits lies between 0 to 7, thus forming an octal number, as input. Implement the function to return its decimal equivalent. The algorithm to convert the octal number to its decimal equivalent is as follows:-
Multiply each digit of the octal number starting with the right most digit and moving leftwards, with increasing powers of 8 starting with 80.
While converting 127 to its decimal equivalent, we start from the right, multiplying
7 * 80 = 7
2 * 81 = 16
1 * 82 = 64
Adding up the results together, we get 7 + 16 + 64 = 87, which is the output.
int OctalToDecimal(int n)