You are given three arrays and two numbers M and K. Find a lexicographically minimum such that there are exactly K indices where for some integer . Also, you are given ranges of , , and -- (. Here, a triplet of integers is considered to be lexicographically smaller than a triplet if sequence is lexicographically smaller than sequence . A sequence a is lexicographically smaller than a sequence b if in the first position where a and b differ, the sequence a has a smaller element than the corresponding element in b.
Example:
Input:4 3 4 5 6 1 2 6 9 11 5 6 1 1 1 1 10 1 10 1 10Output:3 3 3
Approach
Java
public class ThreeArrays {public static void main(String[] args) {int n = 4;int m = 3;int k = 4;long[] a = { 5, 2, 11, 2 };long[] b = { 6, 6, 5, 1 };long[] c = { 1, 9, 6, 1 };long xl = 1;long xr = 10;long yl = 1;long yr = 10;long zl = 1;long zr = 10;long xptr = xl, yptr, zptr;do {yptr = yl;do {zptr = zl;do {int count = 0;for (int i = 0; i < n; i++) {long exp = xptr * a[i] + yptr * b[i] - zptr * c[i];if (exp % m == 0)count++;}if (count == k) {System.out.println(xptr + " " + yptr + " " + zptr);System.exit(0);}zptr++;} while (((zptr % m) != (zl % m)) && (zptr <= zr));yptr++;} while ((yptr % m) != (yl % m) && (yptr <= yr));xptr++;} while ((xptr % m) != (xl % m) && (xptr <= xr));System.out.println(-1);}}
No comments:
Post a Comment