Bookmark and Share

free counters
Back to Problem list
Problem 33: Find the largest number among an integer set without using -if
condition, bit operations,<,>,>>,<<,? Operators and also without using any 
library function except scanf and printf.

Solution:

#include<stdio.h>

int a[100];

int ABS(int n) { // This is the most important function, return abs value of n
    int up = 0, down = 0, count = 0;
    for(count = 0; up != n && down != n; count++) { // finding abs value
        down--;
        up++;
    }
    return count;
}
int getMax(int x, int y) { // this function returns largest value among x and y
    int d = x - y;
    d = ABS(d);
    int res = (x - y == -d)*y + ( x-y == d)*x; // calculating max between x and y
    return res;
}
int  main()
{

    int n, i, max = -10000000;
    scanf("%d",&n);
    for(i = 0; i<n; i++) {
        scanf("%d",&a[i]); // >= -1000 && <= 1000
    }
    for(i = 0; i<n; i++) {
        max = getMax(max, a[i]);
    }
    printf("%d\n",max);
    return 0;
}

Back to Problem list