# Cocubes Coding Questions with Answers for Cocubes Coding Test

Cocubes coding questions with answers are given here. These are the most repeated coding questions asked in Cocubes coding test.

In Cocubes coding round, you can only code in these languages – C, C++, C#, Java

Before you begin to practice the below questions, you can also check these

## Cocubes 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:
3
1 2 3
Output:
3
Here, Co-prime pairs are (1, 2), (2, 3), (1, 3)

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

`#include<stdio.h>int coprime(int a, int b){   int gcd;while ( a != 0 ){gcd = a; a = b%a;  b = gcd;}if(gcd == 1)return 1;elsereturn 0;}int count_pairs(int arr[], int n){ int count = 0; for (int i = 0; i < n - 1; i++) {for (int j = i + 1; j < n; j++){if (coprime(arr[i], arr[j]))       count++;              }            }return count;}int main(){int n;scanf("%d", &n);int a, i;for(i=0; i<n; i++)scanf("%d", &a[i]);printf("%d", count_pairs(a, n));return 0;}`

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>int main(){int a,n,i,e1,size,count=0;scanf("%d",&size);for(i=0;i<size;i++)scanf("%d",&a[i]);scanf("%d",&e1);scanf("%d",&n);for(i=0;i<size;i++){if(e1==a[i])count++;//If 'n'th occurrence found then print it's index and exit.if(count==n){printf("%d",i);return 0;}}//If 'n' occurrence not found then print '-1'.printf("%d",-1);return 0;}`

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 1:
3
1  2  3
2
Output 2: Found

`#include<stdio.h>#define MAX_SIZE 20int main(){int n, i, j, min_index, array[MAX_SIZE], x;scanf("%d", &n);for(i = 0; i < n; i++)scanf("%d", &array[i]);scanf("%d", &x);for(i = 0; i < n; i++){if(x == array[i]){printf("Found\n");return 0;}}printf("Missing\n");   return 0;}`

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>int main(){int a, size, i, j = 0, big, sec_big;scanf("%d", &size);for(i=0; i<size; i++)scanf("%d", &a[i]);big=a;for(i=1;i<size;i++){if(big<a[i]){big=a[i];j = i;}}sec_big =a[size-j-1];for(i=1;i<size;i++){if(sec_big <a[i] && j != i)sec_big =a[i];}printf("%d", sec_big);return 0;}`

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 size of 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>int main() {int  i, j, count = 0;int arr, search, r, c;scanf("%d  %d", &r, &c);for (i = 0; i < r; i++){for (j = 0; j < c; j++)scanf("%d", &arr[i][j]);}scanf("%d", &search);for (i = 0; i < r; i++){      for (j = 0; j < c; j++){if (arr[i][j] == search){printf("(%d , %d)\n", i, j);count++;}}}            if (count == 0)printf("(-1,-1)");return 0;}`