Computer Science, asked by praveenrandy8001, 1 year ago

Given an array of elements of length n, ranging from 0 to n-1, your task is to write a program that rearranges the elements of the array. All elements may not be present in the array, if element is not present then there will be -1 present in the array. Rearrange the array such that a[i] = i and if i is not present, display -1 at that place.

Answers

Answered by nikhil2692
0

Answer:

vsjxglaglsvlavlxqxvladlgdlabvxpablfa

Answered by pradeepkumark24cse
0

Answer:

import java.util.*;

public class ArraysInterchange{

public static void main(String[]args){

Scanner in=new Scanner(System.in);

int num=in.nextInt();

int[] arr=new int[num];

int temp=0;

for(int i=0;i<num;i++){

arr[i]=in.nextInt();

}

for(int i=0;i<arr.length;i++){

for(int j=0;j<arr.length;j++){

if(arr[j]==i){

temp=arr[j];

arr[j]=arr[i];

arr[i]=temp;

}

}

}

for(int i=0;i<num;i++){

if(arr[i]!=i){

arr[i]=-1;

}

}

for(int i=0;i<num;i++){

System.out.print(arr[i]+" ");

}

}

}

Explanation:

Similar questions