|
| 1 | +#include <assert.h> |
| 2 | +#include <limits.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stddef.h> |
| 6 | +#include <stdint.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +char* readline(); |
| 12 | +char** split_string(char*); |
| 13 | + |
| 14 | + |
| 15 | +void calculate_the_maximum(int n, int k) { |
| 16 | +int i,j; |
| 17 | +int max=0; |
| 18 | +int h=0; |
| 19 | +for(i=1;i<n;i++) |
| 20 | +{ |
| 21 | + for(j=i+1;j<=n;j++) |
| 22 | + { int v=i&j; |
| 23 | + if(v>=k) |
| 24 | + break; |
| 25 | + if(max<v) |
| 26 | + max=v; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | +} |
| 31 | +printf("%d\n",max); |
| 32 | + |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +int main() |
| 38 | +{ |
| 39 | + char* t_endptr; |
| 40 | + char* t_str = readline(); |
| 41 | + int t = strtol(t_str, &t_endptr, 10); |
| 42 | + |
| 43 | + if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); } |
| 44 | + |
| 45 | + for (int t_itr = 0; t_itr < t; t_itr++) { |
| 46 | + char** nk = split_string(readline()); |
| 47 | + |
| 48 | + char* n_endptr; |
| 49 | + char* n_str = nk[0]; |
| 50 | + int n = strtol(n_str, &n_endptr, 10); |
| 51 | + |
| 52 | + if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } |
| 53 | + |
| 54 | + char* k_endptr; |
| 55 | + char* k_str = nk[1]; |
| 56 | + int k = strtol(k_str, &k_endptr, 10); |
| 57 | + calculate_the_maximum(n,k); |
| 58 | + if (k_endptr == k_str || *k_endptr != '\0') { |
| 59 | + exit(EXIT_FAILURE); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +char* readline() { |
| 67 | + size_t alloc_length = 1024; |
| 68 | + size_t data_length = 0; |
| 69 | + char* data = malloc(alloc_length); |
| 70 | + |
| 71 | + while (true) { |
| 72 | + char* cursor = data + data_length; |
| 73 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 74 | + |
| 75 | + if (!line) { break; } |
| 76 | + |
| 77 | + data_length += strlen(cursor); |
| 78 | + |
| 79 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } |
| 80 | + |
| 81 | + size_t new_length = alloc_length << 1; |
| 82 | + data = realloc(data, new_length); |
| 83 | + |
| 84 | + if (!data) { break; } |
| 85 | + |
| 86 | + alloc_length = new_length; |
| 87 | + } |
| 88 | + |
| 89 | + if (data[data_length - 1] == '\n') { |
| 90 | + data[data_length - 1] = '\0'; |
| 91 | + } |
| 92 | + |
| 93 | + data = realloc(data, data_length); |
| 94 | + |
| 95 | + return data; |
| 96 | +} |
| 97 | +//Aditya Seth |
| 98 | +char** split_string(char* str) { |
| 99 | + char** splits = NULL; |
| 100 | + char* token = strtok(str, " "); |
| 101 | + |
| 102 | + int spaces = 0; |
| 103 | + |
| 104 | + while (token) { |
| 105 | + splits = realloc(splits, sizeof(char*) * ++spaces); |
| 106 | + if (!splits) { |
| 107 | + return splits; |
| 108 | + } |
| 109 | + |
| 110 | + splits[spaces - 1] = token; |
| 111 | + |
| 112 | + token = strtok(NULL, " "); |
| 113 | + } |
| 114 | + |
| 115 | + return splits; |
| 116 | +} |
0 commit comments