-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65ce7ed
commit 2dff809
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for JAVA | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
BufferedReader read = | ||
new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
while (t-- > 0) { | ||
int N = Integer.parseInt(read.readLine()); | ||
|
||
String S[] = read.readLine().split(" "); | ||
|
||
int[] arr = new int[N]; | ||
|
||
for(int i=0 ; i<N ; i++) | ||
arr[i] = Integer.parseInt(S[i]); | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.singleElement(arr,N)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
static int singleElement(int[] arr , int n) | ||
{ | ||
// code here | ||
int result = 0; | ||
int x, sum; | ||
|
||
for (int i = 0; i < 32; i++) | ||
{ | ||
sum = 0; | ||
x = (1 << i); | ||
for (int j = 0; j < n; j++) | ||
{ | ||
if ((arr[j] & x) != 0) | ||
sum++; | ||
} | ||
|
||
if ((sum % 3) != 0) | ||
result |= x; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution | ||
{ | ||
public int minOperations(int[] nums) | ||
{ | ||
int ans = 0; | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
|
||
for (final int num : nums) | ||
count.merge(num, 1, Integer::sum); | ||
|
||
for (final int freq : count.values()) | ||
{ | ||
if (freq == 1) | ||
return -1; | ||
|
||
ans += (freq + 2) / 3; | ||
} | ||
|
||
return ans; | ||
} | ||
} |