diff --git a/src/com/company/ArraysandVectors_01/Minswap.java b/src/com/company/ArraysandVectors_01/Minswap.java index 0706e71..f4cd7e2 100644 --- a/src/com/company/ArraysandVectors_01/Minswap.java +++ b/src/com/company/ArraysandVectors_01/Minswap.java @@ -1,44 +1,35 @@ package com.company.ArraysandVectors_01; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.Vector; - -public class Minswap { - - public static Vector pairSum(Vector arr, int Sum) { - //Logic - Set s = new HashSet<>(); - Vector result = new Vector<>(); - - for (int i = 0; i < arr.size(); i++) { - - int x = Sum - arr.get(i); - if (s.contains(x)) { - result.add(x); - result.add(arr.get(i)); - return result; - } - //insert the current no inside set - s.add(arr.get(i)); - } - - return null; +import java.util.*; + +public class MinSwap { + public static void main(String[] args) { + int[] input = new int[]{10,11,5,4,3,2,1}; + int ans = 0; + Map map = new HashMap<>(); + for(int i=0;i arr = new Vector<>(Arrays.asList(ar)); - - Vector p = pairSum(arr,S); - if(p.size()==0){ - System.out.println("no such pair"); - } - else{ - System.out.println(p.get(0)+" , "+ p.get(1)); - } + // get the index of the input and make the sort and store the index in sortIndex + Arrays.sort(input); + List sortIndex = new ArrayList<>(); + for(int i:input){ + sortIndex.add(map.get(i)); + } + List visited = new ArrayList<>(Collections.nCopies(input.length,false)); + for(int i=0;i