Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Friday, 22 May 2020

check Array elements are consecutive


public class CheckArrayIsConsecutive {

  static boolean checkConsecutive(int[] arr){

        int min = Integer.MAX_VALUE;
        for(int i=0;i<arr.length;i++){
            if(min>arr[i]){
                min = arr[i];
            }
        }

        for(int i=0;i<arr.length;i++){
            if(Math.abs(arr[i])-min>=arr.length){ //means array size small than required consecutive element
                return false;
            }
            if(arr[Math.abs(arr[i])-min]<0){ //if we want to store negative at index there already a negative value
                return false;
            }

            arr[Math.abs(arr[i])-min] = -   arr[Math.abs(arr[i])-min];

        }


        return true;
    }
    public static void main(String[] args) {
        int[] arr = {77,78,76,75,72,73,74};
        boolean bool = checkConsecutive(arr);
        System.out.println(bool);
    }
}


output: true


if input below then output false:
 int[] arr = {77,78,76,75,72,73,79};

ref: https://github.com/mission-peace/interview/blob/master/src/com/interview/array/CheckIfArrayElementsAreConsecutive.java

Sunday, 17 March 2019

Convert array into Zig-Zag fashion

public class ZigZag {
    public static void main(String[] args) {
          int arr[] = new int[]{4, 3, 7,7, 8, 6, 2, 1};
          zigzag(arr);
    }
   
    public static void zigzag(int[] arr) {
       
        boolean flag = true ;//<
       
        for(int i=0;i<arr.length-1;i++) {
            if(flag) {
                if(arr[i]>arr[i+1]) {
                    int temp = arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1] = temp;
                }
            }else {
                if(arr[i]< arr[i+1]) {
                    int temp = arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1] = temp;
                }
            }
            flag = !flag;
        }
       
        System.out.println(Arrays.toString(arr));
    }
}
output: [3, 7, 4, 8, 6, 7, 1, 2]

Monday, 8 October 2018

FindMissing number in AP log(n)

public class FindMissing {

    public static void main(String[] args) {
        int[] arr = {2,4,6,8,12,14,16,18,20,22,24,26};
        int val = findMissing(arr, 0, arr.length-1, 2);
        System.out.println(val);
    }
   
   
    static int findMissing(int[] arr, int start, int end,int diff) {
        if (end >= start) {
            int mid = (start + end) / 2;
            if (mid<end && (arr[mid+1] - arr[mid]) != diff) {
                return arr[mid] + diff;
            } else if (mid>0 && (arr[mid] - arr[mid-1]) != diff) {
                return arr[mid-1] + diff;
            }
//if difference in left and right element of mid are equal to diff then we need to decide to move either //left or right as we will check if our series is correct till mid then we will move to right otherwise left
//for checking till mid series  is correct we will use formula : arr[mid]==arr[0]+mid*diff


else if (arr[mid] == arr[0] + mid * diff) {
                return findMissing(arr, mid + 1, end, diff);
            } else {
                return findMissing(arr, start, mid - 1, diff);
            }

        }
        return 0;
    }

}

Sunday, 19 August 2018

find max sub array program

Program1: find sum of maxSubArray

 public int maxSubArray(final int[] A) {
     int largestSum = Integer.MIN_VALUE;
        int tempSum = 0;
        for(int i=0;i<A.length;i++) {
            if(tempSum+A[i]<A[i]) {
                tempSum = A[i];
            }else {
                tempSum = tempSum+A[i];
            }
           
            if((tempSum>Integer.MIN_VALUE && tempSum>largestSum) || tempSum>0) {
                 if(largestSum<tempSum){
                    largestSum = tempSum;
                
                    }
            }else {
                tempSum=0;
            }
        }
        return largestSum;
    }


intput:{-2,1,-3,-4,-1,-2,-1,-7,-5,-4}

output: 1


innput:{-160,-20,-10}
output -10

input: -160,-20,-10,1,2
output: 3



Program2: find sum of maxSubArray with start and end index

public static void findMaxSumSubArray(int[] arr) {
        int largestSum = Integer.MIN_VALUE;
        int tempSum = 0;
        int startIndex = 0;
        int endIndex = 0;
        int s = 0;
        for(int i=0;i<arr.length;i++) {
            if(tempSum+arr[i]<arr[i]) {
                tempSum = arr[i];
            }else {
                tempSum = tempSum+arr[i];
            }
           
            if((tempSum>Integer.MIN_VALUE && tempSum>largestSum) || tempSum>0) {
                 if(largestSum<tempSum){
                     endIndex = i;
                     startIndex = s;
                    largestSum = tempSum;
               
                    }
            }else {
                s = i+1;
                tempSum=0;
            }
        }
        System.out.println("index start::"+startIndex +" and end: "+endIndex);
        System.out.println("sum::"+largestSum);
    }


Saturday, 18 August 2018

Find Noble Integer Program

Question: Given an integer array, find if an integer p exists in the array such that the number of integers greater than p in the array equals to p
If such an integer is found return 1 else return -1.

Solution:
static int checkNobleInteger(int[] arr) {
        for(int i=0;i<arr.length;i++) {
           
            int count = 0;
            for(int j=0;j<arr.length;j++) {
               
                if(arr.length-j+count<arr[i]) {
                    break;
                }
               
                if(arr[j]>arr[i]) {
                    count++;
                }
            }
            if(arr[i]==count) {
                return 1;
            }
        }
        return -1;
    }

input: {4, 3, 5, 6, 7, 8}
output: 1


input: {6, 3, 5, 6, 7, 8}
output: -1

Ref: https://www.interviewbit.com

Find maximum sub array of positve number

 public int[] maxset(int[] A) {
       
        long tempSum = 0;
        long finalSum = 0;
        ArrayList<Integer> maxArray = new ArrayList<>();
        ArrayList<Integer> newArray = new ArrayList<>();
 
        for(int i=0;i<A.length;i++) {
            if(A[i]>=0){
                tempSum = tempSum+A[i];
                newArray.add(A[i]);
          
                if(tempSum>finalSum ||( tempSum==finalSum && newArray.size()>maxArray.size())) {
                    finalSum = tempSum;
                    maxArray = (ArrayList<Integer>)newArray.clone();
                    
                }
            }else {
                tempSum = 0;
                 newArray = new ArrayList<>();
                        
            }
        }
       
        int[] list = new int[maxArray.size()];
        for(int i=0;i<maxArray.size();i++) {
            list[i] = maxArray.get(i);
        }
       
        return list;
       
   
    }

input:  int[] arr = {1967513926, 1540383426, -1303455736, -521595368};
output: [1967513926, 1540383426]

input: int[] arr = {0,0,-1,0}
output: [0,0]



links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...