I am solving a problem josephus problem where i require to set all elements of array to 1, but by using memset its not happening.I tried printing the array it has been filled by a random number. My solution to problem is correct(i placed 1 manually and checked).I have used memset before too but never faced this problem.(i tried running it on cses and clion).
the line i used is:
int arr[n];
memset (arr,1,sizeof(arr));
My code is provided below:(line 44)
#include<bits/stdc++.h>
using namespace std;
long long multi(long long a, long long b, long long mod){
return (a * b) % mod;
}
long long power(long long a, long long b, long long mod){
long long powans = 1;
for(; b > 0; a = multi(a, a, mod), b /= 2) if(b % 2 == 1) powans = multi(powans, a, mod);
return powans;
}
void fastscan(int &x)
{
bool neg=false;
register int c;
x =0;
c=getchar();
if(c=='-')
{
neg = true;
c=getchar();
}
for(;(c>47 && c<58);c=getchar())
x = (x<<1) + (x<<3) +c -48;
if(neg)
x *=-1;
}
int gcd(int a,int b){
if(a%b==0)
return b;
else
return gcd(b,a%b);
}
void solve(){
int n;
fastscan(n);
int arr[n];
memset (arr,1,sizeof(arr));
int count=0;
int del=0;
int i=0;
while(del!=n){
if(arr[i]==1){
if(count==1){
cout<<i+1<<" ";
count=0;
del++;
arr[i]=0;
}
else
count++;
}
i++;
if(i==n)
i=0;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
Any help is appreciated thanks.