Related
here is my recursive function -
private static int minCostClimbingStairs(int[] cost) {
int cost1 = minimumCost(0, cost);
int cost2 = minimumCost(1, cost);
return Math.min(cost1, cost2);
}
private static int minimumCost(int index, int cost[]) {
System.out.println("== "+ index);
if (index == cost.length - 1) return cost[cost.length - 1];
if (index == cost.length - 2) return cost[cost.length - 2];
int cost1 = minimumCost(index + 1, cost) + cost[index];
int cost2 = minimumCost(index + 2, cost) + cost[index];
return Math.min(cost1, cost2);
}
It is running indefinitely for the following inputs -
int arr[] = {841, 462, 566, 398, 243, 248, 238, 650, 989, 576, 361, 126, 334, 729, 446, 897, 953, 38, 195, 679, 65, 707, 196, 705, 569, 275, 259, 872, 630, 965, 978, 109, 56, 523, 851, 887, 91, 544, 598, 963, 305, 481, 959, 560, 454, 883, 50, 216, 732, 572, 511, 156, 177, 831, 122, 667, 548, 978, 771, 880, 922, 777, 990, 498, 525, 317, 469, 151, 874, 202, 519, 139, 670, 341, 514, 469, 858, 913, 94, 849, 839, 813, 664, 163, 3, 802, 21, 634, 944, 901, 446, 186, 843, 742, 330, 610, 932, 614, 625, 169, 833, 4, 81, 55, 124, 294, 71, 24, 929, 534, 621, 543, 417, 534, 427, 327, 179, 90, 341, 949, 368, 692, 646, 290, 488, 145, 273, 617, 596, 82, 538, 751, 80, 616, 763, 826, 932, 184, 630, 478, 163, 925, 259, 237, 839, 602, 60, 786, 603, 413, 816, 278, 4, 35, 243, 64, 631, 405, 23, 638, 618, 829, 481, 877, 756, 482, 999, 973, 718, 157, 262, 752, 931, 882, 741, 40, 77, 535, 542, 879, 607, 879, 321, 46, 210, 116, 244, 830, 591, 285, 382, 925, 48, 497, 913, 203, 239, 696, 162, 623, 291, 525, 950, 27, 546, 293, 108, 577, 672, 354, 256, 3, 671, 998, 22, 989, 557, 424, 251, 923, 542, 243, 46, 488, 80, 374, 372, 334, 190, 817, 150, 742, 362, 196, 75, 193, 162, 645, 859, 758, 433, 903, 199, 289, 175, 303, 475, 818, 213, 576, 181, 668, 243, 297, 572, 549, 840, 161, 292, 719, 226, 338, 981, 345, 203, 655, 210, 65, 111, 746, 76, 935, 406, 646, 976, 567, 32, 726, 638, 674, 727, 861, 426, 297, 349, 464, 973, 341, 452, 826, 223, 805, 940, 458, 468, 967, 107, 345, 987, 553, 407, 916, 103, 324, 367, 864, 74, 946, 712, 596, 105, 194, 79, 634, 855, 703, 70, 170, 543, 208, 739, 632, 663, 880, 857, 824, 258, 743, 488, 659, 647, 470, 958, 492, 211, 927, 356, 488, 744, 570, 143, 674, 502, 589, 270, 80, 6, 463, 506, 556, 495, 713, 407, 229, 689, 280, 162, 454, 757, 565, 267, 575, 417, 948, 607, 269, 852, 938, 560, 24, 222, 580, 604, 800, 628, 487, 485, 615, 796, 384, 555, 226, 412, 445, 503, 810, 949, 966, 28, 768, 83, 213, 883, 963, 831, 390, 951, 378, 497, 440, 780, 209, 734, 290, 96, 398, 146, 56, 445, 880, 910, 858, 671, 164, 552, 686, 748, 738, 837, 556, 710, 787, 343, 137, 298, 685, 909, 828, 499, 816, 538, 604, 652, 7, 272, 729, 529, 343, 443, 593, 992, 434, 588, 936, 261, 873, 64, 177, 827, 172, 712, 628, 609, 328, 672, 376, 628, 441, 9, 92, 525, 222, 654, 699, 134, 506, 934, 178, 270, 770, 994, 158, 653, 199, 833, 802, 553, 399, 366, 818, 523, 447, 420, 957, 669, 267, 118, 535, 971, 180, 469, 768, 184, 321, 712, 167, 867, 12, 660, 283, 813, 498, 192, 740, 696, 421, 504, 795, 894, 724, 562, 234, 110, 88, 100, 408, 104, 864, 473, 59, 474, 922, 759, 720, 69, 490, 540, 962, 461, 324, 453, 91, 173, 870, 470, 292, 394, 771, 161, 777, 287, 560, 532, 339, 301, 90, 411, 387, 59, 67, 828, 775, 882, 677, 9, 393, 128, 910, 630, 396, 77, 321, 642, 568, 817, 222, 902, 680, 596, 359, 639, 189, 436, 648, 825, 46, 699, 967, 202, 954, 680, 251, 455, 420, 599, 20, 894, 224, 47, 266, 644, 943, 808, 653, 563, 351, 709, 116, 849, 38, 870, 852, 333, 829, 306, 881, 203, 660, 266, 540, 510, 748, 840, 821, 199, 250, 253, 279, 672, 472, 707, 921, 582, 713, 900, 137, 70, 912, 51, 250, 188, 967, 14, 608, 30, 541, 424, 813, 343, 297, 346, 27, 774, 549, 931, 141, 81, 120, 342, 288, 332, 967, 768, 178, 230, 378, 800, 408, 272, 596, 560, 942, 612, 910, 743, 461, 425, 878, 254, 929, 780, 641, 657, 279, 160, 184, 585, 651, 204, 353, 454, 536, 185, 550, 428, 125, 889, 436, 906, 99, 942, 355, 666, 746, 964, 936, 661, 515, 978, 492, 836, 468, 867, 422, 879, 92, 438, 802, 276, 805, 832, 649, 572, 638, 43, 971, 974, 804, 66, 100, 792, 878, 469, 585, 254, 630, 309, 172, 361, 906, 628, 219, 534, 617, 95, 190, 541, 93, 477, 933, 328, 984, 117, 678, 746, 296, 232, 240, 532, 643, 901, 982, 342, 918, 884, 62, 68, 835, 173, 493, 252, 382, 862, 672, 803, 803, 873, 24, 431, 580, 257, 457, 519, 388, 218, 970, 691, 287, 486, 274, 942, 184, 817, 405, 575, 369, 591, 713, 158, 264, 826, 870, 561, 450, 419, 606, 925, 710, 758, 151, 533, 405, 946, 285, 86, 346, 685, 153, 834, 625, 745, 925, 281, 805, 99, 891, 122, 102, 874, 491, 64, 277, 277, 840, 657, 443, 492, 880, 925, 65, 880, 393, 504, 736, 340, 64, 330, 318, 703, 949, 950, 887, 956, 39, 595, 764, 176, 371, 215, 601, 435, 249, 86, 761, 793, 201, 54, 189, 451, 179, 849, 760, 689, 539, 453, 450, 404, 852, 709, 313, 529, 666, 545, 399, 808, 290, 848, 129, 352, 846, 2, 266, 777, 286, 22, 898, 81, 299, 786, 949, 435, 434, 695, 298, 402, 532, 177, 399, 458, 528, 672, 882, 90, 547, 690, 935, 424, 516, 390, 346, 702, 781, 644, 794, 420, 116, 24, 919, 467, 543, 58, 938, 217, 502, 169, 457, 723, 122, 158, 188, 109, 868, 311, 708, 8, 893, 853, 376, 359, 223, 654, 895, 877, 709, 940, 195, 323, 64, 51, 807, 510, 170, 508, 155, 724, 784, 603, 67, 316, 217, 148, 972, 19, 658, 5, 762, 618, 744, 534, 956, 703, 434, 302, 541, 997, 214, 429, 961, 648, 774, 244, 684, 218, 49, 729, 990, 521, 948, 317, 847, 76, 566, 415, 874, 399, 613, 816, 613, 467, 191};
However, it seems to exit properly for small inputs. such as -
int arr[] = {1, 100, 1, 1, 1, 100, 1, 1, 100};
int arr[] = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1};
int arr[] = {0,0,0,1};
int arr[] = {10,15,20};
Assuming cost is going to be positive i.e. > 0 you can initiate an array of integers of length equal to input's length and use it to memoize the intermediate solutions like below:
See if memo[index] is not zero i.e. not populated then recurse otherwise use it.
private static int minimumCost(int index, int cost[], int memo[]) {
System.out.println("== "+ index);
if (index == cost.length - 1) return cost[cost.length - 1];
if (index == cost.length - 2) return cost[cost.length - 2];
if (memo[index] != 0) return memo[index];
int cost1 = minimumCost(index + 1, cost, memo) + cost[index];
int cost2 = minimumCost(index + 2, cost, memo) + cost[index];
memo[index] = Math.min(cost1, cost2);
return memo[index];
}
I am trying to create a method that resizes all images within a specific folder without disrupting the subdirectories formatting. I have two integer arrays that specify the width and height of the image that I want to be resized. I use a recursive call on the method to iterate through the folder and its subdirectories.
The problem is whenever I use the recursive call, it starts from the beginning of the width and height array when I want it to start where it left off. How do I fix this?
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor) throws IOException {
int width[] = {36, 48, 72, 96, 96, 48, 72, 36, 48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 2008, 1024, 1536, 1536, 768, 768, 960, 480, 640, 640, 640, 640, 320, 320, 64, 480};
int height[] = {36, 48, 72, 96, 96, 48, 72, 36,48, 72, 96, 128, 48, 50, 80, 80, 1024, 40, 114, 57, 58, 60, 120, 180, 144, 72, 144, 76, 152, 87, 57, 114, 72, 144, 114, 57, 144, 72, 180, 128, 64, 173, 48, 62, 180, 800, 480, 320, 200, 480, 320, 1280, 720, 480, 320, 480, 240, 225, 1536, 783, 2008, 2008, 1004, 1004, 640, 320, 960, 960, 1136, 1136, 480, 480, 64, 800};
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[i] + " Height: " + height[i]);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[i], height[i], backgroundColor);
}
}
}
EDIT:
I changed my code to this, but now it won't pass through the directories properly. Odd because I didn't change anything how it passes through directories. Any help would be appreciated.
resizeFiles(File[] folderToResize, String inputImagePath, int backgroundColor, int width[], int height[], int previousIndex) throws IOException { //won't walk through the directories properly
for (int i = 0; i < folderToResize.length; i++) {
if (folderToResize[i].isDirectory()) {
resizeFiles(folderToResize[i].listFiles(), inputImagePath, backgroundColor, width, height, i);
} else {
System.out.println("File Pathway: " + folderToResize[i].getAbsolutePath() + " Width: " + width[previousIndex] + " Height: " + height[previousIndex] + " Index: " + previousIndex);
resize(inputImagePath, folderToResize[i].getAbsolutePath(), width[previousIndex], height[previousIndex], backgroundColor);
break;
}
}
}
While running the below snippet the loop executes properly, but the elements are not added for some reason.
for(i=5;i<=500;i++) {
x.add(i);
check(x,i);
}
public static void check(ArrayList<Integer> x,Integer i){
for(int j=0;j<x.size();j++){
for(int k=0;k<x.size();k++){
if(x.get(j)!=i&& x.get(k)!=i){
if(x.contains(x.get(k)+x.get(j)))
x.remove(Integer.valueOf(i));
}
}
}
}
You are removing the int i from the list instead of the computed value. You do your contains method, you should remove that part. Every time this is run it will remove whatever you plug in for i in the parameters. So every time you add something you then just the method and delete what you just added.
The assignment was rather interesting, but the way you tried to do was really too expensive (and very wrong, because you modified the list during iteration)
So here's a bit of improvement :
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
IntStream.range(5, 500)
.filter(i -> !check(list,i)) // check first
.forEach( i -> list.add(i)); // add after
System.out.println(list);
}
// we assume (rightly so) that list is ordered
private static boolean check(List<Integer> list, int value) {
boolean found = false;
for(int j=0; j < list.size(); j++){
Integer leftSide = list.get(j);
if(leftSide > value/2){
break; // as the check is symmetrical, stop after value/2 has been overtaken
}
Integer complementaryValue = value - leftSide;
for(int k=j+1; k < list.size(); k++){ // iterate over the rest of the list (because right is necessarily > left)
Integer rightSide = list.get(k);
if(rightSide > complementaryValue){
break; // no need to continue further
}
if(complementaryValue.equals(rightSide)){
found = true;
System.out.println(value+" = "+leftSide+" + "+rightSide);
break;
}
}
if(found){
break; // or return true
}
}
return found;
}
You can try it.
This is still far from optimal, work can still be done on the inner loop. A dichotomic search for the complementary value would be way better.
The final list is
[5, 6, 7, 8, 9, 10, 20, 21, 22, 23, 24, 35, 36, 37, 38, 39, 50, 51, 52, 53, 54, 65, 66, 67, 68, 69, 80, 81, 82, 83, 84, 95, 96, 97, 98, 99, 110, 111, 112, 113, 114, 125, 126, 127, 128, 129, 140, 141, 142, 143, 144, 155, 156, 157, 158, 159, 170, 171, 172, 173, 174, 185, 186, 187, 188, 189, 200, 201, 202, 203, 204, 215, 216, 217, 218, 219, 230, 231, 232, 233, 234, 245, 246, 247, 248, 249, 260, 261, 262, 263, 264, 275, 276, 277, 278, 279, 290, 291, 292, 293, 294, 305, 306, 307, 308, 309, 320, 321, 322, 323, 324, 335, 336, 337, 338, 339, 350, 351, 352, 353, 354, 365, 366, 367, 368, 369, 380, 381, 382, 383, 384, 395, 396, 397, 398, 399, 410, 411, 412, 413, 414, 425, 426, 427, 428, 429, 440, 441, 442, 443, 444, 455, 456, 457, 458, 459, 470, 471, 472, 473, 474, 485, 486, 487, 488, 489]
we are given a read only array of n integers from 1 to n. Each integer appears exactly once except A which appears twice and B which is missing.
Return A and B.
I know my solution is not space efficient but i am wondering why i am getting wrong output for cases like:
389, 299, 65, 518, 361, 103, 342, 406, 24, 79, 192, 181, 178, 205, 38, 298, 218, 143, 446, 324, 82, 41, 312, 166, 252, 59, 91, 6, 248, 395, 157, 332, 352, 57, 106, 246, 506, 261, 16, 470, 224, 228, 286, 121, 193, 241, 203, 36, 264, 234, 386, 471, 225, 466, 81, 58, 253, 468, 31, 197, 15, 282, 334, 171, 358, 209, 213, 158, 355, 243, 75, 411, 43, 485, 291, 270, 25, 100, 194, 476, 70, 402, 403, 109, 322, 421, 313, 239, 327, 238, 257, 433, 254, 328, 163, 436, 520, 437, 392, 199, 63, 482, 222, 500, 454, 84, 265, 508, 416, 141, 447, 258, 384, 138, 47, 156, 172, 319, 137, 62, 85, 154, 97, 18, 360, 244, 272, 93, 263, 262, 266, 290, 369, 357, 176, 317, 383, 333, 204, 56, 521, 502, 326, 353, 469, 455, 190, 393, 453, 314, 480, 189, 77, 129, 439, 139, 441, 443, 351, 528, 182, 101, 501, 425, 126, 231, 445, 155, 432, 418, 95, 375, 376, 60, 271, 74, 11, 419, 488, 486, 54, 460, 321, 341, 174, 408, 131, 115, 107, 134, 448, 532, 292, 289, 320, 14, 323, 61, 481, 371, 151, 385, 325, 472, 44, 335, 431, 187, 51, 88, 105, 145, 215, 122, 162, 458, 52, 496, 277, 362, 374, 26, 211, 452, 130, 346, 10, 315, 459, 92, 531, 467, 309, 34, 281, 478, 477, 136, 519, 196, 240, 12, 288, 302, 119, 356, 503, 527, 22, 27, 55, 343, 490, 127, 444, 308, 354, 278, 497, 191, 294, 117, 1, 396, 125, 148, 285, 509, 208, 382, 297, 405, 245, 5, 330, 311, 133, 274, 275, 118, 463, 504, 39, 99, 442, 337, 169, 140, 104, 373, 221, 499, 413, 124, 510, 159, 465, 80, 276, 83, 329, 524, 255, 387, 259, 397, 491, 517, 23, 4, 230, 48, 349, 412, 142, 114, 487, 381, 164, 35, 67, 498, 73, 440, 108, 226, 96, 132, 144, 207, 235, 33, 69, 128, 236, 364, 198, 475, 173, 493, 150, 90, 515, 111, 68, 232, 340, 112, 526, 492, 512, 495, 429, 146, 336, 17, 350, 251, 7, 184, 76, 380, 359, 293, 19, 49, 345, 227, 212, 430, 89, 474, 279, 201, 398, 347, 273, 37, 185, 177, 102, 304, 295, 422, 94, 426, 514, 116, 183, 180, 494, 42, 305, 152, 390, 30, 247, 451, 32, 388, 331, 78, 424, 368, 394, 188, 306, 449, 8, 214, 120, 179, 280, 511, 409, 338, 153, 507, 370, 461, 217, 161, 483, 147, 242, 86, 417, 268, 71, 462, 420, 167, 513, 379, 307, 522, 435, 113, 296, 457, 525, 45, 529, 423, 427, 2, 438, 64, 316, 46, 40, 13, 516, 367, 233, 110, 318, 250, 283, 216, 186, 310, 237, 377, 365, 175, 479, 378, 66, 414, 473, 165, 210, 50, 348, 372, 363, 339, 20, 168, 284, 415, 505, 206, 53, 223, 434, 202, 123, 399, 400, 135, 269, 428, 219, 456, 28, 464, 267, 489, 98, 391, 195, 366, 300, 484, 533, 229, 213, 149, 160, 256, 303, 530, 301, 29, 404, 344, 401, 220, 287, 9, 407, 170, 450, 523, 249, 72, 410, 3, 21, 200, 260
Expected Output:
213 87
Actual Output :
213 3
Java Code What I have Tried so far
public class Solution {
// DO NOT MODIFY THE LIST
public ArrayList<Integer> repeatedNumber(final List<Integer> a) {
int n=a.size();
int rep=0,b=0;
int[] arr= new int[n+1];
for(int i=0;i<n+1;i++) //value i is at index i
arr[i]=i;
arr[0]=-1;
for(int val : a)
{
if(arr[val]!=-1)
arr[val]=-1;
else
{
rep=val;
break;
}
}
for(int i=0; i<n+1; i++)
{
if(arr[i]!=-1)
{
b=i;
break;
}
}
ArrayList<Integer> ans = new ArrayList<Integer>();
ans.add(rep);
ans.add(b);
return ans;
}
}
You find a mistake like this by reasoning that you need one complete pass over the array to detect a missing value. You may stop earlier for the duplicate.
for(int val : a){
if(arr[val]!=-1){
arr[val]=-1;
} else {
rep=val;
///////////////// Omit: break;
}
}
Later Basically the same idea but using a bit more of Java, making it shorter:
int dup = 0;
int abs = 0;
BitSet present = new BitSet( a.size() + 1 );
for( int x: a ){
if( present.get( x ) ){
dup = x;
} else {
present.set( x );
}
}
abs = present.nextClearBit( 1 );
If you may modify the original array (or list), you can avoid using some extra storage:
int dup = 0;
int abs = 0;
for( int i = 0; i < a.length; ++i ){
if( a[i] <= 0 ) continue;
int val = a[i];
a[i] = 0;
while( true ){
if( a[val-1] == -val ){
dup = val;
break;
} else {
int h = a[val-1];
a[val-1] = -val;
if( h == 0 ) break;
val = h;
}
}
}
for( int i = 0; i < a.length; ++i ){
if( a[i] >= 0 ){
abs = i+1;
break;
}
}
I would begin by using List.toArray(T[]) to get an array, sorting the array and then iterating the sorted array once. Something like,
public ArrayList<Integer> repeatedNumber(final List<Integer> a) {
Integer[] arr = a.toArray(new Integer[0]);
Arrays.sort(arr);
Integer[] r = new Integer[2];
for (int i = 1; i < arr.length; i++) {
int prev = arr[i - 1];
if (prev == arr[i]) {
r[0] = prev;
} else if (prev != arr[i] - 1) {
r[1] = prev + 1;
}
}
return new ArrayList<Integer>(Arrays.asList(r));
}
which I tested with your input and got (as requested)
[213, 87]
Here is a solution that doesn't alter the original list and finds the missing one using only maths.
public static void main(String[] args) {
find(new int[]{389, 299, 65, 518, 361, 103, 342, 406, 24, 79, 192, 181, 178, 205, 38, 298, 218, 143, 446, 324, 82, 41, 312, 166, 252, 59, 91, 6, 248, 395, 157, 332, 352, 57, 106, 246, 506, 261, 16, 470, 224, 228, 286, 121, 193, 241, 203, 36, 264, 234, 386, 471, 225, 466, 81, 58, 253, 468, 31, 197, 15, 282, 334, 171, 358, 209, 213, 158, 355, 243, 75, 411, 43, 485, 291, 270, 25, 100, 194, 476, 70, 402, 403, 109, 322, 421, 313, 239, 327, 238, 257, 433, 254, 328, 163, 436, 520, 437, 392, 199, 63, 482, 222, 500, 454, 84, 265, 508, 416, 141, 447, 258, 384, 138, 47, 156, 172, 319, 137, 62, 85, 154, 97, 18, 360, 244, 272, 93, 263, 262, 266, 290, 369, 357, 176, 317, 383, 333, 204, 56, 521, 502, 326, 353, 469, 455, 190, 393, 453, 314, 480, 189, 77, 129, 439, 139, 441, 443, 351, 528, 182, 101, 501, 425, 126, 231, 445, 155, 432, 418, 95, 375, 376, 60, 271, 74, 11, 419, 488, 486, 54, 460, 321, 341, 174, 408, 131, 115, 107, 134, 448, 532, 292, 289, 320, 14, 323, 61, 481, 371, 151, 385, 325, 472, 44, 335, 431, 187, 51, 88, 105, 145, 215, 122, 162, 458, 52, 496, 277, 362, 374, 26, 211, 452, 130, 346, 10, 315, 459, 92, 531, 467, 309, 34, 281, 478, 477, 136, 519, 196, 240, 12, 288, 302, 119, 356, 503, 527, 22, 27, 55, 343, 490, 127, 444, 308, 354, 278, 497, 191, 294, 117, 1, 396, 125, 148, 285, 509, 208, 382, 297, 405, 245, 5, 330, 311, 133, 274, 275, 118, 463, 504, 39, 99, 442, 337, 169, 140, 104, 373, 221, 499, 413, 124, 510, 159, 465, 80, 276, 83, 329, 524, 255, 387, 259, 397, 491, 517, 23, 4, 230, 48, 349, 412, 142, 114, 487, 381, 164, 35, 67, 498, 73, 440, 108, 226, 96, 132, 144, 207, 235, 33, 69, 128, 236, 364, 198, 475, 173, 493, 150, 90, 515, 111, 68, 232, 340, 112, 526, 492, 512, 495, 429, 146, 336, 17, 350, 251, 7, 184, 76, 380, 359, 293, 19, 49, 345, 227, 212, 430, 89, 474, 279, 201, 398, 347, 273, 37, 185, 177, 102, 304, 295, 422, 94, 426, 514, 116, 183, 180, 494, 42, 305, 152, 390, 30, 247, 451, 32, 388, 331, 78, 424, 368, 394, 188, 306, 449, 8, 214, 120, 179, 280, 511, 409, 338, 153, 507, 370, 461, 217, 161, 483, 147, 242, 86, 417, 268, 71, 462, 420, 167, 513, 379, 307, 522, 435, 113, 296, 457, 525, 45, 529, 423, 427, 2, 438, 64, 316, 46, 40, 13, 516, 367, 233, 110, 318, 250, 283, 216, 186, 310, 237, 377, 365, 175, 479, 378, 66, 414, 473, 165, 210, 50, 348, 372, 363, 339, 20, 168, 284, 415, 505, 206, 53, 223, 434, 202, 123, 399, 400, 135, 269, 428, 219, 456, 28, 464, 267, 489, 98, 391, 195, 366, 300, 484, 533, 229, 213, 149, 160, 256, 303, 530, 301, 29, 404, 344, 401, 220, 287, 9, 407, 170, 450, 523, 249, 72, 410, 3, 21, 200, 260});
}
public static void find(int[] numbers){
int sum = 0;
int duplicate = -1;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
if(duplicate == -1) {
for (int j = i + 1; j < numbers.length; j++) {
if(numbers[i] == numbers[j]){
duplicate = numbers[i];
}
}
}
}
int missing = triangle(numbers.length) - (sum - duplicate);
System.out.println(duplicate + " " + missing);
}
public static int triangle(int amount) {
return (int) ((Math.pow(amount, 2) + amount) / 2);
}
I would suggest a better approach to do this. Here is logical steps that you need to follow.
I am explaining with an Example
ex:- incorrect Array => {20,30,40,40} and Correct Array => {20,30,40,60}
First Calculate Sum of correct Array and incorrect Array.
incorrect Array sum => 130 and Correct Array => 150.
Calculate the difference between the sum of these two Arrays.
Difference will be -20(Negative).
Then Find the Integer value which is Repeated.
Using loop Iteration find out the Number which is getting RepeatedFrom Incorrect Array. So Repeated Number will be 40.
Now Substract this Repeated Number with Difference You Found (Between the two Sum).
(-20)-(-40) = -60 You Got Your Missing Number .
Finally You will got your Missing Number in Negative. it is more efficient way to do this.
Note :- If you Iterate throughout one Incorrect array (n) in (n*n) in Nested Loops and again in Nested Loop Iterate throughout Correct array (n) in (n*n) So, Total will be (n*n*n*n). Its very hectic actually. In Solution which is given by me will max upto ((n*n)+n+n). So Definitely
For whatever reason, despite the fact that it's entirely generic and works with integers whenever it's tested with strings it seems to skip the last iteration of merging. I've scoured my code for a couple hours now and I can't see why it isn't working correctly so any insight anyone has would be well received!
import java.util.Iterator; import
java.util.LinkedList;
/** * * #author paul */ public
class MergeSort> {
LinkedList<T> theList;
MergeSort(LinkedList<T> toBeSorted) {
theList = toBeSorted;
}
public LinkedList<T> sort() {
return trueSort(theList);
}
private LinkedList<T> trueSort(LinkedList<T> sorting) {
if (sorting.size() <= 1) {
return sorting;
}
LinkedList<T> left, right, sorted;
left = new LinkedList<T>();
right = new LinkedList<T>();
int middle = sorting.size() / 2;
Iterator<T> sojourner = sorting.iterator();
for (int i = 0; sojourner.hasNext(); i++) {
if (i < middle) {
left.add(sojourner.next());
} else {
right.add(sojourner.next());
}
}
return trueMerge(trueSort(left),
trueSort(right));
}
private LinkedList<T> trueMerge(LinkedList<T> left,
LinkedList right) {
LinkedList result = new LinkedList();
while (left.size() > 0 || right.size() > 0) {
if (left.size() > 0 && right.size() > 0) {
if (left.getFirst().compareTo(right.getFirst())
< 0) {
result.add(left.pop());
} else {
result.add(right.pop());
}
} else if (left.size() > 0) {
result.add(left.pop());
} else {
result.add(right.pop());
}
}
return result;
} }
Here's my main java file
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
/**
*
* #author paul
*/
public class Main {
public static Random Rand;
public static int randomNumber(int min, int max) {
return min + (int) (Rand.nextDouble() * ((max - min) +
1));
}
public static <T> String getString(LinkedList<T> linkInt) {
String s = "";
Iterator<T> interLink = linkInt.iterator();
for (int i = 0; interLink.hasNext(); i++) {
s = s + interLink.next().toString();
if (interLink.hasNext()) {
s = s + ", ";
}
if ((i + 1) % 10 == 0) {
s = s + "\n";
}
}
return s;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Rand = new Random();
LinkedList<Integer> numbers = new LinkedList<Integer>();
for(int i = 0; i< 100;i++){
numbers.add(randomNumber(1,1000));
}
System.out.println(getString(numbers));
MergeSort m = new MergeSort(numbers); //change this
later to be purely static
numbers = m.sort();
System.out.println(getString(numbers));
LinkedList<String> words = new LinkedList<String>();
words.add("Hello");
words.add("MY");
words.add("name");
words.add("Is");
words.add("Barthoal");
words.add("I");
words.add("Enjoy");
words.add("long");
words.add("beach");
words.add("walks");
words.add("would");
words.add("you");
words.add("like");
words.add("to");
words.add("come");
words.add("Join");
words.add("me");
words.add("in");
words.add("my");
words.add("StarDestroyer-MobileHome?
(TM)");
System.out.println(getString(words));
MergeSort mm = new MergeSort(words);
words = mm.sort();
System.out.println(getString(words));
}
}
This outputs:
304, 842, 342, 794, 574, 99, 250, 885,
408, 387, 899, 73, 391, 883, 771,
848, 968, 504, 129, 370, 994, 897,
649, 345, 983, 326, 688, 547, 541,
567, 777, 987, 201, 326, 298, 959,
166, 962, 864, 797, 512, 505, 609,
208, 21, 43, 458, 442, 138, 570, 455,
442, 516, 294, 406, 310, 215, 212,
397, 98, 938, 496, 263, 973, 571,
861, 687, 276, 927, 608, 421, 831,
820, 510, 68, 172, 504, 8, 976, 992,
68, 497, 33, 233, 607, 587, 611, 695,
834, 338, 448, 978, 359, 413, 1, 819,
18, 977, 693, 649
1, 8, 18, 21, 33, 43, 68, 68, 73, 98,
99, 129, 138, 166, 172, 201, 208, 212,
215, 233, 250, 263, 276, 294, 298,
304, 310, 326, 326, 338, 342, 345,
359, 370, 387, 391, 397, 406, 408,
413, 421, 442, 442, 448, 455, 458,
496, 497, 504, 504, 505, 510, 512,
516, 541, 547, 567, 570, 571, 574,
587, 607, 608, 609, 611, 649, 649,
687, 688, 693, 695, 771, 777, 794,
797, 819, 820, 831, 834, 842, 848,
861, 864, 883, 885, 897, 899, 927,
938, 959, 962, 968, 973, 976, 977,
978, 983, 987, 992, 994
Hello, MY, name, Is, Barthoal, I,
Enjoy, long, beach, walks, would,
you, like, to, come, Join, me, in, my,
StarDestroyer-MobileHome? (TM)
Barthoal, Enjoy, Hello, I, Is, Join,
MY, StarDestroyer-MobileHome? (TM),
beach, come, in, like, long, me, my,
name, to, walks, would, you
As you can see, the numbers are fully sorted. The strings seem to have missed the last iteration of merging. So what exactly went wrong?
String comparison is case sensitive. That is, all uppercase strings will be placed before the lower case ones. Your merge sort algorithm should allow to take an own comparator which could be String.CASE_INSENSITIVE_ORDER for your use case.
Your code is fine and it sorts correctly.
You need to understand that upper-case letters are different than lower-case letters. when comparing a "My" wit "beach" you will get that "My" should come before "beach" (because upper-case letters come before lower-case letters).
This is the reason why in your result, all the upper case letters get all words that start with a capital letter are placed at the beginning.
You may want to use the compareToIgnoreCase() method (defined on Strings) to compare your string while ignoring the case of the letters.
See details of this method here: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String
(And please reformat your code)