Bitwise Operators wrong output - c

Problem : https://www.hackerrank.com/challenges/bitwise-operators-in-c/problem
I tried the problem using C but the output comes out to be
1
5
1
instead of
2
3
3
I cant find the fault in this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k)
{
int answer1=0;
int answer2=0;
int answer3=0;
for(int i=1;i<n;i++)
{
for(int j=i+1; j<=n;j++)
{
if((i&j >=answer1) && (i&j < k))
{
answer1 = (i&j);
}
if((i|j >=answer2) && (i|j < k))
{
answer2 = (i|j);
}
if((i^j >=answer3) && (i^j < k))
{
answer3 = (i^j);
}
}
}
printf("%d\n%d\n%d",answer1,answer2,answer3);
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}

I don't like putting complete answeres but since I had it solved i hope it can help you.
I broke it down so it will be easier to understand
watch the shortened if statment (condition) ? True_val : False_val
void calculate_the_maximum(int n, int k) {
int max_and = 0;
int max_or = 0;
int max_xor = 0;
int tOr, tAnd, tXor;
for (int i = 1; i <= n ; i++)
for (int j = 1; j <= n; j++) {
if ( i <= j ) continue;
tAnd = (i & j);
tOr = (i | j);
tXor = (i ^ j);
max_and = ((tAnd > max_and) && (tAnd <k)) ? tAnd : max_and;
max_or = ((tOr > max_or) && (tOr <k)) ? tOr : max_or;
max_xor = ((tXor > max_xor) && (tXor < k)) ? tXor : max_xor;
}
printf("%d\n%d\n%d", max_and, max_or, max_xor);
}

Related

How the counting is conducted in this function?

See the function search below, it returns count but I don't see where this variable count acts/works (the function is found in a library , click here to download the source code, you will find kmp.c in \source\algos):
#include "include/define.h"
#include "include/main.h"
void preKmp(unsigned char *x, int m, int kmpNext[]) {
int i, j;
i = 0;
j = kmpNext[0] = -1;
while (i < m) {
while (j > -1 && x[i] != x[j])
j = kmpNext[j];
i++;
j++;
if (i<m && x[i] == x[j])
kmpNext[i] = kmpNext[j];
else
kmpNext[i] = j;
}
}
int search(unsigned char *x, int m, unsigned char *y, int n) {
int i, j, kmpNext[XSIZE], count;
/* Preprocessing */
BEGIN_PREPROCESSING
preKmp(x, m, kmpNext);
END_PREPROCESSING
/* Searching */
BEGIN_SEARCHING
count = 0;
i = j = 0;
while (j < n) {
while (i > -1 && x[i] != y[j])
i = kmpNext[i];
i++;
j++;
if (i >= m) {
OUTPUT(j - i);
i = kmpNext[i];
}
}
END_SEARCHING
return count;
}
Can any one explain how it works?
The OUTPUT macro that is defined in ./source/algos/include/define.h looks like this:
#define OUTPUT(j) count++
And in search you have
if (i >= m) {
OUTPUT(j - i); // <- here
i = kmpNext[i];
}
which explains how count is updated.

The Damerau-Levenshtein distance in C

