Skip to content

added data structure questions #1845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions data_structures/Backtracking/sudoku.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*

Input:
1
3 0 6 5 0 8 4 0 0
5 2 0 0 0 0 0 0 0
0 8 7 0 0 0 0 3 1
0 0 3 0 1 0 0 8 0
9 0 0 8 6 3 0 0 5
0 5 0 0 9 0 6 0 0
1 3 0 0 0 0 2 5 0
0 0 0 0 0 0 0 7 4
0 0 5 2 0 6 3 0 0

Output:
3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9

*/

#include<iostream>
using namespace std;
#define n 9

void print(int a[n][n])
{
int i,j;

for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<< a[i][j]<<" ";
}

cout<<"\n";
}

bool rowsafe(int a[n][n], int row, int num )
{
int i,j;

for(i=0;i<9;i++)
if(a[row][i]==num)
return false;

return true;
}



bool colsafe(int a[n][n], int col, int num )
{
int i,j;

for(i=0;i<9;i++)
if(a[i][col]==num)
return false;

return true;
}



bool boxsafe(int a[n][n], int row,int col, int num )
{
int i,j;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(a[i+row][j+col]==num)
return false;

return true;
}


bool safe(int a[n][n], int row, int col, int num)
{
if(rowsafe(a,row,num) && colsafe(a,col,num) && boxsafe(a,row-row%3,col-col%3,num) && a[row][col]==0)
return true;

return false;
}
bool notassigned(int a[n][n], int &r, int &c)
{
int i,j;

for(r=0;r<n;r++)
for(c=0;c<n;c++)
if(a[r][c]==0)
return false;

return true;
}

bool solve(int a[n][n])
{
int r, c;

if(notassigned(a, r, c))
return true;


int i,j;

for(i=1;i<=9;i++)
{
if(safe(a,r,c,i))
{
a[r][c]=i;
if(solve(a))
return true;
a[r][c]=0;
}
}
return false;


}

int main()
{
//code

int t;
cin>>t;
while(t--)
{
int a[n][n];

int i,j;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];


if(solve(a))
print(a);

else
cout<<-1<<"\n";

}

return 0;
}
58 changes: 58 additions & 0 deletions data_structures/BitManipulation/Different_Bits_Sum_Pairwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'''
Problem Description
We define f(X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f(2, 7) = 2, since binary representation of 2 and 7 are 010 and 111,respectively. The first and the third bit differ, so f(2, 7) = 2. You are given an array of N positive integers, A1, A2 ,..., AN. Find sum of f(Ai, Aj) for all pairs (i, j) such that 1 ≤ i, j ≤ N. Return the answer modulo 109+7.


Problem Constraints

1 <= N <= 100000
1 <= A[i] <= INTMAX



Input Format
First and only argument of input contains a single integer array A.


Output Format
Return a single integer denoting the sum.


Example Input
Input 1:
A = [1, 3, 5]



Example Output
Ouptut 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8



Example Explanation
Explanation 1:
f(1, 1) + f(1, 3) + f(1, 5) + f(3, 1) + f(3, 3) + f(3, 5) + f(5, 1) + f(5, 3) + f(5, 5)
= 0 + 1 + 1 + 1 + 0 + 2 + 1 + 2 + 0
'''

class Solution:
# @param A : list of integers
# @return an integer
def cntBits(self, A):
n=len(A)
mod=1000000007
temp=1
count=0
for j in range (0,32):
c0=0
c1=0
for i in range (0,n):
if A[i]&temp:
c1+=1
else:
c0+=1
temp<<=1
count=(count%mod + (c0*c1*2)%mod)%mod

return count
51 changes: 51 additions & 0 deletions data_structures/BitManipulation/InterestingArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''
Problem Description
You have an array A[] with N elements. We have 2 types of operation available on this array :
We can split a element B into 2 elements C and D such that B = C + D.
We can merge 2 elements P and Q as one element R such that R = P^Q i.e XOR of P and Q.
You have to determine whether it is possible to make array A[] containing only 1 element 0 after several splits and/or merge?


Problem Constraints
1<=N<=100000
1<=A[i]<=10^6


Input Format
The first argument is an integer array A of size N.


Output Format
Return "Yes" if it is possible otherwise return "No".


Example Input
A=[9,17]


Example Output
Yes


Example Explanation
Following is one possible sequence of operations -
1) Merge i.e 9 XOR 17 = 24
2) Split 24 into two parts each of size 12
3) Merge i.e 12 XOR 12 = 0
As there is only 1 element i.e 0. So it is possible.
'''


class Solution:
# @param A : list of integers
# @return a strings
def solve(self, A):
sum=0
n=len(A)
for i in range (0,n):
sum+=A[i]

if sum%2==0:
return "Yes"
else:
return "No"
50 changes: 50 additions & 0 deletions data_structures/BitManipulation/Min_XOR_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'''
Min XOR value
Problem Description
Given an integer array A of N integers, find the pair of integers in the array which have minimum XOR value. Report the minimum XOR value.


Problem Constraints
2 <= length of the array <= 100000
0 <= A[i] <= 109


Input Format
First and only argument of input contains an integer array A.


Output Format
Return a single integer denoting minimum xor value.


Example Input
Input 1:
A = [0, 2, 5, 7]
Input 2:
A = [0, 4, 7, 9]



Example Output
Output 1:
2
Output 2:
3
'''

class Solution:
# @param A : list of integers
# @return an integer
def findMinXor(self, A):
A.sort(reverse=False)
n=len(A)
m= int(sys.float_info.max)
for i in range (0,n-1):
xor=A[i]^A[i+1]
#print(xor)
#print(m)
if xor < m:
m=xor
# print(m)
return m

62 changes: 62 additions & 0 deletions data_structures/BitManipulation/ReverseBit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'''
Problem Description
Reverse the bits of an 32 bit unsigned integer A.


Problem Constraints
0 <= A <= 232



Input Format
First and only argument of input contains an integer A.


Output Format
Return a single unsigned integer denoting minimum xor value.


Example Input
Input 1:
0
Input 2:
3



Example Output
Output 1:
0
Output 2:
3221225472



Example Explanation
Explanation 1:
00000000000000000000000000000000 => 00000000000000000000000000000000
Explanation 2:
00000000000000000000000000000011 => 11000000000000000000000000000000
'''

unsigned int Solution::reverse(unsigned int A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details



unsigned int rev_num=A,count=31;
A>>=1;
while(A){
rev_num<<=1;
rev_num|=(A&1);
A>>=1;
count--;
}
rev_num<<=count;
return rev_num;

}

Loading