File
File
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.
Note:
The re-arrangement is to be done in-place i.e you cannot use another array.
Assumption:
You may assume that the array is of even length.
Example:
Input:
2 4 6 8 20 15 10 5
Output:
5 10 15 20 8 6 4 2
int* ReverseArray(int* arr, int length)
{
int t,i;
for(i=0;i<length/2;i++)
{
t=arr[i];
arr[i]=arr[length-i-1];
arr[length-i-1]=t;
}
return arr;
}
**************************************************
You are given a function,
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.
Example:
Input:
5
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
Output:
3024
Explanation:
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.
int FindMaxProduct(int** arr, int n)
{
int i,max=0,prod,j,
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((j-3)>=0) /* left and right */
{
prod=arr[i][j]*arr[i][j-1]*arr[i][j-
2]*arr[i][j-3];
if(max<prod)
max=prod;
}
if((i-3)>=0) /* up and down */
{
prod=arr[i][j]*arr[i-1][j]*arr[i-2]
[j]*arr[i-3][j];
if(max<prod)
max=prod;
}
if((i-3)>=0 && (j-3) >=0) /* Diagonal and anti-
diagonal */
{
prod=arr[i][j]*arr[i-1][j-1]*arr[i-2][j
-2]*arr[i-3][j-3];
if(max<prod)
max=prod;
}
}
}
return max;
}
*********************************
You are given a function,
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.
Note:
1. Indices start from 0.
2. If no equi-spaced �1�s are found, then print �-1�
Example:
Input:
011001001000001
Output:
2
5
8
Explanation:
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)
{
Int I,first1=0,second1=0,third1=0,count=0,found=0;
for(i=0;str[i]!=��;i++)
{
If(str[i]==�1�)
{
First1=Second1;
Second1=third1;
Third1=I;
Count++;
}
If(count>=3 && (second1-first1)==(third1 � second1 ))
{
Found=1;
Break;
}
}
If(found==1)
Printf(� %d %d %d�,first1,second1,third1);
Else
Printf(�-1�);
}
***********************
The function takes three integers �x�, �y�, �z� as input. Implement the function to
return the least common multiple of the three numbers.
Assumption:
You may assume that LCM of negative numbers is the same as that of their positive
equivalents.
Example:
Input:
2
3
4
Output:
12
Explanation:
The smallest number divisible by 2, 3 and 4 is 12, hence it it the output.
int FindLeastCommonMultiple(int x, int y, int z)
{
Int I;
I=x;
While(1)
{
If(i%x==0 && i%y==0 && I %z==0)
{
Return I;
}
I=i+x;
}
}
***********************
You are given a function,
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.
Example:
Input:
127
Output:
87
Explanation:
While converting 127 to its decimal equivalent, we start from the right,
multiplying
7 * 80 = 7
2 * 81 = 16
1 * 82 = 64
}
Share this:
Like this:
Related
Cocubes coding questions
Please write coding for the following two problems and submit code through
comments. Thanks in advance1. You are given a function,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�
September 3, 2015
In "cocubes"
Find sum leaving our row and col: cocubes coding question
In "cocubes"
AMCAT COMPUTER PROGRAMMMING PREVIOUS QUESTIONS (PAPERS)-2
Question 1 Which of the following Sorting Algorithm will perform the worst if the
numbers are ordered in the opposite form? A. Quick Sort B. Radix C. Bubble D.
Selection Correct Op: A Quick sort performs the worst if arranged in alphabetic/
ascending order Question 2 Binary Search can have�
In "amcat"
Posted in: cocubes
Post navigation
? Cocubes coding questions
Techmahindra selection pattern ( exam pattern) 2016 ?