I'm working on a spell checker and I need to optimize the Damerau-Levenshtein distance. For example... when 2 words have a distance greater than 3 (or a limit), I need to break the function. This will reduce the execution time. i was thinking veryfy when the min is greater than 3, but through the proccess, sometimes the min is greater, because Damerau-Levenshtein distance works on a table of min values. I think to add a condition like i == j, but when a word is longer than the other it fail. The code is version from Internet.
#include <stdio.h>
#include "levenshtein.h"
#include <stdlib.h>
#include <string.h>
int min_(int a, int b){
return (a<b)?a:b;
}
int dist_dlevenshtein(char* p_string1, char* p_string2, int umbral)
{
int l_string_length1 = strlen(p_string1);
int l_string_length2 = strlen(p_string2);
int d[l_string_length1+1][l_string_length2+1];
int i;
int j;
int l_cost;
for (i = 0;i <= l_string_length1;i++)
{
d[i][0] = i;
}
for(j = 0; j<= l_string_length2; j++)
{
d[0][j] = j;
}
for (i = 1;i <= l_string_length1;i++)
{
for(j = 1; j<= l_string_length2; j++)
{
if( p_string1[i-1] == p_string2[j-1] )
{
l_cost = 0;
}
else
{
l_cost = 1;
}
d[i][j] = min_(
d[i-1][j] + 1, // delete
min_(d[i][j-1] + 1, // insert
d[i-1][j-1] + l_cost) // substitution
);
if( (i > 1) &&
(j > 1) &&
(p_string1[i-1] == p_string2[j-2]) &&
(p_string1[i-2] == p_string2[j-1])
)
{
d[i][j] = min_(d[i][j],d[i-2][j-2] + l_cost); // transposition
}
}
}
return d[l_string_length1][l_string_length2];
}```

Passing an array to function a looping through it in C

I know I'm asking pretty basic question here, but could anyone give me a hint how to properly loop through an array which is initialized in another function ? I tried googling, found tons of videos but I didn't manage to get it right just yet. Would anyone please help me find out, what I'm missing in my code ? I'm a struggling beginner. Thanks in advance for your time.
My code (not functioning):
#include <stdio.h>
#define ARR_RANGE 1000000
#define GREATEST_NUMBER 1000000
void sieve(int eratosthenes[]);
int main()
{
int eratosthenes[ARR_RANGE];
int n = 999;
int c;
for(i = 2; i <= arrLen; ++i)
{
if(eratosthenes[i]!= -1)
{
int c = 0;
while(n % i == 0)
{
n /= i;
++c;
}
if(c >= 2)
{
printf("%d^%d x ", i, c);
}
else if(c == 1)
{
printf("%d x ", i);
}
}
else
continue;
}
return 0;
}
void sieve(int eratosthenes[])
{
for(int i = 1; i < GREATEST_NUMBER; ++i)
{
eratosthenes[i] = i;
}
for(int i = 2; i*i < GREATEST_NUMBER; ++i)
{
if(eratosthenes[i] != -1)
{
for(int j = 2*i; j < GREATEST_NUMBER ; j += i)
eratosthenes[j] = -1;
}
}
int arrLen = sizeof eratosthenes / sizeof eratosthenes[0];
}
In the main is not visible the call of function sieve,so the array is not passed into function, to do this you have to write in the main sieve(eratosthenes); (Passage by reference)

I want to print the following pyramid of number pattern

1
2 4
3 5 7
6 8 10 12
9 11 13 15 17
Following is the code in which I am not able to print the pyramid:-
int main()
{
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("%d ",i*j);
}
printf("\n");
}
return 0;
}
You need to track both even and odd numbers .
#include <stdio.h>
int main()
{
int even=1,odd=2;
int n=10;
for (int i = 1; i <= n; i++)
{
int a= (i % 2 == 0);
for (int j = 1; j < i; j++)
{
if(a)
{
printf("%d ",even);
}
else
{
printf("%d ",odd);
}
even += a ? 2 : 0;
odd += a ? 0 : 2;
}
printf("\n");
}
return 0;
}
Not very clean and compact algorithm but sth like this would work:
#include <stdio.h>
#include <stdlib.h>
int main() {
char tmp[10];
int n = 0, row = 1, odd = 1, even = 2, c = 0, selectOdd, fin = 0;
printf("maximum number: ");
scanf("%s", tmp);
n = atoi(tmp);
if (n != 0) {
while (fin < 2) {
selectOdd = row % 2;
c = row;
if (selectOdd) {
while (c != 0) {
printf("%3d", odd);
odd += 2;
if (odd > n) {
fin++;
break;
}
c--;
}
}
else {
while (c != 0) {
printf("%3d", even);
even += 2;
if (even > n) {
fin++;
break;
}
c--;
}
}
printf("\n");
row++;
}
}
return 0;
}
it's simple
your algorithm is odd, even, odd,... and so on
so you start with odd number until reach line number
for next line is even and you can find start number with this
you just need find number at start of line and continue print number number
in each step you just need
num += 2;
remember 'lineIndex' start from 1
num = (lineIndex - 1) * 2 + lineIndex % 2;
this is a full code
#include <stdio.h>
int main(){
int numIndex;
int lineIndex;
int num;
for (lineIndex = 1; lineIndex <= 5; lineIndex++) {
num = (lineIndex - 1) * 2 + lineIndex % 2;
for (numIndex = 0; numIndex < lineIndex; numIndex++) {
printf("%2d ", num);
num += 2;
}
printf("\n");
}
}

Find elements from an array whose sum equals a given number

I am try to write a A recursive implementation, but it dont work. Can you help me to find mistake. I need just a recursive solution on C.
void findsum(int arr[],int i, int k){
if (k <= 0 || arr[i] > k) return;
if (arr[i] <= k) {
k -= arr[i];
if (k == 0) {
NSLog(#"Summ %d, %d",arr[i], arr[i]); return;
}
}
if (i == (25)) return;
for (int a=i ;a<25; a ++ ) {
findsum(arr, a, k);
}
}
int main()
{
int set[] = {18897109, 12828837, 9461105, 6371773, 5965343, 5946800, 5582170, 5564635, 5268860, 4552402, 4335391, 4296250, 4224851, 4192887, 3439809, 3279833, 3095313, 2812896, 2783243, 2710489, 2543482, 2356285, 2226009, 2149127, 2142508, 2134411};
int sum = 100000000;
int n = sizeof(set)/sizeof(set[0]);
for (int i = 0; i < n; i++) {
findsum(set, i, sum);
}
}
You code not stop because every time you assign i=0 by loop
for (i = 0 ;i<26; i ++ ) {
findsum(arr, i, k);
}
In findsum function.
So as per my suggestion you can make some changes as like
first in main
int main()
{
int set[] = {18897109, 12828837, 9461105, 6371773, 5965343, 5946800, 5582170, 5564635, 5268860, 4552402, 4335391, 4296250, 4224851, 4192887, 3439809, 3279833, 3095313, 2812896, 2783243, 2710489, 2543482, 2356285, 2226009, 2149127, 2142508, 2134411};
int sum = 100000000,i;
int n = sizeof(set)/sizeof(set[0]);
findsum(set, n, sum);
}
And second in findsum like
void findsum(int arr[],int i, int k)
{
if (k <= 0 || arr[i] > k) return;
if (arr[i] <= k)
{
k -= arr[i];
if (k == 0) {
NSLog(#"Summ %d, %d",arr[i], arr[i]); return;
}
}
if (i == 25) return;
findsum(arr, i, k);
}

Resources