Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Here is the Question.
Find the largest GCD of the input variable with the values of an array.
Inputs are as follows:-
First-line contains two integers, N and Q.
Second-line contains N integers which form the arr[].
Next, Q lines contain an integer M, the time in seconds she wishes to go back.
#include <stdio.h>
int main(){
int n,q;
scanf("%d %d", &n,&q);
int a[n];
int gcdn[n];
for (int i = 0;i < n; i++) {
scanf("%d",&a[i]); }
while (q>0){
q--;
int x;
scanf("%d",&x);
for(int i=0;i<n;i++) {
gcdn[i] = gcd(a[i],x);
}
printf("%d \n",max(gcdn,n));
}
}
int gcd (int a, int b) {
if (b==0)
return a;
else
return gcd(b, a%b);
}
int max(int *a,int n) {
int max= a[0];
for(int i = 1;i<n;i++) {
if(a[i]>min) {
max= a[i];
}
}
return max;
}
How can I make this code better in terms of competitive programming? And what other languages can make this code better and more efficient?
The most obvious performance improvement is that your array is not sorted: instead of simply adding an element to the array, you'll need to do a search first (which is O(\log(n))), but once you need to get the maximum of an array, you just need the first (or the last) entry.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can you guys please help me with this code. I know this is basic but I am now really obsessed with it. I am trying to implement a very simple version of mergesort, but the output is all wrong. Please try to fix this code instead of writing a completely new one.
#include <stdio.h>
void merge_them(int a[], int l, int m, int r)
{
int i=l, j=m+1;
int final[r-l+1];
int p=0;
for(int k=0; k<=r-l+1; k++)
{
if(a[i]<a[j])
{
final[p]=a[i];
i++;
p++;
}
else if(a[i]>=a[j])
{
final[p]=a[j];
j++;
p++;
}
}
j=0;
for( i=l; i<r+1; i++)
{
a[i]=final[j];
j++;
}
}
void merge(int a[], int l, int r)
{
if(l<r)
{
int m = (l+r)/2;
merge(a,l,m);
merge(a,m+1,r);
merge_them(a,l,m,r);
}
}
int main()
{
int a[10] = {10,9,8,7,6,5,4,3,2,1};
merge(a,0,9);
for(int i=0; i<10; i++)
printf("%d ",a[i]);
}
UPDATE: This was not a homework. I was just revisiting some sorting algos based on recursion and tried to implement Merge sort. Anyway, I switched to while loop and used a more precise condition check, which led to correct sorted outputs.
There are actually three bugs:
The condition k<=r-l+1 in the for statement is wrong and it will have it do one more loop. It should be k<r-l+1 (< should be used instead of <=) like the condition i<r+1 used later.
The condition a[i]<a[j] is wrong because it isn't checking if the parts are used up. It should be j > r || (i <= m && a[i]<a[j]).
if(a[i]>=a[j]) should be removed because if you don't take an element from one part, you will obviously take from another part.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this C program code here, which determines the highest contiguous value for an array:
#include <stdio.h>
int inputs[8];
int calcSum(int i, int j);
int main(void) {
int i, j, maxSum = 0, tempSum = 0;
int length = sizeof(inputs)/sizeof(inputs[0]);
for(i=0;i<length;i++) {
scanf("%d", &inputs[i]);
}
for(i=0;i<length;i++) {
for(j=i;j<length;j++) {
tempSum = calcSum(i,j);
if(tempSum > maxSum) {
maxSum = tempSum;
}
}
}
printf("%d\n", maxSum);
return 0;
}
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Even though this code looks correct to me, it outputs a wrong result. Sampling adding any sort of printf("") (can be empty as well) between tempSum = ... and if(tempSum >...) will make the code output the correct answer for all test cases. I even rewrote the entire code from scratch and still get the same issue.
For example, the number series: 5 2 -1 -2 -4 3 5 -6 should output 8, which it does once the printf("") is added, otherwise it outputs 38...and I have no idea why. Can you please explain, where I went wrong?
In this code:
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
You need to make sure you initialize sum to a starting value:
int calcSum(int i, int j) {
int c, sum;
sum=0;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following code in c for counting frequency of number from array:
#define MAX 10
int flag=0;
void display(int no,int cnt,int visi[]);//function declaration
int main()
{
int arr[]={1,1,1,2,3,4,2,2,3,1};//asume any array or we can enter from user
int visited[MAX];
int i,j,no,cnt=1;
clrscr();
for(i=0;i<10;i++)//loop
{
no=arr[i];
cnt=1;
for(j=i+1;j<10;j++)
{
if(no==arr[j])
cnt++;
}
display(no,cnt,visited);
}
return 0;
}
void display(int no,int cnt,int visited[])
{
int static i;
int j;
if(flag==1)
for(j=0;j<=i;j++)
{
if(visited[j]==no)
goto a;
}
i++;
flag=1;
printf("\n%d=%d",no,cnt);
visited[i]=no;
a:
}
Please help to improve my code or suggest any other technology for effectiveness
is this algorithm effective and efficient or not please give feedback.
You can sort the array first by merge sort (O(n log n)) and then calculate the frequency of a number by single loop like this-:
int j=0;
for( i = 0; i < 9; i++ )
{
if (arr[i] == arr[i+1])
cnt++;
else
{
visited[j] = cnt;
cnt = 0;
j++;
}
}
to count frequency of numbers in array, try this code
#include <stdio.h>
#define MAX 10
int countArray[MAX];
int main()
{
int arr[]={1,1,1,2,3,4,2,2,3,1},i;
for(i=0;i<MAX;i++)
countArray[i]=0;
for(i=0;i<MAX;i++)
countArray[arr[i]]++;
for(i=0;i<MAX;i++)
{
if(countArray[i])
printf("%d %d\n",i,countArray[i]);
}
return 0;
}
You don't say this explicitly, but it looks as if you had an array of non-negative numbers whsoe values is smaller than MAX.
If the range of the numbers is known, you can create an array of counts. Visit each element of the array once and increment the count for that element. Then pass through the array of counts and output it as appropriate.
This method works well if the range of valid numbers and therefore the size of the count array is small. (You have the same problem for your visited array, whose size is the same as the size of an array of counts.)
The example below implements counting with an array of counts. The code also takes care of values that fall outside the valid range of MIN to MAX inclusively.
#include <stdio.h>
#define MIN 1
#define MAX 10
int main()
{
int arr[] = {1, 1, 1, 2, 3, 4, 2, 2, 3};
int narr = sizeof(arr) / sizeof(arr[0]);
int count[MAX + 1 - MIN] = {0};
int uncounted = 0;
int i;
for(i = 0; i < narr; i++) {
if (arr[i] < MIN || arr[i] > MAX) {
uncounted++;
} else {
count[arr[i] - MIN]++;
}
}
for(i = MIN; i < MAX + 1; i++) {
if (count[i - MIN]) {
printf("element %d ocurs %d times.\n", i, count[i - MIN]);
}
}
if (uncounted) {
printf("%d elements were not accounted for.\n", uncounted);
}
return 0;
}
int main()
Is implementation dependant. You do not wan't to use this, some compilers will not accept it.
Use
int main(void)
instead
for(i=0;i<10;i++)//loop
You already defined MAX, so why not use it there. Stuff like this makes code harder to maintain.
The efficiency is dependant on the input values you have. For small arrays with small numbers, this works fine otherwise.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to solve the PRIME1 problem on Spoj. I implemented this code using segmented sieve of Eratosthenes
#include <stdio.h>
long int primes[100000];
int main(void) {
long int t, m, n, i, j, p;
scanf("%ld",&t);
while(t--) {
scanf("%ld",&m);
scanf("%ld",&n);
for(i=2; i*i<=n; i++) {
p=m/i;
p=p*i;
for(j=p; j<=n; j+=i) {
if(j!=i)
primes[j-m] = 1;
}
}
for(i=0; i<(n-m+1); i++) {
if(primes[i] == 0)
printf("%ld ",i+m);
else
continue;
}
printf("\n");
}
return 0;
}
Its works fine when value of n-m is not very high. But if the value of n-m gets very high (i.e. near about 100000), it gives Segmentation fault. Why there is such kind of behaviour?
Also, when I declared the primes array inside main, the code works correctly.
#include <stdio.h>
int main(void) {
long int t, m, n, i, j, p;
scanf("%ld",&t);
while(t--) {
scanf("%ld",&m);
scanf("%ld",&n);
if(m==1) m=2;
long int primes[n-m+1];
for(i=0;i<n-m+1;i++)
primes[i]=0;
for(i=2; i*i<=n; i++) {
p=m/i;
p=p*i;
for(j=p; j<=n; j+=i) {
if(j!=i)
primes[j-m] = 1;
}
}
for(i=0; i<(n-m+1); i++) {
if(primes[i] == 0)
printf("%ld ",i+m);
else
continue;
}
printf("\n");
}
return 0;
}
Why the program gives Segmentation fault when I declare the primes array globally?
In the first you are declaring primes to have a fixed size of 100000 so if you ever try to access an element outside that range you'll be accessing memory you should not, thus a seg fault.
In the second example you declare it to be size n-m+1 which will make sure it's always big enough to do what you want.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have to write a C code that finds the smallest value in an array using recursion. I've already done it using the for loop, but recursion is trickier . Can someone help me??
The minimum of a single item array is that single item (base case or the termination condition).
The min of an array is the minimum of [the first item, the minimum from the rest (excluding the first item)]
Here is simple code for finding minimum value using recursion,
int rec(int a[],int n)
{
int min;
if(n==1)
return a[0];
else {
min=rec(a,n-1);
if(min<a[n-1])
return min;
else
return a[n-1];
}
}
void main()
{
int i,j,n,a[20];
printf("enter n :");
scanf("%d",&n);
printf("enter values : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n%d",rec(a,n));
getch();
}
#include <stdio.h>
int minimum(int *a_s, int *a_e, int candidate){
if(a_s == a_e)
return candidate;
return minimum(a_s+1, a_e, *a_s < candidate ? *a_s : candidate);
}
int main(void){
int array[] = { 1,3,-2,0,-1};
printf("%d ", minimum(array+1, &array[sizeof(array)/sizeof(*array)], *array));
return 0;
}
After accept answer.
Below is a recursive solution that does not chew up the stack. Estimate max stack depth at O(ln2(n)). Other solutions look like the maximum stack depth is O(n).
int ArrayMin(const int a[], size_t n) {
if (n <= 1) {
if (n < 0) return 0; // Handle degenerate ArrayMin( ,0)
return a[0];
}
size_t nhalf = n / 2;
int left = ArrayMin(a, nhalf);
int right = ArrayMin(&a[nhalf], n - nhalf);
return left < right ? left : right;
}
Answered after 9 hours, we can assume homework due date is past.
Consider first element of the array is minimum.
Call the function by passing base address of the array and number of elements.
Check for any other number is less then the minimum value, if yes assign that value to minimum.
For every iteration increment the array address and decrement the no of elements!
Try this code-
#include<stdio.h>
int min;
int arrfun(int *arr,int n)
{
if(n){
if(*arr < min) // check any no is less than minimum.
min = *arr; // if yes assign it
}
else
return min; // when n becomes 0 it returns the minimum element
arrfun(++arr,--n); // recursive call
}
int main()
{
int arr[]={7,3,9,2,1,6};
min = arr[0]; // Taking first element is minimum
printf("minimum is: %d\n",arrfun(arr,6)); // call the function by passing address and no of elements in array
return 0;
}