deep.aman91
BAN USERclass CheckSortedByOneSwap{
public static void main(String... a){
CheckSortedByOneSwap obj = new CheckSortedByOneSwap();
System.out.println("True :: " + obj.solution(new int[] {4,2,2,2,5}));
System.out.println("False :: " + obj.solution(new int[] {4,2,2,2,3}));
System.out.println("True :: " + obj.solution(new int[] {3,2,2,2,3,3,4,4,4}));
System.out.println("True :: " + obj.solution(new int[] {3,2}));
System.out.println("True :: " + obj.solution(new int[] {3,2,2}));
System.out.println("True :: " + obj.solution(new int[] {3,2,4}));
System.out.println("False :: " + obj.solution(new int[] {8,2,2,4}));
System.out.println("True :: " + obj.solution(new int[] {8}));
}
boolean solution(int[] arr){
int len = arr.length;
int indexForMisplaced = -1;
int secondIndexForMisplaced = len-1;
for(int i=1;i<len;i++){
if(indexForMisplaced < 0){
if(arr[i-1] > arr[i]){
indexForMisplaced = i-1;
}
}
else{
if(arr[indexForMisplaced] <= arr[i]){
secondIndexForMisplaced = i-1;
break;
}
}
}
if(indexForMisplaced != -1){
int temp = arr[indexForMisplaced];
arr[indexForMisplaced]=arr[secondIndexForMisplaced];
arr[secondIndexForMisplaced]=temp;
}
for(int i=1;i<len;i++){
if(arr[i-1]>arr[i]){
return false;
}
}
return true;
}
}
class FindTwoWithGivenSum{
public static void main(String... a){
int[] arr = {5,4,6,1,11,8};
int number = 10;
// Sorted !!
for(int i=arr.length-1;i>0;i--){
for(int j=0;j<i;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i: arr){
System.out.print(i);
}
// Find closest smallest element for a given no.
int indexClosest = 0;
for(;indexClosest<arr.length;indexClosest++){
if(number<arr[indexClosest]){
indexClosest--;
break;
}
}
if(indexClosest == arr.length){
indexClosest--;
}
System.out.println("\n closest Index:"+indexClosest);
boolean solutionFound = false;
if(indexClosest>=0){
int startIndex = 0;
int endIndex = indexClosest;
while(startIndex<endIndex && !solutionFound){
if(arr[startIndex]+arr[endIndex] ==number){
System.out.println(arr[startIndex]+","+arr[endIndex]);
solutionFound=true;
}
else if(arr[startIndex]+arr[endIndex] > number){
endIndex--;
}
else{
startIndex++;
}
}
}
if(!solutionFound){
System.out.println("No such combination of numbers found in given array!!");
}
}
}
class LongestPositiveSequenceArray{
public static void main(String... a){
int [] myArray = {1,2,-3,2,3,4,-6,1,2,3,4,5,-8,5,6};
//int [] myArray = {1,2,3,-1,1,2,3,4};
int index=0;
int count = 0;
int tempCount = 0;
int currentIndex = 0;
for(int i=1; i<myArray.length;i++){
if(myArray[i-1]>0 && myArray[i-1]<=myArray[i]){
if(count == 0){
currentIndex = i-1;
}
count++;
}
else{
if(tempCount < count){
tempCount = count;
index = currentIndex;
}
count =0;
}
}
if(tempCount > count){
count=tempCount;
}
else{
index = currentIndex;
}
if(count==0){
System.out.println("No sequence found!!");
}
else{
System.out.println("Sequence found with length:"+(count+1)+", at index:"+index);
}
}
}
class LongestPositiveSequenceArray{
public static void main(String... a){
int [] myArray = {1,2,-3,2,3,4,-6,1,2,3,4,5,-8,5,6};
//int [] myArray = {1,2,3,-1,1,2,3,4};
int index=0;
int count = 0;
int tempCount = 0;
int currentIndex = 0;
for(int i=1; i<myArray.length;i++){
if(myArray[i-1]>0 && myArray[i-1]<=myArray[i]){
if(count == 0){
currentIndex = i-1;
}
count++;
}
else{
if(tempCount < count){
tempCount = count;
index = currentIndex;
}
count =0;
}
}
if(tempCount > count){
count=tempCount;
}
else{
index = currentIndex;
}
if(count==0){
System.out.println("No sequence found!!");
}
else{
System.out.println("Sequence found with length:"+(count+1)+", at index:"+index);
}
}
}
Another Solution::
}
- deep.aman91 May 24, 2015