Cocubes Coding Questions with Answers for Cocubes Coding Test

Cocubes Coding Questions with Answers for Cocubes Coding Test

If you have struggled a lot to solve coding questions in Cocubes and have not got rid of them yet, then you have landed at the right place. Here you can find a complete list of all coding questions asked in the Cocubes coding round and also solutions to those questions.



Cocubes Coding Questions with AnswersCocubes Coding Questions with Answers


1) Count the number of co-prime pairs in an array. (Any two numbers whose GCD is 1 are be called as co-prime)


Input:

The first line contains an integer T, total number of elements. Then follow T elements.


Output:

Count the number of co-prime pairs in an array.


Constraints:

1 ? T ? 25

1 ? elements ? 100


Sample Input and Output:


Input 1:

3

1 2 3


Output 1:

3

Here, Co-prime pairs are (1, 2), (2, 3), (1, 3)


Input 2:

4

4 8 3 9


Output 2:

4

Here, Co-prime pairs are (4, 3), (8, 3), (4, 9 ), (8, 9 )



#include<stdio.h>nint coprime(int a, int b)n{ nint gcd;nwhile ( a != 0 )n{ngcd = a; a = b%a; b = gcd;n}nif(gcd == 1)nreturn 1;nelsenreturn 0;n}nint count_pairs(int arr[], int n)n{nint count = 0;nfor (int i = 0; i < n - 1; i++)n{nfor (int j = i + 1; j < n; j++)n{nif (coprime(arr[i], arr[j]))n   count++;n       }     n}nreturn count;n}nnint main()n{nint n;nscanf("%d", &n);nint a[25], i;nfor(i=0; i<n; i++)nscanf("%d", &a[i]);nprintf("%d", count_pairs(a, n));nreturn 0;n}n


Get access to exclusive Cocubes Coding Questions by clicking the banner below

cocubes coding questions


2) Search for Nth Occurrence


Given an array, number to search (say e1), and occurrence (say n), print the index of the nth occurrence of e1 in the array. If e1 does not occur n times, then print the index as -1.


Input and Output:


Get the size of an array and get elements one by one. Input the number to be searched and occurrence. For example, 7 => Size of an array 1 4 6 7 6 3 6 => array elements 6 => number to be searched 3 => 3rd occurrence of number 6 Output: 6 Explanation: Number 6, 3rd occurrence position is 6


Sample Input and Output:


Input:

7

1 4 6 7 6 3 6

6

3


Output:

6


#include<stdio.h>nint main()n{nint a[100],n,i,e1,size,count=0;nscanf("%d",&size);nfor(i=0;i<size;i++)nscanf("%d",&a[i]);nscanf("%d",&e1);nscanf("%d",&n);nfor(i=0;i<size;i++)n{nif(e1==a[i])ncount++;n//If 'n'th occurrence found then print it's index and exit.nif(count==n)n{nprintf("%d",i);nreturn 0;n}n}n//If 'n' occurrence not found then print '-1'.nprintf("%d",-1);nreturn 0;n}n


3) Search for an element in an array:


Program to search for an element in the given array.


Input and Output:


The input consists of n + 2 lines. The first line consists a single integer n, The next n lines consist of 1 integer element part of the array. The last line consists of an integer to be searched. Output found or missing based on whether the element is present in the array or not. Note: max value of n is 100.


Sample Input and Output:


Input 1:

3

1 2 3

6


Output 1:Missing


Input 2:

3

1 2 3

2


Output 2:Found


#include<stdio.h>n#define MAX_SIZE 20nint main()n{nint n, i, j, min_index, array[MAX_SIZE], x;nscanf("%d", &n);nfor(i = 0; i < n; i++)nscanf("%d", &array[i]);nscanf("%d", &x);nfor(i = 0; i < n; i++)n{nif(x == array[i])n{nprintf("Foundn");nreturn 0;n}n}nprintf("Missingn"); nreturn 0;n}n


4) Second largest number


Input:

The first line contains an integer T, total number of elements. Then follow T integers.


Output:

Display the second largest among the given T integers.


Constraints:

1 ? T ? 1000

1 ? integers ? 1000000


Sample Input and Output:


Input:

7

23 45 7 34 25 25 89


Output:

45


#include<stdio.h>nint main()n{nint a[50], size, i, j = 0, big, sec_big;nscanf("%d", &size);nfor(i=0; i<size; i++)nscanf("%d", &a[i]);nbig=a[0];nfor(i=1;i<size;i++)n{nif(big<a[i])n{nbig=a[i];nj = i;n}n}nsec_big =a[size-j-1];nfor(i=1;i<size;i++)n{nif(sec_big <a[i] && j != i)nsec_big =a[i];n}nprintf("%d", sec_big);nreturn 0;n}n


Practice more such questions from Cocubes with FACE Prep PRO! Click the link below for more details

cocubes coding questions


5) Search index in a sorted array:


Program to find the target value in a two-dimensional matrix.


Input and Output:


Get a target element and return its coordinates. If the value didn’t exist, the program had to return (-1,-1).The first line of input is the sizeof row and column, followed rxc elements. The third line of input is the element to be searched in the rxc matrix.


Sample Input and Output:


Input 1:

4 2

0 9 8 7 6 5 4 3

3


Output 1:

(3, 1)


#include<stdio.h>nint main()n{nint i, j, count = 0;nint arr[10][10], search, r, c;nscanf("%d %d", &r, &c);nfor (i = 0; i < r; i++)n{nfor (j = 0; j < c; j++)nscanf("%d", &arr[i][j]);n}nscanf("%d", &search);nfor (i = 0; i < r; i++)n{  nfor (j = 0; j < c; j++)n{nif (arr[i][j] == search)n{nprintf("(%d , %d)n", i, j);ncount++;n}n}n}     nif (count == 0)nprintf("(-1,-1)");nreturn 0;n}n

Few more Cocubes coding questions with solutions in all languages





Cocubes Coding Questions with Answers