Whatz wrong with that answer?Plz help me
question is:
You are given an array A of N integers. You are to fulfill M queries. Each query has one of the following three types:
C d : Rotate the array A clockwise by d units.
A d : Rotate the array A anticlockwise by d units.
R d : Query for the value of the element, currently being the d-th in the array A.
Input
The first line contains two numbers - N and M respectively.
The next line contains N space separated Integers, denoting the array A.
Each of the following M lines contains a query in the one of the forms described above.
Output
For each query of type R output the answer on a separate line.
Constraints
1 ≤ N ≤ 100000
1 ≤ M ≤ 100000
1 ≤ d ≤ N, in all the queries
1 ≤ elements of A ≤ 1000000
The array A and the queries of the type R are 1-based.
Example
Input:
5 5
5 4 3 3 9
R 1
C 4
R 5
A 3
R 2
Output:
5
3
3
Solution:
#include<stdio.h>
//#include<iostream>
//using namespace std;
int a[100001];
int index=0,n;
void clock(int);
void ant_clock(int);
void display(int);
int main()
{
unsigned int i,m;
char x;
int y;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(m--)
{
scanf(" %c%d",&x,&y);
if(1 <= y <= n)
{
if(x=='R')
display(y);
else if(x=='A')
ant_clock(y);
else if(x=='C')
clock(y);
}
else
return 0;
}
return 0;
}
void display(int y)
{
int j=index,x=0;
while(1)
{
if(x==y-1)
break;
j++;
x++;
if(j==n)
j=0;
}
printf("%d",a[j]);
}
void ant_clock(int y)
{
int j;
for(j=0;j<y;j++)
{
index--;
if(index==-1)
index=n-1;
}
}
void clock(int y)
{
int j;
for(j=0;j<y;j++)
{
index++;
if(index==n)
index=0;
}
}
It seems to be a problem with the query indices being 1-based. And your code has unnecessary loops in it.
#include <stdio.h>
int arr[100000];
int main() {
int base = 0, size, i, m;
char x;
scanf("%d%d", &size, &m);
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
while (m-- > 0) {
scanf(" %c%d", &x, &i);
if (x == 'R') {
printf("%d\n", arr[(base + i - 1) % size]);
} else if (x == 'A') {
if ((base -= i) < 0)
base += size;
} else if (x == 'C') {
base = (base + i) % size;
}
}
return 0;
}
Related
so I've been struggling with this example for a good hour now and I can't even begin to process how should I do this.
Write a program that, for given n and m, forms a matrix as described.
The matrix should be m x m, and it's filled "spirally" with it's
beginning in the upper left corner. The first value in the matrix is
the number n. It's repeated until the "edge" of the matrix, at which
point the number increments. After the number 9 goes 0. 0 ≤ n ≤ 9, 0 ≤
m ≤ 9
Some time ago I had made a function to display the numbers 1 to n on an odd-sized grid.
The principle was to start from the center and to shift by ;
x = 1
x box on the right
x box on the bottom
x++
x box on the left
x box at the top
x++
With this simple algorithm, you can easily imagine to maybe start from the center of your problem and decrement your value, it seems easier to start from the center.
Here is the code that illustrates the above solution, to be adapted of course for your problem, it's only a lead.
#define WE 5
void clock(int grid[WE][WE])
{
int count;
int i;
int reach;
int flag;
int tab[2] = {WE / 2, WE / 2}; //x , y
count = 0;
flag = 0;
i = 0;
reach = 1;
grid[tab[1]][tab[0]] = count;
for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
{
if(flag % 2 == 0)
{
for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
grid[tab[1]][tab[0]] = count;
if(reach < WE - 1)
for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
grid[tab[1]][tab[0]] = count;
}
else
{
for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
grid[tab[1]][tab[0]] = count;
for(int top = 0; top < reach; top++, tab[1]--, count++)
grid[tab[1]][tab[0]] = count;
}
}
}
I finally solved it. If anybody's interested, here's how I did it:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
int j;
for(j=0;j<m;j++)
{
if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
}
}
//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
int i;
for(i=0;i<m;i++)
{
if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
}
}
int main()
{
int n, m, i, j, r=1, c=1, row=-1, col=-1;
scanf("%d %d",&n, &m);
int arr[m][m];
//Fill array with -1 everywhere
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
arr[i][j] = -1;
}
}
//Calculate which row/column to fill (variables row/col)
//Fill row then column then row than column...
for(i=0;i<2*m;i++)
{
if(i%2==0)
{
row = (r%2==0) ? m-r/2 : r/2;
fillRow(m, n, arr, row);
n++;
r++;
}
else if(i%2==1)
{
col = (c%2==0) ? c/2-1 : m-c/2-1;
fillCol(m, n, arr, col);
n++;
c++;
}
}
//If an element is larger than 9, decrease it by 10
//Prints the elements
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]>9) arr[i][j] -=10;
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
I've written a program to find common prime divisors, the greatest common divisor and the least common multiple. My method is finding the gcd first and then decompose the gcd into prime factors. This is my code:
#include <stdio.h>
#include <stdlib.h>
int prime(int x) {
int y;
for (y = 2; y < x; y++) {
if (x % y != 0)
continue;
else
return 1;
}
return 0;
}
int main() {
int n, m, i, gcd, lcm, k;
// Input m and n
printf("Enter m = ");
scanf("%d",&m);
printf("Enter n = ");
scanf("%d",&n);
// Common prime divisors
lcm = m * n;
while (m != n)
if (m > n) m = m - n;
else n = n - m;
gcd = m;
lcm = lcm / gcd;
if (m <= 1)
printf("No common prime divisors");
else {
printf("Common prime divisors: ");
for (i = 2; i <= m - 1; i++) {
k = prime(i);
if (k = 1)
if (m % i == 0)
printf("%d ", i);
}
}
printf("\nGreatest common divisor: %d\nLeast common multiple: %d", gcd, lcm);
return 0;
}
It seems that the program is working but when I type m = 2 and n = 4 the common prime divisors, which is supposed to be '2', doesn't appear.
So what's wrong in my code?
If m = 2, then, the below loop can't execute because 2-1 = 1, and i starts out at 2.
for (i = 2; i <= m - 1; i++)
Additionally, if (k=1) is an assignment. You want if (k==1).
You are given a tree (a simple connected graph with no cycles).
Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains an even number of nodes.
https://www.hackerrank.com/challenges/even-tree/problem
In the above link the test cases are given. For sampple input 1, I am getting 0 as output instead of expected value 2.
#include<stdio.h>
#include<stdlib.h>
int ans = 0;
int v, e;
int visited[201];
int gph[201][201];
int dfs(int i) {
int num_nodes;
int num_vertex = 0;
visited[i] = 1;
for (int j = 1; j <= v; j++) {
if (visited[i] == 0 && gph[i][j] == 1) {
num_nodes = dfs(j);
if (num_nodes % 2 == 0)
ans++;
else
num_vertex += num_nodes;
}
}
return num_vertex + 1;
}
int main() {
scanf("%d %d", &v, &e); // vertices and edges
int u, v;
for (int i = 0; i < e; i++) {
scanf("%d %d", &u, &v); //edges of undirected graph
gph[u][v] = 1;
gph[v][u] = 1;
}
dfs(1);
printf("%d", ans);
}
Test case:
10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8
Expected output: 2
Actual output: 0
// ans is total number of edges removed and al is adjacency list of the tree.
int dfs(int node)
{
visit[node]=true;
int num_vertex=0;
for(int i=0;i<al[node].size();i++)
{
if(!visit[al[node][i]])
{
int num_nodes=dfs(al[node][i]);
if(num_nodes%2==0)
ans++;
else
num_vertex+=num_nodes;
}
}
return num_vertex+1;
}
There is a typo. The condition expression in if clause
if (visited[i] == 0 && gph[i][j] == 1)
should be
if (visited[j] == 0 && gph[i][j] == 1)
BTW, the constraints in hackrank question is 2 <= n <= 100, so you definitely don't need a fixed array of size 201.
Intro to problem
You are given a tree. If we select 2 distinct nodes uniformly at random, what's the probability that the distance between these 2 nodes is a prime number?
Input
The first line contains a number N: the number of nodes in this tree.
The following N-1 lines contain pairs a[i] and b[i], which means there is an edge with length 1 between a[i] and b[i].
Output
Output a real number denote the probability we want.
You'll get accept if the difference between your answer and standard answer is no more than 10^-6.
#include<stdio.h>
#include<math.h>
int checkprime(int d);
int fact(int m);
void main()
{
int N, i, j, f, p = 0, t, d, x, y, z;
float result;
int a[49999], b[49999];
printf("Enter the number of nodes\n");
scanf("%d", &N);
printf("\n");
for (i = 0; i < N - 1; i++)
{
scanf("%d\t%d", &a[i], &b[i]);//Inputting the nodes
}
for (i = 0; i < N - 1; i++)
{
for (j = i; j < N - 1; j++)
{
d = b[j] - a[i];//Taking distance between nodes
f = checkprime(d);//Checking if it is prime
if (f == 1)
{
p = p + 1;//If found prime,then increasing the number of
//possibilities
}
}
}
x = fact(N);
y = fact(2);
z = fact(N - 2);
y = y * z;
t = x / y;//finding C(N,2).Combination of number of nodes and pair of 2
result = p / t;//finding probability
printf("\n\n%f", result);
}
int checkprime(int d)//function to check prime
{
int k, flag = 1;
for (k = 2; k < d / 2; k++)
{
if (d % k == 0)
{
flag = 0;
}
}
return flag;
}
int fact(int m)//function to calculate factorial
{
int k, r = 1;
for (k = m; k > 1; k--)
{
r = r * k;
}
return r;
}
I want do a program of factorization like this: 72 = 3 * 2 ^ 3 ^ 2
in C language, how can i do the program ?
I tried to do this but I can not do it :
#include <stdio.h>
int main(){
int n;
int j;
printf("Insert a positive integer number greater than 1\n")
scanf("%d", &n);
j = 2;
do
{
if( n % j == 0)
{
printf("%d\n", j);
n = n / j;
}
else{
j++;
}
}
while ( n > 1);
}
Assuming that given 72, you want to output 2^3 X 3^2 this code should do it:
/* Decides when to print multiplication sign */
const char *get_mult_string()
{
static int first_divisor=1;
if (first_divisor==1) {
first_divisor=0;
return "";
} else {
return " X ";
}
}
void factorize() {
int n;
int j;
printf("Insert a positive integer number greater than 1: ");
scanf("%d", &n);
j = 2;
int power_count=0;
do {
if (n % j == 0) {
power_count++;
n = n / j;
}
else {
if (power_count>0) {
printf("%s(%d^%d)", get_mult_string(), j, power_count);
power_count=0;
}
j++;
}
}
while (n > 1);
if (power_count>0) {
printf("%s(%d^%d)\n", get_mult_string(), j, power_count);
}
}
Try creating an array, where each slot represents a prime number, and initialize them all to 0. For example, array[0] means 2, array[1] 3, array[2] 5, array[3] 7, array[4] 11.
Rather than printing out the prime number when you reach it, as you do in your code, increment that slot in the array. In the end, you can call something like printf("2^%d * 3^%d * 5^%d * 7^%d * 11^%d",array[0],array[1],array[2],array[3],array[4]).
Note: switch statements would probably be your friend here.