Sie sind auf Seite 1von 4

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

Fresher [0Yr - 1Yr Exp]

00:11:42

to test end

Find the equations

Right click and copy/paste are


not available in this region.
b=
Find
all
the
equation(a
c) in an array of positive numbers.
1
2

Notes:
b>1
Implement your own power function.
You should not use additional array or any other data structures
other than the input array. However, you are allowed to modify the
input array.
The numbers a & b should be adjacent in the array whereas c can be
present anywhere.
Once a number occurred as part of an equation, that number should
not be part of any other equation in the array.

Input Format:
The first line is input for N.
N number of input lines follow. Each of the line contains an integer input that
belongs to the array of N.

Output Format:
Print all the numbers satisfying the equationin each line.

Sample Input
12
7
2
3
4
0
8
1
5
1
9
6
8

https://www.hackerrank.com/tests/80bdrlfojm4/questions/e8q2ildjddj

1/4

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

Sample Output
2
3
8
1
5
1

Explanation
In the above sample,
3 occurs immediately after 2 and 23 = 8 which occurs later in the array.
5 occurs immediately after 1 and 15 = 1 which occurs later in the array.
Since 8 is occurred already as part of the equation23= 8, 81 = 8 should not be
printed.

YOUR ANSWER
Draft saved 08:17 pm

1#include <math.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <assert.h>
6 #include <limits.h>
7 #include <stdbool.h>
8
9/*
10 * Complete the function below.
11 *
12 * Ensure you also pass the size of the integer array
in the variable
13 * result_size, which will be used while iterating
your return array.
14 * See the code inside the main function to understand
how this works.
15 */
16int pow(int a,int b){
17 int i=0,res;
18 while(i<b){
19 res=a*a;
20 i++
https://www.hackerrank.com/tests/80bdrlfojm4/questions/e8q2ildjddj

2/4

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

21 }
22 return res;
23 }
24void swap(int *a,int *b){
25 int temp;
26 temp=*a;
27 *a=*b;
28 *b=temp;
29 }
30int* findEquations(int arr_size, int* arr, int*
result_size) {
31 int i,res;
32 for(i=0;i<arr_size-1;i++){
33 res=pow(arr[i],arr[i+1]);
34 for(j=0;j<arr_size;j++){
35 if(arr[j]==res){
36 swap()
37 }
38 }
39 }
40
41 }
42

https://www.hackerrank.com/tests/80bdrlfojm4/questions/e8q2ildjddj

3/4

11/1/2015

Fresher [0Yr - 1Yr Exp] :: powered by HackerRank

43int main() {
44 FILE *f = fopen(getenv("OUTPUT_PATH"), "w");
45 int res_size;
46 int* res;
47
48 int _arr_size = 0;
49 int _arr_i;
50 scanf("%d\n", &_arr_size);
51 int _arr[_arr_size];
52 for(_arr_i = 0; _arr_i < _arr_size; _arr_i++) {
53 int _arr_item;
54 scanf("%d", &_arr_item);
55
56 _arr[_arr_i] = _arr_item;
57 }
58
59 res = findEquations(_arr_size, _arr, &res_size);
60 int res_i;
61 for(res_i=0; res_i < res_size; res_i++) {
62
63 fprintf(f, "%d\n", res[res_i]);
64
65 }
66
67
68 fclose(f);
69 return 0;
70 }
71
Line: 36 Col: 22

Test against custom input

Download sample testcases

Run Code

Submit code & Continue

The input/output files have Unix line

endings. Do not use Notepad to edit them on windows.

About

Privacy policy

https://www.hackerrank.com/tests/80bdrlfojm4/questions/e8q2ildjddj

Terms of service

4/4

Das könnte Ihnen auch gefallen