import java.util.*;
import java.lang.*;
class Main
{
{
int a[]={4,1,2,7,9,3};
int length=a.length;
length--;
mergesort(a,0,length);
if(SumToTarget(a,0,length,4)==1)
System.
out.
println("Duplicates exist in the array"); else
System.
out.
println("Duplicates does not exist in the array"); }
/* Function to check whether the duplicates whose sum equal to target num exist or not */
public static int SumToTarget(int a[],int start,int last , int num)
{
int first_index=start;
int last_index=last;
/*if first_index becomes greater than last_index it means duplicates does not exist */
while(first_index<last_index)
{
/* if sum of dupliactes is equal to required sum it means duplicates are present hence returning 1 */
if((a[first_index]+a[last_index])==num )
{
System.
out.
println("Two numbers are:->"+a
[first_index
]+" "+a
[last_index
]); return 1;
}
/*if sum of two number is greater than the required sum then the last_index will be decreased*/
else if((a[first_index]+a[last_index])>num)
{
last_index--;
}
/*if sum of two number is less than the required sum then the first_index will be increased*/
else if((a[first_index]+a[last_index])<num)
{
first_index++;
}
}
/*program will reach here only if dupliactes consequently will return 0 */
return 0;
}
public static void mergesort(int a[],int start,int last)
{
if(start<last)
{
int mid=(start+last)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,last);
merge(a,start,last,mid);
}
}
public static void merge(int a[],int start ,int last,int mid )
{
int j,k,l;
int c[]=new int[a.length];
j=start;
k=start;
l=mid+1;
while(j<=mid && l<=last )
{
if(a[j]<a[l])
{
c[k]=a[j];
k++;
j++;
}
else
{
c[k]=a[l];
k++;
l++;
}
}
while(j<=mid)
{
c[k]=a[j];
k++;
j++;
}
while(l<=last)
{
c[k]=a[l];
k++;
l++;
}
for(int i=start;i<=k-1;i++)
{
a[i]=c[i];
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CgpjbGFzcyBNYWluCnsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbiAoU3RyaW5nW10gYXJncykgdGhyb3dzIGphdmEubGFuZy5FeGNlcHRpb24KICAgICAgICB7CiAgICAgICAgaW50IGFbXT17NCwxLDIsNyw5LDN9OwogIC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ICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlR3byBudW1iZXJzIGFyZTotPiIrYVtmaXJzdF9pbmRleF0rIiAiK2FbbGFzdF9pbmRleF0pOwogICAgICAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgICAgICAgICAgIAogICAgICAgICAgICB9CiAgICAgICAgLyppZiBzdW0gb2YgdHdvIG51bWJlciBpcyBncmVhdGVyIHRoYW4gdGhlIHJlcXVpcmVkIHN1bSB0aGVuIHRoZSBsYXN0X2luZGV4IHdpbGwgYmUgZGVjcmVhc2VkKi8KICAgICAgICBlbHNlIGlmKChhW2ZpcnN0X2luZGV4XSthW2xhc3RfaW5kZXhdKT5udW0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGxhc3RfaW5kZXgtLTsKICAgICAgICAgICAgfQogICAgICAgIC8qaWYgc3VtIG9mIHR3byBudW1iZXIgaXMgbGVzcyB0aGFuIHRoZSByZXF1aXJlZCBzdW0gdGhlbiB0aGUgZmlyc3RfaW5kZXggd2lsbCBiZSBpbmNyZWFzZWQqLyAgICAKICAgICAgICBlbHNlIGlmKChhW2ZpcnN0X2luZGV4XSthW2xhc3RfaW5kZXhdKTxudW0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZpcnN0X2luZGV4Kys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLypwcm9ncmFtIHdpbGwgcmVhY2ggaGVyZSBvbmx5IGlmIGR1cGxpYWN0ZXMgY29uc2VxdWVudGx5IHdpbGwgcmV0dXJuIDAgKi8KICAgICAgICAgICByZXR1cm4gMDsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgIG1lcmdlc29ydChpbnQgYVtdLGludCBzdGFydCxpbnQgbGFzdCkKICAgIHsKICAgICAgICBpZihzdGFydDxsYXN0KQogICAgICAgIHsKICAgICAgICAgICAgaW50IG1pZD0oc3RhcnQrbGFzdCkvMjsKICAgICAgICAgICAgbWVyZ2Vzb3J0KGEsc3RhcnQsbWlkKTsKICAgICAgICAgICAgbWVyZ2Vzb3J0KGEsbWlkKzEsbGFzdCk7CiAgICAgICAgICAgIG1lcmdlKGEsc3RhcnQsbGFzdCxtaWQpOwogICAgICAgIH0KICAgIH0KICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtZXJnZShpbnQgYVtdLGludCBzdGFydCAsaW50IGxhc3QsaW50IG1pZCApCiAgICB7CiAgICAgICAgaW50IGosayxsOwogICAgICAgIGludCBjW109bmV3IGludFthLmxlbmd0aF07CiAgICAgICAgaj1zdGFydDsKICAgICAgICBrPXN0YXJ0OwogICAgICAgIGw9bWlkKzE7CiAgICAgICAgd2hpbGUoajw9bWlkICYmIGw8PWxhc3QgKQogICAgICAgIHsKICAgICAgICAgICAgaWYoYVtqXTxhW2xdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjW2tdPWFbal07CiAgICAgICAgICAgICAgICBrKys7CiAgICAgICAgICAgICAgICBqKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjW2tdPWFbbF07CiAgICAgICAgICAgICAgICBrKys7CiAgICAgICAgICAgICAgICBsKys7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUoajw9bWlkKQogICAgICAgIHsKICAgICAgICAgICAgY1trXT1hW2pdOwogICAgICAgICAgICBrKys7CiAgICAgICAgICAgIGorKzsKICAgICAgICB9CiAgICAgICAgd2hpbGUobDw9bGFzdCkKICAgICAgICB7CiAgICAgICAgY1trXT1hW2xdOwogICAgICAgIGsrKzsKICAgICAgICBsKys7CiAgICAgICAgfQogICAgICAgCiAgICAgICAgZm9yKGludCBpPXN0YXJ0O2k8PWstMTtpKyspCiAgICAgICAgewogICAgICAgICAgICBhW2ldPWNbaV07CiAgICAgICAgfQogICAgfQp9Cg==