Add

Array Java Coding 5 Problems for Interviews | Array program in java


1.Cyclically rotate an array by one

Given an array, rotate the array by one position in clock-wise direction.

Example 1:

Input:

N = 5

A[] = {1, 2, 3, 4, 5}

Output:

5 1 2 3 4

Example 2:

Input:
N = 8
A[] = {9, 8, 7, 6, 4, 2, 1, 3}
Output:
3 9 8 7 6 4 2 1

Circular rotate,

class Compute {   
    public void rotate(int arr[], int n)
    {
        int prev = arr[0];        
        for(int i = 1; i < n; i++){
            int temp = arr[i];
            arr[i] = prev;
            prev = temp;
        }
        arr[0] = prev;
    }
}
 

void rotate(int arr[], int n)
{
   /* PSEUDOCODE   
     1. Shift last element, (n-1)th element, to a temp array
     2. Shift n-2th element to position n-1 
     3. Shift n-3th element to n-2...
     4. Loop till 0th element
     5. Shift temp array's occupant to index zero of original array. 

  */   
   int temp[1]; //1.
   temp[0]=arr[n-1];
   for( int i=n-2; i>=0; i--){ //2, 3, 4.
       arr[i+1]=arr[i];
   }
   arr[0]=temp[0]; //5. 

}

2.Missing number in array 

Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.

Example 1:

Input:
N = 5
A[] = {1,2,3,5}
Output: 4

Example 2:

Input:
N = 10
A[] = {6,1,2,8,3,4,7,10,5}
Output: 9

class Solution:
   def MissingNumber(self,array,n):
       # code here
       ans = int(n*(n+1)/2)-sum(array)
       return ans;

finding the sum of 1 to n numbers without looping (saves time) with a constant space and substracting it with the sum of the given array hence the difference the number (which is our answer.)

O(n) Time and O{1)space

int MissingNumber(vector<int>& arr, int n) {
       // Your code goes here
       int i=0;
       while(i<n-1){
           int curr=arr[i]-1;
           if(arr[i]>n-1){
               i++;
           }
          else if(arr[i]!=arr[curr]){
               swap(arr[i],arr[curr]);
           }
           else{

  i++;
           }
       }
       for(int i=0;i<arr.size();i++){
           if(arr[i]!=i+1){
               return i+1;
           }           
       }
       return n;
   }

3.Count pairs with given sum 

Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K.

Example 1:

Input:
N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explanation: 
arr[0] + arr[1] = 1 + 5 = 6 
and arr[1] + arr[3] = 5 + 1 = 6.

Example 2:

Input:
N = 4, X = 2
arr[] = {1, 1, 1, 1}
Output: 6
Explanation: 
Each 1 will produce sum 2 with any 1.

 int getPairsCount(int arr[], int n, int k) {
    int ans=0;
    unordered_map<int,int>m;
    for(int i=0;i<n;i++){
        int temp=k-arr[i];
        if(m[temp]!=0){
            ans+=m[temp];
            m[arr[i]]++;
        }
        else{
            m[arr[i]]++;

  }
        }
       return ans;
   }

int getPairsCount(int arr[], int n, int k) {
       // code here
     unordered_map<int,int> m;
     for(int i=0;i<n;i++)
     m[arr[i]]++;
     int count=0;
     for(int i=0;i<n;i++){
         if(m[arr[i]]>0){
             m[arr[i]]--;
             if(m.find(k-arr[i])!=m.end()){

  count+=m[k-arr[i]];
             }
         }
     }
     return count;
   }

      return count/2;

 public:
   int getPairsCount(int arr[], int n, int k) {
       // code here
      int i;
      int count=0;
      unordered_map<int,int>m;
      for(i=0;i<n;i++)
      {
          m[arr[i]]++;

  }
      for(i=0;i<n;i++)
      {
          count+=(m[k-arr[i]]);
          if((k-arr[i]==arr[i]))
          {
              count--;
          }
      }
   }

4.Find duplicates in an array 

Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array.

Example 1:

Input:
N = 4
a[] = {0,3,1,2}
Output: -1
Explanation: N=4 and all elements from 0
to (N-1 = 3) are present in the given
array. Therefore output is -1.

Example 2:

Input:
N = 5
a[] = {2,3,1,2,3}
Output: 2 3 
Explanation: 2 and 3 occur more than once
in the given array.

class Solution{
 public:
   vector<int> duplicates(int arr[], int n) {
       vector<int>v;
       int last;
       sort(arr,arr+n);
       for(int i=0;i<n;i++){
       if(arr[i]==arr[i+1] && arr[i]!=last)
       {v.push_back(arr[i]);
       last=arr[i];
       }
       }
       if(v.size()==0)

 v.push_back(-1);
       return v;
}
};

let obj = {}, arr = [], cnt = 0;
       for(var i = 0; i < a.length; i++) {
           if(obj[a[i]]) {
               cnt++;
                if(!arr.includes(a[i])) {
               arr.push(a[i]);                 
               }
           } else {
               obj[a[i]] = true;
           }
       }       
       if(cnt == 0) {
           return [-1];
       } else {
return arr.sort(function(a, b) { return a - b; });
       }
 

5.Quick Sort

Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot.
Given an array arr[], its starting position low and its ending position high.

Implement the partition() and quickSort() functions to sort the array.

Example 1:

Input: 
N = 5 
arr[] = { 4, 1, 3, 9, 7}
Output:
1 3 4 7 9

Example 2:

Input: 
N = 9
arr[] = { 2, 1, 6, 10, 4, 1, 3, 9, 7}
Output:
1 1 2 3 4 6 7 9 10
 

C++ code

public:
    //Function to sort an array using quick sort algorithm.
void quickSort(int arr[], int low, int high){
   int temp;
   if(low<high){
       temp=partition(arr,low,high);
       quickSort(arr,low,temp-1);
       quickSort(arr,temp+1,high);
   }
}  
public:
int partition (int arr[], int low, int high){
     int index=low;
     for(int i=low;i<high;i++){
         if(arr[i]<arr[high]){
             swap(arr[i], arr[index++]);
         }
     }
     swap(arr[high],arr[index]);
     return index;
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.