KEMBAR78
Array Practice | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
20 views50 pages

Array Practice

Uploaded by

onlinetonistark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views50 pages

Array Practice

Uploaded by

onlinetonistark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Note : All these questions are form the arrays topic in geeks for

geeks. To find similar questions, filter geeks for geeks practice


questions based on company name and concepts. Practicing
previously asked questions can help a lot to crack any company’s
interview. Solve atleast 2 each day.

Refer :
https://www.geeksforgeeks.org/explore?page=3&company=Zoho&sortBy
=difficulty

(i) ARRAYS

Q1)​ Alternative Sorting

Given an array arr[] of N distinct integers, output the array in such a way
that the first element is first maximum and the second element is the first
minimum, and so on.

Example 1:

Input: N = 7, arr[] = {7, 1, 2, 3, 4,

5, 6}

Output: 7 1 6 2 5 3 4

Explanation:The first element is first

maximum and second element is first

minimum and so on.


Example 2:

Input: N = 8, arr[] = {1, 6, 9, 4, 3,

7, 8, 2}

Output: 9 1 8 2 7 3 6 4

SOLUTION :

#include<stdio.h>

void main()

int array[20],i,j,length,temp,mid;

printf("enter the number of elements : ");

scanf("%d",&length);

mid=length/2;

for(i=0;i<length;i++)

scanf("%d",&array[i]);

for(i=0;i<length;i++)

for(j=i+1;j<length;j++)
{

if(array[i]>array[j])

temp=array[j];

array[j]=array[i];

array[i]=temp;

for(i=0;i<mid;i++)

printf("%d %d ",array[--length],array[i]);

if(i==--length){

printf("%d",array[i]);

}
Q2)​ Sort in specific order

Given an array A of positive integers. Your task is to sort them in such a


way that the first part of the array contains odd numbers sorted in
descending order, rest portion contains even numbers sorted in ascending
order.

Example 1:

Input:

N=7

Arr = {1, 2, 3, 5, 4, 7, 10}

Output:

7 5 3 1 2 4 10

Explanation:

Array elements 7 5 3 1 are odd

and sorted in descending order,

whereas 2 4 10 are even numbers

and sorted in ascending order.

Example 2:
Input:

N=7

Arr = {0, 4, 5, 3, 7, 2, 1}

Output:

7531024

SOLUTION :

#include<stdio.h>

void main()

int array[20],i,j,length,output[20],temp;

printf("enter the number of elements : ");

scanf("%d",&length);

for(i=0;i<length;i++)

scanf("%d",&array[i]);

