Find duplicates under given constraints
Last Updated :
11 Feb, 2023
A sorted array contains 6 different numbers, only 1 number is repeated five times. So there are total 10 numbers in array. Find the duplicate numbers using two comparisons only.
Examples :
Input: arr[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30}
Output: 1
Input: arr[] = {1, 2, 3, 3, 3, 3, 3, 5, 9, 10}
Output: 3
Asked in Yahoo
An important observation is, arr[4] or arr[5] is an occurrence of repeated element for sure. Below is the implementation based on this observation.
Implementation:
CPP
// C++ program to find duplicate element under
// given constraints.
#include<bits/stdc++.h>
using namespace std;
// This function assumes array is sorted, has
// 10 elements, there are total 6 different
// elements and one element repeats 5 times.
int findDuplicate(int a[])
{
if (a[3] == a[4])
return a[3];
else if (a[4] == a[5])
return a[4];
else
return a[5];
}
// Driver code
int main()
{
int a[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30};
cout << findDuplicate(a);
return 0;
}
Java
// Java program to find duplicate element under
// given constraints.
class Num{
// This function assumes array is sorted, has
// 10 elements, there are total 6 different
// elements and one element repeats 5 times.
static int findDuplicate(int a[])
{
if (a[3] == a[4])
return a[3];
else if (a[4] == a[5])
return a[4];
else
return a[5];
}
// Driver code
public static void main(String[] args)
{
int a[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30};
System.out.println(findDuplicate(a));
}
}
//This code is contributed by
//Smitha Dinesh Semwal
Python3
# Python 3 program to find duplicate
# element under given constraints.
# This function assumes array is sorted, has 10
# elements, there are total 6 different elements
# and one element repeats 5 times.
def findDuplicate(a):
if (a[3] == a[4]):
return a[3]
elif (a[4] == a[5]):
return a[4]
else:
return a[5]
# Driver code
a = [1, 1, 1, 1, 1, 5, 7, 10, 20, 30]
print(findDuplicate(a))
# This code is contributed by Yash Agarwal
C#
// C# program to find duplicate
// element under given constraints.
using System;
class GFG {
// This function assumes array is
// sorted, has 10 elements, there
// are total 6 different elements
// and one element repeats 5 times.
static int findDuplicate(int []a)
{
if (a[3] == a[4])
return a[3];
else if (a[4] == a[5])
return a[4];
else
return a[5];
}
// Driver code
public static void Main()
{
int []a = {1, 1, 1, 1, 1, 5,
7, 10, 20, 30};
Console.Write(findDuplicate(a));
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP program to find duplicate
// element under given constraints.
// This function assumes array is
// sorted, has 10 elements, there
// are total 6 different elements
// and one element repeats 5 times.
function findDuplicate($a)
{
if ($a[3] == $a[4])
return $a[3];
else if ($a[4] == $a[5])
return $a[4];
else
return $a[5];
}
// Driver code
$a = array(1, 1, 1, 1, 1,
5, 7, 10, 20, 30);
echo findDuplicate($a);
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// JavaScript program to find duplicate element under
// given constraints.
// This function assumes array is sorted, has
// 10 elements, there are total 6 different
// elements and one element repeats 5 times.
function findDuplicate(a)
{
if (a[3] == a[4])
return a[3];
else if (a[4] == a[5])
return a[4];
else
return a[5];
}
// Driver code
let a = [1, 1, 1, 1, 1, 5, 7, 10, 20, 30];
document.write(findDuplicate(a));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Exercise: Extend the above problem for an array with n different elements, size of array is 2*(n-1) and one element repeats (n-1) times.
Similar Reads
Find 5 non-intersecting ranges with given constraints Given Five integers A, B, C, D and E. the task is to find five non-intersecting ranges [X1, Y1], [X2, Y2], [X3, Y3], [X4, Y4], [X5, Y5] such that, the following 5 conditions hold. X1 + Y1 = min(A, B)X2 * Y2 = max(A, B)|X3 - Y3| = C?X4 / Y4? = DX5 % Y5 = E Examples: Input: A = 6, B = 36, C = 20, D =
6 min read
Find minimum time to finish all jobs with given constraints Given an array job[], where each element represents the time required to complete a specific job. There are k identical assignees available to complete these jobs, and each assignee takes t units of time to complete one unit of a job. The task is to determine the minimum time required to complete al
11 min read
Create Y[] by given Array X[] following given condition Given an array X[] of length N, Where (1 ? Xi ? N). Create an array Y[], where the element at Y[i] should divide an element present at Y[X[i]-1]. and (X[i]-1) is the maximum such position in which the element is divisible by Y[i]. Note: If there are multiple possible arrays Y[]. Then print any one o
4 min read
Find out the maximum value of the expression according to the given constraint Given an array A[] of length N. Then your task is to output the maximum value of (Sum/LCM). Where Sum is the addition of all the elements of A[] and LCM is a number between [1, â] that can't be achieved by any possible non-empty set of A[]. Examples: Input: N = 5, A[] = {1, 2, 3, 4, 5} Output: 2 Exp
9 min read
Counting Arrays with divisibility constraint Given two integers X and Y, the task is to find the number of different arrays that can be constructed of size largest possible size that follow the conditions below: Every element of the array should not be less than X and should not be greater than Y.Every element should occur at most once in the
9 min read