Find element that appears once in sorted array
Problem : There are numbers in pair find out odd one
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
abc:
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=ab.nextInt();
for(int i=0;i<n-1;i+=2)
{
if(arr[i]!=arr[i+1])
{
System.out.println(arr[i]);
continue abc;
}
}
if(n%2!=0)
System.out.println(arr[n-1]);
}
}
}
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
abc:
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=ab.nextInt();
for(int i=0;i<n-1;i+=2)
{
if(arr[i]!=arr[i+1])
{
System.out.println(arr[i]);
continue abc;
}
}
if(n%2!=0)
System.out.println(arr[n-1]);
}
}
}
0 Comments:
Post a Comment