Given an array arr[] consisting of N positive integers, the task is to find the nearest perfect power of 2 of the nearest perfect squares of unique array elements. If the array does not contain any unique element, then print -1.
Examples:
Input: arr[] = {4, 11, 4, 3, 4}
Output: 4 8
Explanation:
The unique elements in the given array are 11 and 3.
The nearest perfect square of 11 and 3 are 9 and 4 respectively.
The nearest power of 2 of 9 and 4 are 8 and 4 respectively.
Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: -1
Naive Approach: The simplest approach is to traverse the array and for each array element with single occurrence, print the nearest perfect power of 2 of the nearest perfect square of the array element. Otherwise, if there are no unique elements present in the array, then print -1.
Time Complexity: O(N2*log N)
Auxiliary Space: O(1)
Efficient Approach: The above can be optimized by Hashing. Follow the steps to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int perfectSquare(int num)
{
int sr = sqrt(num);
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((num - a) < (b - num)) {
return a;
}
else {
return b;
}
}
int powerOfTwo(int num)
{
int lg = log2(num);
int p = pow(2, lg);
return p;
}
void uniqueElement(int arr[], int N)
{
bool ans = true;
unordered_map<int, int> freq;
for (int i = 0; i < N; i++) {
freq[arr[i]]++;
}
for (auto el : freq) {
if (el.second == 1) {
ans = false;
int ps = perfectSquare(el.first);
cout << powerOfTwo(ps) << ' ';
}
}
if (ans)
cout << "-1";
}
int main()
{
int arr[] = { 4, 11, 4, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
uniqueElement(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int perfectSquare(int num)
{
int sr = (int)(Math.sqrt(num));
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((num - a) < (b - num)) {
return a;
}
else {
return b;
}
}
static int powerOfTwo(int num)
{
int lg = (int)(Math.log(num) / Math.log(2));
int p = (int)(Math.pow(2, lg));
return p;
}
static void uniqueElement(int arr[], int N)
{
boolean ans = true;
HashMap<Integer, Integer> freq
= new HashMap<Integer, Integer>();
for (int i = 0; i < N; i++) {
if (freq.containsKey(arr[i])) {
freq.put(arr[i], freq.get(arr[i]) + 1);
}
else {
freq.put(arr[i], 1);
}
}
for (Map.Entry<Integer, Integer> el :
freq.entrySet()) {
if (el.getValue() == 1) {
ans = false;
int ps = perfectSquare(el.getKey());
System.out.print(powerOfTwo(ps) + " ");
}
}
if (ans)
System.out.print("-1");
}
public static void main(String[] args)
{
int arr[] = { 4, 11, 4, 3, 4 };
int N = arr.length;
uniqueElement(arr, N);
}
}
|
Python3
from math import sqrt, log2, pow
def perfectSquare(num):
sr = int(sqrt(num))
a = sr * sr
b = (sr + 1) * (sr + 1)
if ((num - a) < (b - num)):
return a
else:
return b
def powerOfTwo(num):
lg = int(log2(num))
p = int(pow(2, lg))
return p
def uniqueElement(arr, N):
ans = True
freq = {}
for i in range(N):
if (arr[i] in freq):
freq[arr[i]] += 1
else:
freq[arr[i]] = 1
res = []
for key,value in freq.items():
if (value == 1):
ans = False
ps = perfectSquare(key)
res.append(powerOfTwo(ps))
res.sort(reverse = False)
for x in res:
print(x, end = " ")
if (ans):
print("-1")
if __name__ == '__main__':
arr = [4, 11, 4, 3, 4]
N = len(arr)
uniqueElement(arr, N)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
static int perfectSquare(int num)
{
int sr = (int)(Math.Sqrt(num));
int a = sr * sr;
int b = (sr + 1) * (sr + 1);
if ((num - a) < (b - num))
{
return a;
}
else
{
return b;
}
}
static int powerOfTwo(int num)
{
int lg = (int)(Math.Log(num) / Math.Log(2));
int p = (int)(Math.Pow(2, lg));
return p;
}
static void uniqueElement(int[] arr, int N)
{
bool ans = true;
Dictionary<int,
int> freq = new Dictionary<int,
int>();
for(int i = 0; i < N; i++)
{
if (freq.ContainsKey(arr[i]))
{
freq[arr[i]] = freq[arr[i]] + 1;
}
else
{
freq[arr[i]] = 1;
}
}
foreach(var el in freq.OrderBy(el => el.Key))
{
if (el.Value == 1)
{
ans = false;
int ps = perfectSquare(el.Key);
Console.Write(powerOfTwo(ps) + " ");
}
}
if (ans)
Console.Write("-1");
}
public static void Main(string[] args)
{
int[] arr = { 4, 11, 4, 3, 4 };
int N = arr.Length;
uniqueElement(arr, N);
}
}
|
Javascript
<script>
function perfectSquare(num)
{
let sr = Math.floor(Math.sqrt(num));
let a = sr * sr;
let b = (sr + 1) * (sr + 1);
if ((num - a) < (b - num))
{
return a;
}
else
{
return b;
}
}
function powerOfTwo(num)
{
let lg = Math.floor(Math.log2(num));
let p = Math.pow(2, lg);
return p;
}
function uniqueElement(arr, N)
{
let ans = true;
arr.reverse();
let freq = new Map();
for(let i = 0; i < N; i++)
{
freq[arr[i]]++;
if (freq.has(arr[i]))
{
freq.set(arr[i],
freq.get(arr[i]) + 1)
}
else[
freq.set(arr[i], 1)
]
}
for(let el of freq)
{
if (el[1] == 1)
{
ans = false;
let ps = perfectSquare(el[0]);
document.write(powerOfTwo(ps) + ' ');
}
}
if (ans)
document.write("-1");
}
let arr = [ 4, 11, 4, 3, 4 ];
let N = arr.length;
uniqueElement(arr, N);
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)