-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrearrange_array.java
More file actions
47 lines (41 loc) · 945 Bytes
/
rearrange_array.java
File metadata and controls
47 lines (41 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Input -> 5
6 8 2 1 4
Output -> -1 1 2 -1 4
Expression : if arr contains i print i else print -1
*/
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int i,j,temp,d=0,k=0;
int[] ar=new int[n];
for(i=0;i<n;i++)
ar[i]=scan.nextInt();
for(i=0;i<n;i++)
System.out.print(ar[i]+" ");
System.out.println();
int[] res=new int[n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==ar[j])
{
res[i]=ar[j];
k=0;
break;
}
else
k=1;
}
if(k==1)
res[i]=-1;
}
for(i=0;i<n;i++)
System.out.print(res[i]+" ");
}
}