Professional Documents
Culture Documents
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 22-Oct-2022 L-6 Biwise Algorithms
FAL (2022-23) STS3006 TH AP2022232000027 Reference Material I 22-Oct-2022 L-6 Biwise Algorithms
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Introduction
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Processing: As per above inputs
x = 1234
y = 5678
a = 12, b = 34
c = 56, d = 78
Step 1: a * c = 672
Step 2: b * d = 2652
Step 3: (a+b)(c+d) = 134 * 46 = 6164
Step 4: 6164 - 2652 - 672 = 2840
Step 5: 6720000 + 2652 + 284000 = 7006652
Output: 7006652
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
Integer halfMaxNumLength
= (maxNumLength / 2) + (maxNumLength % 2);
long maxNumLengthTen
= (long)Math.pow(10, halfMaxNumLength);
long a = x / maxNumLengthTen;
long b = x % maxNumLengthTen;
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
public static void main(String[] args)
long c = y / maxNumLengthTen;
{
long d = y % maxNumLengthTen;
long expectedProduct = 1234 * 5678;
long actualProduct = mult(1234, 5678);
long z0 = mult(a, c);
long z1 = mult(a + b, c + d);
System.out.println("Expected 1 : " +
long z2 = mult(b, d); expectedProduct);
long ans = (z0 * (long)Math.pow(10,
halfMaxNumLength * 2) +
System.out.println("Actual 1 : " +
((z1 - z0 - z2) * (long)Math.pow(10, actualProduct + "\n\n");
halfMaxNumLength) + z2));
assert(expectedProduct == actualProduct);
return ans;
expectedProduct = 102 * 313;
}
actualProduct = mult(102, 313);
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
assert(expectedProduct == actualProduct); actualProduct = mult(x, y);
expectedProduct = 1345 * 63456; // Again printing the expected
actualProduct = mult(1345, 63456); and
System.out.println("Expected 3 : " + // corresponding actual product
expectedProduct);
System.out.println("Expected: "
System.out.println("Actual 3 : " +
actualProduct + "\n\n"); + expectedProduct);
assert(expectedProduct == System.out.println("Actual: " +
actualProduct); actualProduct + "\n\n");
Integer x = null; assert(expectedProduct ==
Integer y = null; actualProduct);
Integer MAX_VALUE = 10000; }
Random r = new Random(); }
for (int i = 0; i < MAX_VALUE; i++) { }
x = (int) r.nextInt(MAX_VALUE);
y = (int) r.nextInt(MAX_VALUE);
expectedProduct = x * y;
Output
if (i == 9999) {
// Prove assertions catch the bad stuff.
Product 1 : 85348320
expectedProduct = 1;
Product 2 : 21726
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Longest Sequence of 1 after flipping a
bit
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Longest Sequence
• The reason for using the LCS is to restrict the element of the
subsequences from occupying the consecutive position within
the original sequences.
Examples
Input : 1775
Output : 8
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Long Sequence-Example Program
Output :
4
8
5
© 2016 SMART
SMART TRAINING RESOURCES Training
INDIA PVT. Resources
LTD. Pvt. Ltd. © 2018 SMART Training Resources Pvt. Ltd.
Swap two nibbles in a byte
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Swap two nibbles in a byte
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example Program
class Main{
public static void main(String arg[]) { Output
70
int x = 100;
int ans= (x & 0x0F) << 4 | (x & 0xF0) >> 4; //0x0F=15; 0xF0=240;
System.out.print(ans);
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Thank You …
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.