for(i=0;i<length;i++)
{

for(j=i+1;j<length;j++)

if(array[i]<array[j])

temp=array[j];

array[j]=array[i];

array[i]=temp;

j=0,temp=length;

for(i=0;i<length;i++)

if(array[i]%2!=0){

output[j++]=array[i];

else{

output[--temp]=array[i];

for(i=0;i<length;i++)
{

printf("%d ",output[i]);

Q3)​ Number of occurrence

Given a sorted array Arr of size N and a number X, you need to find the
number of occurrences of X in Arr.

Example 1:

Input:

N = 7, X = 2

Arr[] = {1, 1, 2, 2, 2, 2, 3}

Output: 4

Explanation: 2 occurs 4 times in the

given array.

Example 2:

Input:

N = 7, X = 4
Arr[] = {1, 1, 2, 2, 2, 2, 3}

Output: 0

Explanation: 4 is not present in the

given array.

SOLUTION :

#include<stdio.h>

int main()

int array[20],x,length,count=0;

printf("enter the number of elements : ");

scanf("%d",&length);

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

scanf("%d",&x);

int f=0,l=length-1;

int mid=(f+l)/2;
while(f<l){

if((array[mid]==x && array[mid-1]<x) || mid==0){

break;

else if(array[mid]>x){

l=mid-1;

mid=(f+l)/2;

else{

f=mid+1;

mid=(f+l)/2;

if(array[mid]!=x){

printf("\n0");

return 0;

f=mid;

while(array[f++]==x){

count++;

printf("\n%d\n",count);

return 0;
}

Q4)​ Total count

Given an array Arr of N positive integers and a number K where K is used


as a threshold value to divide each element of the array into sum of
different numbers. Find the sum of count of the numbers in which array
elements are divided.

Example 1:

Input:

N = 4, K = 3

Arr[] = {5, 8, 10, 13}

Output: 14

Explanation: Each number can be expressed as sum

of different numbers less than or equal to K as

5 (3 + 2), 8 (3 + 3 + 2), 10 (3 + 3 + 3 + 1),

13 (3 + 3 + 3 + 3 + 1). So, the sum of count

of each element is (2+3+4+5)=14.


Example 2:

Input:

N = 5, K = 4

Arr[] = {10, 2, 3, 4, 7}

Output: 8

Explanation: Each number can be expressed as sum of

different numbers less than or equal to K as

10 (4 + 4 + 2), 2 (2), 3 (3), 4 (4) and 7 (4 + 3).

So, the sum of count of each element is

(3 + 1 + 1 + 1 + 2) = 8.

SOLUTION :

#include<stdio.h>

int main()

int array[20],x,length,count=0;

printf("enter the number of elements : ");

scanf("%d",&length);

printf("enter the elements : ");

for(int i=0;i<length;i++)

{
scanf("%d",&array[i]);

printf("enter the threshold value : ");

scanf("%d",&x);

for(int i=0;i<length;i++)

if(array[i]%x!=0){

count+=((array[i]/x)+1);

else{

count+=(array[i]/x);

printf("\n%d",count);

Q5)​ Remove duplicate elements from sorted Array

Given a sorted array A[] of size N, delete all the duplicated elements from
A[]. Modify the array such that if there are X distinct elements in it then the
first X positions of the array should be filled with them in increasing order
and return the number of distinct elements in the array.
Note:

1. Don't use set or HashMap to solve the problem.

2. You must return the number of distinct elements(X) in the array, the
generated output will print all the elements of the modified array from index
0 to X-1.

Example 1:

Input:

N=5

Array = {2, 2, 2, 2, 2}

Output: {2}

Explanation: After removing all the duplicates

only one instance of 2 will remain.

Example 2:

Input:

N=3

Array = {1, 2, 2}

Output: {1, 2}
SOLUTION :

#include<stdio.h>

int main()

int array[20],length;

printf("enter the number of elements : ");

scanf("%d",&length);

printf("enter the elements : ");

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

/*int cur=0,nxt=0,i=0;

while(cur<length || nxt<length){

if(cur==nxt){

array[i++]=array[cur];

if(array[cur]==array[nxt]){

nxt++;

else{

cur=nxt;

}
}*/

int i,j=0;

for(i=0;i<length;i++){

if(array[i]!=array[i+1]){

array[j++]=array[i];

array[i++]=array[length-1];

for(int i=0;i<j;i++)

printf("%d ",array[i]);

Q6)​ 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.

Note: The extra space is only for the array to be returned.

Try and perform all operations within the provided 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.

SOLUTION :

#include<stdio.h>

int main()

{
int array[20],length,count=0,flag=0;

printf("enter the number of elements : ");

scanf("%d",&length);

printf("enter the elements : ");

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

/*for(int i=0;i<length;i++){

count=0;

for(int j=i+1;j<length;j++){

if(array[i]==array[j]){

count++;

array[j]==-1;

if(count>0){

printf("%d ",array[i]);

flag=1;

if(flag==0){

printf("-1");

}*/
for(int i=0;i<length;i++){

array[array[i]%length] +=length;

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

if((array[i]/length)>1){

printf("%d ",i);

else{

count++;

if(count==length){

printf("-1");

Q7)​ Index Of an Extra Element

Given two sorted arrays of distinct elements. There is only 1


difference between the arrays. First array has one element extra
added in between. Find the index of the extra element.

Example 1:
Input:

N=7

A[] = {2,4,6,8,9,10,12}

B[] = {2,4,6,8,10,12}

Output: 4

Explanation: In the second array, 9 is

missing and it's index in the first array

is 4.

Example 2:

Input:

N=6

A[] = {3,5,7,9,11,13}

B[] = {3,5,7,11,13}

Output: 3

SOLUTION :

#include<stdio.h>
int main()

int l1,l2;

printf("enter the number of elements : ");

scanf("%d",&l1);

int array1[l1];

printf("enter the elements : ");

for(int i=0;i<l1;i++)

scanf("%d",&array1[i]);

printf("enter the number of elements : ");

scanf("%d",&l2);

int array2[l2];

printf("enter the elements : ");

for(int i=0;i<l2;i++)

scanf("%d",&array2[i]);

if(array1[l1-1]!=array2[l2-1]){

printf("%d",l1-1);

}
else{

int f=0,l=l2-1,mid;

while(f<l){

mid=(f+l)/2;

if((array1[mid]!=array2[mid])&&(array1[mid-1]==array2[mid-1])){

printf("%d",mid);

break;

else if(array1[mid]==array2[mid]){

f=mid+1;

else{

l=mid-1;

/*int sum1=0,sum2=0;

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

sum1+=array1[i];

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

sum2+=array2[i];

}
sum1=sum1-sum2;

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

if(array1[i]==sum1){

printf("%d",i);

}*/

Q8)​ Frequencies of Limited Range Array Elements

Given an array A[] of N positive integers which can contain integers from 1
to P where elements can be repeated or can be absent from the array. Your
task is to count the frequency of all elements from 1 to N.

Note: The elements greater than N in the array can be ignored for counting

and do modify the array in-place.

Example 1:

Input:

N=5

arr[] = {2, 3, 2, 3, 5}

P=5
Output:

02201

Explanation:

Counting frequencies of each array element

We have:

1 occurring 0 times.

2 occurring 2 times.

3 occurring 2 times.

4 occurring 0 times.

5 occurring 1 time.

Example 2:

Input:

N=4

arr[] = {3,3,3,3}

P=3

Output:

0040

Explanation:
Counting frequencies of each array element

We have:

1 occurring 0 times.

2 occurring 0 times.

3 occurring 4 times.

4 occurring 0 times.

SOLUTION :

#include<stdio.h>

int main()

int length,x;

printf("enter the number of elements : ");

scanf("%d",&length);

int array[20];

printf("enter the elements : ");

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

}
scanf("%d",&x);

int i=0;

while(i<length){

if(array[i]<0){

i++;

else{

int temp;

if(array[array[i]-1]>0){

temp=array[array[i]-1];

array[array[i]-1]=-1;

if(array[i]>0){

array[i]=temp;

else{

i++;

else{

array[array[i]-1]-=1;

i++;

}
}

for(int i=0;i<x;i++)

if(array[i]<0){

printf("%d --> %d \n",(i+1),array[i]*(-1));

else{

printf("%d --> 0 \n",i+1);

Q9)​ Next Greater Even Number

Given a positive integer X. The task is to find the smallest even number E
such that

E > X and all digits in X and E are the same.

Note: All the digits in X should be same with digits in E.

Example 1:

Input:
X = 34722641

Output:

34724126

Explanation:

Next greater number with same

digits as in input is 34724126.

Example 2:

Input:

X = 111

Output:

-1

Explanation:

You can't rearrange the digits to get an answer.

SOLUTION :
Q10)​Sum of two numbers represented as arrays

Given two numbers represented by two different arrays A and B. The task
is to find the sum array. The sum array is an array representation of
addition of two input arrays.

Example 1:

Input:

N = 3, M = 3

A[] = {5, 6, 3}

B[] = {8, 4, 2}

Output: 1 4 0 5

Explanation: As 563 + 842 = 1405.

Example 2:

Input:

N = 6, M = 4

A[] = {2, 2, 7, 5, 3, 3}

B[] = {4, 3, 3, 8}

Output: 2 3 1 8 7 1

Explanation: As 227533 + 4338 = 231871.


SOLUTION :

#include<stdio.h>

int main()

int l1,l2,size;

printf("enter the number of elements : ");

scanf("%d",&l1);

int array1[l1];

printf("enter the elements : ");

for(int i=0;i<l1;i++)

scanf("%d",&array1[i]);

printf("enter the number of elements : ");

scanf("%d",&l2);

int array2[l2];

printf("enter the elements : ");

for(int i=0;i<l2;i++)

scanf("%d",&array2[i]);
}

size= l1>l2?l1:l2;

int out[size];

int i=size-1,carry=0,val;

l1--;l2--;

while(i>=0){

val=carry;

if(l1>=0){

val+=array1[l1];

if(l2>=0){

val+=array2[l2];

carry=val/10;

val=val%10;

out[i--]=val;

l1--;l2--;

if(carry!=0){

printf("%d \n",carry);

}
for(int i=0;i<size;i++){

printf("%d ",out[i]);

Q11)​Bitonic Generator Sort

Given an array arr[] of N distinct numbers, the task is to sort all even-placed
numbers in increasing and odd-place numbers in decreasing order. The
modified array should contain all sorted even-placed numbers followed by
reverse sorted odd-placed numbers.

Note that the first element is considered as even because of its index 0.

Example 1:

Input: N = 8

arr[] = {0, 1, 2, 3, 4, 5, 6, 7}

Output: 0 2 4 6 7 5 3 1

Explanation:

Even-place elements : 0, 2, 4, 6

Odd-place elements : 1, 3, 5, 7
Even-place elements in increasing order :

0, 2, 4, 6

Odd-Place elements in decreasing order :

7, 5, 3, 1

Example 2:

Input: N = 9

arr[] = {3, 1, 2, 4, 5, 9, 13, 14, 12}

Output: 2 3 5 12 13 14 9 4 1

Explanation:

Even-place elements : 3, 2, 5, 13, 12

Odd-place elements : 1, 4, 9, 14

Even-place elements in increasing order :

2, 3, 5, 12, 13

Odd-Place elements in decreasing order :

14, 9, 4, 1

SOLUTION :
Q12)​​ Key Pair

Given an array Arr of N positive integers and another number X. Determine


whether or not there exist two elements in Arr whose sum is exactly X.

Example 1:

Input:
N = 6, X = 16
Arr[] = {1, 4, 45, 6, 10, 8}
Output: Yes

Explanation: Arr[3] + Arr[4] = 6 + 10 = 16


Example 2:

Input:
N = 5, X = 10
Arr[] = {1, 2, 4, 3, 6}
Output: Yes

Explanation: Arr[2] + Arr[4] = 4 + 6 = 10

SOLUTION :
#include<stdio.h>

int main()
{
int array[20],length,temp,x;
printf("enter the number of elements : ");
scanf("%d",&length);
printf("enter the elements : ");
for(int i=0;i<length;i++)
{
scanf("%d",&array[i]);
}
scanf("%d",&x);
for(int i=0;i<length;i++){
for(int j=i+1;j<length;j++){
if(array[i]+array[j]==x){
printf("yes : %d + %d =%d",array[i],array[j],x);
return 0;
}
}
}
printf("no");
return 0;
}

Q13)​ Ease the Array

Given an array of integers of size, N. Assume ‘0’ as the invalid number and
all others as a valid number. Write a program that modifies the array in
such a way that if the next number is a valid number and is the same as the
current number, double the current number value and replace the next
number with 0. After the modification, rearrange the array such that all 0’s
are shifted to the end and the sequence of the valid number or new
doubled number is maintained as in the original array.

Example 1:

​Input : arr[ ] = {2, 2, 0, 4, 0, 8}

Output : 4 4 8 0 0 0
Explanation:
At index 0 and 1 both the elements are same.
So, we can change the element at index 0 to
4 and element at index 1 is 0 then we shift
all the values to the end of the array.
So, array will become [4, 4, 8, 0, 0, 0].

Example 2:

Input : arr[ ] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8}

Output : 4 2 12 8 0 0 0 0 0 0

SOLUTION :
#include<stdio.h>

void main()

int array[20],length,count=0;

printf("enter the number of elements : ");

scanf("%d",&length);

printf("enter the elements : ");

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

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

if(array[i]!=0 && array[i+1]==array[i]){


array[i]*=2;

array[i+1]=0;

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

if (array[i] != 0){

array[count++] = array[i];

while (count < length){

array[count++] = 0;

for(int i=0;i<length;i++)

printf("%d ",array[i]);

}
Q14)​Element with left side smaller and right side greater

Given an unsorted array of size N. Find the first element in array such that
all of its left elements are smaller and all right elements to it are greater
than it.

Note: Left and right side elements can be equal to required element. And

extreme elements cannot be required element.

Example 1:

Input:

N=4

A[] = {4, 2, 5, 7}

Output:

Explanation:

Elements on left of 5 are smaller than 5

and on right of it are greater than 5.

Example 2:

Input:
N=3

A[] = {11, 9, 12}

Output:

-1

SOLUTION :

#include<stdio.h>

void main()

int array[20],length,temp,max_till[20];

printf("enter the number of elements : ");

scanf("%d",&length);

printf("enter the elements : ");

for(int i=0;i<length;i++)

scanf("%d",&array[i]);

max_till[0]=array[0];

for(int i=1;i<length;i++)

if(array[i]>max_till[i-1]){

max_till[i]=array[i];
}

else{

max_till[i]=max_till[i-1];

int min_right=array[length-1],flag=0;

for(int i=length-2;i>0;i--)

if(array[i]<min_right){

min_right=array[i];

if(array[i]==max_till[i]){

flag=i;

break;

if(flag==0){

printf("-1");

else{

printf("%d",array[flag]);

}
Q15)​Smallest greater elements in whole array

Given an array A of N length. We need to calculate the next smallest


greater element for each element in a given array. If the next greater
element is not available in a given array then we need to fill -10000000 at
that index place.

Example 1:

Input : arr[] = {13, 6, 7, 12}

Output : _ 7 12 13

Explanation:

Here, at index 0, 13 is the greatest value

in given array and no other array element

is greater from 13. So at index 0 we fill

'-10000000'.

Example 2:

Input : arr[] = {6, 3, 9, 8, 10, 2, 1, 15, 7}

Output : 7 6 10 9 15 3 2 _ 8
Explanation: Here, at index 7, 15 is the greatest

value in given array and no other array element is

greater from 15. So at index 7 we fill '-10000000'.

SOLUTION :

Q16)​The Celebrity Problem

A celebrity is a person who is known to all but does not know anyone at a
party. If you go to a party of N people, find if there is a celebrity in the party
or not.

A square NxN matrix M[][] is used to represent people at the party such that
if an element of row i and column j is set to 1 it means ith person knows jth
person. Here M[i][i] will always be 0.

Note: Follow 0 based indexing.

Follow Up: Can you optimize it to O(N)

Example 1:

Input:
N=3
M[][] = {{0 1 0},
{0 0 0},
{0 1 0}}
Output: 1

Explanation: 0th and 2nd person both

know 1. Therefore, 1 is the celebrity.

Example 2:

Input:
N=2
M[][] = {{0 1},
{1 0}}
Output: -1

Explanation: The two people at the party both

know each other. None of them is a celebrity.

SOLUTION :
#include<stdio.h>

void main()
{
int n;
printf("enter n value : ");
scanf("%d",&n);
printf("enter the elements : ");
int array[20][20];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++){
scanf("%d",&array[i][j]);
}
}

int i=0,j=n-1;
while(i<j){
if(array[i][j]==1){
if(array[j][i]==1){

j--;

i++;

else{
if(array[j][i]==0){

i++;
}

j--;

}
int flag=1;
printf("%d\n",j);
for(i=0;i<n;i++){
if((array[i][j]==0 || array[j][i]==1) && i!=j){

flag=0;
}
}
if(flag==1){
printf("%d is a celebrity.",j);
}
else{
printf("no one is celebrity.");
}
}

Q17)​ Rearrange Array Alternately

Given a sorted array of positive integers. Your task is to rearrange the array
elements alternatively i.e first element should be max value, second should be
min value, third should be second max, fourth should be second min and so
on.

Note: Modify the original array itself. Do it without using any extra space. You
do not have to return anything.

Example 1:

Input:
n=6
arr[] = {1,2,3,4,5,6}
Output: 6 1 5 2 4 3
Explanation: Max element = 6, min = 1,
second max = 5, second min = 2, and
so on... Modified array is : 6 1 5 2 4 3.
Example 2:

Input:
n = 11
arr[]={10,20,30,40,50,60,70,80,90,100,110}
Output:110 10 100 20 90 30 80 40 70 50 60
Explanation: Max element = 110, min = 10,
second max = 100, second min = 20, and
so on... Modified array is :
110 10 100 20 90 30 80 40 70 50 60.

SOLUTION :

Q18)​ Kadane's Algorithm

Given an array Arr[] of N integers. Find the contiguous sub-array(containing


at least one number) which has the maximum sum and return its sum.

Example 1:

Input:
N=5
Arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.

Example 2:

Input:
N=4
Arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1
of element (-1)

SOLUTION:

Q19)​ Largest Number formed from an Array

Given a list of non negative integers, arrange them in such a manner that
they form the largest number possible.The result is going to be very large,
hence return the result in the form of a string.

Example 1:

Input:

N=5
Arr[] = {3, 30, 34, 5, 9}
Output: 9534330

Explanation: Given numbers are {3, 30, 34,

5, 9}, the arrangement 9534330 gives the


largest value.
Example 2:

Input:

N=4
Arr[] = {54, 546, 548, 60}
Output: 6054854654

Explanation: Given numbers are {54, 546,

548, 60}, the arrangement 6054854654


gives the largest value.

SOLUTION :

Q20)​Find missing in second array

Given two arrays A and B contains integers of size N and M, the task is to
find numbers which are present in the first array, but not present in the
second array.

Example 1:

Input: N = 6, M = 5

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

B[] = {2, 3, 1, 0, 5}

Output: 4 10

Explanation: 4 and 10 are present in

first array, but not in second array.

Example 2:

Input: N = 5, M = 5

A[] = {4, 3, 5, 9, 11}

B[] = {4, 9, 3, 11, 10}


Output: 5

Explanation: Second array does not

contain element 5.

SOLUTION :

Q21)​Integer to Words

Write a function to convert a given number into words.

Example 1:

Input:

N = 438237764

Output: forty three crore eighty two lakh

thirty seven thousand seven hundred and

sixty four

Example 2:

Input:

N = 1000
Output: one thousand

SOLUTION :

Q21) Sum of upper and lower triangles


Given a square matrix of size N*N, print the sum of upper and
lower triangular elements. Upper Triangle consists of elements on the
diagonal and above it. The lower triangle consists of elements on the
diagonal and below it.
Solution : Sum of upper and lower triangles | Practice |
GeeksforGeeks

You might also like